-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_up.js
More file actions
43 lines (35 loc) · 1.14 KB
/
Copy pathmigrate_up.js
File metadata and controls
43 lines (35 loc) · 1.14 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
import { findByExtension } from './lib/fs'
import { closeDbConnection, runSql } from './lib/mysql'
import { cOk, cWarn, cError, cAdvice, cInfo } from './lib/console'
//Getting all migration files
const allFiles = await findByExtension('./migrations', 'js')
//Getting all executed migrations
const [rows] = await runSql(() => `
SELECT *
FROM migrations
`)
const executedMigrations = rows.map(item => item.migration)
// //Filtering files for migration
const filesForMigrations = allFiles.sort().
filter(file => !executedMigrations.includes(file))
//Running and saving migrations
if (filesForMigrations.length > 0) {
for (const file of filesForMigrations) {
const { up } = await import(`./migrations/${file}`)
try {
await runSql(up)
await runSql(() => `
INSERT INTO migrations (migration)
VALUES ('${file}')
`)
cOk(`${file} migrate executed!`)
} catch (e) {
cError(`${file} migrate crashed!`, e)
}
}
} else {
cWarn('No one files to migrate.')
cAdvice('You may run command:', 'bun run migrate_create.js')
}
//Closing mysql db connection for exit from this script
closeDbConnection()