shmig icon indicating copy to clipboard operation
shmig copied to clipboard

Multiple `shmig up` commands running at the same time

Open shamrin opened this issue 6 years ago • 1 comments

It seems to me (after reading shmig source code), it's not safe to run multiple shmig up commands at the same time. It could happen in real life if one tries to do shmig up as part of application instance startup. With multiple instances of those running.

This problem is discussed at stackexchange.com, with a potential solution using SELECT FROM ... FOR UPDATE:

  • BEGIN
  • SELECT FROM $SCHEMA_TABLE FOR UPDATE WHERE version=$VERSION
  • if the former statement returns a value, stop
  • apply the migration (ROLLBACK if it fails and stop)
  • INSERT INTO $SCHEMA_TABLE(version) VALUES ($VERSION)
  • COMMIT

@mbucc Would you be open to have this problem fixed in shmig? I could try to contribute the solution.

shamrin avatar Jun 01 '19 17:06 shamrin

Hi,

This would be a valuable fix.

From a quick google, it looks like mysql and postgresql support "select for update"; sqlite3 does not. But I think it is fine to implement for whatever database you are using; I would certainly accept such a pull request.

I would like to add a test to shmig_test to verify bug before fixing it; I'll try and work one up this weekend.

Thanks,

Mark

mbucc avatar Jun 04 '19 02:06 mbucc