-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
AbstractClient.ts
276 lines (215 loc) · 7.94 KB
/
AbstractClient.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
import type {
AbstractClientOptions,
AmountMigrateT,
AmountRollbackT,
DBDialects,
FileEntryT,
Info,
LoggerFn,
QueryHandler,
QueryT,
QueryWithString,
} from "../types.ts";
import type {
AbstractMigration,
AbstractMigrationProps,
} from "../wrappers/AbstractMigration.ts";
import { AbstractSeed, AbstractSeedProps } from "../wrappers/AbstractSeed.ts";
import { COL_FILE_NAME, TABLE_MIGRATIONS } from "../consts.ts";
import { green } from "../deps.ts";
import { getDurationFromTimestamp } from "../cli/utils.ts";
import { NessieError } from "../cli/errors.ts";
/** The abstract client which handles most of the logic related to database communication. */
export abstract class AbstractClient<Client> {
protected logger: LoggerFn = () => undefined;
client: Client;
/** Migration files read from the migration folders */
migrationFiles: FileEntryT[] = [];
/** Seed files read from the seed folders */
seedFiles: FileEntryT[] = [];
/** The current dialect, given by the Client e.g. pg, mysql, sqlite */
dialect?: DBDialects | string;
protected get QUERY_GET_LATEST() {
return `SELECT ${COL_FILE_NAME} FROM ${TABLE_MIGRATIONS} ORDER BY ${COL_FILE_NAME} DESC LIMIT 1;`;
}
protected get QUERY_GET_ALL() {
return `SELECT ${COL_FILE_NAME} FROM ${TABLE_MIGRATIONS} ORDER BY ${COL_FILE_NAME} DESC;`;
}
protected QUERY_MIGRATION_INSERT: QueryWithString = (fileName) =>
`INSERT INTO ${TABLE_MIGRATIONS} (${COL_FILE_NAME}) VALUES ('${fileName}');`;
protected QUERY_MIGRATION_DELETE: QueryWithString = (fileName) =>
`DELETE FROM ${TABLE_MIGRATIONS} WHERE ${COL_FILE_NAME} = '${fileName}';`;
constructor(options: AbstractClientOptions<Client>) {
this.client = options.client;
}
protected _parseAmount(
amount: AmountRollbackT,
maxAmount = 0,
isMigration = true,
): number {
const defaultAmount = isMigration ? maxAmount : 1;
if (amount === "all") return maxAmount;
if (amount === undefined) return defaultAmount;
if (typeof amount === "string") {
amount = isNaN(parseInt(amount)) ? defaultAmount : parseInt(amount);
}
return Math.min(maxAmount, amount);
}
/** Runs the `up` method on all available migrations after filtering and sorting. */
protected async _migrate(
amount: AmountMigrateT,
latestMigration: string | undefined,
queryHandler: QueryHandler,
) {
this.logger(amount, "Amount pre");
this.logger(latestMigration, "Latest migrations");
this._sliceMigrationFiles(latestMigration);
amount = this._parseAmount(amount, this.migrationFiles.length, true);
this.logger(
this.migrationFiles,
"Filtered and sorted migration files",
);
if (amount < 1) {
console.info("Nothing to migrate");
return;
}
console.info(
green(`Starting migration of ${this.migrationFiles.length} files`),
"\n----\n",
);
const t1 = performance.now();
for (const [i, file] of this.migrationFiles.entries()) {
if (i >= amount) break;
console.info(green(`Migrating ${file.name}`));
const t2 = performance.now();
await this._migrationHandler(file, queryHandler);
const duration2 = getDurationFromTimestamp(t2);
console.info(`Done in ${duration2} seconds\n----\n`);
}
const duration1 = getDurationFromTimestamp(t1);
console.info(green(`Migrations completed in ${duration1} seconds`));
}
/** Runs the `down` method on defined number of migrations after retrieving them from the DB. */
async _rollback(
amount: AmountRollbackT,
allMigrations: string[] | undefined,
queryHandler: QueryHandler,
) {
this.logger(allMigrations, "Files to rollback");
this.logger(amount, "Amount pre");
if (!allMigrations || allMigrations.length < 1) {
console.info("Nothing to rollback");
return;
}
amount = this._parseAmount(amount, allMigrations.length, false);
this.logger(amount, "Received amount to rollback");
console.info(
green(`Starting rollback of ${amount} files`),
"\n----\n",
);
const t1 = performance.now();
for (const [i, fileName] of allMigrations.entries()) {
if (i >= amount) break;
const file = this.migrationFiles
.find((migrationFile) => migrationFile.name === fileName);
if (!file) {
throw new NessieError(`Migration file '${fileName}' is not found`);
}
console.info(`Rolling back ${file.name}`);
const t2 = performance.now();
await this._migrationHandler(file, queryHandler, true);
const duration2 = getDurationFromTimestamp(t2);
console.info(`Done in ${duration2} seconds\n----\n`);
}
const duration1 = getDurationFromTimestamp(t1);
console.info(green(`Rollback completed in ${duration1} seconds`));
}
/** Runs the `run` method on seed files. Filters on the matcher. */
async _seed(matcher = ".+.ts") {
const files = this.seedFiles.filter((el) =>
el.name === matcher || new RegExp(matcher).test(el.name)
);
if (files.length < 1) {
console.info(`No seed file found with matcher '${matcher}'`);
return;
}
console.info(
green(`Starting seeding of ${files.length} files`),
"\n----\n",
);
const t1 = performance.now();
for await (const file of files) {
// deno-lint-ignore no-explicit-any
const exposedObject: Info<any> = {
dialect: this.dialect!,
};
console.info(`Seeding ${file.name}`);
const SeedClass: new (
props: AbstractSeedProps<Client>,
) => AbstractSeed<this> = (await import(file.path)).default;
const seed = new SeedClass({ client: this.client });
const t2 = performance.now();
await seed.run(exposedObject);
const duration2 = getDurationFromTimestamp(t2);
console.info(`Done in ${duration2} seconds\n----\n`);
}
const duration1 = getDurationFromTimestamp(t1);
console.info(green(`Seeding completed in ${duration1} seconds`));
}
/** Sets the logger for the client. Given by the State. */
setLogger(fn: LoggerFn) {
this.logger = fn;
}
/** Splits and trims queries. */
protected splitAndTrimQueries(query: string) {
return query.split(";").filter((el) => el.trim() !== "");
}
/** Filters and sort files in ascending order. */
private _sliceMigrationFiles(queryResult: string | undefined): void {
if (!queryResult) return;
const sliceIndex = this.migrationFiles
.findIndex((file) => file.name >= queryResult);
if (sliceIndex !== undefined) {
this.migrationFiles = this.migrationFiles.slice(sliceIndex + 1);
}
}
/** Handles migration files. */
private async _migrationHandler(
file: FileEntryT,
queryHandler: QueryHandler,
isDown = false,
) {
// deno-lint-ignore no-explicit-any
const exposedObject: Info<any> = {
dialect: this.dialect!,
};
const MigrationClass: new (
props: AbstractMigrationProps<Client>,
) => AbstractMigration<this> = (await import(file.path)).default;
const migration = new MigrationClass({ client: this.client });
if (isDown) {
await migration.down(exposedObject);
await queryHandler(this.QUERY_MIGRATION_DELETE(file.name));
} else {
await migration.up(exposedObject);
await queryHandler(this.QUERY_MIGRATION_INSERT(file.name));
}
}
/** Prepares the db connection */
abstract prepare(): Promise<void>;
/** Updates timestamp format */
abstract updateTimestamps(): Promise<void>;
/** Closes the db connection */
abstract close(): Promise<void>;
/** Handles the migration */
abstract migrate(amount: AmountMigrateT): Promise<void>;
/** Handles the rollback */
abstract rollback(amount: AmountRollbackT): Promise<void>;
/** Handles the seeding */
abstract seed(matcher?: string): Promise<void>;
/** Universal wrapper for db query execution */
// deno-lint-ignore no-explicit-any
abstract query(query: QueryT): Promise<any>;
/** Gets all entries from the migration table */
abstract getAll(): Promise<string[]>;
}