Exemple #1
0
class SchemaTableCommand(SchemaCommand):
    def add_arguments(self, parser):
        parser.add_argument(
            'args',
            metavar='table_name',
            nargs='+',
            help='Table names.'
        )
        super(SchemaTableCommand, self).add_arguments(parser)

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

        all_tables = self.schema_tool.get_tables()
        if not set(args).issubset(set(all_tables)):
            raise CommandError("Unrecognized tables: %s" %
                               list(set(args) - set(all_tables)))

        result = SchemaDump()
        for table_name in args:
            app_label = self.schema_tool.get_app_by_table(table_name)
            if not result.app_exists(app_label):
                app_result = SchemaAppDump(app_label)
                result.add_app(app_result)
            else:
                app_result = result.get_app(app_label)
            app_result.add_table(
                self.handle_table(table_name, **options))

        self.stdout.write(str(result))
Exemple #2
0
    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))
Exemple #3
0
class SchemaTableCommand(SchemaCommand):
    def add_arguments(self, parser):
        parser.add_argument('args',
                            metavar='table_name',
                            nargs='+',
                            help='Table names.')
        super(SchemaTableCommand, self).add_arguments(parser)

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

        all_tables = self.schema_tool.get_tables()
        if not set(args).issubset(set(all_tables)):
            raise CommandError("Unrecognized tables: %s" %
                               list(set(args) - set(all_tables)))

        result = SchemaDump()
        for table_name in args:
            app_label = self.schema_tool.get_app_by_table(table_name)
            if not result.app_exists(app_label):
                app_result = SchemaAppDump(app_label)
                result.add_app(app_result)
            else:
                app_result = result.get_app(app_label)
            app_result.add_table(self.handle_table(table_name, **options))

        self.stdout.write(str(result))
Exemple #4
0
    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))
Exemple #5
0
def test_schema_tool_supported_database():
    if get_current_db_type() != 'mysql':
        with pytest.raises(UnsupportedDBError):
            SchemaTool()
        return

    assert SchemaTool()
Exemple #6
0
 def handle(self, **options):
     schema_tool = SchemaTool()
     if options['tables']:
         self.stdout.write(jsonify(schema_tool.get_tables()))
     else:
         self.stdout.write(
             jsonify(schema_tool.get_defaults())
         )
Exemple #7
0
def test_schema_table(capfd):
    call_command('schema', 'table', 'pootle_store_store')
    out, err = capfd.readouterr()
    schema_tool = SchemaTool()
    assert jsonify(schema_tool.get_table_fields('pootle_store_store')) in out
    assert jsonify(schema_tool.get_table_indices('pootle_store_store')) in out
    assert jsonify(
        schema_tool.get_table_constraints('pootle_store_store')) in out
Exemple #8
0
def test_schema_tables(capfd):
    if get_current_db_type() != 'mysql':
        pytest.skip("unsupported database")

    call_command('schema', '--tables')
    out, err = capfd.readouterr()
    schema_tool = SchemaTool()
    result = SchemaDump()
    result.load(json.loads(out))
    assert schema_tool.get_tables() == result.tables
Exemple #9
0
def test_schema_table(capfd):
    if get_current_db_type() != 'mysql':
        pytest.skip("unsupported database")

    call_command('schema', 'table', 'pootle_store_store')
    out, err = capfd.readouterr()
    schema_tool = SchemaTool()
    result = SchemaDump()
    result.load(json.loads(out))
    app_label = schema_tool.get_app_by_table('pootle_store_store')
    table_result = result.apps[app_label].tables['pootle_store_store']
    _test_table_result(schema_tool, table_result, 'pootle_store_store')
Exemple #10
0
def test_schema_app(capfd):
    if get_current_db_type() != 'mysql':
        pytest.skip("unsupported database")

    call_command('schema', 'app', 'pootle_store')
    out, err = capfd.readouterr()
    result = SchemaDump()
    result.load(json.loads(out))
    schema_tool = SchemaTool('pootle_store')
    for table_name in schema_tool.get_tables():
        table_result = result.apps['pootle_store'].tables[table_name]
        _test_table_result(schema_tool, table_result, table_name)
