Skip to content

Commit

Permalink
Added example for using custom state storage
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 86e0795a46fd813aefc8afbd7da5d450211fed0c
Author: Wes Todd <[email protected]>
Date:   Mon Sep 24 16:59:45 2018 -0700

    example cleanup

commit 2bb649489ffb86451f82303e5be32a4b04b1301b
Merge: b62234a 9ee0385
Author: Wes Todd <[email protected]>
Date:   Mon Sep 24 16:45:12 2018 -0700

    Merge branch 'master' of https://github.com/roemba/node-migrate into roemba-master

commit 9ee0385
Author: Roemer Hendrikx <[email protected]>
Date:   Sun Sep 16 22:17:51 2018 +0200

    Small fix so that inserting into the migrations table with a new migration object generates a proper conflict

commit b6eed64
Author: Roemer Hendrikx <[email protected]>
Date:   Sun Sep 16 21:26:19 2018 +0200

    Changed database column type from text to jsonb and made delete and insert an upsert

    This removes the need foor JSON.parse or JSON.stringify

commit c663c9a
Author: Roemer Hendrikx <[email protected]>
Date:   Tue Aug 21 17:54:58 2018 +0200

    Moved the custom state storage example to its own example folder

commit 07cb6d3
Author: Roemer Hendrikx <[email protected]>
Date:   Tue Aug 21 17:53:58 2018 +0200

    Rewritten the example to use promises instead of async, as async is not supported in node 6. Also added a create table statement in the save function.

commit 076aa67
Author: Roemer Hendrikx <[email protected]>
Date:   Mon Aug 20 22:17:05 2018 +0200

    added example for using a custom state storage
  • Loading branch information
wesleytodd committed Sep 25, 2018
1 parent b62234a commit 067797c
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions examples/custom-state-storage/custom-state-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict'
const migrate = require('migrate')
const {Client} = require('pg')
const pg = new Client()

/**
* Stores and loads the executed migrations in the database. The table
* migrations is only one row and stores a JSON of the data that the
* migrate package uses to know which migrations have been executed.
*/
const customStateStorage = {
load: async function (fn) {
await pg.connect()

// Load the single row of migration data from the database
const {rows} = await pg.query('SELECT data FROM schema.migrations')

if (rows.length !== 1) {
console.log('Cannot read migrations from database. If this is the first time you run migrations, then this is normal.')
return fn(null, {})
}

// Call callback with new migration data object
await pg.end()
fn(null, rows[0].data)
},

save: async function (set, fn) {
await pg.connect()

// Check if table 'migrations' exists and if not, create it.
await pg.query('CREATE TABLE IF NOT EXISTS schema.migrations (id integer PRIMARY KEY, data jsonb NOT NULL)')

await pg.query(`
INSERT INTO schema.migrations (id, data)
VALUES (1, $1)
ON CONFLICT (id) DO UPDATE SET data = $1
`, [{
lastRun: set.lastRun,
migrations: set.migrations
}])

await pg.end()
fn()
}
}

/**
* Main application code
*/
migrate.load({
// Set class as custom stateStore
stateStore: customStateStorage
}, function (err, set) {
if (err) {
throw err
}

set.up((err2) => {
if (err2) {
throw err2
}

console.log('Migrations successfully ran')
})
})

0 comments on commit 067797c

Please sign in to comment.