toimpl.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. # mypy: allow-untyped-defs, allow-incomplete-defs, allow-untyped-calls
  2. # mypy: no-warn-return-any, allow-any-generics
  3. from typing import TYPE_CHECKING
  4. from sqlalchemy import schema as sa_schema
  5. from . import ops
  6. from .base import Operations
  7. from ..util.sqla_compat import _copy
  8. if TYPE_CHECKING:
  9. from sqlalchemy.sql.schema import Table
  10. @Operations.implementation_for(ops.AlterColumnOp)
  11. def alter_column(
  12. operations: "Operations", operation: "ops.AlterColumnOp"
  13. ) -> None:
  14. compiler = operations.impl.dialect.statement_compiler(
  15. operations.impl.dialect, None
  16. )
  17. existing_type = operation.existing_type
  18. existing_nullable = operation.existing_nullable
  19. existing_server_default = operation.existing_server_default
  20. type_ = operation.modify_type
  21. column_name = operation.column_name
  22. table_name = operation.table_name
  23. schema = operation.schema
  24. server_default = operation.modify_server_default
  25. new_column_name = operation.modify_name
  26. nullable = operation.modify_nullable
  27. comment = operation.modify_comment
  28. existing_comment = operation.existing_comment
  29. def _count_constraint(constraint):
  30. return not isinstance(constraint, sa_schema.PrimaryKeyConstraint) and (
  31. not constraint._create_rule or constraint._create_rule(compiler)
  32. )
  33. if existing_type and type_:
  34. t = operations.schema_obj.table(
  35. table_name,
  36. sa_schema.Column(column_name, existing_type),
  37. schema=schema,
  38. )
  39. for constraint in t.constraints:
  40. if _count_constraint(constraint):
  41. operations.impl.drop_constraint(constraint)
  42. operations.impl.alter_column(
  43. table_name,
  44. column_name,
  45. nullable=nullable,
  46. server_default=server_default,
  47. name=new_column_name,
  48. type_=type_,
  49. schema=schema,
  50. existing_type=existing_type,
  51. existing_server_default=existing_server_default,
  52. existing_nullable=existing_nullable,
  53. comment=comment,
  54. existing_comment=existing_comment,
  55. **operation.kw,
  56. )
  57. if type_:
  58. t = operations.schema_obj.table(
  59. table_name,
  60. operations.schema_obj.column(column_name, type_),
  61. schema=schema,
  62. )
  63. for constraint in t.constraints:
  64. if _count_constraint(constraint):
  65. operations.impl.add_constraint(constraint)
  66. @Operations.implementation_for(ops.DropTableOp)
  67. def drop_table(operations: "Operations", operation: "ops.DropTableOp") -> None:
  68. kw = {}
  69. if operation.if_exists is not None:
  70. kw["if_exists"] = operation.if_exists
  71. operations.impl.drop_table(
  72. operation.to_table(operations.migration_context), **kw
  73. )
  74. @Operations.implementation_for(ops.DropColumnOp)
  75. def drop_column(
  76. operations: "Operations", operation: "ops.DropColumnOp"
  77. ) -> None:
  78. column = operation.to_column(operations.migration_context)
  79. operations.impl.drop_column(
  80. operation.table_name, column, schema=operation.schema, **operation.kw
  81. )
  82. @Operations.implementation_for(ops.CreateIndexOp)
  83. def create_index(
  84. operations: "Operations", operation: "ops.CreateIndexOp"
  85. ) -> None:
  86. idx = operation.to_index(operations.migration_context)
  87. kw = {}
  88. if operation.if_not_exists is not None:
  89. kw["if_not_exists"] = operation.if_not_exists
  90. operations.impl.create_index(idx, **kw)
  91. @Operations.implementation_for(ops.DropIndexOp)
  92. def drop_index(operations: "Operations", operation: "ops.DropIndexOp") -> None:
  93. kw = {}
  94. if operation.if_exists is not None:
  95. kw["if_exists"] = operation.if_exists
  96. operations.impl.drop_index(
  97. operation.to_index(operations.migration_context),
  98. **kw,
  99. )
  100. @Operations.implementation_for(ops.CreateTableOp)
  101. def create_table(
  102. operations: "Operations", operation: "ops.CreateTableOp"
  103. ) -> "Table":
  104. kw = {}
  105. if operation.if_not_exists is not None:
  106. kw["if_not_exists"] = operation.if_not_exists
  107. table = operation.to_table(operations.migration_context)
  108. operations.impl.create_table(table, **kw)
  109. return table
  110. @Operations.implementation_for(ops.RenameTableOp)
  111. def rename_table(
  112. operations: "Operations", operation: "ops.RenameTableOp"
  113. ) -> None:
  114. operations.impl.rename_table(
  115. operation.table_name, operation.new_table_name, schema=operation.schema
  116. )
  117. @Operations.implementation_for(ops.CreateTableCommentOp)
  118. def create_table_comment(
  119. operations: "Operations", operation: "ops.CreateTableCommentOp"
  120. ) -> None:
  121. table = operation.to_table(operations.migration_context)
  122. operations.impl.create_table_comment(table)
  123. @Operations.implementation_for(ops.DropTableCommentOp)
  124. def drop_table_comment(
  125. operations: "Operations", operation: "ops.DropTableCommentOp"
  126. ) -> None:
  127. table = operation.to_table(operations.migration_context)
  128. operations.impl.drop_table_comment(table)
  129. @Operations.implementation_for(ops.AddColumnOp)
  130. def add_column(operations: "Operations", operation: "ops.AddColumnOp") -> None:
  131. table_name = operation.table_name
  132. column = operation.column
  133. schema = operation.schema
  134. kw = operation.kw
  135. if column.table is not None:
  136. column = _copy(column)
  137. t = operations.schema_obj.table(table_name, column, schema=schema)
  138. operations.impl.add_column(table_name, column, schema=schema, **kw)
  139. for constraint in t.constraints:
  140. if not isinstance(constraint, sa_schema.PrimaryKeyConstraint):
  141. operations.impl.add_constraint(constraint)
  142. for index in t.indexes:
  143. operations.impl.create_index(index)
  144. with_comment = (
  145. operations.impl.dialect.supports_comments
  146. and not operations.impl.dialect.inline_comments
  147. )
  148. comment = column.comment
  149. if comment and with_comment:
  150. operations.impl.create_column_comment(column)
  151. @Operations.implementation_for(ops.AddConstraintOp)
  152. def create_constraint(
  153. operations: "Operations", operation: "ops.AddConstraintOp"
  154. ) -> None:
  155. operations.impl.add_constraint(
  156. operation.to_constraint(operations.migration_context)
  157. )
  158. @Operations.implementation_for(ops.DropConstraintOp)
  159. def drop_constraint(
  160. operations: "Operations", operation: "ops.DropConstraintOp"
  161. ) -> None:
  162. operations.impl.drop_constraint(
  163. operations.schema_obj.generic_constraint(
  164. operation.constraint_name,
  165. operation.table_name,
  166. operation.constraint_type,
  167. schema=operation.schema,
  168. )
  169. )
  170. @Operations.implementation_for(ops.BulkInsertOp)
  171. def bulk_insert(
  172. operations: "Operations", operation: "ops.BulkInsertOp"
  173. ) -> None:
  174. operations.impl.bulk_insert( # type: ignore[union-attr]
  175. operation.table, operation.rows, multiinsert=operation.multiinsert
  176. )
  177. @Operations.implementation_for(ops.ExecuteSQLOp)
  178. def execute_sql(
  179. operations: "Operations", operation: "ops.ExecuteSQLOp"
  180. ) -> None:
  181. operations.migration_context.impl.execute(
  182. operation.sqltext, execution_options=operation.execution_options
  183. )