Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration Callbacks #881

Open
mike-usa opened this issue Feb 25, 2022 · 0 comments
Open

Migration Callbacks #881

mike-usa opened this issue Feb 25, 2022 · 0 comments

Comments

@mike-usa
Copy link

mike-usa commented Feb 25, 2022

I've reached a unique case of trying to remove all artifacts of an initial migration. In the migrate down I attempt to remove pgmigrations table, which understandably causes an issue, since afterwards the node-pg-migrate tries to delete the migration record from the table that no longer exists.

The question is if node-pg-migrate provides callbacks that I've yet to discover in the docs. My case of this cleanup is unique and unlikely to happen often, but I envision wanting to build notifiers (reports or emails) after failures/commits. If my database is 3 migrations behind, it'd be nice to wait for all three migrations to finish to consolidate into one report.


1645591900441_my-first-migration.js

/* eslint-disable camelcase */

exports.shorthands = undefined;

exports.up = (pgm) => {
  pgm.createTable('users', {
    id: 'id',
    name: { type: 'varchar(1000)', notNull: true },
    createdAt: {
      type: 'timestamp',
      notNull: true,
      default: pgm.func('current_timestamp'),
    },
  })

  pgm.createTable('posts', {
    id: 'id',
    userId: {
      type: 'integer',
      notNull: true,
      references: '"users"',
      onDelete: 'cascade',
    },
    body: { type: 'text', notNull: true },
    createdAt: {
      type: 'timestamp',
      notNull: true,
      default: pgm.func('current_timestamp'),
    },
  })

  pgm.createIndex('posts', 'userId')
}

exports.down = (pgm) => {
  pgm.dropTable('posts')
  pgm.dropTable('users')
  pgm.dropTable('pgmigrations',{ ifExists: true })  // <-- problematic line
}

command line

> npx node-pg-migrate "down" "-s" "some-schema"

> Migrating files:
> - 1645591900441_my-first-migration
### MIGRATION 1645591900441_my-first-migration (DOWN) ###
DROP TABLE "posts";
DROP TABLE "users";
DROP TABLE IF EXISTS "pgmigrations";
DELETE FROM "some-schema"."pgmigrations" WHERE name='1645591900441_my-first-migration';


Error executing:
DELETE FROM "some-schema"."pgmigrations" WHERE name='1645591900441_my-first-migration';
            ^^^^

relation "some-schema.pgmigrations" does not exist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant