0

I'm currently trying to determine how to apply mysql database patches via mercurial commit hooks. Basically, I have an incoming commit hook pointing to some script in my search path. This all works. My real question is how do keep a series of mysql alter table statements that can be applied every time I pull (incoming mercurial hook), that won't error when run again. For instance:

I add a new column to a table. So in my altertables.sql I add something like:

ALTER TABLE `thecompany_tbl` ADD `firstLogin` INT NOT NULL DEFAULT '1';

I run this the first time via the automated commit hook. Works fine. Next time I pull, it will try and run this again, and yield something like:

#1060 - Duplicate column name 'firstLogin'

Anyway around this? It would be great if I could just keep a file with all the alterations and then apply it every time I pull. Thanks for any advice!

Greg
  • 6,453
  • 9
  • 45
  • 61

1 Answers1

1

You need to alter your script so that it doesn't make the change if it has already been applied.

This answer shows how to check if a column exists.

Alternatively, you could add a table to the database to contain a version number that the script checks and updates. That might be easier as the other option involves checking each type of change with a different query.

Community
  • 1
  • 1
Steve Kaye
  • 6,262
  • 2
  • 23
  • 27