Esempio n. 1
0
    def handle(self, **options):
        try:
            schema_tool = SchemaTool()
        except UnsupportedDBError as e:
            raise CommandError(e)

        result = SchemaDump()
        if options['tables']:
            result.set_table_list(schema_tool.get_tables())
            self.stdout.write(str(result))
        else:
            result.load({'defaults': schema_tool.get_defaults()})
            for app_label in schema_tool.app_configs:
                app_dump = SchemaAppDump(app_label)
                table_names = schema_tool.get_app_tables(app_label)
                for table_name in table_names:
                    table_dump = SchemaTableDump(table_name)
                    table_dump.load({
                        'fields':
                        schema_tool.get_table_fields(table_name),
                        'indices':
                        schema_tool.get_table_indices(table_name),
                        'constraints':
                        schema_tool.get_table_constraints(table_name)
                    })
                    app_dump.add_table(table_dump)
                if table_names:
                    result.add_app(app_dump)
            self.stdout.write(str(result))
Esempio n. 2
0
    def handle(self, **options):
        try:
            schema_tool = SchemaTool()
        except UnsupportedDBError as e:
            raise CommandError(e)

        result = SchemaDump()
        if options['tables']:
            result.set_table_list(schema_tool.get_tables())
            self.stdout.write(str(result))
        else:
            result.load({
                'defaults': schema_tool.get_defaults()})
            for app_label in schema_tool.app_configs:
                app_dump = SchemaAppDump(app_label)
                table_names = schema_tool.get_app_tables(app_label)
                for table_name in table_names:
                    table_dump = SchemaTableDump(table_name)
                    table_dump.load({
                        'fields': schema_tool.get_table_fields(table_name),
                        'indices': schema_tool.get_table_indices(table_name),
                        'constraints':
                            schema_tool.get_table_constraints(table_name)})
                    app_dump.add_table(table_dump)
                if table_names:
                    result.add_app(app_dump)
            self.stdout.write(str(result))
Esempio n. 3
0
class SchemaAppCommand(SchemaCommand):
    def add_arguments(self, parser):
        parser.add_argument(
            'args',
            metavar='app_label',
            nargs='+',
            help='Application labels.'
        )
        parser.add_argument(
            '--tables',
            action='store_true',
            default=False,
            dest='tables',
            help='Print all table names.',
        )
        super(SchemaAppCommand, self).add_arguments(parser)

    def handle(self, *args, **options):
        try:
            self.schema_tool = SchemaTool(*args)

        except (LookupError, ImportError) as e:
            raise CommandError("%s. Are you sure your INSTALLED_APPS "
                               "setting is correct?" % e)

        if options['tables']:
            result = dict(apps={})
            for app_label in args:
                result['apps'] = {
                    'name': app_label,
                    'tables': self.schema_tool.get_app_tables(app_label),
                }

            self.stdout.write(jsonify(result))
        else:
            result = dict(apps={})
            for app_label in args:
                result['apps'][app_label] = {
                    'name': app_label,
                    'tables': {}
                }
                for table_name in self.schema_tool.get_app_tables(app_label):
                    result['apps'][app_label]['tables'].update(
                        self.handle_table(table_name, **options)
                    )

            self.stdout.write(jsonify(result))
Esempio n. 4
0
class SchemaAppCommand(SchemaCommand):
    def add_arguments(self, parser):
        parser.add_argument(
            'args',
            metavar='app_label',
            nargs='+',
            help='Application labels.'
        )
        parser.add_argument(
            '--tables',
            action='store_true',
            default=False,
            dest='tables',
            help='Print all table names.',
        )
        super(SchemaAppCommand, self).add_arguments(parser)

    def handle(self, *args, **options):
        try:
            self.schema_tool = SchemaTool(*args)
        except (LookupError, ImportError) as e:
            raise CommandError("%s. Are you sure your INSTALLED_APPS "
                               "setting is correct?" % e)
        except UnsupportedDBError as e:
            raise CommandError(e)

        if options['tables']:
            result = SchemaDump()
            for app_label in args:
                result.set_table_list(
                    self.schema_tool.get_app_tables(app_label))

            self.stdout.write(str(result))
        else:
            result = SchemaDump()
            for app_label in args:
                app_result = SchemaAppDump(app_label)
                for table_name in self.schema_tool.get_app_tables(app_label):
                    app_result.add_table(
                        self.handle_table(table_name, **options))
                result.add_app(app_result)

            self.stdout.write(str(result))
