From c07f19bbb30e9b25f6cb10e5b4ac946e6ce6af77 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Sat, 30 Nov 2024 13:55:29 +0100 Subject: [PATCH] Merge migrations --- server/internal/db/migrations.go | 115 +++++++++++++++---------------- 1 file changed, 55 insertions(+), 60 deletions(-) diff --git a/server/internal/db/migrations.go b/server/internal/db/migrations.go index f59b20f..a5d1586 100644 --- a/server/internal/db/migrations.go +++ b/server/internal/db/migrations.go @@ -15,70 +15,65 @@ var migrations = []Migration{ { Version: 1, SQL: ` - -- Create users table - CREATE TABLE IF NOT EXISTS users ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - 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 users table + CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + 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 INTEGER PRIMARY KEY AUTOINCREMENT, - 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 0, - git_enabled BOOLEAN NOT NULL DEFAULT 0, - git_url TEXT, - git_user TEXT, - git_token TEXT, - git_auto_commit BOOLEAN NOT NULL DEFAULT 0, - git_commit_msg_template TEXT DEFAULT '${action} ${filename}', - FOREIGN KEY (user_id) REFERENCES users (id) - ); - `, - }, - { - Version: 2, - SQL: ` - -- 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 workspaces table with integrated settings + CREATE TABLE IF NOT EXISTS workspaces ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + 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 0, + git_enabled BOOLEAN NOT NULL DEFAULT 0, + git_url TEXT, + git_user TEXT, + git_token TEXT, + git_auto_commit BOOLEAN NOT NULL DEFAULT 0, + git_commit_msg_template TEXT DEFAULT '${action} ${filename}', + git_commit_name TEXT, + git_commit_email TEXT, + show_hidden_files BOOLEAN NOT NULL DEFAULT 0, + created_by INTEGER REFERENCES users(id), + updated_by INTEGER REFERENCES users(id), + updated_at TIMESTAMP, + FOREIGN KEY (user_id) REFERENCES users (id) + ); - -- Add show_hidden_files field to workspaces - ALTER TABLE workspaces ADD COLUMN show_hidden_files BOOLEAN NOT NULL DEFAULT 0; + -- 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 + ); - -- Add 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 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 + ); - -- Add audit fields to workspaces - ALTER TABLE workspaces ADD COLUMN created_by INTEGER REFERENCES users(id); - ALTER TABLE workspaces ADD COLUMN updated_by INTEGER REFERENCES users(id); - ALTER TABLE workspaces ADD COLUMN updated_at TIMESTAMP; - - -- 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); + `, }, }