-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from Sharaal/Support-Migrations-#32
Support Migrations #32
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters