You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[+] Running 2/0 β Network sqlc_default Created 0.1s β Container postgres Created 0.0sAttaching to postgrespostgres |postgres | PostgreSQL Database directory appears to contain a database; Skipping initializationpostgres |postgres |postgres | 2023-09-28 09:17:50.737 UTC [1] LOG: starting PostgreSQL 16.0 (Debian 16.0-1.pgdg120+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bitpostgres | 2023-09-28 09:17:50.737 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432postgres | 2023-09-28 09:17:50.737 UTC [1] LOG: listening on IPv6 address "::", port 5432postgres | 2023-09-28 09:17:50.740 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"postgres | 2023-09-28 09:17:50.751 UTC [30] LOG: database system was shut down at 2023-09-28 08:50:35 UTCpostgres | 2023-09-28 09:17:50.770 UTC [1] LOG: database system is ready to accept connections
3. You have to migrate the database.
π― It is a "database-first" ORM as opposed to "code-first" (like gorm/gorp). That means you must first create your database schema.
$ migrate -path database/migrations -database "postgresql://user:password@localhost:5432/fiber_demo?sslmode=disable" -verbose up
2023/09/28 20:00:00 Start buffering 1/u create_initial_table2023/09/28 20:00:00 Read and execute 1/u create_initial_table2023/09/28 20:00:00 Finished 1/u create_initial_table (read 24.693541ms, ran 68.30925ms)2023/09/28 20:00:00 Finished after 100.661625ms2023/09/28 20:00:00 Closing source and database
3. Rollback Migrate
$ migrate -path database/migrations -database "postgresql://user:password@localhost:5432/fiber_demo?sslmode=disable" -verbose down
2023/09/28 20:00:00 Are you sure you want to apply all down migrations? [y/N]y2023/09/28 20:00:00 Applying all down migrations2023/09/28 20:00:00 Start buffering 1/d create_initial_table2023/09/28 20:00:00 Read and execute 1/d create_initial_table2023/09/28 20:00:00 Finished 1/d create_initial_table (read 39.681125ms, ran 66.220125ms)2023/09/28 20:00:00 Finished after 1.83152475s
4. Use sqlc
1. Install
# Go 1.17 and above:
$ go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
# Go 1.16 and below:
go get github.com/sqlc-dev/sqlc/cmd/sqlc
-- name: GetAuthors :manySELECT*FROM author;
-- name: GetAuthor :oneSELECT*FROM author WHERE id = $1;
-- name: NewAuthor :oneINSERT INTO author (email, name) VALUES ($1, $2) RETURNING *;
-- name: UpdateAuthor :oneUPDATE author SET email = $1, name = $2WHERE id = $3 RETURNING *;
-- name: DeleteAuthor :execDELETEFROM author WHERE id = $1;
post.sql
-- name: GetPosts :manySELECT*FROM post;
-- name: GetPost :oneSELECT*FROM post WHERE id = $1;
-- name: NewPost :oneINSERT INTO post (title, content, author) VALUES ($1, $2, $3) RETURNING *;
-- name: UpdatePost :oneUPDATE post SET title = $1, content = $2, author = $3WHERE id = $4 RETURNING *;
-- name: DeletePost :execDELETEFROM post WHERE id = $1;