import 'dotenv/config'; import Database from 'better-sqlite3'; import { MssqlProvider } from '../src/providers/mssql/index.js'; async function migrateSqlite(sqlitePath) { console.log(`Migrating SQLite database at: ${sqlitePath}`); const db = new Database(sqlitePath); const columns = db.pragma('table_info("manifest")'); const hasCreatedAt = columns.some((c) => c.name === 'created_at'); if (hasCreatedAt) { console.log('SQLite manifest already has created_at column.'); db.close(); return; } db.pragma('foreign_keys = OFF'); db.exec(` BEGIN TRANSACTION; CREATE TABLE manifest_new ( id INTEGER PRIMARY KEY, company_id INTEGER NOT NULL, "number" TEXT NOT NULL, "type" TEXT NOT NULL, entity_id INTEGER, external_number TEXT, "date" TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')), created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')), FOREIGN KEY (company_id) REFERENCES company(id) ON DELETE NO ACTION, FOREIGN KEY (entity_id) REFERENCES entity(id) ON DELETE SET NULL ); INSERT INTO manifest_new (id, company_id, "number", "type", entity_id, external_number, "date", created_at) SELECT id, company_id, "number", "type", entity_id, external_number, "date", "date" FROM manifest; DROP TABLE manifest; ALTER TABLE manifest_new RENAME TO manifest; COMMIT; `); db.pragma('foreign_keys = ON'); const fkErrors = db.pragma('foreign_key_check'); if (fkErrors.length > 0) { console.error('Foreign key check failed after SQLite migration:', fkErrors); } else { console.log('SQLite migration completed successfully.'); } console.log('Updated SQLite manifest columns:', db.pragma('table_info("manifest")')); console.log('Updated SQLite manifest sample row:', db.prepare('SELECT * FROM manifest LIMIT 1').get()); db.close(); } async function migrateMssql() { console.log('Migrating MSSQL database...'); const config = { server: process.env.MSSQL_SERVER, database: process.env.MSSQL_DATABASE, user: process.env.MSSQL_USER, password: process.env.MSSQL_PASSWORD, options: { encrypt: false, trustServerCertificate: true, }, }; const provider = new MssqlProvider(config); try { const pool = await provider.ensurePool(); const checkRes = await pool.request().query(` SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'manifest') AND name = 'created_at' `); if (checkRes.recordset.length > 0) { console.log('MSSQL manifest already has created_at column.'); } else { await pool.request().query(` ALTER TABLE manifest ADD created_at DATETIME2 NOT NULL CONSTRAINT DF_manifest_created_at DEFAULT SYSUTCDATETIME(); `); await pool.request().query(` UPDATE manifest SET created_at = [date]; `); console.log('MSSQL migration completed successfully.'); } const colsRes = await pool.request().query(` SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'manifest' `); console.log('Updated MSSQL manifest columns:', colsRes.recordset); const sampleRes = await pool.request().query(` SELECT TOP 1 * FROM manifest `); console.log('Updated MSSQL manifest sample row:', sampleRes.recordset[0]); await provider.close(); } catch (err) { console.error('MSSQL migration failed:', err); await provider.close(); throw err; } } async function run() { await migrateSqlite('/home/seb/src/dashback/data/dash.db'); await migrateMssql(); } run().catch((err) => { console.error('Migration failed:', err); process.exit(1); });