sql-migrate icon indicating copy to clipboard operation
sql-migrate copied to clipboard

Add hook support

Open diegobernardes opened this issue 5 years ago • 3 comments

Today the package already know some special comments, I'm proposing to add hook points as special comments where plain Go code could execute along the migration. This is very useful during transformations caused by the migration.

-- +migrate Up
ALTER TABLE users ADD COLUMN confirmation BOOLEAN;
-- +hook addConfirmation

-- +migrate Down
ALTER TABLE users DROP COLUMN confirmation BOOLEAN;

Using the standalone cli, don't know how this could work, but, using the library is kinda easy:

type hook func(db *sql.DB, tx *sql.Tx) error

migrations := &migrate.MemoryMigrationSource{
    Migrations: []*migrate.Migration{
        &migrate.Migration{
            Id:   "123",
            Up:   []string{"CREATE TABLE people (id int)"},
            Down: []string{"DROP TABLE people"},
            Hook: map[string]hook{"addConfirmation": addConfirmation},
        },
    },
}

func addConfirmation(db *sql.DB, tx *sql.Tx) error {
  // custom logic here
  return nil
}

diegobernardes avatar Sep 13 '18 12:09 diegobernardes

Hi, could you explain what you mean by this?

rubenv avatar Sep 13 '18 12:09 rubenv

Yes, sorry, clicked in the create by mistake, 1 sec, writing the message.

diegobernardes avatar Sep 13 '18 12:09 diegobernardes

I like the idea of hooks and I don't mind that they might not be available through the CLI.

If we do add them it's probably best to scan for them when using the CLI and refuse to operate when a hook is detected. Just to make sure things either happen correctly or not at all.

There's a real danger for abuse though, so they should be used sparingly.

rubenv avatar Sep 13 '18 12:09 rubenv