Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add blog post about setting up database #26

Merged
merged 4 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ endif
## Lint:
lint: lint-go lint-dockerfile lint-yaml ## Run all available linters

## DB:
migrate-table: ## Create the goose migration file for a new table
$(eval TABLE_NAME=$(shell read -p "Enter the table name: " table; echo $$table))
goose -dir db/migrations create create_$(TABLE_NAME) sql

lint-dockerfile: ## Lint your Dockerfile
# If dockerfile is present we lint it.
ifeq ($(shell test -e ./Dockerfile && echo -n yes),yes)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ By adding yourself to the [ADOPTERS.md](./ADOPTERS.md) file, you help increase t
<!-- ACKNOWLEDGMENTS -->
## Acknowledgments

* [Best-README-Template](https://github.com/othneildrew/Best-README-Template)
- [Best-README-Template](https://github.com/othneildrew/Best-README-Template)

<p align="right">(<a href="#readme-top">back to top</a>)</p>

Expand Down
16 changes: 16 additions & 0 deletions db/migrations/20240621180201_create_album.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- +goose Up
-- +goose StatementBegin
SELECT 'up SQL query';
-- +goose StatementEnd
CREATE TABLE albums (
ID uuid PRIMARY KEY,
Title TEXT NOT NULL,
Artist TEXT NOT NULL,
Price NUMERIC(10,2) NOT NULL
);

-- +goose Down
-- +goose StatementBegin
SELECT 'down SQL query';
-- +goose StatementEnd
drop table albums;
22 changes: 22 additions & 0 deletions db/queries/albums.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- name: ListAlbums :many
SELECT *
FROM albums;

-- name: GetAlbum :one
SELECT *
FROM albums
WHERE id = $1 LIMIT 1;

-- name: CreateAlbum :one
Insert INTO albums (id, title, artist, price)
VALUES ($1, $2, $3, $4)
RETURNING *;

-- name: DeleteAlbum :exec
DELETE FROM albums
WHERE id = $1;

-- name: UpdateAlbum :exec
Update albums
SET title = $2, artist = $3, price = $4
WHERE id = $1;
126 changes: 126 additions & 0 deletions db/sqlc/albums.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions db/sqlc/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions db/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 101 additions & 0 deletions docs/blog/2024-06-21-setting-up-database-process.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
slug: setting-up-database-process
title: Setting up database process
authors:
name: Sarvsav Sharma
title: Go Starter Template Core Team
url: https://github.com/sarvsav
image_url: https://github.com/sarvsav.png
tags: [database, sqlc, goose]
---

Let us understand on how to set up the things that will help to connect and perform the crud operations with database.

## Requirements

We will use postgres for our development, and below are the list of tools that needs to be installed and setup in the environment PATH. Use the installation script from `scripts` directory for installation of tools.

- [goose](https://github.com/pressly/goose)
- [sqlc](https://github.com/sqlc-dev/sqlc)

## Creating migration

First step, is to create a table inside the database to store data. To create a table, use the command `migrate-table` from `make` file. For demonstration purpose, let's use the [albums example](https://go.dev/doc/tutorial/web-service-gin).

```shell
$ make migrate-table
Enter the table name: album
goose -dir db/migrations create create_album sql
2024/06/21 20:02:01 Created new file: db/migrations/20240621180201_create_album.sql
```

It will create a new file inside folder `db/migrations` with date_create__varname__.sql, with default queries. Next, we need to add a create table syntax that will run when we use `goose up` and drop table syntax on running `goose down`.

This is the content of the file.

```sql
-- +goose Up
-- +goose StatementBegin
SELECT 'up SQL query';
-- +goose StatementEnd
CREATE TABLE albums (
ID uuid PRIMARY KEY,
Title TEXT NOT NULL,
Artist TEXT NOT NULL,
Price NUMERIC(10,2) NOT NULL
);

-- +goose Down
-- +goose StatementBegin
SELECT 'down SQL query';
-- +goose StatementEnd
drop table albums;
```

## Writing queries

Next step is to write the sql queries for **CRUD** operations in `sqlc` format inside folder named `db/queries`. These sql queries would be used to generate the `go` code with help of `sqlc`. The filename should be same as of the table name. Follow the naming convention for your function names.

- CreatResource
- GetResource
- ListResources
- UpdateResource
- DeleteResource

## Writing sqlc configuration file

Create a file named in `sqlc.yaml` in your project root. This file defines the configuration for sqlc. It is used to generate Go code from SQL queries.

```yaml
## Filename: sqlc.yaml
version: "2"
cloud:
organization: "Go Starter Template"
project: "GoStarterTemplate"
hostname: "go-starter-template.com"
sql:
- engine: "postgresql"
queries: "./db/queries/"
schema: "./db/migrations/"
gen:
go:
package: "db"
out: "./db/sqlc"
overrides:
go: null
plugins: []
rules: []
options: {}
```

## Generating Go code

The go code now can simply be generated using the `sqlc` command.

```shell
sqlc generate
```

It will generate the files inside `db/sqlc` directory. Please _DO NOT EDIT_ them as they get overwritten by sqlc.

The above process will setup the things to work with database using Go.
49 changes: 49 additions & 0 deletions docs/blog/2024-06-22-http-codes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
slug: http-codes-and-meanings
title: Http Status Code and Meanings
authors:
name: Sarvsav Sharma
title: Http Status Code and Meanings
url: https://github.com/sarvsav
image_url: https://github.com/sarvsav.png
tags: [http-status-codes]
---

## 2xx Successful

| Status Code | Meaning |
|-------------|--------------|
| 200 | OK |
| 201 | Created |
| 204 | No Content |

## 3xx Redirection

| Status Code | Meaning |
|-------------|----------------------|
| 301 | Moved Permanently |
| 302 | Found |
| 304 | Not Modified |

## 4xx Client Error

| Status Code | Meaning |
|-------------|----------------------|
| 400 | Bad Request |
| 401 | Unauthorized |
| 403 | Forbidden |
| 404 | Not Found |
| 405 | Method Not Allowed |
| 408 | Request Timeout |
| 409 | Conflict |
| 410 | Gone |
| 422 | Unprocessable Content|

## 5xx Server Error

| Status Code | Meaning |
|-------------|------------------------|
| 500 | Internal Server Error |
| 502 | Bad Gateway |
| 503 | Service Unavailable |
| 504 | Gateway Timeout |
6 changes: 6 additions & 0 deletions docs/blog/authors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ slorber:
title: Docusaurus maintainer
url: https://sebastienlorber.com
image_url: https://github.com/slorber.png

sarvsav:
name: Sarvsav Sharma
title: Maintainer of Go Starter Template
url: https://github.com/sarvsav
image_url: https://github.com/sarvsav.png
Loading
Loading