Commit 079257f4 authored by sim's avatar sim

[DB] Alembic: factorize ReversibleOp

parent 382d518a
...@@ -7,6 +7,8 @@ the name of "ReplaceableObject" class. ...@@ -7,6 +7,8 @@ the name of "ReplaceableObject" class.
This recipe is directly borrowed from Alembic documentation, see This recipe is directly borrowed from Alembic documentation, see
http://alembic.zzzcomputing.com/en/latest/cookbook.html#replaceable-objects http://alembic.zzzcomputing.com/en/latest/cookbook.html#replaceable-objects
**2017-10-09** ReversibleOp.define has been added to reduce boilerplate code.
""" """
from alembic.operations import Operations, MigrateOperation from alembic.operations import Operations, MigrateOperation
...@@ -58,57 +60,32 @@ class ReversibleOp(MigrateOperation): ...@@ -58,57 +60,32 @@ class ReversibleOp(MigrateOperation):
operations.invoke(drop_old) operations.invoke(drop_old)
operations.invoke(create_new) operations.invoke(create_new)
@classmethod
def define(cls, name, cname=None, register=Operations.register_operation):
def create(self):
return CreateOp(self.target)
@Operations.register_operation("create_view", "invoke_for_target") def drop(self):
@Operations.register_operation("replace_view", "replace") return DropOp(self.target)
class CreateViewOp(ReversibleOp):
def reverse(self):
return DropViewOp(self.target)
@Operations.register_operation("drop_view", "invoke_for_target")
class DropViewOp(ReversibleOp):
def reverse(self):
return CreateViewOp(self.target)
@Operations.register_operation("create_sp", "invoke_for_target")
@Operations.register_operation("replace_sp", "replace")
class CreateSPOp(ReversibleOp):
def reverse(self):
return DropSPOp(self.target)
@Operations.register_operation("drop_sp", "invoke_for_target")
class DropSPOp(ReversibleOp):
def reverse(self):
return CreateSPOp(self.target)
@Operations.register_operation("create_trigger", "invoke_for_target") name = name.lower()
@Operations.register_operation("replace_trigger", "replace") cname = cname or name.capitalize()
class CreateTriggerOp(ReversibleOp):
def reverse(self):
return DropTriggerOp(self.target)
CreateOp = type('Create%sOp' % cname, (ReversibleOp,), {'reverse': drop})
DropOp = type('Drop%sOp' % cname, (ReversibleOp,), {'reverse': create})
@Operations.register_operation("drop_trigger", "invoke_for_target") CreateOp = register('create_' + name, 'invoke_for_target')(CreateOp)
class DropTriggerOp(ReversibleOp): CreateOp = register('replace_' + name, 'replace')(CreateOp)
def reverse(self):
return CreateTriggerOp(self.target)
DropOp = register('drop_' + name, 'invoke_for_target')(DropOp)
@Operations.register_operation("create_role", "invoke_for_target") return (CreateOp, DropOp)
@Operations.register_operation("replace_sp", "replace")
class CreateRoleOp(ReversibleOp):
def reverse(self):
return DropRoleOp(self.target)
@Operations.register_operation("drop_role", "invoke_for_target") CreateViewOp, DropViewOp = ReversibleOp.define('view')
class DropRoleOp(ReversibleOp): CreateRoleOp, DropRoleOp = ReversibleOp.define('role')
def reverse(self): CreateSPOp, DropSPOp = ReversibleOp.define('sp', 'SP')
return CreateRoleOp(self.target) CreateTriggerOp, DropTriggerOp = ReversibleOp.define('trigger')
@Operations.implementation_for(CreateViewOp) @Operations.implementation_for(CreateViewOp)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment