feat: add cleanup plan for obsolete files and migration script for project status constraint

This commit is contained in:
2025-12-19 13:49:22 +01:00
parent 2b27583c28
commit afd0c26fbb
2 changed files with 253 additions and 0 deletions

View File

@@ -0,0 +1,87 @@
import db from './src/lib/db.js';
console.log('Starting migration to add cancelled status to project_status constraint...');
try {
// Disable foreign key constraints temporarily
db.pragma('foreign_keys = OFF');
console.log('Disabled foreign key constraints');
// Since SQLite doesn't support modifying CHECK constraints directly,
// we need to recreate the table with the new constraint
// First, create a backup table with current data
db.exec('CREATE TABLE projects_backup AS SELECT * FROM projects');
console.log('Created backup table');
// Drop the original table
db.exec('DROP TABLE projects');
console.log('Dropped original table');
// Recreate the table with the updated constraint
db.exec(`
CREATE TABLE projects (
project_id INTEGER PRIMARY KEY AUTOINCREMENT,
contract_id INTEGER,
project_name TEXT NOT NULL,
project_number TEXT NOT NULL,
address TEXT,
plot TEXT,
district TEXT,
unit TEXT,
city TEXT,
investment_number TEXT,
finish_date TEXT,
wp TEXT,
contact TEXT,
notes TEXT,
project_type TEXT CHECK(project_type IN ('design', 'construction', 'design+construction')) DEFAULT 'design',
project_status TEXT CHECK(project_status IN ('registered', 'in_progress_design', 'in_progress_construction', 'fulfilled', 'cancelled')) DEFAULT 'registered',
coordinates TEXT,
created_by TEXT,
assigned_to TEXT,
created_at TEXT,
updated_at TEXT,
FOREIGN KEY (contract_id) REFERENCES contracts(contract_id)
)
`);
console.log('Created new table with updated constraint');
// Copy data back
db.exec('INSERT INTO projects SELECT * FROM projects_backup');
console.log('Restored data from backup');
// Drop backup table
db.exec('DROP TABLE projects_backup');
console.log('Cleaned up backup table');
// Re-enable foreign key constraints
db.pragma('foreign_keys = ON');
console.log('Re-enabled foreign key constraints');
// Verify the new constraint
const schema = db.prepare('SELECT sql FROM sqlite_master WHERE type=\'table\' AND name=\'projects\'').get();
console.log('New table definition:');
console.log(schema.sql);
console.log('Migration completed successfully!');
} catch (error) {
console.error('Migration failed:', error);
// Re-enable foreign keys in case of error
try {
db.pragma('foreign_keys = ON');
} catch (e) {
console.error('Failed to re-enable foreign keys:', e);
}
// Try to restore from backup if it exists
try {
db.exec('DROP TABLE IF EXISTS projects');
db.exec('ALTER TABLE projects_backup RENAME TO projects');
console.log('Restored from backup due to error');
} catch (restoreError) {
console.error('Failed to restore from backup:', restoreError);
}
}