-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.js
More file actions
237 lines (202 loc) · 7.69 KB
/
Copy pathmigrate.js
File metadata and controls
237 lines (202 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
const fs = require('node:fs');
const path = require('node:path');
const Database = require('better-sqlite3');
const DB_PATH = './storage/application.db';
const MIGRATIONS_DIR = 'migrations';
const SEEDER_FILE = 'seeder.sql';
const FEATURE_CONFIG_DIR = path.join('config', 'features');
const { installFeatures } = require('./lib/features');
const { log } = require('./lib/logger');
let featuresInstalled = false;
if (!fs.existsSync(path.dirname(DB_PATH))) {
fs.mkdirSync(path.dirname(DB_PATH), { recursive: true });
log.info(`Created database directory: '${path.dirname(DB_PATH)}'`);
}
function installCurrentFeatures() {
if (featuresInstalled) return;
installFeatures();
featuresInstalled = true;
}
function execSqlFile(db, filePath, label) {
log.info(`Applying SQL from ${label}: ${filePath}...`);
try {
const sql = fs.readFileSync(filePath, 'utf8');
db.exec(sql);
log.info(`Successfully applied ${label}.`);
} catch (err) {
log.error(`FAILED to apply ${label} '${filePath}'.`);
throw err;
}
}
const loadInstalledFeatureConfigs = () => {
if (!fs.existsSync(FEATURE_CONFIG_DIR)) return [];
return fs.readdirSync(FEATURE_CONFIG_DIR)
.filter(file => file.endsWith('.json'))
.map(file => {
const configPath = path.join(FEATURE_CONFIG_DIR, file);
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
return {
name: config.name || path.basename(file, '.json'),
config,
};
});
};
const getFeatureSqlFiles = (feature, type) => {
const configuredFiles = Array.isArray(feature.config?.db?.[type]) ? feature.config.db[type] : [];
const cwd = process.cwd();
return configuredFiles
.map(file => path.normalize(file))
.filter(file => {
const resolvedPath = path.normalize(path.join(cwd, file));
const relativePath = path.relative(cwd, resolvedPath);
return !relativePath.startsWith('..') && !path.isAbsolute(relativePath) && fs.existsSync(resolvedPath) && file.endsWith('.sql');
})
.sort();
};
/**
* Applies data from the seeder files.
* @param {Database.Database} db The database instance.
*/
function applySeeder(db) {
installCurrentFeatures();
if (fs.existsSync(SEEDER_FILE)) {
execSqlFile(db, SEEDER_FILE, 'base seeder');
} else {
log.info(`Seeder file '${SEEDER_FILE}' not found, skipping.`);
}
loadInstalledFeatureConfigs().forEach(feature => {
getFeatureSqlFiles(feature, 'seeds').forEach(seedFile => {
execSqlFile(db, seedFile, `feature seeder ${feature.name}`);
});
});
}
function applyMigrations() {
log.info('Starting database migration process...');
installCurrentFeatures();
const db = new Database(DB_PATH, {
// verbose: console.log
});
try {
db.exec(`
CREATE TABLE IF NOT EXISTS schema_migrations (
version TEXT PRIMARY KEY NOT NULL,
applied_at TEXT NOT NULL DEFAULT (datetime('now'))
)
`);
log.info(`Make sure 'schema_migrations' table exists.`);
} catch (err) {
log.error(`Failed to create migrations table: ${err.message}`);
db.close();
process.exit(1);
}
const getAppliedStmt = db.prepare(`SELECT version FROM schema_migrations`);
const appliedRows = getAppliedStmt.all();
const appliedVersions = new Set(appliedRows.map(row => row.version));
log.info(`Found ${appliedVersions.size} applied migrations.`);
if (!fs.existsSync(MIGRATIONS_DIR)) {
log.info(`Migrations directory '${MIGRATIONS_DIR}' not found. Nothing to do.`);
}
const baseMigrationFiles = fs.existsSync(MIGRATIONS_DIR)
? fs.readdirSync(MIGRATIONS_DIR)
.filter(file => file.endsWith('.sql'))
.sort()
.map(file => ({
displayName: file,
filePath: path.join(MIGRATIONS_DIR, file),
version: file.split('-')[0],
}))
: [];
const featureMigrationFiles = loadInstalledFeatureConfigs().flatMap(feature => {
return getFeatureSqlFiles(feature, 'migrations').map(file => {
const filename = path.basename(file);
return {
displayName: `${feature.name}/${filename}`,
filePath: file,
version: `feature:${feature.name}:${filename.split('-')[0]}`,
};
});
});
const pendingMigrations = [...baseMigrationFiles, ...featureMigrationFiles]
.filter(migration => !appliedVersions.has(migration.version));
if (pendingMigrations.length === 0) {
log.info('Database schema is up to date. No new migrations to apply.');
} else {
log.info(`Found ${pendingMigrations.length} pending migrations to apply.`);
for (const migration of pendingMigrations) {
const applyMigrationTx = db.transaction(() => {
try {
const sql = fs.readFileSync(migration.filePath, 'utf8');
db.exec(sql);
const recordMigrationStmt = db.prepare(`INSERT INTO schema_migrations (version) VALUES (?)`);
recordMigrationStmt.run(migration.version);
log.info(`Successfully applied migration: ${migration.displayName}`);
} catch (err) {
log.error(`FAILED to apply migration ${migration.displayName}. Rolling back.`);
throw err;
}
});
try {
applyMigrationTx();
} catch (err) {
log.error(`Migration failed: ${err.message}`);
db.close();
process.exit(1);
}
}
}
try {
applySeeder(db);
} catch (err) {
log.error(`Seeding failed: ${err.message}`);
db.close();
process.exit(1);
}
log.info('Migration and seeding process finished successfully.');
db.close();
}
/**
* Creates a new, empty .sql file in the migrations directory.
* @param {string} name
*/
function createMigration(name) {
if (!name) {
log.error('Migration name is required. Usage: node migrate.js create <MigrationName>');
process.exit(1);
}
const sanitizedName = name.replace(/\s+/g, '_').replace(/[^a-zA-Z0-9_]/g, '');
const timestamp = Math.floor(Date.now() / 1000);
const filename = `${timestamp}-${sanitizedName}.sql`;
const filepath = path.join(MIGRATIONS_DIR, filename);
if (!fs.existsSync(MIGRATIONS_DIR)) {
fs.mkdirSync(MIGRATIONS_DIR);
log.info(`Created migrations directory: '${MIGRATIONS_DIR}'`);
}
fs.writeFileSync(filepath, `-- Add your SQL migration statements here for ${filename}\n`);
log.info(`Created new migration file: ${filepath}`);
}
function main() {
const command = process.argv[2];
const argument = process.argv[3];
switch (command) {
case 'apply':
applyMigrations();
break;
case 'create':
createMigration(argument);
break;
case 'seed':
applySeeder(new Database(DB_PATH));
break;
case 'setup':
applyMigrations();
break;
default:
log.info('Usage:');
log.info(' node migrate.js <command> [args]');
log.info(' node migrate.js seed - Applies data from the seeder files.');
log.info(' node migrate.js apply - Applies pending migrations and seeds data.');
log.info(' node migrate.js create <Name> - Creates a new, empty migration file.');
process.exit(1);
}
}
main();