Skip to content

Commit 6fa5dba

Browse files
committedMay 15, 2018
Add prettier and format
1 parent 1f2fae2 commit 6fa5dba

File tree

15 files changed

+1445
-1340
lines changed

15 files changed

+1445
-1340
lines changed
 

Diff for: ‎.eslintrc

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
---
22
extends: "@mft/eslint-config-momentumft"
3+
rules:
4+
indent: ["error", 2, { "SwitchCase": 1 }]

Diff for: ‎.prettierrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"semi": false,
3+
"trailingComma": "all",
4+
"bracketSpacing": false
5+
}

Diff for: ‎package-lock.json

+977-972
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,20 @@
2727
"node": ">8.9.3"
2828
},
2929
"scripts": {
30-
"lint": "eslint src",
30+
"check-formatting": "./node_modules/.bin/prettier '**/*.js' --list-different",
31+
"fix-formatting": "./node_modules/.bin/prettier '**/*.js' --write",
32+
"lint": "eslint src && npm run check-formatting",
3133
"test-integration": "ava",
3234
"test-unit": "ava src/**/__unit__/**/*.js",
3335
"test": "npm run test-unit && npm run lint && npm run test-integration",
34-
"preversion": "npm test"
36+
"preversion": "npm test",
37+
"prepush": "npm test"
3538
},
3639
"dependencies": {
3740
"dedent-js": "^1.0.1",
3841
"pg": "^7.1.2",
3942
"pgtools": "^0.3",
43+
"prettier": "^1.12.1",
4044
"sql-template-strings": "^2.2.2"
4145
},
4246
"devDependencies": {

Diff for: ‎src/__tests__/create.js

+96-82
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const PASSWORD = startPostgres.PASSWORD
1010

1111
let port
1212

13-
test.cb.before((t) => {
13+
test.cb.before(t => {
1414
port = startPostgres(CONTAINER_NAME, t)
1515
})
1616

@@ -23,109 +23,123 @@ test("successful creation", () => {
2323
})
2424
})
2525

26-
test("bad arguments - no database name", (t) => {
27-
return t.throws(createDb({
28-
user: "postgres",
29-
password: PASSWORD,
30-
host: "localhost",
31-
port,
32-
}))
33-
.then((err) => {
34-
t.regex(err.message, /database name/)
35-
})
36-
})
37-
38-
test("bad arguments - empty db config", (t) => {
39-
return t.throws(createDb("create-test-no-config", {}))
40-
.then((err) => {
41-
t.regex(err.message, /config/)
26+
test("bad arguments - no database name", t => {
27+
return t
28+
.throws(
29+
createDb({
30+
user: "postgres",
31+
password: PASSWORD,
32+
host: "localhost",
33+
port,
34+
}),
35+
)
36+
.then(err => {
37+
t.regex(err.message, /database name/)
4238
})
4339
})
4440

45-
test("bad arguments - incorrect user", (t) => {
46-
return t.throws(createDb("create-test-user", {
47-
user: "nobody",
48-
password: PASSWORD,
49-
host: "localhost",
50-
port,
51-
}))
52-
.then((err) => {
53-
t.regex(err.message, /nobody/)
41+
test("bad arguments - empty db config", t => {
42+
return t.throws(createDb("create-test-no-config", {})).then(err => {
43+
t.regex(err.message, /config/)
5444
})
5545
})
5646

57-
test("bad arguments - incorrect password", (t) => {
58-
return t.throws(createDb("create-test-password", {
59-
user: "postgres",
60-
password: "not_the_password",
61-
host: "localhost",
62-
port,
63-
}))
64-
.then((err) => {
65-
t.regex(err.message, /password/)
66-
})
47+
test("bad arguments - incorrect user", t => {
48+
return t
49+
.throws(
50+
createDb("create-test-user", {
51+
user: "nobody",
52+
password: PASSWORD,
53+
host: "localhost",
54+
port,
55+
}),
56+
)
57+
.then(err => {
58+
t.regex(err.message, /nobody/)
59+
})
6760
})
6861

69-
test("bad arguments - incorrect host", (t) => {
70-
return t.throws(createDb("create-test-host", {
71-
user: "postgres",
72-
password: PASSWORD,
73-
host: "sillyhost",
74-
port,
75-
}))
76-
.then((err) => {
77-
t.regex(err.message, /sillyhost/)
78-
})
62+
test("bad arguments - incorrect password", t => {
63+
return t
64+
.throws(
65+
createDb("create-test-password", {
66+
user: "postgres",
67+
password: "not_the_password",
68+
host: "localhost",
69+
port,
70+
}),
71+
)
72+
.then(err => {
73+
t.regex(err.message, /password/)
74+
})
7975
})
8076

81-
test("bad arguments - incorrect port", (t) => {
82-
return t.throws(createDb("create-test-port", {
83-
user: "postgres",
84-
password: PASSWORD,
85-
host: "localhost",
86-
port: 1234,
87-
}))
88-
.then((err) => {
89-
t.regex(err.message, /1234/)
90-
})
77+
test("bad arguments - incorrect host", t => {
78+
return t
79+
.throws(
80+
createDb("create-test-host", {
81+
user: "postgres",
82+
password: PASSWORD,
83+
host: "sillyhost",
84+
port,
85+
}),
86+
)
87+
.then(err => {
88+
t.regex(err.message, /sillyhost/)
89+
})
90+
})
91+
92+
test("bad arguments - incorrect port", t => {
93+
return t
94+
.throws(
95+
createDb("create-test-port", {
96+
user: "postgres",
97+
password: PASSWORD,
98+
host: "localhost",
99+
port: 1234,
100+
}),
101+
)
102+
.then(err => {
103+
t.regex(err.message, /1234/)
104+
})
91105
})
92106

93107
test("already created", () => {
94-
const create = () => createDb("create-test-duplicate", {
95-
user: "postgres",
96-
password: PASSWORD,
97-
host: "localhost",
98-
port,
99-
})
108+
const create = () =>
109+
createDb("create-test-duplicate", {
110+
user: "postgres",
111+
password: PASSWORD,
112+
host: "localhost",
113+
port,
114+
})
100115

101-
return create()
102-
.then(create)
116+
return create().then(create)
103117
})
104118

105119
test("database name included in config", () => {
106-
const create = () => createDb("create-test-db-name", {
107-
database: "somethingsilly",
108-
user: "postgres",
109-
password: PASSWORD,
110-
host: "localhost",
111-
port,
112-
})
120+
const create = () =>
121+
createDb("create-test-db-name", {
122+
database: "somethingsilly",
123+
user: "postgres",
124+
password: PASSWORD,
125+
host: "localhost",
126+
port,
127+
})
113128

114-
return create()
115-
.then(create)
129+
return create().then(create)
116130
})
117131

118132
test("custom default database name", () => {
119-
const create = () => createDb("create-test-default-db", {
120-
defaultDatabase: "postgres",
121-
user: "postgres",
122-
password: PASSWORD,
123-
host: "localhost",
124-
port,
125-
})
133+
const create = () =>
134+
createDb("create-test-default-db", {
135+
defaultDatabase: "postgres",
136+
user: "postgres",
137+
password: PASSWORD,
138+
host: "localhost",
139+
port,
140+
})
126141

127-
return create()
128-
.then(create)
142+
return create().then(create)
129143
})
130144

131145
test.after.always(() => {

Diff for: ‎src/__tests__/fixtures/start-postgres.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ module.exports = (containerName, t) => {
88
try {
99
const events = spawn("docker", [
1010
"events",
11-
"--filter", "type=container",
12-
"--filter", `container=${containerName}`,
13-
"--filter", "event=health_status",
11+
"--filter",
12+
"type=container",
13+
"--filter",
14+
`container=${containerName}`,
15+
"--filter",
16+
"event=health_status",
1417
])
15-
events.stdout.on("data", (data) => {
18+
events.stdout.on("data", data => {
1619
data = data.toString()
1720

1821
if (data.includes("health_status: healthy")) {
@@ -21,7 +24,7 @@ module.exports = (containerName, t) => {
2124
t.end()
2225
}
2326
})
24-
events.on("error", (err) => {
27+
events.on("error", err => {
2528
console.error("Error in 'docker events' process:", err)
2629
events.kill()
2730
t.fail(err)
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = (name) => `CREATE TABLE ${name} (
1+
module.exports = name => `CREATE TABLE ${name} (
22
id integer
33
);
44
`

Diff for: ‎src/__tests__/migrate.js

+214-183
Large diffs are not rendered by default.

Diff for: ‎src/__unit__/file-name-parser/index.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test("parse name: 1.sql", t => {
1111
name: "1.sql",
1212
type: "sql",
1313
},
14-
"should parse correctly without name, the parsed name must be the fileName"
14+
"should parse correctly without name, the parsed name must be the fileName",
1515
)
1616
})
1717

@@ -24,7 +24,7 @@ test("parse name: 1file.sql", t => {
2424
name: "file",
2525
type: "sql",
2626
},
27-
"should parse correctly without separator"
27+
"should parse correctly without separator",
2828
)
2929
})
3030

@@ -37,7 +37,7 @@ test("parse name: 1-file.sql", t => {
3737
name: "file",
3838
type: "sql",
3939
},
40-
"should parse correctly with dash separator"
40+
"should parse correctly with dash separator",
4141
)
4242
})
4343

@@ -50,7 +50,7 @@ test("parse name: 1_file.sql", t => {
5050
name: "file",
5151
type: "sql",
5252
},
53-
"should parse correctly with underscore separator"
53+
"should parse correctly with underscore separator",
5454
)
5555
})
5656

@@ -63,7 +63,7 @@ test("parse name: 1-2_file.sql", t => {
6363
name: "2_file",
6464
type: "sql",
6565
},
66-
"should parse correctly returning everything after dash separator as name"
66+
"should parse correctly returning everything after dash separator as name",
6767
)
6868
})
6969

@@ -76,7 +76,7 @@ test("parse name: 1_2_file.sql", t => {
7676
name: "2_file",
7777
type: "sql",
7878
},
79-
"should parse correctly returning everything after underscore separator as name"
79+
"should parse correctly returning everything after underscore separator as name",
8080
)
8181
})
8282

@@ -89,7 +89,7 @@ test("parse name: 1_file.SQL", t => {
8989
name: "file",
9090
type: "sql",
9191
},
92-
"should parse correctly with case insensitive"
92+
"should parse correctly with case insensitive",
9393
)
9494
})
9595

@@ -102,7 +102,7 @@ test("parse name: 0001_file.sql", t => {
102102
name: "file",
103103
type: "sql",
104104
},
105-
"should parse correctly with left zeros"
105+
"should parse correctly with left zeros",
106106
)
107107
})
108108

Diff for: ‎src/__unit__/run-migration/index.js

+30-43
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ test.before(() => {
1818
normalSqlFile = contents
1919
}),
2020

21-
readFile(__dirname + "/fixtures/no-transaction.sql", "utf8").then(contents => {
22-
noTransactionSqlFile = contents
23-
}),
21+
readFile(__dirname + "/fixtures/no-transaction.sql", "utf8").then(
22+
contents => {
23+
noTransactionSqlFile = contents
24+
},
25+
),
2426

2527
Promise.resolve().then(() => {
2628
normalJsFile = loadSqlFromJs(__dirname + "/fixtures/normal.sql.js")
@@ -49,26 +51,22 @@ test("runs a simple migration", t => {
4951
t.is(
5052
query.firstCall.args[0],
5153
"START TRANSACTION",
52-
"should begin a transaction"
54+
"should begin a transaction",
5355
)
5456

5557
t.is(
5658
query.secondCall.args[0],
5759
migration.sql,
58-
"should execute the migration"
60+
"should execute the migration",
5961
)
6062

6163
t.deepEqual(
6264
query.thirdCall.args[0].values,
6365
[migration.id, migration.name, migration.hash],
64-
"should record the running of the migration in the database"
66+
"should record the running of the migration in the database",
6567
)
6668

67-
t.is(
68-
query.lastCall.args[0],
69-
"COMMIT",
70-
"should complete the transaction"
71-
)
69+
t.is(query.lastCall.args[0], "COMMIT", "should complete the transaction")
7270
})
7371
})
7472

@@ -83,26 +81,22 @@ test("runs a simple js migration", t => {
8381
t.is(
8482
query.firstCall.args[0],
8583
"START TRANSACTION",
86-
"should begin a transaction"
84+
"should begin a transaction",
8785
)
8886

8987
t.is(
9088
query.secondCall.args[0],
9189
migration.sql,
92-
"should execute the migration"
90+
"should execute the migration",
9391
)
9492

9593
t.deepEqual(
9694
query.thirdCall.args[0].values,
9795
[migration.id, migration.name, migration.hash],
98-
"should record the running of the migration in the database"
96+
"should record the running of the migration in the database",
9997
)
10098

101-
t.is(
102-
query.lastCall.args[0],
103-
"COMMIT",
104-
"should complete the transaction"
105-
)
99+
t.is(query.lastCall.args[0], "COMMIT", "should complete the transaction")
106100
})
107101
})
108102

@@ -117,7 +111,7 @@ test("rolls back when there is an error inside a transactiony migration", t => {
117111
t.is(query.lastCall.args[0], "ROLLBACK", "should perform a rollback")
118112
t.true(
119113
e.message.indexOf("There was a problem") >= 0,
120-
"should throw an error"
114+
"should throw an error",
121115
)
122116
})
123117
})
@@ -131,34 +125,27 @@ test("does not run the migration in a transaction when instructed", t => {
131125
return run(migration).then(() => {
132126
t.is(query.callCount, 2)
133127

134-
t.is(
135-
query.firstCall.args[0],
136-
migration.sql,
137-
"should run the migration"
138-
)
128+
t.is(query.firstCall.args[0], migration.sql, "should run the migration")
139129

140130
t.deepEqual(
141131
query.secondCall.args[0].values,
142132
[migration.id, migration.name, migration.hash],
143-
"should record the running of the migration in the database"
133+
"should record the running of the migration in the database",
144134
)
145135
})
146136
})
147137

148-
test(
149-
"does not roll back when there is an error inside a transactiony migration",
150-
t => {
151-
const query = sinon.stub().rejects(new Error("There was a problem"))
152-
const run = runMigration(migrationTableName, {query})
153-
154-
const migration = buildMigration(noTransactionSqlFile)
155-
156-
return run(migration).catch(e => {
157-
sinon.assert.neverCalledWith(query, "ROLLBACK")
158-
t.true(
159-
e.message.indexOf("There was a problem") >= 0,
160-
"should throw an error"
161-
)
162-
})
163-
}
164-
)
138+
test("does not roll back when there is an error inside a transactiony migration", t => {
139+
const query = sinon.stub().rejects(new Error("There was a problem"))
140+
const run = runMigration(migrationTableName, {query})
141+
142+
const migration = buildMigration(noTransactionSqlFile)
143+
144+
return run(migration).catch(e => {
145+
sinon.assert.neverCalledWith(query, "ROLLBACK")
146+
t.true(
147+
e.message.indexOf("There was a problem") >= 0,
148+
"should throw an error",
149+
)
150+
})
151+
})

Diff for: ‎src/create.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
const pgtools = require("pgtools")
22

3-
module.exports = createDb
4-
53
// Time out after 10 seconds - should probably be able to override this
64
const DEFAULT_TIMEOUT = 10000
75

8-
async function createDb(dbName, dbConfig = {}, config = {}) { // eslint-disable-line complexity
6+
module.exports = async function createDb(dbName, dbConfig = {}, config = {}) {
7+
// eslint-disable-line complexity
98
const {user, password, host, port} = dbConfig
109
if (typeof dbName !== "string") {
1110
throw new Error("Must pass database name as a string")
@@ -19,6 +18,12 @@ async function createDb(dbName, dbConfig = {}, config = {}) { // eslint-disable-
1918
throw new Error("Database config problem")
2019
}
2120

21+
return create(dbName, dbConfig, config)
22+
}
23+
24+
async function create(dbName, dbConfig, config) {
25+
const {user, password, host, port} = dbConfig
26+
2227
const log = config.logger || (() => {})
2328

2429
log(`Attempting to create database: ${dbName}`)
@@ -33,15 +38,21 @@ async function createDb(dbName, dbConfig = {}, config = {}) { // eslint-disable-
3338
}
3439

3540
try {
36-
await pgtools.createdb(pgtoolsConfig, dbName)
37-
.timeout(DEFAULT_TIMEOUT, `Timed out trying to create database: ${dbName}`) // pgtools uses Bluebird
41+
await pgtools
42+
.createdb(pgtoolsConfig, dbName)
43+
.timeout(
44+
DEFAULT_TIMEOUT,
45+
`Timed out trying to create database: ${dbName}`,
46+
) // pgtools uses Bluebird
3847
log(`Created database: ${dbName}`)
3948
} catch (err) {
4049
if (err) {
4150
// we are not worried about duplicate db errors
4251
if (err.name !== "duplicate_database") {
4352
log(err)
44-
throw new Error(`Error creating database. Caused by: '${err.name}: ${err.message}'`)
53+
throw new Error(
54+
`Error creating database. Caused by: '${err.name}: ${err.message}'`,
55+
)
4556
} else {
4657
log(`'${dbName}' database already exists`)
4758
}

Diff for: ‎src/files-loader.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ module.exports.load = async (directory, log) => {
3232
...fileNames.reduce(filterAndResolveFileName(directory), []),
3333
]
3434

35-
const unorderedMigrations = await Promise.all(migrationFiles.map(migrationFile.load))
35+
const unorderedMigrations = await Promise.all(
36+
migrationFiles.map(migrationFile.load),
37+
)
3638

3739
// Arrange in ID order
3840
orderedMigrations = unorderedMigrations.sort((a, b) => a.id - b.id)

Diff for: ‎src/migrate.js

+36-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ const dedent = require("dedent-js")
55
const runMigration = require("./run-migration")
66
const filesLoader = require("./files-loader")
77

8-
module.exports = async function migrate(dbConfig = {}, migrationsDirectory, config = {}) { // eslint-disable-line complexity
8+
module.exports = async function migrate(
9+
dbConfig = {},
10+
migrationsDirectory,
11+
config = {},
12+
) {
13+
// eslint-disable-line complexity
914
if (
1015
typeof dbConfig.database !== "string" ||
1116
typeof dbConfig.user !== "string" ||
@@ -19,13 +24,17 @@ module.exports = async function migrate(dbConfig = {}, migrationsDirectory, conf
1924
throw new Error("Must pass migrations directory as a string")
2025
}
2126

27+
return runMigrations(dbConfig, migrationsDirectory, config)
28+
}
29+
30+
async function runMigrations(dbConfig, migrationsDirectory, config) {
2231
const log = config.logger || (() => {})
2332

2433
const migrationTableName = "migrations"
2534

2635
const client = new pg.Client(dbConfig)
2736

28-
client.on("error", (err) => {
37+
client.on("error", err => {
2938
log(`pg client emitted an error: ${err.message}`)
3039
})
3140

@@ -37,7 +46,11 @@ module.exports = async function migrate(dbConfig = {}, migrationsDirectory, conf
3746

3847
const migrations = await filesLoader.load(migrationsDirectory, log)
3948

40-
const appliedMigrations = await fetchAppliedMigrationFromDB(migrationTableName, client, log)
49+
const appliedMigrations = await fetchAppliedMigrationFromDB(
50+
migrationTableName,
51+
client,
52+
log,
53+
)
4154

4255
validateMigrations(migrations, appliedMigrations)
4356

@@ -46,9 +59,9 @@ module.exports = async function migrate(dbConfig = {}, migrationsDirectory, conf
4659
const completedMigrations = []
4760

4861
for (const migration of filteredMigrations) {
49-
const result = await runMigration(
50-
migrationTableName, client, log
51-
)(migration)
62+
const result = await runMigration(migrationTableName, client, log)(
63+
migration,
64+
)
5265
completedMigrations.push(result)
5366
}
5467

@@ -88,27 +101,35 @@ async function fetchAppliedMigrationFromDB(migrationTableName, client, log) {
88101
// Validates mutation order and hash
89102
function validateMigrations(migrations, appliedMigrations) {
90103
const indexNotMatch = (migration, index) => migration.id !== index
91-
const invalidHash = (migration) => appliedMigrations[migration.id] && appliedMigrations[migration.id].hash !== migration.hash
104+
const invalidHash = migration =>
105+
appliedMigrations[migration.id] &&
106+
appliedMigrations[migration.id].hash !== migration.hash
92107

93108
// Assert migration IDs are consecutive integers
94109
const notMatchingId = migrations.find(indexNotMatch)
95110
if (notMatchingId) {
96-
throw new Error(`Found a non-consecutive migration ID on file: '${notMatchingId.fileName}'`)
111+
throw new Error(
112+
`Found a non-consecutive migration ID on file: '${
113+
notMatchingId.fileName
114+
}'`,
115+
)
97116
}
98117

99118
// Assert migration hashes are still same
100119
const invalidHashes = migrations.filter(invalidHash)
101120
if (invalidHashes.length) {
102121
// Someone has altered one or more migrations which has already run - gasp!
103122
throw new Error(dedent`
104-
Hashes don't match for migrations '${invalidHashes.map(({fileName}) => fileName)}'.
123+
Hashes don't match for migrations '${invalidHashes.map(
124+
({fileName}) => fileName,
125+
)}'.
105126
This means that the scripts have changed since it was applied.`)
106127
}
107128
}
108129

109130
// Work out which migrations to apply
110131
function filterMigrations(migrations, appliedMigrations) {
111-
const notAppliedMigration = (migration) => !appliedMigrations[migration.id]
132+
const notAppliedMigration = migration => !appliedMigrations[migration.id]
112133

113134
return migrations.filter(notAppliedMigration)
114135
}
@@ -118,7 +139,11 @@ function logResult(completedMigrations, log) {
118139
if (completedMigrations.length === 0) {
119140
log("No migrations applied")
120141
} else {
121-
log(`Successfully applied migrations: ${completedMigrations.map(({name}) => name)}`)
142+
log(
143+
`Successfully applied migrations: ${completedMigrations.map(
144+
({name}) => name,
145+
)}`,
146+
)
122147
}
123148
}
124149

Diff for: ‎src/migration-file.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,24 @@ const getFileName = filePath => path.basename(filePath)
1313

1414
const getFileContents = async filePath => readFile(filePath, "utf8")
1515

16-
const hashString = string => crypto.createHash("sha1").update(string, "utf8").digest("hex")
16+
const hashString = string =>
17+
crypto
18+
.createHash("sha1")
19+
.update(string, "utf8")
20+
.digest("hex")
1721

1822
const getSqlStringLiteral = (filePath, contents, type) => {
1923
let result
2024
switch (type) {
21-
case "sql":
22-
result = contents
23-
break
24-
case "js":
25-
result = dedent(loadSqlFromJs(filePath))
26-
break
27-
default:
28-
result = null
29-
break
25+
case "sql":
26+
result = contents
27+
break
28+
case "js":
29+
result = dedent(loadSqlFromJs(filePath))
30+
break
31+
default:
32+
result = null
33+
break
3034
}
3135
return result
3236
}

Diff for: ‎src/run-migration.js

+28-16
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,41 @@ const dedent = require("dedent-js")
33

44
const noop = () => {}
55
const insertMigration = async (migrationTableName, client, migration, log) => {
6-
log(`Saving migration to '${migrationTableName}': ${migration.id} | ${migration.name} | ${migration.hash}`)
7-
8-
const sql = SQL`INSERT INTO `.append(migrationTableName).append(SQL` ("id", "name", "hash") VALUES (${migration.id},${migration.name},${migration.hash})`)
6+
log(
7+
`Saving migration to '${migrationTableName}': ${migration.id} | ${
8+
migration.name
9+
} | ${migration.hash}`,
10+
)
11+
12+
const sql = SQL`INSERT INTO `
13+
.append(migrationTableName)
14+
.append(
15+
SQL` ("id", "name", "hash") VALUES (${migration.id},${migration.name},${
16+
migration.hash
17+
})`,
18+
)
919

1020
log(`Executing query: ${sql.text}:${sql.values}`)
1121

1222
return client.query(sql)
1323
}
1424

15-
module.exports = (migrationTableName, client, log = noop) => async migration => {
16-
const inTransaction = migration.sql.includes("-- postgres-migrations disable-transaction") === false
25+
module.exports = (
26+
migrationTableName,
27+
client,
28+
log = noop,
29+
) => async migration => {
30+
const inTransaction =
31+
migration.sql.includes("-- postgres-migrations disable-transaction") ===
32+
false
1733

1834
log(`Running migration in transaction: ${inTransaction}`)
1935

20-
const begin = inTransaction
21-
? () => client.query("START TRANSACTION")
22-
: noop
36+
const begin = inTransaction ? () => client.query("START TRANSACTION") : noop
2337

24-
const end = inTransaction
25-
? () => client.query("COMMIT")
26-
: noop
38+
const end = inTransaction ? () => client.query("COMMIT") : noop
2739

28-
const cleanup = inTransaction
29-
? () => client.query("ROLLBACK")
30-
: noop
40+
const cleanup = inTransaction ? () => client.query("ROLLBACK") : noop
3141

3242
try {
3343
await begin()
@@ -42,9 +52,11 @@ module.exports = (migrationTableName, client, log = noop) => async migration =>
4252
} finally {
4353
throw new Error(
4454
dedent`
45-
An error occurred running '${migration.name}'. Rolled back this migration.
55+
An error occurred running '${
56+
migration.name
57+
}'. Rolled back this migration.
4658
No further migrations were run.
47-
Reason: ${err.message}`
59+
Reason: ${err.message}`,
4860
)
4961
}
5062
}

0 commit comments

Comments
 (0)
Please sign in to comment.