-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
226 lines (204 loc) · 6.21 KB
/
index.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
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
const _ = require("lodash");
const utils = require("./lib/utils");
const DatabaseConnectionUrlBuilder = require("./lib/databaseConnectionUrlBuilder");
const MigrationsHandler = require("./handlers/migrationsHandler");
const SequelizeCliHandler = require("./handlers/sequelizeCliHandler");
class SequelizeMigrations {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
const dbConnectionOptions = {
dbDialect: {
usage: "Specify the database dialect (one of: 'mysql', 'mariadb', 'postgres', 'mssql')",
default: '',
type: 'string'
},
dbHost: {
usage: "Specify the database host",
default: '',
type: 'string'
},
dbPort: {
usage: "Specify the database port",
default: '',
type: 'string'
},
dbName: {
usage: "Specify the database name",
default: '',
type: 'string'
},
dbUsername: {
usage: "Specify the database username",
default: '',
type: 'string'
},
dbPassword: {
usage: "Specify the database password",
default: '',
type: 'string'
}
}
this.commands = {
migrations: {
usage: "Sequelize migrations management for Serverless",
options: {
path: {
usage: "Specify the migrations path (default is './migrations')",
shortcut: "p",
default: "./migrations",
type: 'string'
},
verbose: {
usage: "Shows sequelize logs",
shortcut: "v",
type: 'boolean'
}
},
commands: {
create: {
usage: "Create a migration file",
lifecycleEvents: ["run"],
options: {
name: {
usage: "Specify the name of the migration to be created",
shortcut: "n",
required: true,
type: 'boolean'
}
}
},
up: {
usage: "Execute all pending migrations",
lifecycleEvents: ["run"],
options: {
rollback: {
usage:
"Rolls back applied migrations in case of error (default is false)",
shortcut: "r",
default: false,
type: 'boolean'
},
...dbConnectionOptions
}
},
down: {
usage: "Rolls back one or more migrations",
lifecycleEvents: ["run"],
options: {
times: {
usage: "Specify how many times to roll back (default is 1)",
shortcut: "t",
default: 1,
type: 'string'
},
name: {
usage:
'Specify the name of the migration to be rolled back (e.g. "--name create-users.js")',
shortcut: "n",
type: 'string'
},
...dbConnectionOptions
}
},
reset: {
usage: "Rolls back all migrations",
lifecycleEvents: ["run"],
options: {
...dbConnectionOptions
}
},
list: {
usage: "Shows a list of migrations",
lifecycleEvents: ["show"],
options: {
status: {
usage:
"Specify the status of migrations to be listed (--status pending [default] or --status executed)",
shortcut: "s",
default: "pending",
type: 'string'
},
...dbConnectionOptions
}
}
}
}
};
this.hooks = {
"migrations:up:run": this.migrate.bind(this),
"migrations:down:run": this.revert.bind(this),
"migrations:reset:run": this.reset.bind(this),
"migrations:list:show": this.list.bind(this),
"migrations:create:run": this.createMigration.bind(this)
};
this.verbose = this.options.verbose || this.options.v;
this.path =
this.options.path ||
this.options.p ||
_.get(this.serverless, "service.custom.migrationsPath");
}
setUpMigrationsHandler() {
const databaseConnectionUrlBuilder = new DatabaseConnectionUrlBuilder(this.serverless, this.options);
const database = databaseConnectionUrlBuilder.build();
const migrationsHandler = new MigrationsHandler(
this.serverless,
database,
this.path,
this.verbose
);
migrationsHandler.initialize();
return migrationsHandler;
}
async migrate() {
try {
const migrationsHandler = this.setUpMigrationsHandler();
const success = await migrationsHandler.migrate(this.options.rollback);
if (!success) process.exit(1);
} catch (e) {
this.serverless.cli.log(`Error trying to apply migrations: \n${e}`);
process.exit(1);
}
}
async revert() {
try {
const migrationsHandler = this.setUpMigrationsHandler();
const times = parseInt(this.options.times);
await migrationsHandler.revert(times, this.options.name);
} catch (e) {
this.serverless.cli.log(`Error trying to rollback migrations: \n${e}`);
process.exit(1);
}
}
async reset() {
try {
const migrationsHandler = this.setUpMigrationsHandler();
await migrationsHandler.reset();
} catch (e) {
this.serverless.cli.log(`Error trying to revert all migrations: \n${e}`);
process.exit(1);
}
}
async list() {
try {
const migrationsHandler = this.setUpMigrationsHandler();
await migrationsHandler.list(this.options.status);
} catch (e) {
this.serverless.cli.log(`Error trying to list migrations: \n${e}`);
process.exit(1);
}
}
setUpSequelizeCliHandler() {
return new SequelizeCliHandler(this.serverless, this.path);
}
createMigration() {
try {
const sequelizeCliHandler = this.setUpSequelizeCliHandler();
sequelizeCliHandler.createMigration(this.options.name);
} catch (e) {
this.serverless.cli.log(`Error trying to create migration: \n${e}`);
process.exit(1);
}
}
}
module.exports = SequelizeMigrations;