How to use the smartmin.management.commands.collect_sql.SqlType function in smartmin

To help you get started, we’ve selected a few smartmin examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nyaruka / smartmin / smartmin / management / tests.py View on Github external
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')
        ])
github nyaruka / smartmin / smartmin / management / tests.py View on Github external
]),
            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')
        ])
github nyaruka / smartmin / smartmin / management / commands / collect_sql.py View on Github external
"""
        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)
github nyaruka / smartmin / smartmin / management / commands / collect_sql.py View on Github external
"""
        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)
github nyaruka / smartmin / smartmin / management / commands / collect_sql.py View on Github external
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
github nyaruka / smartmin / smartmin / management / commands / collect_sql.py View on Github external
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)