Esempio n. 5
0
class SchemaAppCommand(SchemaCommand):
    def add_arguments(self, parser):
        parser.add_argument('args',
                            metavar='app_label',
                            nargs='+',
                            help='Application labels.')
        parser.add_argument(
            '--tables',
            action='store_true',
            default=False,
            dest='tables',
            help='Print all table names.',
        )
        super(SchemaAppCommand, self).add_arguments(parser)

    def handle(self, *args, **options):
        try:
            self.schema_tool = SchemaTool(*args)
        except (LookupError, ImportError) as e:
            raise CommandError("%s. Are you sure your INSTALLED_APPS "
                               "setting is correct?" % e)
        except UnsupportedDBError as e:
            raise CommandError(e)

        if options['tables']:
            result = SchemaDump()
            for app_label in args:
                result.set_table_list(
                    self.schema_tool.get_app_tables(app_label))

            self.stdout.write(str(result))
        else:
            result = SchemaDump()
            for app_label in args:
                app_result = SchemaAppDump(app_label)
                for table_name in self.schema_tool.get_app_tables(app_label):
                    app_result.add_table(
                        self.handle_table(table_name, **options))
                result.add_app(app_result)

            self.stdout.write(str(result))
Esempio n. 6
0
def test_schema_tool():
    if get_current_db_type() != 'mysql':
        pytest.skip("unsupported database")

    schema_tool = SchemaTool()
    defaults = schema_tool.get_defaults()
    assert set(defaults.keys()) == TEST_MYSQL_SCHEMA_PARAM_NAMES['defaults']
    for app_label in schema_tool.app_configs:
        for table in schema_tool.get_app_tables(app_label):
            row = schema_tool.get_table_fields(table).values()[0]
            assert (set([x.lower() for x in row.keys()]).issubset(
                TEST_MYSQL_SCHEMA_PARAM_NAMES['tables']['fields']))
            row = schema_tool.get_table_indices(table).values()[0]
            assert (set([x.lower() for x in row.keys()]).issubset(
                TEST_MYSQL_SCHEMA_PARAM_NAMES['tables']['indices']))
            row = schema_tool.get_table_constraints(table).values()[0]
            assert (set([x.lower() for x in row.keys()]).issubset(
                TEST_MYSQL_SCHEMA_PARAM_NAMES['tables']['constraints']))
Esempio n. 7
0
def test_schema_tool():
    schema_tool = SchemaTool()
    if isinstance(schema_tool.schema_dumper, MySQLSchemaDumper):
        defaults = schema_tool.get_defaults()
        assert set(
            defaults.keys()) == TEST_MYSQL_SCHEMA_PARAM_NAMES['defaults']
        for app_label in schema_tool.app_configs:
            for table in schema_tool.get_app_tables(app_label):
                row = schema_tool.get_table_fields(table)[0]
                assert (set([
                    x.lower() for x in row.keys()
                ]) == TEST_MYSQL_SCHEMA_PARAM_NAMES['tables']['fields'])
                row = schema_tool.get_table_indices(table)[0]
                assert (set([
                    x.lower() for x in row.keys()
                ]) == TEST_MYSQL_SCHEMA_PARAM_NAMES['tables']['indices'])
                row = schema_tool.get_table_constraints(table)[0]
                assert (set([
                    x.lower() for x in row.keys()
                ]) == TEST_MYSQL_SCHEMA_PARAM_NAMES['tables']['constraints'])
