Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Apr 19, 2020. It is now read-only.

Commit cbe26f9

Browse filesBrowse files
committed
Add src/*
1 parent 14718a0 commit cbe26f9
Copy full SHA for cbe26f9

File tree

Expand file treeCollapse file tree

6 files changed

+543
-0
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+543
-0
lines changed

‎src/index.ts

Copy file name to clipboard
+122Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { SynorError } from '@synor/core'
2+
import { createConnection } from 'mysql'
3+
import { performance } from 'perf_hooks'
4+
import { getQueryStore } from './queries'
5+
import { ensureMigrationRecordTable } from './utils/ensure-migration-record-table'
6+
import { getEngineConfig } from './utils/get-engine-config'
7+
import { getMySQLConfig } from './utils/get-mysql-config'
8+
import { runQuery } from './utils/run-query'
9+
10+
type DatabaseEngine = import('@synor/core').DatabaseEngine
11+
type DatabaseEngineFactory = import('@synor/core').DatabaseEngineFactory
12+
type MigrationSource = import('@synor/core').MigrationSource
13+
14+
type MySQLDatabaseEngine = DatabaseEngine
15+
16+
export const MySQLDatabaseEngine: DatabaseEngineFactory = (
17+
uri,
18+
{ baseVersion, getAdvisoryLockId, getUserInfo }
19+
): MySQLDatabaseEngine => {
20+
const engineConfig = getEngineConfig(uri)
21+
const mysqlConfig = getMySQLConfig(uri)
22+
23+
const advisoryLockId = getAdvisoryLockId(
24+
mysqlConfig.database,
25+
engineConfig.migrationTableName
26+
).join(':')
27+
28+
const connection = createConnection(mysqlConfig)
29+
30+
const queryStore = getQueryStore(connection, {
31+
migrationTableName: engineConfig.migrationTableName,
32+
databaseName: mysqlConfig.database,
33+
advisoryLockId
34+
})
35+
36+
let appliedBy = ''
37+
38+
const open: MySQLDatabaseEngine['open'] = async () => {
39+
appliedBy = await getUserInfo()
40+
await queryStore.openConnection()
41+
await ensureMigrationRecordTable(queryStore, baseVersion)
42+
}
43+
44+
const close: MySQLDatabaseEngine['close'] = async () => {
45+
await queryStore.closeConnection()
46+
}
47+
48+
const lock: MySQLDatabaseEngine['lock'] = async () => {
49+
const lockResult = await queryStore.getLock()
50+
if ([0, null].includes(lockResult)) {
51+
throw new SynorError('Failed to Get Lock', { lockId: advisoryLockId })
52+
}
53+
}
54+
55+
const unlock: MySQLDatabaseEngine['unlock'] = async () => {
56+
const lockResult = await queryStore.releaseLock()
57+
if ([0, null].includes(lockResult)) {
58+
throw new SynorError('Failed to Release Lock', { lockId: advisoryLockId })
59+
}
60+
}
61+
62+
const drop: MySQLDatabaseEngine['drop'] = async () => {
63+
const tableNames = await queryStore.getTableNames()
64+
await queryStore.dropTables(tableNames)
65+
}
66+
67+
const run: MySQLDatabaseEngine['run'] = async ({
68+
version,
69+
type,
70+
title,
71+
hash,
72+
body
73+
}: MigrationSource) => {
74+
let dirty = false
75+
76+
const startTime = performance.now()
77+
78+
try {
79+
await runQuery(connection, body)
80+
} catch (err) {
81+
dirty = true
82+
83+
throw err
84+
} finally {
85+
const endTime = performance.now()
86+
87+
await queryStore.addRecord({
88+
version,
89+
type,
90+
title,
91+
hash,
92+
appliedAt: new Date(),
93+
appliedBy,
94+
executionTime: endTime - startTime,
95+
dirty
96+
})
97+
}
98+
}
99+
100+
const repair: MySQLDatabaseEngine['repair'] = async records => {
101+
await queryStore.deleteDirtyRecords()
102+
103+
for (const { id, hash } of records) {
104+
await queryStore.updateRecord(id, { hash })
105+
}
106+
}
107+
108+
const records: MySQLDatabaseEngine['records'] = async startId => {
109+
return queryStore.getRecords(startId)
110+
}
111+
112+
return {
113+
open,
114+
close,
115+
lock,
116+
unlock,
117+
drop,
118+
run,
119+
repair,
120+
records
121+
}
122+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.