diff --git a/server/services/store/sqlstore/migrate.go b/server/services/store/sqlstore/migrate.go new file mode 100644 index 000000000..d0b2a2e7a --- /dev/null +++ b/server/services/store/sqlstore/migrate.go @@ -0,0 +1,35 @@ +package sqlstore + +import ( + "github.com/golang-migrate/migrate/v4" + "github.com/golang-migrate/migrate/v4/database" + "github.com/golang-migrate/migrate/v4/database/postgres" + "github.com/golang-migrate/migrate/v4/database/sqlite3" + _ "github.com/golang-migrate/migrate/v4/source/file" + _ "github.com/lib/pq" +) + +func (s *SQLStore) Migrate() error { + var driver database.Driver + var err error + var migrationsDir string + if s.dbType == "sqlite3" { + driver, err = sqlite3.WithInstance(s.db, &sqlite3.Config{}) + migrationsDir = "file://./server/services/store/sqlstore/migrations/sqlite" + } + if s.dbType == "postgres" { + driver, err = postgres.WithInstance(s.db, &postgres.Config{}) + migrationsDir = "file://./server/services/store/sqlstore/migrations/postgres" + } + m, err := migrate.NewWithDatabaseInstance(migrationsDir, s.dbType, driver) + if err != nil { + return err + } + // defer m.Close() + + err = m.Up() + if err != nil && err != migrate.ErrNoChange { + return err + } + return nil +} diff --git a/server/services/store/sqlstore/migrations/postgres/000001_init.down.sql b/server/services/store/sqlstore/migrations/postgres/000001_init.down.sql new file mode 100644 index 000000000..7a4fcb3dc --- /dev/null +++ b/server/services/store/sqlstore/migrations/postgres/000001_init.down.sql @@ -0,0 +1 @@ +DROP TABLE blocks; diff --git a/server/services/store/sqlstore/migrations/postgres/000001_init.up.sql b/server/services/store/sqlstore/migrations/postgres/000001_init.up.sql new file mode 100644 index 000000000..c38e097c2 --- /dev/null +++ b/server/services/store/sqlstore/migrations/postgres/000001_init.up.sql @@ -0,0 +1,13 @@ +CREATE TABLE IF NOT EXISTS blocks ( + id VARCHAR(36), + insert_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), + parent_id VARCHAR(36), + schema BIGINT, + type TEXT, + title TEXT, + fields TEXT, + create_at BIGINT, + update_at BIGINT, + delete_at BIGINT, + PRIMARY KEY (id, insert_at) +); diff --git a/server/services/store/sqlstore/migrations/sqlite/000001_init.down.sql b/server/services/store/sqlstore/migrations/sqlite/000001_init.down.sql new file mode 100644 index 000000000..7a4fcb3dc --- /dev/null +++ b/server/services/store/sqlstore/migrations/sqlite/000001_init.down.sql @@ -0,0 +1 @@ +DROP TABLE blocks; diff --git a/server/services/store/sqlstore/migrations/sqlite/000001_init.up.sql b/server/services/store/sqlstore/migrations/sqlite/000001_init.up.sql new file mode 100644 index 000000000..2046637a6 --- /dev/null +++ b/server/services/store/sqlstore/migrations/sqlite/000001_init.up.sql @@ -0,0 +1,13 @@ +CREATE TABLE IF NOT EXISTS blocks ( + id VARCHAR(36), + insert_at DATETIME NOT NULL DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')), + parent_id VARCHAR(36), + schema BIGINT, + type TEXT, + title TEXT, + fields TEXT, + create_at BIGINT, + update_at BIGINT, + delete_at BIGINT, + PRIMARY KEY (id, insert_at) +);