From 3c00c5adaa7897ae73dd7201fec19124d90b6535 Mon Sep 17 00:00:00 2001 From: johnnyjoy Date: Wed, 11 Jun 2025 20:49:26 +0800 Subject: [PATCH] refactor: add migrator tests --- .../{prod => }/1.0/00__visibility.sql | 0 store/migration/postgres/{dev => }/LATEST.sql | 0 store/migration/postgres/prod/LATEST.sql | 76 ------------------- .../{prod => }/0.2/00__create_index.sql | 0 .../{prod => }/0.3/00__add_og_metadata.sql | 0 .../{prod => }/0.4/00__add_shortcut_title.sql | 0 .../sqlite/{prod => }/0.5/00__drop_idp.sql | 0 .../sqlite/{prod => }/0.5/01__collection.sql | 0 .../sqlite/{prod => }/1.0/00__visibility.sql | 0 store/migration/sqlite/{dev => }/LATEST.sql | 0 store/migration/sqlite/prod/LATEST.sql | 76 ------------------- store/migrator.go | 6 +- store/test/migrator_test.go | 40 ++++++++-- 13 files changed, 35 insertions(+), 163 deletions(-) rename store/migration/postgres/{prod => }/1.0/00__visibility.sql (100%) rename store/migration/postgres/{dev => }/LATEST.sql (100%) delete mode 100644 store/migration/postgres/prod/LATEST.sql rename store/migration/sqlite/{prod => }/0.2/00__create_index.sql (100%) rename store/migration/sqlite/{prod => }/0.3/00__add_og_metadata.sql (100%) rename store/migration/sqlite/{prod => }/0.4/00__add_shortcut_title.sql (100%) rename store/migration/sqlite/{prod => }/0.5/00__drop_idp.sql (100%) rename store/migration/sqlite/{prod => }/0.5/01__collection.sql (100%) rename store/migration/sqlite/{prod => }/1.0/00__visibility.sql (100%) rename store/migration/sqlite/{dev => }/LATEST.sql (100%) delete mode 100644 store/migration/sqlite/prod/LATEST.sql diff --git a/store/migration/postgres/prod/1.0/00__visibility.sql b/store/migration/postgres/1.0/00__visibility.sql similarity index 100% rename from store/migration/postgres/prod/1.0/00__visibility.sql rename to store/migration/postgres/1.0/00__visibility.sql diff --git a/store/migration/postgres/dev/LATEST.sql b/store/migration/postgres/LATEST.sql similarity index 100% rename from store/migration/postgres/dev/LATEST.sql rename to store/migration/postgres/LATEST.sql diff --git a/store/migration/postgres/prod/LATEST.sql b/store/migration/postgres/prod/LATEST.sql deleted file mode 100644 index 75e0683..0000000 --- a/store/migration/postgres/prod/LATEST.sql +++ /dev/null @@ -1,76 +0,0 @@ --- migration_history -CREATE TABLE migration_history ( - version TEXT NOT NULL PRIMARY KEY, - created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()) -); - --- workspace_setting -CREATE TABLE workspace_setting ( - key TEXT NOT NULL UNIQUE, - value TEXT NOT NULL -); - --- user -CREATE TABLE "user" ( - id SERIAL PRIMARY KEY, - created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()), - updated_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()), - row_status TEXT NOT NULL CHECK (row_status IN ('NORMAL', 'ARCHIVED')) DEFAULT 'NORMAL', - email TEXT NOT NULL UNIQUE, - nickname TEXT NOT NULL, - password_hash TEXT NOT NULL, - role TEXT NOT NULL CHECK (role IN ('ADMIN', 'USER')) DEFAULT 'USER' -); - -CREATE INDEX idx_user_email ON "user"(email); - --- user_setting -CREATE TABLE user_setting ( - user_id INTEGER REFERENCES "user"(id) NOT NULL, - key TEXT NOT NULL, - value TEXT NOT NULL, - PRIMARY KEY (user_id, key) -); - --- shortcut -CREATE TABLE shortcut ( - id SERIAL PRIMARY KEY, - creator_id INTEGER REFERENCES "user"(id) NOT NULL, - created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()), - updated_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()), - row_status TEXT NOT NULL CHECK (row_status IN ('NORMAL', 'ARCHIVED')) DEFAULT 'NORMAL', - name TEXT NOT NULL UNIQUE, - link TEXT NOT NULL, - title TEXT NOT NULL DEFAULT '', - description TEXT NOT NULL DEFAULT '', - visibility TEXT NOT NULL CHECK (visibility IN ('PRIVATE', 'WORKSPACE', 'PUBLIC')) DEFAULT 'PRIVATE', - tag TEXT NOT NULL DEFAULT '', - og_metadata TEXT NOT NULL DEFAULT '{}' -); - -CREATE INDEX idx_shortcut_name ON shortcut(name); - --- activity -CREATE TABLE activity ( - id SERIAL PRIMARY KEY, - creator_id INTEGER NOT NULL, - created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()), - type TEXT NOT NULL DEFAULT '', - level TEXT NOT NULL CHECK (level IN ('INFO', 'WARN', 'ERROR')) DEFAULT 'INFO', - payload TEXT NOT NULL DEFAULT '{}' -); - --- collection -CREATE TABLE collection ( - id SERIAL PRIMARY KEY, - creator_id INTEGER REFERENCES "user"(id) NOT NULL, - created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()), - updated_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()), - name TEXT NOT NULL UNIQUE, - title TEXT NOT NULL DEFAULT '', - description TEXT NOT NULL DEFAULT '', - shortcut_ids INTEGER ARRAY NOT NULL, - visibility TEXT NOT NULL CHECK (visibility IN ('PRIVATE', 'WORKSPACE', 'PUBLIC')) DEFAULT 'PRIVATE' -); - -CREATE INDEX idx_collection_name ON collection(name); diff --git a/store/migration/sqlite/prod/0.2/00__create_index.sql b/store/migration/sqlite/0.2/00__create_index.sql similarity index 100% rename from store/migration/sqlite/prod/0.2/00__create_index.sql rename to store/migration/sqlite/0.2/00__create_index.sql diff --git a/store/migration/sqlite/prod/0.3/00__add_og_metadata.sql b/store/migration/sqlite/0.3/00__add_og_metadata.sql similarity index 100% rename from store/migration/sqlite/prod/0.3/00__add_og_metadata.sql rename to store/migration/sqlite/0.3/00__add_og_metadata.sql diff --git a/store/migration/sqlite/prod/0.4/00__add_shortcut_title.sql b/store/migration/sqlite/0.4/00__add_shortcut_title.sql similarity index 100% rename from store/migration/sqlite/prod/0.4/00__add_shortcut_title.sql rename to store/migration/sqlite/0.4/00__add_shortcut_title.sql diff --git a/store/migration/sqlite/prod/0.5/00__drop_idp.sql b/store/migration/sqlite/0.5/00__drop_idp.sql similarity index 100% rename from store/migration/sqlite/prod/0.5/00__drop_idp.sql rename to store/migration/sqlite/0.5/00__drop_idp.sql diff --git a/store/migration/sqlite/prod/0.5/01__collection.sql b/store/migration/sqlite/0.5/01__collection.sql similarity index 100% rename from store/migration/sqlite/prod/0.5/01__collection.sql rename to store/migration/sqlite/0.5/01__collection.sql diff --git a/store/migration/sqlite/prod/1.0/00__visibility.sql b/store/migration/sqlite/1.0/00__visibility.sql similarity index 100% rename from store/migration/sqlite/prod/1.0/00__visibility.sql rename to store/migration/sqlite/1.0/00__visibility.sql diff --git a/store/migration/sqlite/dev/LATEST.sql b/store/migration/sqlite/LATEST.sql similarity index 100% rename from store/migration/sqlite/dev/LATEST.sql rename to store/migration/sqlite/LATEST.sql diff --git a/store/migration/sqlite/prod/LATEST.sql b/store/migration/sqlite/prod/LATEST.sql deleted file mode 100644 index 3933460..0000000 --- a/store/migration/sqlite/prod/LATEST.sql +++ /dev/null @@ -1,76 +0,0 @@ --- migration_history -CREATE TABLE migration_history ( - version TEXT NOT NULL PRIMARY KEY, - created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')) -); - --- workspace_setting -CREATE TABLE workspace_setting ( - key TEXT NOT NULL UNIQUE, - value TEXT NOT NULL -); - --- user -CREATE TABLE user ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')), - updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')), - row_status TEXT NOT NULL CHECK (row_status IN ('NORMAL', 'ARCHIVED')) DEFAULT 'NORMAL', - email TEXT NOT NULL UNIQUE, - nickname TEXT NOT NULL, - password_hash TEXT NOT NULL, - role TEXT NOT NULL CHECK (role IN ('ADMIN', 'USER')) DEFAULT 'USER' -); - -CREATE INDEX idx_user_email ON user(email); - --- user_setting -CREATE TABLE user_setting ( - user_id INTEGER NOT NULL, - key TEXT NOT NULL, - value TEXT NOT NULL, - UNIQUE(user_id, key) -); - --- shortcut -CREATE TABLE shortcut ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - creator_id INTEGER NOT NULL, - created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')), - updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')), - row_status TEXT NOT NULL CHECK (row_status IN ('NORMAL', 'ARCHIVED')) DEFAULT 'NORMAL', - name TEXT NOT NULL UNIQUE, - link TEXT NOT NULL, - title TEXT NOT NULL DEFAULT '', - description TEXT NOT NULL DEFAULT '', - visibility TEXT NOT NULL CHECK (visibility IN ('PRIVATE', 'WORKSPACE', 'PUBLIC')) DEFAULT 'PRIVATE', - tag TEXT NOT NULL DEFAULT '', - og_metadata TEXT NOT NULL DEFAULT '{}' -); - -CREATE INDEX idx_shortcut_name ON shortcut(name); - --- activity -CREATE TABLE activity ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - creator_id INTEGER NOT NULL, - created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')), - type TEXT NOT NULL DEFAULT '', - level TEXT NOT NULL CHECK (level IN ('INFO', 'WARN', 'ERROR')) DEFAULT 'INFO', - payload TEXT NOT NULL DEFAULT '{}' -); - --- collection -CREATE TABLE collection ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - creator_id INTEGER NOT NULL, - created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')), - updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')), - name TEXT NOT NULL UNIQUE, - title TEXT NOT NULL DEFAULT '', - description TEXT NOT NULL DEFAULT '', - shortcut_ids INTEGER[] NOT NULL, - visibility TEXT NOT NULL CHECK (visibility IN ('PRIVATE', 'WORKSPACE', 'PUBLIC')) DEFAULT 'PRIVATE' -); - -CREATE INDEX idx_collection_name ON collection(name); diff --git a/store/migrator.go b/store/migrator.go index a78dadb..40d9a1e 100644 --- a/store/migrator.go +++ b/store/migrator.go @@ -156,11 +156,7 @@ func (s *Store) preMigrate(ctx context.Context) error { } func (s *Store) getMigrationBasePath() string { - mode := "dev" - if s.profile.Mode == "prod" { - mode = "prod" - } - return fmt.Sprintf("migration/%s/%s/", s.profile.Driver, mode) + return fmt.Sprintf("migration/%s/", s.profile.Driver) } func (s *Store) GetCurrentSchemaVersion() (string, error) { diff --git a/store/test/migrator_test.go b/store/test/migrator_test.go index 83b87d1..c89a92f 100644 --- a/store/test/migrator_test.go +++ b/store/test/migrator_test.go @@ -1,17 +1,45 @@ package teststore import ( - "context" "testing" "github.com/stretchr/testify/require" + "github.com/yourselfhosted/slash/server/common" + "github.com/yourselfhosted/slash/server/profile" + "github.com/yourselfhosted/slash/store" ) func TestGetCurrentSchemaVersion(t *testing.T) { - ctx := context.Background() - ts := NewTestingStore(ctx, t) + tests := []struct { + driver string + expected string + }{ + { + driver: "sqlite", + expected: "1.0.1", + }, + { + driver: "postgres", + expected: "1.0.1", + }, + } - currentSchemaVersion, err := ts.GetCurrentSchemaVersion() - require.NoError(t, err) - require.Equal(t, "1.0.1", currentSchemaVersion) + for _, tt := range tests { + t.Run(tt.driver, func(t *testing.T) { + ts := newTestingStoreWithConfig(tt.driver) + currentSchemaVersion, err := ts.GetCurrentSchemaVersion() + require.NoError(t, err) + require.Equal(t, tt.expected, currentSchemaVersion) + }) + } +} + +// newTestingStoreWithConfig creates a testing store with specific driver configuration +func newTestingStoreWithConfig(driver string) *store.Store { + profile := &profile.Profile{ + Mode: "prod", + Driver: driver, + Version: common.GetCurrentVersion("prod"), + } + return store.New(nil, profile) }