Merge pull request 'refactor: déplacer schema vers sql/schema.sql et sql.Schema()' (#41) from vlbeaudoin/refactor/embed-schema into main

Reviewed-on: #41
This commit is contained in:
Victor Lacasse-Beaudoin 2024-09-16 14:48:29 -04:00
commit 210b3f6524
5 changed files with 20 additions and 10 deletions

View file

@ -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 .

View file

@ -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
}

View file

@ -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"`

7
sql/schema.sql Normal file
View file

@ -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
);

10
sql/sql.go Normal file
View file

@ -0,0 +1,10 @@
package sql
import _ "embed"
//go:embed schema.sql
var schema string
func Schema() string {
return schema
}