forked from trent_larson/crowd-funder-for-time-pwa
refactor(db): consolidate database migrations from 6 to 3
- Add missing migrations table creation to 001_initial migration - Consolidate migrations 003-006 into single 003_active_did_separation migration - Rename migration for better clarity and logical grouping - Preserve all original SQL operations and data integrity constraints - Reduce migration complexity while maintaining functionality This consolidation improves maintainability by grouping related schema changes into logical atomic operations, reducing the total migration count by 50%.
This commit is contained in:
@@ -117,6 +117,11 @@ const MIGRATIONS = [
|
|||||||
id TEXT PRIMARY KEY,
|
id TEXT PRIMARY KEY,
|
||||||
blobB64 TEXT
|
blobB64 TEXT
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS migrations (
|
||||||
|
name TEXT PRIMARY KEY,
|
||||||
|
applied_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||||
|
);
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -126,8 +131,12 @@ const MIGRATIONS = [
|
|||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "003_active_did_separate_table",
|
name: "003_active_did_separation",
|
||||||
sql: `
|
sql: `
|
||||||
|
-- CONSOLIDATED MIGRATION: Combines original migrations 003, 004, 005, and 006
|
||||||
|
-- This migration handles the complete separation of activeDid from settings
|
||||||
|
-- and establishes proper data integrity constraints
|
||||||
|
|
||||||
-- Create new active_identity table with proper constraints
|
-- Create new active_identity table with proper constraints
|
||||||
CREATE TABLE IF NOT EXISTS active_identity (
|
CREATE TABLE IF NOT EXISTS active_identity (
|
||||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||||
@@ -150,40 +159,22 @@ const MIGRATIONS = [
|
|||||||
lastUpdated = datetime('now')
|
lastUpdated = datetime('now')
|
||||||
WHERE id = 1
|
WHERE id = 1
|
||||||
AND EXISTS (SELECT 1 FROM settings WHERE id = 1 AND activeDid IS NOT NULL AND activeDid != '');
|
AND EXISTS (SELECT 1 FROM settings WHERE id = 1 AND activeDid IS NOT NULL AND activeDid != '');
|
||||||
`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "004_remove_activeDid_from_settings",
|
|
||||||
sql: `
|
|
||||||
-- Remove activeDid column from settings table (moved to active_identity)
|
-- Remove activeDid column from settings table (moved to active_identity)
|
||||||
-- Note: SQLite doesn't support DROP COLUMN in older versions
|
-- Note: SQLite doesn't support DROP COLUMN in older versions
|
||||||
-- This migration will be skipped if DROP COLUMN is not supported
|
-- This migration will be skipped if DROP COLUMN is not supported
|
||||||
-- The activeDid column will remain but won't be used by the application
|
-- The activeDid column will remain but won't be used by the application
|
||||||
|
|
||||||
-- Try to drop the activeDid column (works in SQLite 3.35.0+)
|
|
||||||
ALTER TABLE settings DROP COLUMN activeDid;
|
ALTER TABLE settings DROP COLUMN activeDid;
|
||||||
`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "005_eliminate_master_settings_key",
|
|
||||||
sql: `
|
|
||||||
-- Eliminate MASTER_SETTINGS_KEY concept - remove confusing id=1 row
|
-- Eliminate MASTER_SETTINGS_KEY concept - remove confusing id=1 row
|
||||||
-- This creates clean separation: active_identity for current identity, settings for identity config
|
-- This creates clean separation: active_identity for current identity, settings for identity config
|
||||||
|
|
||||||
-- Delete the confusing MASTER_SETTINGS_KEY row (id=1 with accountDid=NULL)
|
|
||||||
DELETE FROM settings WHERE id = 1 AND accountDid IS NULL;
|
DELETE FROM settings WHERE id = 1 AND accountDid IS NULL;
|
||||||
|
|
||||||
-- Reset auto-increment to start from 1 again
|
-- Reset auto-increment to start from 1 again
|
||||||
DELETE FROM sqlite_sequence WHERE name = 'settings';
|
DELETE FROM sqlite_sequence WHERE name = 'settings';
|
||||||
`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "006_add_unique_constraint_accountDid",
|
|
||||||
sql: `
|
|
||||||
-- Add unique constraint to prevent duplicate accountDid values
|
-- Add unique constraint to prevent duplicate accountDid values
|
||||||
-- This ensures data integrity: each identity can only have one settings record
|
-- This ensures data integrity: each identity can only have one settings record
|
||||||
|
|
||||||
-- First, remove any duplicate accountDid entries (keep the most recent one)
|
|
||||||
DELETE FROM settings
|
DELETE FROM settings
|
||||||
WHERE id NOT IN (
|
WHERE id NOT IN (
|
||||||
SELECT MAX(id)
|
SELECT MAX(id)
|
||||||
@@ -192,7 +183,6 @@ const MIGRATIONS = [
|
|||||||
GROUP BY accountDid
|
GROUP BY accountDid
|
||||||
) AND accountDid IS NOT NULL;
|
) AND accountDid IS NOT NULL;
|
||||||
|
|
||||||
-- Add unique constraint on accountDid
|
|
||||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_settings_accountDid_unique ON settings(accountDid);
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_settings_accountDid_unique ON settings(accountDid);
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user