Update migrations for postgres

This commit is contained in:
2025-02-25 22:29:06 +01:00
parent 3b7deaa107
commit 96fc490c1d
6 changed files with 88 additions and 4 deletions

View File

@@ -10,7 +10,7 @@ import (
"github.com/golang-migrate/migrate/v4/source/iofs" "github.com/golang-migrate/migrate/v4/source/iofs"
) )
//go:embed migrations/*.sql //go:embed migrations/sqlite/*.sql migrations/postgres/*.sql
var migrationsFS embed.FS var migrationsFS embed.FS
// Migrate applies all database migrations // Migrate applies all database migrations
@@ -18,7 +18,19 @@ func (db *database) Migrate() error {
log := getLogger().WithGroup("migrations") log := getLogger().WithGroup("migrations")
log.Info("starting database migration") log.Info("starting database migration")
sourceInstance, err := iofs.New(migrationsFS, "migrations") var migrationPath string
switch db.dbType {
case DBTypePostgres:
migrationPath = "migrations/postgres"
case DBTypeSQLite:
migrationPath = "migrations/sqlite"
default:
return fmt.Errorf("unsupported database driver: %s", db.dbType)
}
log.Debug("using migration path", "path", migrationPath)
sourceInstance, err := iofs.New(migrationsFS, migrationPath)
if err != nil { if err != nil {
return fmt.Errorf("failed to create source instance: %w", err) return fmt.Errorf("failed to create source instance: %w", err)
} }

View File

@@ -0,0 +1,9 @@
-- 001_initial_schema.down.sql (PostgreSQL version)
DROP INDEX IF EXISTS idx_sessions_refresh_token;
DROP INDEX IF EXISTS idx_sessions_expires_at;
DROP INDEX IF EXISTS idx_sessions_user_id;
DROP INDEX IF EXISTS idx_workspaces_user_id;
DROP TABLE IF EXISTS sessions;
DROP TABLE IF EXISTS workspaces;
DROP TABLE IF EXISTS system_settings;
DROP TABLE IF EXISTS users;

View File

@@ -0,0 +1,61 @@
-- 001_initial_schema.up.sql (PostgreSQL version)
-- Create users table
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
display_name TEXT,
password_hash TEXT NOT NULL,
role TEXT NOT NULL CHECK(role IN ('admin', 'editor', 'viewer')),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_workspace_id INTEGER
);
-- Create workspaces table with integrated settings
CREATE TABLE IF NOT EXISTS workspaces (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL,
name TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_opened_file_path TEXT,
-- Settings fields
theme TEXT NOT NULL DEFAULT 'light' CHECK(theme IN ('light', 'dark')),
auto_save BOOLEAN NOT NULL DEFAULT FALSE,
git_enabled BOOLEAN NOT NULL DEFAULT FALSE,
git_url TEXT,
git_user TEXT,
git_token TEXT,
git_auto_commit BOOLEAN NOT NULL DEFAULT FALSE,
git_commit_msg_template TEXT DEFAULT '${action} ${filename}',
git_commit_name TEXT,
git_commit_email TEXT,
show_hidden_files BOOLEAN NOT NULL DEFAULT FALSE,
created_by INTEGER REFERENCES users(id),
updated_by INTEGER REFERENCES users(id),
updated_at TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
);
-- Create sessions table for authentication
CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY,
user_id INTEGER NOT NULL,
refresh_token TEXT NOT NULL,
expires_at TIMESTAMP NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
);
-- Create system_settings table for application settings
CREATE TABLE IF NOT EXISTS system_settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create indexes for performance
CREATE INDEX idx_sessions_user_id ON sessions(user_id);
CREATE INDEX idx_sessions_expires_at ON sessions(expires_at);
CREATE INDEX idx_sessions_refresh_token ON sessions(refresh_token);
CREATE INDEX idx_workspaces_user_id ON workspaces(user_id);

View File

@@ -2,6 +2,7 @@
DROP INDEX IF EXISTS idx_sessions_refresh_token; DROP INDEX IF EXISTS idx_sessions_refresh_token;
DROP INDEX IF EXISTS idx_sessions_expires_at; DROP INDEX IF EXISTS idx_sessions_expires_at;
DROP INDEX IF EXISTS idx_sessions_user_id; DROP INDEX IF EXISTS idx_sessions_user_id;
DROP INDEX IF EXISTS idx_workspaces_user_id;
DROP TABLE IF EXISTS sessions; DROP TABLE IF EXISTS sessions;
DROP TABLE IF EXISTS workspaces; DROP TABLE IF EXISTS workspaces;
DROP TABLE IF EXISTS system_settings; DROP TABLE IF EXISTS system_settings;

View File

@@ -56,4 +56,5 @@ CREATE TABLE IF NOT EXISTS system_settings (
-- Create indexes for performance -- Create indexes for performance
CREATE INDEX idx_sessions_user_id ON sessions(user_id); CREATE INDEX idx_sessions_user_id ON sessions(user_id);
CREATE INDEX idx_sessions_expires_at ON sessions(expires_at); CREATE INDEX idx_sessions_expires_at ON sessions(expires_at);
CREATE INDEX idx_sessions_refresh_token ON sessions(refresh_token); CREATE INDEX idx_sessions_refresh_token ON sessions(refresh_token);
CREATE INDEX idx_workspaces_user_id ON workspaces(user_id);

View File

@@ -27,7 +27,6 @@ func TestMigrate(t *testing.T) {
"workspaces", "workspaces",
"sessions", "sessions",
"system_settings", "system_settings",
// Note: golang-migrate uses its own migrations table
"schema_migrations", "schema_migrations",
} }
@@ -45,6 +44,7 @@ func TestMigrate(t *testing.T) {
{"sessions", "idx_sessions_user_id"}, {"sessions", "idx_sessions_user_id"},
{"sessions", "idx_sessions_expires_at"}, {"sessions", "idx_sessions_expires_at"},
{"sessions", "idx_sessions_refresh_token"}, {"sessions", "idx_sessions_refresh_token"},
{"workspaces", "idx_workspaces_user_id"},
} }
for _, idx := range indexes { for _, idx := range indexes {
if !indexExists(t, database, idx.table, idx.name) { if !indexExists(t, database, idx.table, idx.name) {