-
-
Notifications
You must be signed in to change notification settings - Fork 529
/
Copy pathmigrator.js
149 lines (131 loc) · 4.14 KB
/
migrator.js
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
import Umzug from 'umzug';
import _ from 'lodash';
import process from 'process';
import helpers from '../helpers/index';
const Sequelize = helpers.generic.getSequelize();
export function logMigrator(s) {
if (s.indexOf('Executing') !== 0) {
helpers.view.log(s);
}
}
function getSequelizeInstance() {
let config = null;
try {
config = helpers.config.readConfig();
} catch (e) {
helpers.view.error(e);
}
config = _.defaults(config, { logging: logMigrator });
try {
return new Sequelize(config);
} catch (e) {
helpers.view.error(e);
}
}
export async function getMigrator(type, args) {
if (!(helpers.config.configFileExists() || args.url)) {
helpers.view.error(
`Cannot find "${helpers.config.getConfigFile()}". Have you run "sequelize init"?`
);
process.exit(1);
}
const sequelize = getSequelizeInstance();
const migrator = new Umzug({
storage: helpers.umzug.getStorage(type),
storageOptions: helpers.umzug.getStorageOptions(type, { sequelize }),
logging: helpers.view.log,
migrations: {
params: [sequelize.getQueryInterface(), Sequelize],
path: helpers.path.getPath(type),
pattern: /^(?!.*\.d\.ts$).*\.(cjs|js|ts)$/,
},
});
return sequelize
.authenticate()
.then(() => {
// Check if this is a PostgreSQL run and if there is a custom schema specified, and if there is, check if it's
// been created. If not, attempt to create it.
if (helpers.version.getDialectName() === 'pg') {
const customSchemaName = helpers.umzug.getSchema('migration');
if (customSchemaName && customSchemaName !== 'public') {
return sequelize.createSchema(customSchemaName);
}
}
})
.then(() => migrator)
.catch((e) => helpers.view.error(e));
}
export function ensureCurrentMetaSchema(migrator) {
const queryInterface =
migrator.options.storageOptions.sequelize.getQueryInterface();
const tableName = migrator.options.storageOptions.tableName;
const columnName = migrator.options.storageOptions.columnName;
return ensureMetaTable(queryInterface, tableName)
.then((table) => {
const columns = Object.keys(table);
if (columns.length === 1 && columns[0] === columnName) {
return;
} else if (columns.length === 3 && columns.indexOf('createdAt') >= 0) {
// If found createdAt - indicate we have timestamps enabled
helpers.umzug.enableTimestamps();
return;
}
})
.catch(() => {});
}
function ensureMetaTable(queryInterface, tableName) {
return queryInterface.showAllTables().then((tableNames) => {
if (tableNames.indexOf(tableName) === -1) {
throw new Error('No MetaTable table found.');
}
return queryInterface.describeTable(tableName);
});
}
/**
* Add timestamps
*
* @return {Promise}
*/
export function addTimestampsToSchema(migrator) {
const sequelize = migrator.options.storageOptions.sequelize;
const queryInterface = sequelize.getQueryInterface();
const tableName = migrator.options.storageOptions.tableName;
return ensureMetaTable(queryInterface, tableName).then((table) => {
if (table.createdAt) {
return;
}
return ensureCurrentMetaSchema(migrator)
.then(() => queryInterface.renameTable(tableName, tableName + 'Backup'))
.then(() => {
const queryGenerator =
queryInterface.QueryGenerator || queryInterface.queryGenerator;
const sql = queryGenerator.selectQuery(tableName + 'Backup');
return helpers.generic.execQuery(sequelize, sql, {
type: 'SELECT',
raw: true,
});
})
.then((result) => {
const SequelizeMeta = sequelize.define(
tableName,
{
name: {
type: Sequelize.STRING,
allowNull: false,
unique: true,
primaryKey: true,
autoIncrement: false,
},
},
{
tableName,
timestamps: true,
schema: helpers.umzug.getSchema(),
}
);
return SequelizeMeta.sync().then(() => {
return SequelizeMeta.bulkCreate(result);
});
});
});
}