From b7dfd7e1731015cd04d7cafbc8cba2c99a061d3f Mon Sep 17 00:00:00 2001 From: Michael Li Date: Sat, 4 Feb 2023 11:23:39 +0800 Subject: [PATCH] sqlc: add migration support for sqlc scene --- internal/dao/slonik/ce/embed.go | 15 ++++++ internal/dao/slonik/ce/postgres/query.sql | 56 ---------------------- internal/dao/slonik/ce/postgres/schema.sql | 21 -------- internal/dao/slonik/topics.go | 6 +-- internal/migration/migration_embed.go | 32 ++++++++----- 5 files changed, 38 insertions(+), 92 deletions(-) create mode 100644 internal/dao/slonik/ce/embed.go delete mode 100644 internal/dao/slonik/ce/postgres/query.sql delete mode 100644 internal/dao/slonik/ce/postgres/schema.sql diff --git a/internal/dao/slonik/ce/embed.go b/internal/dao/slonik/ce/embed.go new file mode 100644 index 00000000..3cd77972 --- /dev/null +++ b/internal/dao/slonik/ce/embed.go @@ -0,0 +1,15 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +//go:build migration +// +build migration + +package ce + +import ( + "embed" +) + +//go:embed postgres/schema +var Files embed.FS diff --git a/internal/dao/slonik/ce/postgres/query.sql b/internal/dao/slonik/ce/postgres/query.sql deleted file mode 100644 index 33a78434..00000000 --- a/internal/dao/slonik/ce/postgres/query.sql +++ /dev/null @@ -1,56 +0,0 @@ --- name: GetAuthor :one -SELECT * FROM authors -WHERE author_id = $1; - --- name: DeleteBookExecResult :execresult -DELETE FROM books -WHERE book_id = $1; - --- name: DeleteBook :batchexec -DELETE FROM books -WHERE book_id = $1; - --- name: DeleteBookNamedFunc :batchexec -DELETE FROM books -WHERE book_id = sqlc.arg(book_id); - --- name: DeleteBookNamedSign :batchexec -DELETE FROM books -WHERE book_id = @book_id; - --- name: BooksByYear :batchmany -SELECT * FROM books -WHERE year = $1; - --- name: CreateAuthor :one -INSERT INTO authors (name) VALUES ($1) -RETURNING *; - --- name: CreateBook :batchone -INSERT INTO books ( - author_id, - isbn, - book_type, - title, - year, - available, - tags -) VALUES ( - $1, - $2, - $3, - $4, - $5, - $6, - $7 -) -RETURNING *; - --- name: UpdateBook :batchexec -UPDATE books -SET title = $1, tags = $2 -WHERE book_id = $3; - --- name: GetBiography :batchone -SELECT biography FROM authors -WHERE author_id = $1; diff --git a/internal/dao/slonik/ce/postgres/schema.sql b/internal/dao/slonik/ce/postgres/schema.sql deleted file mode 100644 index b79f773f..00000000 --- a/internal/dao/slonik/ce/postgres/schema.sql +++ /dev/null @@ -1,21 +0,0 @@ -CREATE TABLE authors ( - author_id SERIAL PRIMARY KEY, - name text NOT NULL DEFAULT '', - biography JSONB -); - -CREATE TYPE book_type AS ENUM ( - 'FICTION', - 'NONFICTION' -); - -CREATE TABLE books ( - book_id SERIAL PRIMARY KEY, - author_id integer NOT NULL REFERENCES authors(author_id), - isbn text NOT NULL DEFAULT '' UNIQUE, - book_type book_type NOT NULL DEFAULT 'FICTION', - title text NOT NULL DEFAULT '', - year integer NOT NULL DEFAULT 2000, - available timestamp with time zone NOT NULL DEFAULT 'NOW()', - tags varchar[] NOT NULL DEFAULT '{}' -); diff --git a/internal/dao/slonik/topics.go b/internal/dao/slonik/topics.go index 63f4efa3..55a1cc31 100644 --- a/internal/dao/slonik/topics.go +++ b/internal/dao/slonik/topics.go @@ -18,19 +18,19 @@ type topicServant struct { *pgxServant } -func (s *topicServant) CreateTag(tag *core.Tag) (*core.Tag, error) { +func (s *topicServant) UpsertTags(userId int64, tags []string) ([]*core.Tag, error) { // TODO debug.NotImplemented() return nil, nil } -func (s *topicServant) DeleteTag(tag *core.Tag) error { +func (s *topicServant) DecrTagsById(ids []int64) error { // TODO debug.NotImplemented() return nil } -func (s *topicServant) GetTags(conditions *core.ConditionsT, offset, limit int) ([]*core.Tag, error) { +func (s *topicServant) GetTags(category core.TagCategory, offset int, limit int) ([]*core.Tag, error) { // TODO debug.NotImplemented() return nil, nil diff --git a/internal/migration/migration_embed.go b/internal/migration/migration_embed.go index c2232d29..4c0949bb 100644 --- a/internal/migration/migration_embed.go +++ b/internal/migration/migration_embed.go @@ -19,6 +19,7 @@ import ( "github.com/golang-migrate/migrate/v4/source" "github.com/golang-migrate/migrate/v4/source/iofs" "github.com/rocboss/paopao-ce/internal/conf" + "github.com/rocboss/paopao-ce/internal/dao/slonik/ce" "github.com/rocboss/paopao-ce/scripts/migration" "github.com/sirupsen/logrus" ) @@ -55,18 +56,25 @@ func Run() { } migrationsTable := conf.DatabaseSetting.TablePrefix + "schema_migrations" - if cfg.If("MySQL") { - srcDriver, err = iofs.New(migration.Files, "mysql") - dbDriver, err2 = mysql.WithInstance(db, &mysql.Config{MigrationsTable: migrationsTable}) - } else if cfg.If("PostgreSQL") || cfg.If("Postgres") { - srcDriver, err = iofs.New(migration.Files, "postgres") - dbDriver, err2 = postgres.WithInstance(db, &postgres.Config{MigrationsTable: migrationsTable}) - } else if cfg.If("Sqlite3") { - srcDriver, err = iofs.New(migration.Files, "sqlite3") - dbDriver, err2 = sqlite3.WithInstance(db, &sqlite3.Config{MigrationsTable: migrationsTable}) - } else { - srcDriver, err = iofs.New(migration.Files, "mysql") - dbDriver, err2 = mysql.WithInstance(db, &mysql.Config{MigrationsTable: migrationsTable}) + if cfg.If("Gorm") || cfg.If("Sqlx") { + if cfg.If("MySQL") { + srcDriver, err = iofs.New(migration.Files, "mysql") + dbDriver, err2 = mysql.WithInstance(db, &mysql.Config{MigrationsTable: migrationsTable}) + } else if cfg.If("PostgreSQL") || cfg.If("Postgres") { + srcDriver, err = iofs.New(migration.Files, "postgres") + dbDriver, err2 = postgres.WithInstance(db, &postgres.Config{MigrationsTable: migrationsTable}) + } else if cfg.If("Sqlite3") { + srcDriver, err = iofs.New(migration.Files, "sqlite3") + dbDriver, err2 = sqlite3.WithInstance(db, &sqlite3.Config{MigrationsTable: migrationsTable}) + } else { + srcDriver, err = iofs.New(migration.Files, "mysql") + dbDriver, err2 = mysql.WithInstance(db, &mysql.Config{MigrationsTable: migrationsTable}) + } + } else if cfg.If("Sqlc") { + if cfg.If("PostgreSQL") || cfg.If("Postgres") { + srcDriver, err = iofs.New(ce.Files, "postgres/schema") + dbDriver, err2 = postgres.WithInstance(db, &postgres.Config{MigrationsTable: migrationsTable}) + } } if err2 != nil {