Esempio n. 8
0
def test_schema_dump(test_fs):
    if get_current_db_type() != 'mysql':
        pytest.skip("unsupported database")

    schema_tool = SchemaTool()
    expected_result = SchemaDump()
    with test_fs.open(['data', 'schema.json']) as f:
        expected_result.load(data=json.loads(f.read()))

    assert expected_result.defaults == schema_tool.get_defaults()
    for app_label in schema_tool.app_configs:
        expected_app_result = expected_result.get_app(app_label)
        for table_name in schema_tool.get_app_tables(app_label):
            expected_table_result = expected_app_result.get_table(table_name)
            assert (schema_tool.get_table_fields(table_name) ==
                    expected_table_result.fields)
            assert (schema_tool.get_table_indices(table_name) ==
                    expected_table_result.indices)
            assert (schema_tool.get_table_constraints(table_name) ==
                    expected_table_result.constraints)
Esempio n. 9
0
def test_schema_dump(test_fs):
    if get_current_db_type() != 'mysql':
        pytest.skip("unsupported database")

    schema_tool = SchemaTool()
    expected_result = SchemaDump()
    with test_fs.open(['data', 'schema.json']) as f:
        expected_result.load(data=json.loads(f.read()))

    assert expected_result.defaults == schema_tool.get_defaults()
    for app_label in schema_tool.app_configs:
        expected_app_result = expected_result.get_app(app_label)
        for table_name in schema_tool.get_app_tables(app_label):
            expected_table_result = expected_app_result.get_table(table_name)
            assert (schema_tool.get_table_fields(table_name) ==
                    expected_table_result.fields)
            assert (schema_tool.get_table_indices(table_name) ==
                    expected_table_result.indices)
            assert (schema_tool.get_table_constraints(table_name) ==
                    expected_table_result.constraints)
Esempio n. 10
0
def test_schema_tool():
    schema_tool = SchemaTool()
    if isinstance(schema_tool.schema_dumper, MySQLSchemaDumper):
        defaults = schema_tool.get_defaults()
        assert set(defaults.keys()) == TEST_MYSQL_SCHEMA_PARAM_NAMES['defaults']
        for app_label in schema_tool.app_configs:
            for table in schema_tool.get_app_tables(app_label):
                row = schema_tool.get_table_fields(table)[0]
                assert (
                    set([x.lower() for x in row.keys()]) ==
                    TEST_MYSQL_SCHEMA_PARAM_NAMES['tables']['fields']
                )
                row = schema_tool.get_table_indices(table)[0]
                assert (
                    set([x.lower() for x in row.keys()]) ==
                    TEST_MYSQL_SCHEMA_PARAM_NAMES['tables']['indices']
                )
                row = schema_tool.get_table_constraints(table)[0]
                assert (
                    set([x.lower() for x in row.keys()]) ==
                    TEST_MYSQL_SCHEMA_PARAM_NAMES['tables']['constraints']
                )
Esempio n. 11
0
def test_schema_tool():
    if get_current_db_type() != 'mysql':
        pytest.skip("unsupported database")

    schema_tool = SchemaTool()
    defaults = schema_tool.get_defaults()
    assert set(defaults.keys()) == TEST_MYSQL_SCHEMA_PARAM_NAMES['defaults']
    for app_label in schema_tool.app_configs:
        for table in schema_tool.get_app_tables(app_label):
            row = schema_tool.get_table_fields(table).values()[0]
            assert (
                set([x.lower() for x in row.keys()]).issubset(
                    TEST_MYSQL_SCHEMA_PARAM_NAMES['tables']['fields'])
            )
            row = schema_tool.get_table_indices(table).values()[0]
            assert (
                set([x.lower() for x in row.keys()]).issubset(
                    TEST_MYSQL_SCHEMA_PARAM_NAMES['tables']['indices'])
            )
            row = schema_tool.get_table_constraints(table).values()[0]
            assert (
                set([x.lower() for x in row.keys()]).issubset(
                    TEST_MYSQL_SCHEMA_PARAM_NAMES['tables']['constraints'])
            )