From 875a6d1a333d9de9bc47bd2898c6218f3b6ef234 Mon Sep 17 00:00:00 2001 From: Bastien Riviere Date: Mon, 29 Jan 2024 19:32:01 +0100 Subject: [PATCH] feat: create initial database schema --- atlas.hcl | 1 + go.mod | 24 ++++++++++ go.sum | 46 +++++++++++++++++++ internal/db/db.go | 32 +++++++++++++ internal/db/models.go | 13 ++++++ internal/db/query.sql.go | 22 +++++++++ justfile | 3 ++ .../20240129183014_create_url_table.sql | 4 ++ sql/migrations/atlas.sum | 2 + sql/query.sql | 3 ++ sql/schema.sql | 5 ++ 11 files changed, 155 insertions(+) create mode 100644 go.sum create mode 100644 internal/db/db.go create mode 100644 internal/db/models.go create mode 100644 internal/db/query.sql.go create mode 100644 sql/migrations/20240129183014_create_url_table.sql create mode 100644 sql/migrations/atlas.sum create mode 100644 sql/query.sql create mode 100644 sql/schema.sql diff --git a/atlas.hcl b/atlas.hcl index 45d760e..ebf3bdb 100644 --- a/atlas.hcl +++ b/atlas.hcl @@ -1,6 +1,7 @@ env "local" { src = "file://sql/schema.sql" url = "postgres://short:short@localhost:5432/short?sslmode=disable" + dev = "postgres://short:short@localhost:5432/short?sslmode=disable" migration { dir = "file://sql/migrations" } diff --git a/go.mod b/go.mod index a357bdc..9a5b87b 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,27 @@ module github.com/babariviere/short go 1.21.6 + +require ( + github.com/dlclark/regexp2 v1.10.0 // indirect + github.com/fatih/color v1.16.0 // indirect + github.com/ghodss/yaml v1.0.0 // indirect + github.com/go-faster/errors v0.7.1 // indirect + github.com/go-faster/jx v1.1.0 // indirect + github.com/go-faster/yaml v0.4.6 // indirect + github.com/google/uuid v1.5.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/ogen-go/ogen v0.81.2 // indirect + github.com/segmentio/asm v1.2.0 // indirect + go.uber.org/multierr v1.11.0 // indirect + go.uber.org/zap v1.26.0 // indirect + golang.org/x/exp v0.0.0-20230725093048-515e97ebf090 // indirect + golang.org/x/mod v0.14.0 // indirect + golang.org/x/net v0.20.0 // indirect + golang.org/x/sync v0.6.0 // indirect + golang.org/x/sys v0.16.0 // indirect + golang.org/x/text v0.14.0 // indirect + golang.org/x/tools v0.17.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..f41f8bd --- /dev/null +++ b/go.sum @@ -0,0 +1,46 @@ +github.com/dlclark/regexp2 v1.10.0 h1:+/GIL799phkJqYW+3YbOd8LCcbHzT0Pbo8zl70MHsq0= +github.com/dlclark/regexp2 v1.10.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= +github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= +github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= +github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/go-faster/errors v0.7.1 h1:MkJTnDoEdi9pDabt1dpWf7AA8/BaSYZqibYyhZ20AYg= +github.com/go-faster/errors v0.7.1/go.mod h1:5ySTjWFiphBs07IKuiL69nxdfd5+fzh1u7FPGZP2quo= +github.com/go-faster/jx v1.1.0 h1:ZsW3wD+snOdmTDy9eIVgQdjUpXRRV4rqW8NS3t+20bg= +github.com/go-faster/jx v1.1.0/go.mod h1:vKDNikrKoyUmpzaJ0OkIkRQClNHFX/nF3dnTJZb3skg= +github.com/go-faster/yaml v0.4.6 h1:lOK/EhI04gCpPgPhgt0bChS6bvw7G3WwI8xxVe0sw9I= +github.com/go-faster/yaml v0.4.6/go.mod h1:390dRIvV4zbnO7qC9FGo6YYutc+wyyUSHBgbXL52eXk= +github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= +github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/ogen-go/ogen v0.81.2 h1:Dj5vSgC/1oqLE5t0T5qd4ARgsKTupJWsh3rW9/C7Lvk= +github.com/ogen-go/ogen v0.81.2/go.mod h1:10Ch7SIzBMSLB8TVEt8KclMKkRyJ5qCh4Cfs0pdeoh8= +github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys= +github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= +go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= +golang.org/x/exp v0.0.0-20230725093048-515e97ebf090 h1:Di6/M8l0O2lCLc6VVRWhgCiApHV8MnQurBnFSHsQtNY= +golang.org/x/exp v0.0.0-20230725093048-515e97ebf090/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= +golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= +golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo= +golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= +golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= +golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc= +golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/internal/db/db.go b/internal/db/db.go new file mode 100644 index 0000000..9959cd0 --- /dev/null +++ b/internal/db/db.go @@ -0,0 +1,32 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 + +package db + +import ( + "context" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" +) + +type DBTX interface { + Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error) + Query(context.Context, string, ...interface{}) (pgx.Rows, error) + QueryRow(context.Context, string, ...interface{}) pgx.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx pgx.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/db/models.go b/internal/db/models.go new file mode 100644 index 0000000..5d8c6fe --- /dev/null +++ b/internal/db/models.go @@ -0,0 +1,13 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 + +package db + +import () + +type Url struct { + ID int32 + Hash string + LongUrl string +} diff --git a/internal/db/query.sql.go b/internal/db/query.sql.go new file mode 100644 index 0000000..c5a6a58 --- /dev/null +++ b/internal/db/query.sql.go @@ -0,0 +1,22 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 +// source: query.sql + +package db + +import ( + "context" +) + +const getURLByHash = `-- name: GetURLByHash :one +SELECT id, hash, long_url FROM urls +WHERE hash = $1 +` + +func (q *Queries) GetURLByHash(ctx context.Context, hash string) (Url, error) { + row := q.db.QueryRow(ctx, getURLByHash, hash) + var i Url + err := row.Scan(&i.ID, &i.Hash, &i.LongUrl) + return i, err +} diff --git a/justfile b/justfile index 26e2511..c085f62 100644 --- a/justfile +++ b/justfile @@ -11,6 +11,9 @@ run: lint: golangci-lint run +gen: + go generate ./... + sqlc: sqlc generate sqlc vet diff --git a/sql/migrations/20240129183014_create_url_table.sql b/sql/migrations/20240129183014_create_url_table.sql new file mode 100644 index 0000000..e1043b9 --- /dev/null +++ b/sql/migrations/20240129183014_create_url_table.sql @@ -0,0 +1,4 @@ +-- Create "urls" table +CREATE TABLE "public"."urls" ("id" serial NOT NULL, "hash" text NOT NULL, "long_url" text NOT NULL, PRIMARY KEY ("id")); +-- Create index "urls_hash_key" to table: "urls" +CREATE UNIQUE INDEX "urls_hash_key" ON "public"."urls" ("hash"); diff --git a/sql/migrations/atlas.sum b/sql/migrations/atlas.sum new file mode 100644 index 0000000..d2bc96e --- /dev/null +++ b/sql/migrations/atlas.sum @@ -0,0 +1,2 @@ +h1:iaq5N24NlZigxTmAlrTtB5kyXb52a+gVSEHYpfgvvwE= +20240129183014_create_url_table.sql h1:k/l+yABFDePCCXnnZ4OM9k47oE4CdvvN6tjSCZelAo0= diff --git a/sql/query.sql b/sql/query.sql new file mode 100644 index 0000000..b2e648e --- /dev/null +++ b/sql/query.sql @@ -0,0 +1,3 @@ +-- name: GetURLByHash :one +SELECT * FROM urls +WHERE hash = $1; diff --git a/sql/schema.sql b/sql/schema.sql new file mode 100644 index 0000000..27b169c --- /dev/null +++ b/sql/schema.sql @@ -0,0 +1,5 @@ +CREATE TABLE urls ( + id SERIAL PRIMARY KEY, + hash TEXT NOT NULL UNIQUE, + long_url TEXT NOT NULL +);