-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
ClientSQLite.ts
135 lines (111 loc) · 3.82 KB
/
ClientSQLite.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
import { SQLiteClient } 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 SQLiteClientOptions = ConstructorParameters<typeof SQLiteClient>;
/** SQLite client */
export class ClientSQLite extends AbstractClient<SQLiteClient> {
dialect: DBDialects = "sqlite";
protected get QUERY_TRANSACTION_START() {
return `BEGIN TRANSACTION;`;
}
protected get QUERY_TRANSACTION_COMMIT() {
return `COMMIT;`;
}
protected get QUERY_TRANSACTION_ROLLBACK() {
return `ROLLBACK;`;
}
protected get QUERY_MIGRATION_TABLE_EXISTS() {
return `SELECT name FROM sqlite_master WHERE type='table' AND name='${TABLE_MIGRATIONS}';`;
}
protected get QUERY_CREATE_MIGRATION_TABLE() {
return `CREATE TABLE ${TABLE_MIGRATIONS} (id integer NOT NULL PRIMARY KEY autoincrement, ${COL_FILE_NAME} varchar(${MAX_FILE_NAME_LENGTH}) UNIQUE, ${COL_CREATED_AT} datetime NOT NULL DEFAULT CURRENT_TIMESTAMP);`;
}
protected get QUERY_UPDATE_TIMESTAMPS() {
return `UPDATE ${TABLE_MIGRATIONS} SET ${COL_FILE_NAME} = strftime('%Y%m%d%H%M%S', CAST(substr(${COL_FILE_NAME}, 0, instr(${COL_FILE_NAME}, '-')) AS INTEGER) / 1000, 'unixepoch') || substr(${COL_FILE_NAME}, instr(${COL_FILE_NAME}, '-')) WHERE CAST(substr(${COL_FILE_NAME}, 0, instr(${COL_FILE_NAME}, '-')) AS INTEGER) < 1672531200000;`;
}
constructor(...params: SQLiteClientOptions) {
super({ client: new SQLiteClient(...params) });
}
async prepare() {
const queryResult = await this.query(this.QUERY_MIGRATION_TABLE_EXISTS);
const migrationTableExists =
queryResult?.[0]?.[0]?.[0] === TABLE_MIGRATIONS;
if (!migrationTableExists) {
await this.query(this.QUERY_CREATE_MIGRATION_TABLE);
console.info("Database setup complete");
}
}
async updateTimestamps() {
const queryResult = await this.query(this.QUERY_MIGRATION_TABLE_EXISTS);
const migrationTableExists =
queryResult?.[0]?.[0]?.[0] === TABLE_MIGRATIONS;
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 {
ra.push([...this.client!.query(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;
}
// deno-lint-ignore require-await
async close() {
this.client?.close();
}
async migrate(amount: AmountMigrateT) {
const latestMigration = await this.query(this.QUERY_GET_LATEST);
await this._migrate(
amount,
latestMigration?.[0]?.[0]?.[0] as string,
this.query.bind(this),
);
}
async rollback(amount: AmountRollbackT) {
const allMigrations = await this.query(this.QUERY_GET_ALL);
await this._rollback(
amount,
allMigrations?.[0]?.flatMap((el) => el?.[0] as string),
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 = allMigrations?.[0]
?.flatMap((el) => el?.[0]) as string[];
return parsedMigrations;
}
}