Exemple #11
0
    def handle(self, *args, **options):
        self.schema_tool = SchemaTool()
        all_tables = self.schema_tool.get_tables()
        if not set(args).issubset(set(all_tables)):
            raise CommandError("Unrecognized tables: %s" %
                               list(set(args) - set(all_tables)))

        result = {}
        for table_name in args:
            result.update(
                self.handle_table(table_name, **options)
            )

        self.stdout.write(jsonify(result))
Exemple #12
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))
Exemple #13
0
    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))
Exemple #14
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))
Exemple #15
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))
Exemple #16
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))
Exemple #17
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))
Exemple #18
0
    def handle(self, *args, **options):
        try:
            self.schema_tool = SchemaTool()
        except UnsupportedDBError as e:
            raise CommandError(e)

        all_tables = self.schema_tool.get_tables()
        if not set(args).issubset(set(all_tables)):
            raise CommandError("Unrecognized tables: %s" %
                               list(set(args) - set(all_tables)))

        result = SchemaDump()
        for table_name in args:
            app_label = self.schema_tool.get_app_by_table(table_name)
            if not result.app_exists(app_label):
                app_result = SchemaAppDump(app_label)
                result.add_app(app_result)
            else:
                app_result = result.get_app(app_label)
            app_result.add_table(self.handle_table(table_name, **options))

        self.stdout.write(str(result))
Exemple #19
0
def test_schema_app(capfd):
    call_command('schema', 'app', 'pootle_store')
    out, err = capfd.readouterr()
    schema_tool = SchemaTool('pootle_store')
    for table_name in schema_tool.get_tables():
        assert jsonify(schema_tool.get_table_fields(table_name)) in out
        assert jsonify(schema_tool.get_table_indices(table_name)) in out
        assert jsonify(schema_tool.get_table_constraints(table_name)) in out
Exemple #20
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']))
Exemple #21
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)
Exemple #22
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'])
Exemple #23
0
    def handle(self, *args, **options):
        try:
            self.schema_tool = SchemaTool()
        except UnsupportedDBError as e:
            raise CommandError(e)

        all_tables = self.schema_tool.get_tables()
        if not set(args).issubset(set(all_tables)):
            raise CommandError("Unrecognized tables: %s" %
                               list(set(args) - set(all_tables)))

        result = SchemaDump()
        for table_name in args:
            app_label = self.schema_tool.get_app_by_table(table_name)
            if not result.app_exists(app_label):
                app_result = SchemaAppDump(app_label)
                result.add_app(app_result)
            else:
                app_result = result.get_app(app_label)
            app_result.add_table(
                self.handle_table(table_name, **options))

        self.stdout.write(str(result))
Exemple #24
0
class SchemaTableCommand(SchemaCommand):
    def add_arguments(self, parser):
        parser.add_argument(
            'args',
            metavar='table_name',
            nargs='+',
            help='Table names.'
        )
        super(SchemaTableCommand, self).add_arguments(parser)

    def handle(self, *args, **options):
        self.schema_tool = SchemaTool()
        all_tables = self.schema_tool.get_tables()
        if not set(args).issubset(set(all_tables)):
            raise CommandError("Unrecognized tables: %s" %
                               list(set(args) - set(all_tables)))

        result = {}
        for table_name in args:
            result.update(
                self.handle_table(table_name, **options)
            )

        self.stdout.write(jsonify(result))
Exemple #25
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)
Exemple #26
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']
                )
Exemple #27
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'])
            )
Exemple #28
0
def test_schema(capfd):
    call_command('schema')
    out, err = capfd.readouterr()
    schema_tool = SchemaTool()
    assert jsonify(schema_tool.get_defaults()) in out
Exemple #29
0
def test_schema_tables(capfd):
    call_command('schema', '--tables')
    out, err = capfd.readouterr()
    schema_tool = SchemaTool()
    assert jsonify(schema_tool.get_tables()) in out
Exemple #30
0
def test_schema_app_tables(capfd):
    call_command('schema', 'app', 'pootle_store', '--tables')
    out, err = capfd.readouterr()
    schema_tool = SchemaTool('pootle_store')
    assert jsonify(schema_tool.get_tables()) in out
Exemple #31
0
 def handle(self, **options):
     schema_tool = SchemaTool()
     if options['tables']:
         self.stdout.write(jsonify(schema_tool.get_tables()))
     else:
         self.stdout.write(jsonify(schema_tool.get_defaults()))