From 40dcd82e542a5802600cc24024b0ae964239ce4a Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Mon, 16 Sep 2024 14:42:32 -0400 Subject: [PATCH] =?UTF-8?q?refactor:=20d=C3=A9placer=20schema=20vers=20sql?= =?UTF-8?q?/schema.sql=20et=20sql.Schema()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 1 + data/dataclient.go | 3 ++- models/models.go | 9 --------- sql/schema.sql | 7 +++++++ sql/sql.go | 10 ++++++++++ 5 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 sql/schema.sql create mode 100644 sql/sql.go diff --git a/Dockerfile b/Dockerfile index 6a9997a..6090d72 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,6 +11,7 @@ ADD data/ data/ ADD handlers/ handlers/ ADD models/ models/ ADD responses/ responses/ +ADD sql/ sql/ ADD web/ web/ RUN CGO_ENABLED=0 go build -a -o bottin-agenda . diff --git a/data/dataclient.go b/data/dataclient.go index 92c1de0..df6fe32 100644 --- a/data/dataclient.go +++ b/data/dataclient.go @@ -5,6 +5,7 @@ import ( "fmt" "git.agecem.com/agecem/bottin-agenda/models" + "git.agecem.com/agecem/bottin-agenda/sql" _ "github.com/jackc/pgx/stdlib" "github.com/jmoiron/sqlx" "github.com/spf13/viper" @@ -59,7 +60,7 @@ func NewDataClient(connection PostgresConnection) (*DataClient, error) { } func (d *DataClient) Seed() (int64, error) { - result, err := d.DB.Exec(models.Schema) + result, err := d.DB.Exec(sql.Schema()) if err != nil { return 0, err } diff --git a/models/models.go b/models/models.go index f3e6858..96f5ca3 100644 --- a/models/models.go +++ b/models/models.go @@ -2,15 +2,6 @@ package models import "time" -var Schema = ` -CREATE TABLE IF NOT EXISTS transactions ( - id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - membre_id VARCHAR(7), - given_at TIMESTAMP, - is_perpetual BOOLEAN -); -` - type Transaction struct { ID string `db:"id" json:"id"` MembreID string `db:"membre_id" json:"membre_id"` diff --git a/sql/schema.sql b/sql/schema.sql new file mode 100644 index 0000000..9cf1a31 --- /dev/null +++ b/sql/schema.sql @@ -0,0 +1,7 @@ +-- Schema +CREATE TABLE IF NOT EXISTS transactions ( + id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, + membre_id VARCHAR(7) NOT NULL, + given_at TIMESTAMP DEFAULT current_timestamp, + is_perpetual BOOLEAN NOT NULL +); diff --git a/sql/sql.go b/sql/sql.go new file mode 100644 index 0000000..c38ad80 --- /dev/null +++ b/sql/sql.go @@ -0,0 +1,10 @@ +package sql + +import _ "embed" + +//go:embed schema.sql +var schema string + +func Schema() string { + return schema +} -- 2.45.2