From 2efa42651f2973dc27e27122bddf997a0d9a4ec2 Mon Sep 17 00:00:00 2001 From: Bastien Riviere Date: Mon, 29 Jan 2024 18:45:50 +0100 Subject: [PATCH] chore(dev): setup sqlc and atlas --- README.md | 1 + atlas.hcl | 7 +++++++ docker-compose.yaml | 2 +- flake.nix | 12 ++++++++++++ justfile | 23 +++++++++++++++++++++++ sqlc.yaml | 14 ++++++++++++++ 6 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 atlas.hcl create mode 100644 justfile create mode 100644 sqlc.yaml diff --git a/README.md b/README.md index ddf71cc..710c9c3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # short + URL Shortener written in Go diff --git a/atlas.hcl b/atlas.hcl new file mode 100644 index 0000000..45d760e --- /dev/null +++ b/atlas.hcl @@ -0,0 +1,7 @@ +env "local" { + src = "file://sql/schema.sql" + url = "postgres://short:short@localhost:5432/short?sslmode=disable" + migration { + dir = "file://sql/migrations" + } +} diff --git a/docker-compose.yaml b/docker-compose.yaml index 96bdb90..95adb55 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,4 +1,4 @@ -version: '3.1' +version: "3.1" services: db: diff --git a/flake.nix b/flake.nix index be8b429..9ef6673 100644 --- a/flake.nix +++ b/flake.nix @@ -21,6 +21,7 @@ alejandra.enable = true; deadnix.enable = true; gofumpt.enable = true; + prettier.enable = true; }; }; @@ -29,6 +30,17 @@ # Go tools go gopls + + # Database + atlas + sqlc + postgresql_16 + + # Lint + golangci-lint + + # Tools + just ]; }; }; diff --git a/justfile b/justfile new file mode 100644 index 0000000..26e2511 --- /dev/null +++ b/justfile @@ -0,0 +1,23 @@ +alias b := build +alias r := run +alias l := lint + +build: + go build . + +run: + go run . + +lint: + golangci-lint run + +sqlc: + sqlc generate + sqlc vet + +gen-migration NAME: + atlas migrate diff {{NAME}} --env local + atlas migrate apply --env local + +migrate: + atlas migrate apply --env local diff --git a/sqlc.yaml b/sqlc.yaml new file mode 100644 index 0000000..fe38dcd --- /dev/null +++ b/sqlc.yaml @@ -0,0 +1,14 @@ +version: "2" +sql: + - engine: "postgresql" + queries: "sql/query.sql" + schema: "sql/schema.sql" + database: + uri: postgres://short:short@localhost:5432/short + gen: + go: + package: "db" + out: "internal/db" + sql_package: "pgx/v5" + rules: + - sqlc/db-prepare