migration.py 564 B

1234567891011121314151617181920212223
  1. import re
  2. registered = []
  3. module_name_regex = re.compile("^.*m_([0-9]+)_([a-z_]+)")
  4. class Migration:
  5. def __init__(self, version, name, method):
  6. self.version = int(version)
  7. self.name = name
  8. self.method = method
  9. def run(self, db):
  10. self.method(db)
  11. def migration(method):
  12. match = module_name_regex.match(method.__module__)
  13. if not match:
  14. raise Exception(f"Unable to find migration version in #{method.__module__}")
  15. version, name = match.groups()
  16. registered.append(Migration(version, name, method))