mirror of
https://github.com/aykhans/slash-e.git
synced 2026-09-23 14:26:49 +00:00
refactor: add migrator tests
This commit is contained in:
@@ -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);
|
|
||||||
@@ -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);
|
|
||||||
+1
-5
@@ -156,11 +156,7 @@ func (s *Store) preMigrate(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) getMigrationBasePath() string {
|
func (s *Store) getMigrationBasePath() string {
|
||||||
mode := "dev"
|
return fmt.Sprintf("migration/%s/", s.profile.Driver)
|
||||||
if s.profile.Mode == "prod" {
|
|
||||||
mode = "prod"
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("migration/%s/%s/", s.profile.Driver, mode)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) GetCurrentSchemaVersion() (string, error) {
|
func (s *Store) GetCurrentSchemaVersion() (string, error) {
|
||||||
|
|||||||
@@ -1,17 +1,45 @@
|
|||||||
package teststore
|
package teststore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"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) {
|
func TestGetCurrentSchemaVersion(t *testing.T) {
|
||||||
ctx := context.Background()
|
tests := []struct {
|
||||||
ts := NewTestingStore(ctx, t)
|
driver string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
driver: "sqlite",
|
||||||
|
expected: "1.0.1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
driver: "postgres",
|
||||||
|
expected: "1.0.1",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
currentSchemaVersion, err := ts.GetCurrentSchemaVersion()
|
for _, tt := range tests {
|
||||||
require.NoError(t, err)
|
t.Run(tt.driver, func(t *testing.T) {
|
||||||
require.Equal(t, "1.0.1", currentSchemaVersion)
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user