Skip to content

Recipe: Run a specific Migration

Munif Tanjim edited this page Mar 5, 2021 · 1 revision

🚨 Caution 🚨

While this might be sometimes useful in local development environment,

⚠️ THIS IS TOTALLY UNSAFE ⚠️ NOT RECOMMENDED AT ALL ⚠️

It will eventually mess up Synor's Records (unless you are being very very careful).

📝 Script

run-specific-migration.js

const { SynorError, SynorMigration } = require('@synor/core')
const { initSynor } = require('@synor/cli')

async function runSpecificMigration(type, version) {
  if (!type || !version) {
    throw new SynorError('type and version are required!')
  }

  const { database, source } = await initSynor({})

  try {
    await Promise.all([database.open(), source.open()])

    const info = await source.get(version, type)

    if (!info) {
      throw new SynorError('version not found!', 'not_found', { version, type })
    }

    const content = await source.read(info)
    const migration = SynorMigration(info, content)

    try {
      await database.lock()
      await database.run(migration)
    } finally {
      await database.unlock()
    }
  } finally {
    await Promise.all([database.close(), source.close()])
  }
}

const args = process.argv.slice(2)
const type = args[0]
const version = args[1]

runSpecificMigration(type, version)
  .then(() => {
    process.exit(0)
  })
  .catch((err) => {
    console.error(err)
    process.exit(1)
  })

Usage

$ node run-specific-migration.js do   <version>
$ node run-specific-migration.js undo <version>

You can optionally add this to your package.json scripts:

{
  "scripts": {
    "synor:do": "node run-specific-migration.js do",
    "synor:undo": "node run-specific-migration.js undo"
  }
}
$ npm run synor:do   <version>
$ npm run synor:undo <version>
Clone this wiki locally