Skip to content

Commit

Permalink
Merge pull request #33 from Sharaal/Support-Migrations-#32
Browse files Browse the repository at this point in the history
Support Migrations #32
  • Loading branch information
Sharaal committed Aug 22, 2019
2 parents eee30a1 + 6505ea1 commit 3c223b4
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ await sql.query(
)
```

## Migrations

There is also a migrations support to ensure the database is always in the latest schema in all environments. To use this it's needed to create migration files and executing the `migrate` command on every deploy.

Details for these can be found also in the [Wiki -> Migrations](https://github.com/Sharaal/sql-pg/wiki/Migrations).

## Contact

Found a bug or missing a feature? -> Create a new [Issue](https://github.com/Sharaal/sql-pg/issues)
Expand Down
68 changes: 68 additions & 0 deletions bin/migrate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env node

let debug
try {
debug = require('debug')('migrate')
} catch (e) {
debug = console.log
}

const fs = require('fs')
const path = require('path')

;(async () => {
debug('starting migrations')
let client
try {
client = require(path.join(process.cwd(), 'pg.js'))
debug('use the projects `pg.js` to connect to the database')
} catch (e) {
const { Client } = require('pg')
client = new Client({ connectionString: process.env.DATABASE_URL })
debug('use the default to connect to the database')
}
await client.connect()

debug('create table "migrations" if not exists')
await client.query('CREATE TABLE IF NOT EXISTS migrations (file VARCHAR(255) PRIMARY KEY)')

debug('select already processed migrations:')
const processed = (await client.query('SELECT * FROM migrations')).rows
.map(({ file }) => file)
debug('%o', processed)

debug('read directory, filter and sort migrations:')
const directory = path.join(process.cwd(), 'migrations')

const migrations = fs.readdirSync(directory)
.filter(file => (file.endsWith('.js') || file.endsWith('.sql')) && !processed.includes(file))
.sort((fileA, fileB) => parseInt(fileA, 10) - parseInt(fileB, 10))
debug('%o', migrations)

if (migrations.length === 0) {
debug('no unprocessed migrations found, database already up to date')
} else {
for (const file of migrations) {
debug('process migration file: "%s"', file)
try {
await client.query('BEGIN')
if (file.endsWith('.js')) {
await require(path.join(directory, file))(client)
}
if (file.endsWith('.sql')) {
await client.query(fs.readFileSync(path.join(directory, file)).toString())
}
await client.query('INSERT INTO migrations (file) VALUES ($1)', [file])
await client.query('COMMIT')
debug('file successfully processed')
} catch (e) {
await client.query('ROLLBACK')
debug('error: "%s"', e.message)
throw e
}
}
debug('all migrations successfully processed')
}

await client.end()
})()
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"publishConfig": {
"access": "public"
},
"bin" : {
"migrate" : "./bin/migrate.js"
},
"scripts": {
"lint": "standard",
"lint:fix": "standard --fix",
Expand All @@ -30,10 +33,15 @@
"sinon": "^7.2.4",
"standard": "^14.0.0"
},
"peerDependencies": {
"debug": "^4.1.1",
"pg": "^7.12.1"
},
"directories": {
"test": "test"
},
"files": [
"bin/migrate.js",
"index.js",
"LICENSE",
"package.json",
Expand Down

0 comments on commit 3c223b4

Please sign in to comment.