forked from halvardssm/deno-nessie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientMySQL.ts
150 lines (124 loc) · 4.19 KB
/
ClientMySQL.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
import { MySQLClient } from "../deps.ts";
import { AbstractClient } from "./AbstractClient.ts";
import type {
AmountMigrateT,
AmountRollbackT,
DBDialects,
QueryT,
} from "../types.ts";
import {
COL_CREATED_AT,
COL_FILE_NAME,
MAX_FILE_NAME_LENGTH,
TABLE_MIGRATIONS,
} from "../consts.ts";
import { NessieError } from "../cli/errors.ts";
export type MySQLClientOptions = Parameters<MySQLClient["connect"]>;
/**
* MySQL client
*
* This is for MySQL versions >5.5, if you want to use version <=5.5,
* use ClientMySQL55 instead.
*/
export class ClientMySQL extends AbstractClient<MySQLClient> {
protected clientOptions: MySQLClientOptions;
dialect: DBDialects = "mysql";
protected get QUERY_TRANSACTION_START() {
return `START TRANSACTION;`;
}
protected get QUERY_TRANSACTION_COMMIT() {
return `COMMIT;`;
}
protected get QUERY_TRANSACTION_ROLLBACK() {
return `ROLLBACK;`;
}
protected get QUERY_MIGRATION_TABLE_EXISTS() {
return `SELECT * FROM information_schema.tables WHERE table_name = '${TABLE_MIGRATIONS}' LIMIT 1;`;
}
protected get QUERY_CREATE_MIGRATION_TABLE() {
return `CREATE TABLE ${TABLE_MIGRATIONS} (id bigint UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, ${COL_FILE_NAME} varchar(${MAX_FILE_NAME_LENGTH}) NOT NULL UNIQUE, ${COL_CREATED_AT} datetime NOT NULL DEFAULT CURRENT_TIMESTAMP);`;
}
protected get QUERY_UPDATE_TIMESTAMPS() {
return `UPDATE ${TABLE_MIGRATIONS} SET ${COL_FILE_NAME} = CONCAT(FROM_UNIXTIME(CAST(substring_index(${COL_FILE_NAME}, '-', 1) AS SIGNED) / 1000, '%Y%m%d%H%i%S'), substring(file_name, instr( file_name,'-'))) WHERE CAST(substring_index(${COL_FILE_NAME}, '-', 1) AS SIGNED) < 1672531200000;`;
}
constructor(...connectionOptions: MySQLClientOptions) {
super({ client: new MySQLClient() });
this.clientOptions = connectionOptions;
}
async prepare() {
await this.client.connect(...this.clientOptions);
const queryResult = await this.query(this.QUERY_MIGRATION_TABLE_EXISTS);
const migrationTableExists = queryResult?.[0]?.length > 0;
if (!migrationTableExists) {
await this.query(this.QUERY_CREATE_MIGRATION_TABLE);
console.info("Database setup complete");
}
}
async updateTimestamps() {
await this.client.connect(...this.clientOptions);
const queryResult = await this.query(this.QUERY_MIGRATION_TABLE_EXISTS);
const migrationTableExists = queryResult?.[0]?.length > 0;
if (migrationTableExists) {
await this.query(this.QUERY_TRANSACTION_START);
try {
await this.query(this.QUERY_UPDATE_TIMESTAMPS);
await this.query(this.QUERY_TRANSACTION_COMMIT);
console.info("Updated timestamps");
} catch (e) {
await this.query(this.QUERY_TRANSACTION_ROLLBACK);
throw e;
}
}
}
async query(query: QueryT) {
if (typeof query === "string") query = this.splitAndTrimQueries(query);
const ra = [];
for await (const qs of query) {
try {
if (
qs.trim().toLowerCase().startsWith("select") ||
qs.trim().toLowerCase().startsWith("show")
) {
ra.push(await this.client.query(qs));
} else {
ra.push(await this.client.execute(qs));
}
} catch (e) {
if (e?.message === "Query was empty") {
ra.push(undefined);
} else {
throw new NessieError(query + "\n" + e + "\n" + ra.join("\n"));
}
}
}
return ra;
}
async close() {
await this.client.close();
}
async migrate(amount: AmountMigrateT) {
const latestMigration = await this.query(this.QUERY_GET_LATEST);
await this._migrate(
amount,
latestMigration?.[0]?.[0]?.[COL_FILE_NAME],
this.query.bind(this),
);
}
async rollback(amount: AmountRollbackT) {
const allMigrations = await this.getAll();
await this._rollback(
amount,
allMigrations,
this.query.bind(this),
);
}
async seed(matcher?: string) {
await this._seed(matcher);
}
async getAll() {
const allMigrations = await this.query(this.QUERY_GET_ALL);
const parsedMigrations: string[] = allMigrations?.[0]
.map((el: Record<string, string>) => el?.[COL_FILE_NAME]);
return parsedMigrations;
}
}