script.py.mako 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """${message}
  2. Revision ID: ${up_revision}
  3. Revises: ${down_revision | comma,n}
  4. Create Date: ${create_date}
  5. Schema: %(schema)s
  6. """
  7. from typing import Sequence, Union
  8. from alembic import op
  9. import sqlalchemy as sa
  10. ${imports if imports else ""}
  11. # revision identifiers, used by Alembic.
  12. revision: str = ${repr(up_revision)}
  13. down_revision: Union[str, None] = ${repr(down_revision)}
  14. branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
  15. depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
  16. def upgrade() -> None:
  17. # Get the schema name
  18. schema = op.get_context().get_context_kwargs.get('version_table_schema')
  19. """
  20. ### Schema-aware migration
  21. All table operations should include the schema name, for example:
  22. op.create_tables(
  23. 'my_table',
  24. sa.Column('id', sa.Integer(), nullable=False),
  25. sa.Column('name', sa.String(), nullable=True),
  26. schema=schema
  27. )
  28. op.create_index(
  29. 'idx_my_table_name',
  30. 'my_table',
  31. ['name'],
  32. schema=schema
  33. )
  34. """
  35. ${upgrades if upgrades else "pass"}
  36. def downgrade() -> None:
  37. # Get the schema name
  38. schema = op.get_context().get_context_kwargs.get('version_table_schema')
  39. """
  40. ### Schema-aware downgrade
  41. Remember to include schema in all operations, for example:
  42. op.drop_table('my_table', schema=schema)
  43. """
  44. ${downgrades if downgrades else "pass"}