Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
MockMigration(operations=[
RunSQL("DROP INDEX test_2;"),
]),
MockMigration(operations=[
RunSQL("CREATE TRIGGER test_1 AFTER TRUNCATE ON flows_flowstep EXECUTE PROCEDURE foo();"),
RunSQL("CREATE INDEX a_test ON foo(bar);"),
RunPython(mock_run_python)
]),
]
call_command('collect_sql', output_dir='sql')
mock_write_dump.assert_has_calls([
call('indexes', [
SqlObjectOperation("CREATE INDEX a_test ON foo(bar);", SqlType.INDEX, "a_test", True),
SqlObjectOperation("CREATE INDEX test_1 ON foo(bar);", SqlType.INDEX, "test_1", True),
], 'sql'),
call('triggers', [
SqlObjectOperation("CREATE TRIGGER test_1 AFTER TRUNCATE ON flows_flowstep EXECUTE PROCEDURE foo();",
SqlType.TRIGGER, "test_1", True)
], 'sql')
])
]),
MockMigration(operations=[
RunSQL("DROP INDEX test_2;"),
]),
MockMigration(operations=[
RunSQL("CREATE TRIGGER test_1 AFTER TRUNCATE ON flows_flowstep EXECUTE PROCEDURE foo();"),
RunSQL("CREATE INDEX a_test ON foo(bar);"),
RunPython(mock_run_python)
]),
]
call_command('collect_sql', output_dir='sql')
mock_write_dump.assert_has_calls([
call('indexes', [
SqlObjectOperation("CREATE INDEX a_test ON foo(bar);", SqlType.INDEX, "a_test", True),
SqlObjectOperation("CREATE INDEX test_1 ON foo(bar);", SqlType.INDEX, "test_1", True),
], 'sql'),
call('triggers', [
SqlObjectOperation("CREATE TRIGGER test_1 AFTER TRUNCATE ON flows_flowstep EXECUTE PROCEDURE foo();",
SqlType.TRIGGER, "test_1", True)
], 'sql')
])
"""
by_type = {SqlType.INDEX: [], SqlType.FUNCTION: [], SqlType.TRIGGER: []}
for operation in operations:
by_type[operation.sql_type].append(operation)
# optionally sort each operation list by the object name
if not preserve_order:
for obj_type, ops in by_type.items():
by_type[obj_type] = sorted(ops, key=lambda o: o.obj_name)
if by_type[SqlType.INDEX]:
self.write_dump('indexes', by_type[SqlType.INDEX], output_dir)
if by_type[SqlType.FUNCTION]:
self.write_dump('functions', by_type[SqlType.FUNCTION], output_dir)
if by_type[SqlType.TRIGGER]:
self.write_dump('triggers', by_type[SqlType.TRIGGER], output_dir)
"""
Splits the list of SQL operations by type and dumps these to separate files
"""
by_type = {SqlType.INDEX: [], SqlType.FUNCTION: [], SqlType.TRIGGER: []}
for operation in operations:
by_type[operation.sql_type].append(operation)
# optionally sort each operation list by the object name
if not preserve_order:
for obj_type, ops in by_type.items():
by_type[obj_type] = sorted(ops, key=lambda o: o.obj_name)
if by_type[SqlType.INDEX]:
self.write_dump('indexes', by_type[SqlType.INDEX], output_dir)
if by_type[SqlType.FUNCTION]:
self.write_dump('functions', by_type[SqlType.FUNCTION], output_dir)
if by_type[SqlType.TRIGGER]:
self.write_dump('triggers', by_type[SqlType.TRIGGER], output_dir)
if len(tokens) < 3:
return None
# check statement is of form "CREATE|DROP TYPE ..."
if tokens[0].ttype != sql_tokens.DDL or tokens[1].ttype != sql_tokens.Keyword:
return None
if tokens[0].value.upper() in ('CREATE', 'CREATE OR REPLACE'):
is_create = True
elif tokens[0].value.upper() in ('DROP',):
is_create = False
else:
return None
try:
sql_type = SqlType[tokens[1].value.upper()]
except KeyError:
return None
if sql_type == SqlType.FUNCTION:
function = next(filter(lambda t: isinstance(t, sql.Function), tokens), None)
if not function:
raise InvalidSQLException(raw.value)
name = function.get_name()
else:
identifier = next(filter(lambda t: isinstance(t, sql.Identifier), tokens), None)
if not identifier:
raise InvalidSQLException(raw.value)
name = identifier.value
def write_type_dumps(self, operations, preserve_order, output_dir):
"""
Splits the list of SQL operations by type and dumps these to separate files
"""
by_type = {SqlType.INDEX: [], SqlType.FUNCTION: [], SqlType.TRIGGER: []}
for operation in operations:
by_type[operation.sql_type].append(operation)
# optionally sort each operation list by the object name
if not preserve_order:
for obj_type, ops in by_type.items():
by_type[obj_type] = sorted(ops, key=lambda o: o.obj_name)
if by_type[SqlType.INDEX]:
self.write_dump('indexes', by_type[SqlType.INDEX], output_dir)
if by_type[SqlType.FUNCTION]:
self.write_dump('functions', by_type[SqlType.FUNCTION], output_dir)
if by_type[SqlType.TRIGGER]:
self.write_dump('triggers', by_type[SqlType.TRIGGER], output_dir)