mark_migration.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const Adapter = require('../migrations/lib/adapter')
  2. const fs = require('fs').promises
  3. const path = require('path')
  4. async function main(args) {
  5. if (
  6. !args ||
  7. args.length === 0 ||
  8. args.includes('help') ||
  9. args.includes('--help') ||
  10. args.includes('-h')
  11. ) {
  12. console.log('')
  13. console.log('usage: node ./scripts/mark_migration.js migration state')
  14. console.log('')
  15. console.log(' migration: name of migration file')
  16. console.log(' state: executed | unexecuted')
  17. console.log('')
  18. return
  19. }
  20. const migration = args[0]
  21. if (!migration) {
  22. throw new Error('Error: migration must be supplied')
  23. }
  24. const state = args[1]
  25. if (!state) {
  26. throw new Error('Error: migration state must be supplied')
  27. }
  28. try {
  29. await fs.access(path.join(__dirname, '../migrations', `${migration}.js`))
  30. } catch (err) {
  31. throw new Error(
  32. `Error: migration ${migration} does not exist on disk: ${err}`
  33. )
  34. }
  35. console.log(`Marking ${migration} as ${state}`)
  36. process.env.SKIP_TAG_CHECK = 'true'
  37. const adapter = new Adapter()
  38. await adapter.connect()
  39. switch (state) {
  40. case 'executed':
  41. await adapter.markExecuted(migration)
  42. break
  43. case 'unexecuted':
  44. await adapter.unmarkExecuted(migration)
  45. break
  46. default:
  47. throw new Error(`invalid state "${state}"`)
  48. }
  49. console.log('Done')
  50. }
  51. if (require.main === module) {
  52. const args = process.argv.slice(2)
  53. main(args)
  54. .then(() => {
  55. process.exit(0)
  56. })
  57. .catch(err => {
  58. console.error(err)
  59. process.exit(1)
  60. })
  61. }