-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathrunner.ts
More file actions
257 lines (232 loc) · 8.69 KB
/
Copy pathrunner.ts
File metadata and controls
257 lines (232 loc) · 8.69 KB
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
import path from 'path'
import Db, { DBConnection } from './db'
import { ColumnDefinitions } from './operations/tablesTypes'
import { Migration, loadMigrationFiles, RunMigration } from './migration'
import {
MigrationBuilderActions,
MigrationDirection,
RunnerOptionClient,
RunnerOptionUrl,
RunnerOption,
Logger,
} from './types'
import { createSchemalize, getMigrationTableSchema, getSchemas } from './utils'
import migrateSqlFile from './sqlMigration'
// Random but well-known identifier shared by all instances of node-pg-migrate
const PG_MIGRATE_LOCK_ID = 7241865325823964
const idColumn = 'id'
const nameColumn = 'name'
const runOnColumn = 'run_on'
const loadMigrations = async (db: DBConnection, options: RunnerOption, logger: Logger) => {
try {
let shorthands: ColumnDefinitions = {}
const files = await loadMigrationFiles(options.dir, options.ignorePattern)
return (
await Promise.all(
files.map(async (file) => {
const filePath = `${options.dir}/${file}`
const actions: MigrationBuilderActions =
path.extname(filePath) === '.sql'
? await migrateSqlFile(filePath)
: // eslint-disable-next-line global-require,import/no-dynamic-require,security/detect-non-literal-require
require(path.relative(__dirname, filePath))
shorthands = { ...shorthands, ...actions.shorthands }
return new Migration(
db,
filePath,
actions,
options,
{
...shorthands,
},
logger,
)
}),
)
).sort((m1, m2) => {
const compare = m1.timestamp - m2.timestamp
if (compare !== 0) return compare
return m1.name.localeCompare(m2.name)
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
throw new Error(`Can't get migration files: ${err.stack}`)
}
}
const lock = async (db: DBConnection): Promise<void> => {
const [result] = await db.select(`select pg_try_advisory_lock(${PG_MIGRATE_LOCK_ID}) as "lockObtained"`)
if (!result.lockObtained) {
throw new Error('Another migration is already running')
}
}
const unlock = async (db: DBConnection): Promise<void> => {
const [result] = await db.select(`select pg_advisory_unlock(${PG_MIGRATE_LOCK_ID}) as "lockReleased"`)
if (!result.lockReleased) {
throw new Error('Failed to release migration lock')
}
}
const ensureMigrationsTable = async (db: DBConnection, options: RunnerOption): Promise<void> => {
try {
const schema = getMigrationTableSchema(options)
const { migrationsTable } = options
const fullTableName = createSchemalize(
Boolean(options.decamelize),
true,
)({
schema,
name: migrationsTable,
})
const migrationTables = await db.select(
`SELECT table_name FROM information_schema.tables WHERE table_schema = '${schema}' AND table_name = '${migrationsTable}'`,
)
if (migrationTables && migrationTables.length === 1) {
const primaryKeyConstraints = await db.select(
`SELECT constraint_name FROM information_schema.table_constraints WHERE table_schema = '${schema}' AND table_name = '${migrationsTable}' AND constraint_type = 'PRIMARY KEY'`,
)
if (!primaryKeyConstraints || primaryKeyConstraints.length !== 1) {
await db.query(`ALTER TABLE ${fullTableName} ADD PRIMARY KEY (${idColumn})`)
}
} else {
await db.query(
`CREATE TABLE ${fullTableName} ( ${idColumn} SERIAL PRIMARY KEY, ${nameColumn} varchar(255) NOT NULL, ${runOnColumn} timestamp NOT NULL)`,
)
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
throw new Error(`Unable to ensure migrations table: ${err.stack}`)
}
}
const getRunMigrations = async (db: DBConnection, options: RunnerOption) => {
const schema = getMigrationTableSchema(options)
const { migrationsTable } = options
const fullTableName = createSchemalize(
Boolean(options.decamelize),
true,
)({
schema,
name: migrationsTable,
})
return db.column(nameColumn, `SELECT ${nameColumn} FROM ${fullTableName} ORDER BY ${runOnColumn}, ${idColumn}`)
}
const getMigrationsToRun = (options: RunnerOption, runNames: string[], migrations: Migration[]): Migration[] => {
if (options.direction === 'down') {
const downMigrations: Array<string | Migration> = runNames
.filter((migrationName) => !options.file || options.file === migrationName)
.map((migrationName) => migrations.find(({ name }) => name === migrationName) || migrationName)
const { count = 1 } = options
const toRun = (
options.timestamp
? downMigrations.filter((migration) => typeof migration === 'object' && migration.timestamp >= count)
: downMigrations.slice(-Math.abs(count))
).reverse()
const deletedMigrations = toRun.filter((migration): migration is string => typeof migration === 'string')
if (deletedMigrations.length) {
const deletedMigrationsStr = deletedMigrations.join(', ')
throw new Error(`Definitions of migrations ${deletedMigrationsStr} have been deleted.`)
}
return toRun as Migration[]
}
const upMigrations = migrations.filter(
({ name }) => runNames.indexOf(name) < 0 && (!options.file || options.file === name),
)
const { count = Infinity } = options
return options.timestamp
? upMigrations.filter(({ timestamp }) => timestamp <= count)
: upMigrations.slice(0, Math.abs(count))
}
const checkOrder = (runNames: string[], migrations: Migration[]) => {
const len = Math.min(runNames.length, migrations.length)
for (let i = 0; i < len; i += 1) {
const runName = runNames[i]
const migrationName = migrations[i].name
if (runName !== migrationName) {
throw new Error(`Not run migration ${migrationName} is preceding already run migration ${runName}`)
}
}
}
const runMigrations = (toRun: Migration[], method: 'markAsRun' | 'apply', direction: MigrationDirection) =>
toRun.reduce(
(promise: Promise<unknown>, migration) => promise.then(() => migration[method](direction)),
Promise.resolve(),
)
const getLogger = ({ log, logger, verbose }: RunnerOption): Logger => {
let loggerObject: Logger = console
if (typeof logger === 'object') {
loggerObject = logger
} else if (typeof log === 'function') {
loggerObject = { debug: log, info: log, warn: log, error: log }
}
return verbose
? loggerObject
: {
debug: undefined,
info: loggerObject.info.bind(loggerObject),
warn: loggerObject.warn.bind(loggerObject),
error: loggerObject.error.bind(loggerObject),
}
}
export default async (options: RunnerOption): Promise<RunMigration[]> => {
const logger = getLogger(options)
const db = Db((options as RunnerOptionClient).dbClient || (options as RunnerOptionUrl).databaseUrl, logger)
try {
await db.createConnection()
if (!options.noLock) {
await lock(db)
}
if (options.schema) {
const schemas = getSchemas(options.schema)
if (options.createSchema) {
await Promise.all(schemas.map((schema) => db.query(`CREATE SCHEMA IF NOT EXISTS "${schema}"`)))
}
await db.query(`SET search_path TO ${schemas.map((s) => `"${s}"`).join(', ')}`)
}
if (options.migrationsSchema && options.createMigrationsSchema) {
await db.query(`CREATE SCHEMA IF NOT EXISTS "${options.migrationsSchema}"`)
}
await ensureMigrationsTable(db, options)
const [migrations, runNames] = await Promise.all([
loadMigrations(db, options, logger),
getRunMigrations(db, options),
])
if (options.checkOrder) {
checkOrder(runNames, migrations)
}
const toRun: Migration[] = getMigrationsToRun(options, runNames, migrations)
if (!toRun.length) {
logger.info('No migrations to run!')
return []
}
// TODO: add some fancy colors to logging
logger.info('> Migrating files:')
toRun.forEach((m) => {
logger.info(`> - ${m.name}`)
})
if (options.fake) {
await runMigrations(toRun, 'markAsRun', options.direction)
} else if (options.singleTransaction) {
await db.query('BEGIN')
try {
await runMigrations(toRun, 'apply', options.direction)
await db.query('COMMIT')
} catch (err) {
logger.warn('> Rolling back attempted migration ...')
await db.query('ROLLBACK')
throw err
}
} else {
await runMigrations(toRun, 'apply', options.direction)
}
return toRun.map((m) => ({
path: m.path,
name: m.name,
timestamp: m.timestamp,
}))
} finally {
if (db.connected()) {
if (!options.noLock) {
await unlock(db).catch((error) => logger.warn(error.message))
}
db.close()
}
}
}