Skip to content

Commit cdaa69f

Browse files
committed
Add bin script for validating files
1 parent e26f308 commit cdaa69f

File tree

9 files changed

+32
-23
lines changed

9 files changed

+32
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Validate migration ordering when loading files (instead of when applying migrations)
66
- Expose `loadMigrationFiles` publicly, which can be used to validate files in e.g. a pre-push hook
7+
- Add `pg-validate-migrations` bin script
78

89
## 5.0.0
910

README.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,7 @@ While the migration system will notice this and refuse to apply the migrations,
8484

8585
The `loadMigrationFiles` function can be used to check if the migration files satisfy the rules.
8686

87-
```typescript
88-
import {loadMigrationFiles} from "postgres-migrations"
89-
90-
async function validateMigrations() {
91-
try {
92-
await loadMigrationFiles("path/to/migration/files")
93-
} catch (e) {
94-
// Oh no! Something isn't right with the migration files.
95-
throw e
96-
}
97-
}
98-
```
87+
Alternatively, use the `pg-validate-migrations` bin script: `pg-validate-migrations "path/to/migration/files"`.
9988

10089
## Design decisions
10190

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"description": "Stack Overflow style database migrations for PostgreSQL",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
7+
"bin": {
8+
"pg-validate-migrations": "./dist/bin/validate.js"
9+
},
710
"author": "Thom Wright",
811
"keywords": [
912
"postgres",

src/__unit__/migration-file/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import test from "ava"
2-
import {load} from "../../migration-file"
2+
import {loadMigrationFile} from "../../migration-file"
33

44
test("Hashes of JS files should be the same when the SQL is the same", async (t) => {
55
const [js1, js2] = await Promise.all([
6-
load(__dirname + "/fixtures/different-js-same-sql-1/1_js.js"),
7-
load(__dirname + "/fixtures/different-js-same-sql-2/1_js.js"),
6+
loadMigrationFile(__dirname + "/fixtures/different-js-same-sql-1/1_js.js"),
7+
loadMigrationFile(__dirname + "/fixtures/different-js-same-sql-2/1_js.js"),
88
])
99

1010
t.is(js1.hash, js2.hash)
1111
})
1212

1313
test("Hashes of JS files should be different when the SQL is different", async (t) => {
1414
const [js1, js2] = await Promise.all([
15-
load(__dirname + "/fixtures/same-js-different-sql-1/1_js.js"),
16-
load(__dirname + "/fixtures/same-js-different-sql-2/1_js.js"),
15+
loadMigrationFile(__dirname + "/fixtures/same-js-different-sql-1/1_js.js"),
16+
loadMigrationFile(__dirname + "/fixtures/same-js-different-sql-2/1_js.js"),
1717
])
1818

1919
t.not(js1.hash, js2.hash)

src/bin/validate.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env node
2+
// tslint:disable no-console
3+
4+
import {argv} from "process"
5+
import {loadMigrationFiles} from "../files-loader"
6+
7+
async function main(args: Array<string>) {
8+
const directory = args[0]
9+
10+
await loadMigrationFiles(directory, (x) => console.error(x))
11+
}
12+
13+
main(argv.slice(2)).catch((e) => {
14+
console.error(`ERROR: ${e.message}`)
15+
process.exit(1)
16+
})

src/files-loader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as fs from "fs"
22
import * as path from "path"
33
import {promisify} from "util"
4-
import {load as loadMigrationFile} from "./migration-file"
4+
import {loadMigrationFile} from "./migration-file"
55
import {Logger, Migration} from "./types"
66
import {validateMigrationOrdering} from "./validation"
77

@@ -17,7 +17,7 @@ const isValidFile = (fileName: string) => /\.(sql|js)$/gi.test(fileName)
1717
*
1818
* No assertions are made about the validity of the SQL.
1919
*/
20-
export const load = async (
20+
export const loadMigrationFiles = async (
2121
directory: string,
2222
// tslint:disable-next-line no-empty
2323
log: Logger = () => {},

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export {createDb} from "./create"
22
export {migrate} from "./migrate"
3-
export {load as loadMigrationFiles} from "./files-loader"
3+
export {loadMigrationFiles} from "./files-loader"
44

55
export {
66
ConnectionParams,

src/migrate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as pg from "pg"
22
import SQL from "sql-template-strings"
3-
import {load} from "./files-loader"
3+
import {loadMigrationFiles} from "./files-loader"
44
import {runMigration} from "./run-migration"
55
import {
66
BasicPgClient,
@@ -33,7 +33,7 @@ export async function migrate(
3333
if (typeof migrationsDirectory !== "string") {
3434
throw new Error("Must pass migrations directory as a string")
3535
}
36-
const intendedMigrations = await load(migrationsDirectory, log)
36+
const intendedMigrations = await loadMigrationFiles(migrationsDirectory, log)
3737

3838
if ("client" in dbConfig) {
3939
// we have been given a client to use, it should already be connected

src/migration-file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const getSqlStringLiteral = (
3131
}
3232
}
3333

34-
export const load = async (filePath: string) => {
34+
export const loadMigrationFile = async (filePath: string) => {
3535
const fileName = getFileName(filePath)
3636

3737
try {

0 commit comments

Comments
 (0)