-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmigrator.js
More file actions
53 lines (48 loc) · 1.18 KB
/
migrator.js
File metadata and controls
53 lines (48 loc) · 1.18 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
/**
* Umzug system for automatic migration (mount/unmount)
* of database
* Use `npm run migration` to use it manually
*
* @format
*/
import { Sequelize } from "sequelize";
import { Umzug, SequelizeStorage } from "umzug";
import fs from "fs";
import esMain from "es-main";
const sequelize = new Sequelize({
dialect: "postgres",
host: process.env.DB_IP,
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
port: parseInt(process.env.DB_PORT || "5432"),
database: process.env.DB_NAME,
logging: false,
});
const umzug = new Umzug({
migrations: {
glob: "migrations/*.up.sql",
resolve: ({ name, path, context: sequelize }) => ({
name,
up: async () => {
if (path) {
const sql = fs.readFileSync(path).toString();
return sequelize.query(sql);
}
},
down: async () => {
if (path) {
// Get the corresponding `.down.sql` file to undo this migration
const sql = fs.readFileSync(path.replace(".up.sql", ".down.sql")).toString();
return sequelize.query(sql);
}
},
}),
},
context: sequelize,
storage: new SequelizeStorage({ sequelize }),
logger: console,
});
if (esMain(import.meta)) {
umzug.runAsCLI();
}
export { umzug };