Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
tests: fix migration logic
Kyle Mendellf523d41331c2
diff --git a/scripts/init-test-env.ts b/scripts/init-test-env.tsindex 6af206931..db9b97f3b 100644--- a/scripts/init-test-env.ts+++ b/scripts/init-test-env.ts@@ -30,16 +30,29 @@ async function setupTestEnvironment() {console.log(`Ensuring test data directory exists: ${TEST_DATA_DIR}`);const settingsDir = path.join(TEST_DATA_DIR, 'settings');const usersDir = path.join(TEST_DATA_DIR, 'users');+ const stacksDir = path.join(TEST_DATA_DIR, 'stacks');await ensureDirectoryExists(TEST_DATA_DIR);await ensureDirectoryExists(settingsDir);await ensureDirectoryExists(usersDir);+ await ensureDirectoryExists(stacksDir);console.log('All directories ensured.');+ // --- Clean existing database to ensure fresh state ---+ const dbPath = path.join(TEST_DATA_DIR, 'arcane.db');+ try {+ await fs.unlink(dbPath);+ console.log('Removed existing database file');+ } catch (e) {+ console.log('No existing database file to remove');+ }+// --- NOW import database modules (after directories exist) ---const { encrypt } = await import('../src/lib/services/encryption-service');const { DEFAULT_SETTINGS } = await import('../src/lib/services/settings-service');const { runMigrations } = await import('../src/db/migrate');+ const { db } = await import('../src/db/index');+ const { settingsTable } = await import('../src/db/schema');// --- Initialize Database ---console.log('Running database migrations...');@@ -51,21 +64,8 @@ async function setupTestEnvironment() {throw error;}- // --- Initialize Test User ---- console.log(`Initializing test user: ${TEST_USERNAME}`);- const usersFilePath = path.join(usersDir, 'users.json');- const hashedPassword = await bcrypt.hash(TEST_PASSWORD, 10);- const testUser = {- username: TEST_USERNAME,- password: hashedPassword,- roles: ['admin', 'containers:view', 'containers:manage', 'settings:view', 'networks:view', 'networks:manage'],- createdAt: new Date().toISOString()- };- await fs.writeFile(usersFilePath, JSON.stringify([testUser], null, 2), { mode: 0o600 });- console.log(`Test user data written to ${usersFilePath}`);-- // --- Initialize Settings ---- console.log('Initializing settings...');+ // --- Insert initial settings into database ---+ console.log('Inserting initial settings into database...');const testSettings = {...DEFAULT_SETTINGS,onboarding: {@@ -81,10 +81,49 @@ async function setupTestEnvironment() {registryCredentials: []};- const { auth, registryCredentials, ...nonSensitiveSettings } = testSettings;+ const { auth, registryCredentials, ...otherSettings } = testSettings;++ try {+ await db.insert(settingsTable).values({+ dockerHost: otherSettings.dockerHost,+ stacksDirectory: otherSettings.stacksDirectory,+ autoUpdate: otherSettings.autoUpdate,+ autoUpdateInterval: otherSettings.autoUpdateInterval,+ pollingEnabled: otherSettings.pollingEnabled,+ pollingInterval: otherSettings.pollingInterval,+ pruneMode: otherSettings.pruneMode,+ registryCredentials: JSON.stringify(registryCredentials),+ templateRegistries: JSON.stringify(otherSettings.templateRegistries),+ auth: JSON.stringify(auth),+ onboarding: JSON.stringify(otherSettings.onboarding),+ baseServerUrl: otherSettings.baseServerUrl,+ maturityThresholdDays: otherSettings.maturityThresholdDays+ });+ console.log('Initial settings inserted into database.');+ } catch (error) {+ console.error('Failed to insert settings:', error);+ throw error;+ }++ // --- Initialize Test User ---+ console.log(`Initializing test user: ${TEST_USERNAME}`);+ const usersFilePath = path.join(usersDir, 'users.json');+ const hashedPassword = await bcrypt.hash(TEST_PASSWORD, 10);+ const testUser = {+ username: TEST_USERNAME,+ password: hashedPassword,+ roles: ['admin', 'containers:view', 'containers:manage', 'settings:view', 'networks:view', 'networks:manage'],+ createdAt: new Date().toISOString()+ };+ await fs.writeFile(usersFilePath, JSON.stringify([testUser], null, 2), { mode: 0o600 });+ console.log(`Test user data written to ${usersFilePath}`);++ // --- Initialize Settings Files (backup) ---+ console.log('Initializing settings files...');+ const { auth: authSettings, registryCredentials: regCreds, ...nonSensitiveSettings } = testSettings;const dataToSave = {...nonSensitiveSettings,- _encrypted: await encrypt({ auth, registryCredentials })+ _encrypted: await encrypt({ auth: authSettings, registryCredentials: regCreds })};const settingsDatPath = path.join(settingsDir, 'settings.dat');diff --git a/src/db/migrate.ts b/src/db/migrate.tsindex 09a4dd345..85ac41c30 100644--- a/src/db/migrate.ts+++ b/src/db/migrate.ts@@ -7,12 +7,8 @@ export async function runMigrations() {await migrate(db, { migrationsFolder: './src/db/migrations' });console.log('Database migrations completed successfully');} catch (error) {- if (error instanceof Error && (error.message.includes('already exists') || error.message.includes('SQLITE_ERROR'))) {- console.log('Database tables already exist, continuing...');- return; // Don't throw, just continue- }-console.error('Error running migrations:', error);+ // Don't silently continue on migration errors - this is criticalthrow error;}}diff --git a/src/db/migrations/0003_typical_ogun.sql b/src/db/migrations/0003_typical_ogun.sqlindex 5b802cd44..8221707d0 100644--- a/src/db/migrations/0003_typical_ogun.sql+++ b/src/db/migrations/0003_typical_ogun.sql@@ -1,29 +1,29 @@CREATE TABLE `deployments_table` (- `id` text PRIMARY KEY NOT NULL,- `name` text NOT NULL,- `type` text NOT NULL,- `status` text DEFAULT 'pending' NOT NULL,- `agent_id` text NOT NULL,- `task_id` text,- `error` text,- `metadata` text,- `created_at` integer DEFAULT (unixepoch()) NOT NULL,- `updated_at` integer DEFAULT (unixepoch()) NOT NULL+ `id` text PRIMARY KEY NOT NULL,+ `name` text NOT NULL,+ `type` text NOT NULL,+ `status` text DEFAULT 'pending' NOT NULL,+ `agent_id` text NOT NULL,+ `task_id` text,+ `error` text,+ `metadata` text,+ `created_at` integer DEFAULT (unixepoch()) NOT NULL,+ `updated_at` integer DEFAULT (unixepoch()) NOT NULL);--> statement-breakpointPRAGMA foreign_keys=OFF;--> statement-breakpointCREATE TABLE `__new_users_table` (- `id` text PRIMARY KEY NOT NULL,- `username` text NOT NULL,- `password_hash` text,- `display_name` text,- `email` text,- `roles` text DEFAULT '[]' NOT NULL,- `require_password_change` integer DEFAULT false NOT NULL,- `oidc_subject_id` text,- `last_login` integer,- `created_at` integer DEFAULT (unixepoch() NOT NULL,- `updated_at` integer DEFAULT (unixepoch()) NOT NULL+ `id` text PRIMARY KEY NOT NULL,+ `username` text NOT NULL,+ `password_hash` text,+ `display_name` text,+ `email` text,+ `roles` text DEFAULT '[]' NOT NULL,+ `require_password_change` integer DEFAULT false NOT NULL,+ `oidc_subject_id` text,+ `last_login` integer,+ `created_at` integer DEFAULT (unixepoch()) NOT NULL,+ `updated_at` integer DEFAULT (unixepoch()) NOT NULL);--> statement-breakpointINSERT INTO `__new_users_table`("id", "username", "password_hash", "display_name", "email", "roles", "require_password_change", "oidc_subject_id", "last_login", "created_at", "updated_at") SELECT "id", "username", "password_hash", "display_name", "email", "roles", "require_password_change", "oidc_subject_id", "last_login", "created_at", "updated_at" FROM `users_table`;--> statement-breakpointdiff --git a/tests/.auth/login.json b/tests/.auth/login.jsonindex 2a9b977a9..9ea6470cc 100644--- a/tests/.auth/login.json+++ b/tests/.auth/login.json@@ -2,10 +2,10 @@"cookies": [{"name": "kit.session",- "value": "51e972dc20999f886ad5304cc98ca9d304f8c07a97dfef48a600949ded39cffcdf58fa8c19cd35fc2f0ae018a4358203efd2a524e0f9ef6c8deabb3ac9f68e5efaa4529f9cc9aa99d48605624925fa2bf248a3979294ba44a27ff97a259a3214ab580619bc6492eb5899591b6977742e2116329e701d186956f8c7827afeae4f06066bfa5af4a4c2f70d218dff7f3aadcbac7ad4bdf2bf40ad4a0830ab4fc0204bc7be1d9c11320a4488e5d16d3ec8700f477456e7c7684eab174590a7%26id%3D1",+ "value": "93beaccadd8a6498db9b84a10d853984df00b6bd7f14fc711b91a1d907d5e405636188adf7fce01505407b2de783e9801eb97c8ab009b1c24165268d0ad167264584b6431fad606eac5d49200f94a37cd16f9a8e5bdf0c697fdd03411b16e1daa84e7f524d49eb737188da7298447dc15d0c2e6790b0814cdf1e95b61fcfc2ddf4fda3f11f2822ae0376ca380e50abfbacc54d11d67e841d40353186b6c7a911b35a59f96e34261b22d2bd38f3c71d85f223e2f20c27df3e5d01bde260%26id%3D1","domain": "localhost","path": "/",- "expires": 1782709844.331035,+ "expires": 1783454073.37731,"httpOnly": true,"secure": false,"sameSite": "Lax"