mark_migration.mjs 1.7 KB

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