Python django.db.backends.base.schema.BaseDatabaseSchemaEditor() Examples

The following are 7 code examples of django.db.backends.base.schema.BaseDatabaseSchemaEditor(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.db.backends.base.schema , or try the search function .
Example #1
Source File: test_commands.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_migrate_syncdb_deferred_sql_executed_with_schemaeditor(self):
        """
        For an app without migrations, editor.execute() is used for executing
        the syncdb deferred SQL.
        """
        stdout = io.StringIO()
        with mock.patch.object(BaseDatabaseSchemaEditor, 'execute') as execute:
            call_command('migrate', run_syncdb=True, verbosity=1, stdout=stdout, no_color=True)
            create_table_count = len([call for call in execute.mock_calls if 'CREATE TABLE' in str(call)])
            self.assertEqual(create_table_count, 2)
            # There's at least one deferred SQL for creating the foreign key
            # index.
            self.assertGreater(len(execute.mock_calls), 2)
        stdout = stdout.getvalue()
        self.assertIn('Synchronize unmigrated apps: unmigrated_app_syncdb', stdout)
        self.assertIn('Creating tables...', stdout)
        table_name = truncate_name('unmigrated_app_syncdb_classroom', connection.ops.max_name_length())
        self.assertIn('Creating table %s' % table_name, stdout) 
Example #2
Source File: test_process_with_django.py    From eventsourcing with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setup_projections_table(self, process):
        from django.db import connections

        with connections["default"].schema_editor() as schema_editor:
            assert isinstance(schema_editor, BaseDatabaseSchemaEditor)
            try:
                schema_editor.delete_model(self.projection_record_class)
            except django.db.utils.ProgrammingError:
                pass

        with connections["default"].schema_editor() as schema_editor:
            assert isinstance(schema_editor, BaseDatabaseSchemaEditor)
            schema_editor.create_model(self.projection_record_class) 
Example #3
Source File: migrations.py    From hutils with MIT License 5 votes vote down vote up
def database_forwards(self, app_label, schema_editor: BaseDatabaseSchemaEditor, from_state, to_state: ProjectState):
        to_model = to_state.apps.get_model(app_label, self.model_name)
        meta = to_model._meta
        to_field = meta.get_field(self.name)
        if to_field.default != NOT_PROVIDED:
            table_name = schema_editor.quote_name(meta.db_table)
            column = schema_editor.quote_name(to_field.column)
            default = schema_editor.quote_value(to_field.default)
            schema_editor.execute("ALTER TABLE {} ALTER COLUMN {} SET DEFAULT {}".format(table_name, column, default)) 
Example #4
Source File: migrations.py    From hutils with MIT License 5 votes vote down vote up
def database_backwards(self, app_label, schema_editor: BaseDatabaseSchemaEditor, from_state, to_state):
        self.database_forwards(app_label, schema_editor, from_state, to_state) 
Example #5
Source File: test_commands.py    From django-sqlserver with MIT License 5 votes vote down vote up
def test_migrate_syncdb_deferred_sql_executed_with_schemaeditor(self):
        """
        For an app without migrations, editor.execute() is used for executing
        the syncdb deferred SQL.
        """
        with mock.patch.object(BaseDatabaseSchemaEditor, 'execute') as execute:
            call_command('migrate', run_syncdb=True, verbosity=0)
            create_table_count = len([call for call in execute.mock_calls if 'CREATE TABLE' in str(call)])
            self.assertEqual(create_table_count, 2)
            # There's at least one deferred SQL for creating the foreign key
            # index.
            self.assertGreater(len(execute.mock_calls), 2) 
Example #6
Source File: test_commands.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_migrate_syncdb_deferred_sql_executed_with_schemaeditor(self):
        """
        For an app without migrations, editor.execute() is used for executing
        the syncdb deferred SQL.
        """
        with mock.patch.object(BaseDatabaseSchemaEditor, 'execute') as execute:
            call_command('migrate', run_syncdb=True, verbosity=0)
            create_table_count = len([call for call in execute.mock_calls if 'CREATE TABLE' in str(call)])
            self.assertEqual(create_table_count, 2)
            # There's at least one deferred SQL for creating the foreign key
            # index.
            self.assertGreater(len(execute.mock_calls), 2) 
Example #7
Source File: test_commands.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_migrate_syncdb_app_label(self):
        """
        Running migrate --run-syncdb with an app_label only creates tables for
        the specified app.
        """
        stdout = io.StringIO()
        with mock.patch.object(BaseDatabaseSchemaEditor, 'execute') as execute:
            call_command('migrate', 'unmigrated_app_syncdb', run_syncdb=True, stdout=stdout)
            create_table_count = len([call for call in execute.mock_calls if 'CREATE TABLE' in str(call)])
            self.assertEqual(create_table_count, 2)
            self.assertGreater(len(execute.mock_calls), 2)
            self.assertIn('Synchronize unmigrated app: unmigrated_app_syncdb', stdout.getvalue())