-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
292 lines (262 loc) · 8.19 KB
/
cli.ts
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import { parse } from "./deps.ts";
import { Migrate, Migration } from "./migrate.ts";
/** Filters used by the list command. */
export enum ListFilter {
Applied = "applied",
Unapplied = "unapplied",
Moved = "moved",
Deleted = "deleted",
}
/** Initializes the migration table for tracking which migrations have been applied. */
export async function init(migrate: Migrate): Promise<void> {
console.log("Creating migration table if it does not exist");
try {
await migrate.init();
console.log("Created migration table");
} catch {
console.log("Migration table already exists");
}
}
/**
* Loads all migrations current path values into the migration table.
* Returns all loaded migrations.
*/
export async function loadMigrations(migrate: Migrate): Promise<Migration[]> {
console.log("Loading migrations");
const before = await migrate.now();
let migrations = await migrate.getAll();
const deletedMigrationIds = new Set(migrations.map(({ id }) => id));
await migrate.load();
migrations = await migrate.getAll();
for (const { id } of migrations) {
deletedMigrationIds.delete(id);
}
const createdMigrations = migrations.filter((migration) =>
migration.createdAt >= before
);
console.log(
`${createdMigrations.length || "No"} new migration${
createdMigrations.length !== 1 ? "s" : ""
} found`,
);
if (createdMigrations.length < migrations.length) {
const updatedMigrations = migrations.filter((migration) =>
migration.createdAt < before && migration.updatedAt >= before
);
console.log(
`${updatedMigrations.length || "No"} migration${
updatedMigrations.length !== 1 ? "s" : ""
} updated`,
);
console.log(
`${deletedMigrationIds.size || "No"} migration${
deletedMigrationIds.size !== 1 ? "s" : ""
} deleted`,
);
}
return migrations;
}
/**
* Loads all migrations current path values into the migration table.
*/
export async function load(migrate: Migrate): Promise<void> {
console.log("Acquiring migrate lock");
const lock = await migrate.lock();
console.log("Acquired migrate lock");
await loadMigrations(migrate);
console.log("Releasing migrate lock");
await lock.release();
console.log("Released migrate lock");
console.log("Done");
}
/**
* Outputs the status of all migrations. By default it just outputs the counts.
* If the --details or -d flag is provided, it will log the filenames of migrations
* that have not been applied or have been changed since being applied.
*/
export async function status(
migrate: Migrate,
args: string[] = [],
): Promise<void> {
const parsedArgs = parse(args, {
alias: { d: "details" },
});
const { details } = parsedArgs;
let total = 0,
lastId = -1;
const unappliedMigrations: Migration[] = [],
movedMigrations: Migration[] = [],
deletedMigrations: Migration[] = [];
console.log("Checking loaded migrations");
for (const migration of await migrate.getAll()) {
total++;
const { id, path, appliedPath } = migration;
if (id > lastId) lastId = id;
if (!appliedPath) unappliedMigrations.push(migration);
else if (!path) deletedMigrations.push(migration);
else if (path != appliedPath) movedMigrations.push(migration);
}
console.log("Status:");
console.log(` Total: ${total}`);
console.log(` Applied: ${total - unappliedMigrations.length}`);
if (movedMigrations.length) {
console.log(` File moved: ${movedMigrations.length}`);
if (details) {
for (const migration of movedMigrations) {
const { path, appliedPath } = migration;
console.log(` ${appliedPath} -> ${path}`);
}
}
}
if (deletedMigrations.length) {
console.log(` File deleted: ${deletedMigrations.length}`);
if (details) {
for (const migration of deletedMigrations) {
const { appliedPath } = migration;
console.log(` ${appliedPath}`);
}
}
}
if (unappliedMigrations.length) {
console.log(` Not applied: ${unappliedMigrations.length}`);
if (details) {
for (const migration of unappliedMigrations) {
console.log(` ${migration.path}`);
}
}
}
}
/**
* Outputs a list of migrations. By default it outputs all migrations.
* If the --filter flag is provided, it will filter the migrations to only include
* migrations that match the filter.
* The filter options are applied, unapplied, renamed, and deleted.
*/
export async function list(
migrate: Migrate,
args: string[] = [],
): Promise<void> {
const parsedArgs = parse(args);
const { filter } = parsedArgs;
console.log("Checking loaded migrations");
let migrations = await migrate.getAll();
switch (filter) {
case ListFilter.Applied:
console.log(
migrations.length ? "Applied migrations:" : "No applied migrations",
);
migrations = migrations.filter((migration: Migration) =>
migration.appliedAt
);
break;
case ListFilter.Unapplied:
console.log(
migrations.length ? "Unapplied migrations:" : "No unapplied migrations",
);
migrations = migrations.filter((migration: Migration) =>
!migration.appliedAt
);
break;
case ListFilter.Moved:
console.log(
migrations.length ? "Moved migrations:" : "No moved migrations",
);
migrations = migrations.filter((migration: Migration) =>
!!migration.appliedPath && !!migration.path &&
migration.appliedPath !== migration.path
);
break;
case ListFilter.Deleted:
console.log(
migrations.length ? "Deleted migrations:" : "No deleted migrations",
);
migrations = migrations.filter((migration: Migration) => !migration.path);
break;
default:
if (filter != null) console.warn("invalid filter");
console.log(migrations.length ? "All migrations:" : "No migrations");
}
for (const migration of migrations) {
const { path, appliedPath, appliedAt } = migration;
console.log(` ${appliedPath ?? path}`);
if (appliedAt) {
console.log(` applied at: ${appliedAt}`);
} else if (filter !== ListFilter.Unapplied) {
console.log(` not applied`);
}
if (appliedPath && path && path !== appliedPath) {
console.log(` file moved to: ${path}`);
}
if (!path && filter !== ListFilter.Deleted) {
console.log(` file deleted`);
}
}
}
/** Applies all unapplied migrations. */
export async function applyMigrations(
migrate: Migrate,
migrations: Migration[],
): Promise<void> {
const unappliedMigrations = migrations.filter((migration) =>
!migration.appliedPath
);
console.log(
`${unappliedMigrations.length || "No"} unapplied migration${
unappliedMigrations.length !== 1 ? "s" : ""
}`,
);
if (unappliedMigrations.length) {
for (const migration of unappliedMigrations) {
console.log(`Applying migration: ${migration.path}`);
await migrate.apply(migration);
}
console.log("Finished applying all migrations");
}
}
/**
* Applies all unapplied migrations and outputs the filenames.
*/
export async function apply(migrate: Migrate): Promise<void> {
console.log("Acquiring migrate lock");
const lock = await migrate.lock();
console.log("Acquired migrate lock");
console.log("Checking loaded migrations");
const migrations = await migrate.getUnapplied();
await applyMigrations(migrate, migrations);
console.log("Releasing migrate lock");
await lock.release();
console.log("Released migrate lock");
console.log("Done");
}
export type Command = (
migrate: Migrate,
args?: string[],
) => Promise<unknown>;
export interface Commands {
[command: string]: Command;
}
/** Commands used by the migrate cli tool. */
export const commands: Commands = {
init,
load,
status,
list,
apply,
};
/** Runs migrate commands based on `Deno.args`. */
export async function run(migrate: Migrate) {
const [command] = Deno.args;
if (commands[command]) {
console.log("Connecting to database");
try {
await migrate.connect();
} catch (error) {
console.log("Failed to connect to database");
throw error;
}
await commands[command](migrate, Deno.args.slice(1));
await migrate.end();
} else {
console.log("command not found");
}
}