-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.js
More file actions
63 lines (52 loc) · 1.7 KB
/
Copy pathmigrate.js
File metadata and controls
63 lines (52 loc) · 1.7 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
/**
* Simple migration runner for FlowCare
* Runs all migration files in order (001-xxx, 002-xxx, etc.)
*
* Usage:
* node migrations/migrate.js # Run all migrations (up)
* node migrations/migrate.js down # Rollback all migrations (down)
*/
const path = require('path');
const fs = require('fs');
const { Sequelize } = require('sequelize');
require('dotenv').config({ path: path.join(__dirname, '..', '.env') });
const sequelize = new Sequelize(
process.env.DB_NAME || 'flowcare',
process.env.DB_USER || 'flowcare',
process.env.DB_PASSWORD || 'flowcare123',
{
host: process.env.DB_HOST || 'localhost',
port: process.env.DB_PORT || 5432,
dialect: 'postgres',
logging: console.log,
}
);
async function run() {
const direction = process.argv[2] || 'up';
try {
await sequelize.authenticate();
console.log('Connected to database.\n');
// Get all migration files sorted
const migrationFiles = fs
.readdirSync(__dirname)
.filter((f) => f.match(/^\d{3}-.*\.js$/) && f !== 'migrate.js')
.sort();
if (direction === 'down') {
migrationFiles.reverse();
}
const queryInterface = sequelize.getQueryInterface();
for (const file of migrationFiles) {
const migration = require(path.join(__dirname, file));
console.log(`${direction === 'up' ? '▶' : '◀'} ${file}...`);
await migration[direction](queryInterface, Sequelize);
console.log(` ✓ Done\n`);
}
console.log(`All migrations ${direction === 'up' ? 'applied' : 'rolled back'} successfully.`);
} catch (error) {
console.error('Migration failed:', error.message);
process.exit(1);
} finally {
await sequelize.close();
}
}
run();