Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from sqlalchemy import schema as sa_schema 

2 

3from . import ops 

4from .base import Operations 

5from ..util import sqla_compat 

6 

7 

8@Operations.implementation_for(ops.AlterColumnOp) 

9def alter_column(operations, operation): 

10 

11 compiler = operations.impl.dialect.statement_compiler( 

12 operations.impl.dialect, None 

13 ) 

14 

15 existing_type = operation.existing_type 

16 existing_nullable = operation.existing_nullable 

17 existing_server_default = operation.existing_server_default 

18 type_ = operation.modify_type 

19 column_name = operation.column_name 

20 table_name = operation.table_name 

21 schema = operation.schema 

22 server_default = operation.modify_server_default 

23 new_column_name = operation.modify_name 

24 nullable = operation.modify_nullable 

25 comment = operation.modify_comment 

26 existing_comment = operation.existing_comment 

27 

28 def _count_constraint(constraint): 

29 return not isinstance(constraint, sa_schema.PrimaryKeyConstraint) and ( 

30 not constraint._create_rule or constraint._create_rule(compiler) 

31 ) 

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 

43 operations.impl.alter_column( 

44 table_name, 

45 column_name, 

46 nullable=nullable, 

47 server_default=server_default, 

48 name=new_column_name, 

49 type_=type_, 

50 schema=schema, 

51 existing_type=existing_type, 

52 existing_server_default=existing_server_default, 

53 existing_nullable=existing_nullable, 

54 comment=comment, 

55 existing_comment=existing_comment, 

56 **operation.kw 

57 ) 

58 

59 if type_: 

60 t = operations.schema_obj.table( 

61 table_name, 

62 operations.schema_obj.column(column_name, type_), 

63 schema=schema, 

64 ) 

65 for constraint in t.constraints: 

66 if _count_constraint(constraint): 

67 operations.impl.add_constraint(constraint) 

68 

69 

70@Operations.implementation_for(ops.DropTableOp) 

71def drop_table(operations, operation): 

72 operations.impl.drop_table( 

73 operation.to_table(operations.migration_context) 

74 ) 

75 

76 

77@Operations.implementation_for(ops.DropColumnOp) 

78def drop_column(operations, operation): 

79 column = operation.to_column(operations.migration_context) 

80 operations.impl.drop_column( 

81 operation.table_name, column, schema=operation.schema, **operation.kw 

82 ) 

83 

84 

85@Operations.implementation_for(ops.CreateIndexOp) 

86def create_index(operations, operation): 

87 idx = operation.to_index(operations.migration_context) 

88 operations.impl.create_index(idx) 

89 

90 

91@Operations.implementation_for(ops.DropIndexOp) 

92def drop_index(operations, operation): 

93 operations.impl.drop_index( 

94 operation.to_index(operations.migration_context) 

95 ) 

96 

97 

98@Operations.implementation_for(ops.CreateTableOp) 

99def create_table(operations, operation): 

100 table = operation.to_table(operations.migration_context) 

101 operations.impl.create_table(table) 

102 return table 

103 

104 

105@Operations.implementation_for(ops.RenameTableOp) 

106def rename_table(operations, operation): 

107 operations.impl.rename_table( 

108 operation.table_name, operation.new_table_name, schema=operation.schema 

109 ) 

110 

111 

112@Operations.implementation_for(ops.CreateTableCommentOp) 

113def create_table_comment(operations, operation): 

114 table = operation.to_table(operations.migration_context) 

115 operations.impl.create_table_comment(table) 

116 

117 

118@Operations.implementation_for(ops.DropTableCommentOp) 

119def drop_table_comment(operations, operation): 

120 table = operation.to_table(operations.migration_context) 

121 operations.impl.drop_table_comment(table) 

122 

123 

124@Operations.implementation_for(ops.AddColumnOp) 

125def add_column(operations, operation): 

126 table_name = operation.table_name 

127 column = operation.column 

128 schema = operation.schema 

129 kw = operation.kw 

130 

131 t = operations.schema_obj.table(table_name, column, schema=schema) 

132 operations.impl.add_column(table_name, column, schema=schema, **kw) 

133 

134 for constraint in t.constraints: 

135 if not isinstance(constraint, sa_schema.PrimaryKeyConstraint): 

136 operations.impl.add_constraint(constraint) 

137 for index in t.indexes: 

138 operations.impl.create_index(index) 

139 

140 with_comment = ( 

141 sqla_compat._dialect_supports_comments(operations.impl.dialect) 

142 and not operations.impl.dialect.inline_comments 

143 ) 

144 comment = sqla_compat._comment_attribute(column) 

145 if comment and with_comment: 

146 operations.impl.create_column_comment(column) 

147 

148 

149@Operations.implementation_for(ops.AddConstraintOp) 

150def create_constraint(operations, operation): 

151 operations.impl.add_constraint( 

152 operation.to_constraint(operations.migration_context) 

153 ) 

154 

155 

156@Operations.implementation_for(ops.DropConstraintOp) 

157def drop_constraint(operations, operation): 

158 operations.impl.drop_constraint( 

159 operations.schema_obj.generic_constraint( 

160 operation.constraint_name, 

161 operation.table_name, 

162 operation.constraint_type, 

163 schema=operation.schema, 

164 ) 

165 ) 

166 

167 

168@Operations.implementation_for(ops.BulkInsertOp) 

169def bulk_insert(operations, operation): 

170 operations.impl.bulk_insert( 

171 operation.table, operation.rows, multiinsert=operation.multiinsert 

172 ) 

173 

174 

175@Operations.implementation_for(ops.ExecuteSQLOp) 

176def execute_sql(operations, operation): 

177 operations.migration_context.impl.execute( 

178 operation.sqltext, execution_options=operation.execution_options 

179 )