Python django.db.migrations.Migration() Examples
The following are 30
code examples of django.db.migrations.Migration().
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.migrations
, or try the search function
.
Example #1
Source File: test_writer.py From django-sqlserver with MIT License | 6 votes |
def test_sorted_imports(self): """ #24155 - Tests ordering of imports. """ migration = type(str("Migration"), (migrations.Migration,), { "operations": [ migrations.AddField("mymodel", "myfield", models.DateTimeField( default=datetime.datetime(2012, 1, 1, 1, 1, tzinfo=utc), )), ] }) writer = MigrationWriter(migration) output = writer.as_string() self.assertIn( "import datetime\n" "from django.db import migrations, models\n" "from django.utils.timezone import utc\n", output )
Example #2
Source File: fake_model.py From caluma with GNU General Public License v3.0 | 6 votes |
def get_fake_model(fields=None, model_base=models.Model, options=None): fields = fields if fields else {} options = options if options else {} """Create fake model to use during unit tests.""" model = define_fake_model(fields, model_base, options) class TestProject: def clone(self, *_args, **_kwargs): return self class TestMigration(migrations.Migration): operations = [HStoreExtension()] with connection.schema_editor() as schema_editor: migration_executor = MigrationExecutor(schema_editor.connection) migration_executor.apply_migration( TestProject(), TestMigration("caluma_extra", "caluma_core") ) schema_editor.create_model(model) return model
Example #3
Source File: test_writer.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_migration_file_header_comments(self): """ Test comments at top of file. """ migration = type("Migration", (migrations.Migration,), { "operations": [] }) dt = datetime.datetime(2015, 7, 31, 4, 40, 0, 0, tzinfo=utc) with mock.patch('django.db.migrations.writer.now', lambda: dt): for include_header in (True, False): with self.subTest(include_header=include_header): writer = MigrationWriter(migration, include_header) output = writer.as_string() self.assertEqual( include_header, output.startswith( "# Generated by Django %s on 2015-07-31 04:40\n\n" % get_version() ) ) if not include_header: # Make sure the output starts with something that's not # a comment or indentation or blank line self.assertRegex(output.splitlines(keepends=True)[0], r"^[^#\s]+")
Example #4
Source File: test_writer.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_custom_operation(self): migration = type("Migration", (migrations.Migration,), { "operations": [ custom_migration_operations.operations.TestOperation(), custom_migration_operations.operations.CreateModel(), migrations.CreateModel("MyModel", (), {}, (models.Model,)), custom_migration_operations.more_operations.TestOperation() ], "dependencies": [] }) writer = MigrationWriter(migration) output = writer.as_string() result = self.safe_exec(output) self.assertIn("custom_migration_operations", result) self.assertNotEqual( result['custom_migration_operations'].operations.TestOperation, result['custom_migration_operations'].more_operations.TestOperation )
Example #5
Source File: test_writer.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_sorted_imports(self): """ #24155 - Tests ordering of imports. """ migration = type("Migration", (migrations.Migration,), { "operations": [ migrations.AddField("mymodel", "myfield", models.DateTimeField( default=datetime.datetime(2012, 1, 1, 1, 1, tzinfo=utc), )), ] }) writer = MigrationWriter(migration) output = writer.as_string() self.assertIn( "import datetime\n" "from django.db import migrations, models\n" "from django.utils.timezone import utc\n", output )
Example #6
Source File: test_writer.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_custom_operation(self): migration = type("Migration", (migrations.Migration,), { "operations": [ custom_migration_operations.operations.TestOperation(), custom_migration_operations.operations.CreateModel(), migrations.CreateModel("MyModel", (), {}, (models.Model,)), custom_migration_operations.more_operations.TestOperation() ], "dependencies": [] }) writer = MigrationWriter(migration) output = writer.as_string() result = self.safe_exec(output) self.assertIn("custom_migration_operations", result) self.assertNotEqual( result['custom_migration_operations'].operations.TestOperation, result['custom_migration_operations'].more_operations.TestOperation )
Example #7
Source File: test_writer.py From django-sqlserver with MIT License | 6 votes |
def test_custom_operation(self): migration = type(str("Migration"), (migrations.Migration,), { "operations": [ custom_migration_operations.operations.TestOperation(), custom_migration_operations.operations.CreateModel(), migrations.CreateModel("MyModel", (), {}, (models.Model,)), custom_migration_operations.more_operations.TestOperation() ], "dependencies": [] }) writer = MigrationWriter(migration) output = writer.as_string() result = self.safe_exec(output) self.assertIn("custom_migration_operations", result) self.assertNotEqual( result['custom_migration_operations'].operations.TestOperation, result['custom_migration_operations'].more_operations.TestOperation )
Example #8
Source File: fake_model.py From django-localized-fields with MIT License | 6 votes |
def get_fake_model(fields=None, model_base=LocalizedModel, meta_options={}): """Creates a fake model to use during unit tests.""" model = define_fake_model(fields, model_base, meta_options) class TestProject: def clone(self, *_args, **_kwargs): return self @property def apps(self): return self class TestMigration(migrations.Migration): operations = [HStoreExtension()] with connection.schema_editor() as schema_editor: migration_executor = MigrationExecutor(schema_editor.connection) migration_executor.apply_migration( TestProject(), TestMigration("eh", "postgres_extra") ) schema_editor.create_model(model) return model
Example #9
Source File: test_writer.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_simple_migration(self): """ Tests serializing a simple migration. """ fields = { 'charfield': models.DateTimeField(default=datetime.datetime.utcnow), 'datetimefield': models.DateTimeField(default=datetime.datetime.utcnow), } options = { 'verbose_name': 'My model', 'verbose_name_plural': 'My models', } migration = type("Migration", (migrations.Migration,), { "operations": [ migrations.CreateModel("MyModel", tuple(fields.items()), options, (models.Model,)), migrations.CreateModel("MyModel2", tuple(fields.items()), bases=(models.Model,)), migrations.CreateModel( name="MyModel3", fields=tuple(fields.items()), options=options, bases=(models.Model,) ), migrations.DeleteModel("MyModel"), migrations.AddField("OtherModel", "datetimefield", fields["datetimefield"]), ], "dependencies": [("testapp", "some_other_one")], }) writer = MigrationWriter(migration) output = writer.as_string() # We don't test the output formatting - that's too fragile. # Just make sure it runs for now, and that things look alright. result = self.safe_exec(output) self.assertIn("Migration", result)
Example #10
Source File: test_linter.py From django-migration-linter with Apache License 2.0 | 5 votes |
def test_exclude_migration_tests(self): m = Migration("0002_add_new_not_null_field", "app_add_not_null_column") linter = MigrationLinter(exclude_migration_tests=[], database="mysql") linter.lint_migration(m) self.assertTrue(linter.has_errors) linter = MigrationLinter(exclude_migration_tests=["NOT_NULL"], database="mysql") linter.lint_migration(m) self.assertFalse(linter.has_errors)
Example #11
Source File: migrations.py From django-postgres-extra with MIT License | 5 votes |
def apply_migration(operations, state=None, backwards: bool = False): """Executes the specified migration operations using the specified schema editor. Arguments: operations: The migration operations to execute. state: The state state to use during the migrations. backwards: Whether to apply the operations in reverse (backwards). """ state = state or migrations.state.ProjectState.from_apps(apps) class Migration(migrations.Migration): pass Migration.operations = operations migration = Migration("migration", "tests") executor = MigrationExecutor(connection) if not backwards: executor.apply_migration(state, migration) else: executor.unapply_migration(state, migration) return migration
Example #12
Source File: test_migration.py From django-tsvector-field with BSD 3-Clause "New" or "Revised" License | 5 votes |
def migrate(self, ops, state=None): class Migration(migrations.Migration): operations = ops migration = Migration('name', 'tests') inject_trigger_operations([(migration, False)]) with connection.schema_editor() as schema_editor: return migration.apply(state or ProjectState.from_apps(self.apps), schema_editor)
Example #13
Source File: test_writer.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_models_import_omitted(self): """ django.db.models shouldn't be imported if unused. """ migration = type("Migration", (migrations.Migration,), { "operations": [ migrations.AlterModelOptions( name='model', options={'verbose_name': 'model', 'verbose_name_plural': 'models'}, ), ] }) writer = MigrationWriter(migration) output = writer.as_string() self.assertIn("from django.db import migrations\n", output)
Example #14
Source File: plan.py From django-test-migrations with MIT License | 5 votes |
def _filter_predicate(target: MigrationTarget, migration: Migration) -> bool: # when ``None`` passed as migration name then initial migration from # target's app will be chosen and handled properly in ``_get_index`` # so in final all target app migrations will be excluded from plan index = 2 - (target[1] is None) return (migration.app_label, migration.name)[:index] == target[:index]
Example #15
Source File: 0002_update_aliases.py From dissemin with GNU Affero General Public License v3.0 | 5 votes |
def backwards(apps, schema_editor): """ Migration nullified after squash of papers """ pass
Example #16
Source File: test_writer.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_migration_path(self): test_apps = [ 'migrations.migrations_test_apps.normal', 'migrations.migrations_test_apps.with_package_model', 'migrations.migrations_test_apps.without_init_file', ] base_dir = os.path.dirname(os.path.dirname(__file__)) for app in test_apps: with self.modify_settings(INSTALLED_APPS={'append': app}): migration = migrations.Migration('0001_initial', app.split('.')[-1]) expected_path = os.path.join(base_dir, *(app.split('.') + ['migrations', '0001_initial.py'])) writer = MigrationWriter(migration) self.assertEqual(writer.path, expected_path)
Example #17
Source File: test_writer.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_simple_migration(self): """ Tests serializing a simple migration. """ fields = { 'charfield': models.DateTimeField(default=datetime.datetime.utcnow), 'datetimefield': models.DateTimeField(default=datetime.datetime.utcnow), } options = { 'verbose_name': 'My model', 'verbose_name_plural': 'My models', } migration = type("Migration", (migrations.Migration,), { "operations": [ migrations.CreateModel("MyModel", tuple(fields.items()), options, (models.Model,)), migrations.CreateModel("MyModel2", tuple(fields.items()), bases=(models.Model,)), migrations.CreateModel( name="MyModel3", fields=tuple(fields.items()), options=options, bases=(models.Model,) ), migrations.DeleteModel("MyModel"), migrations.AddField("OtherModel", "datetimefield", fields["datetimefield"]), ], "dependencies": [("testapp", "some_other_one")], }) writer = MigrationWriter(migration) output = writer.as_string() # We don't test the output formatting - that's too fragile. # Just make sure it runs for now, and that things look alright. result = self.safe_exec(output) self.assertIn("Migration", result)
Example #18
Source File: test_writer.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_models_import_omitted(self): """ django.db.models shouldn't be imported if unused. """ migration = type("Migration", (migrations.Migration,), { "operations": [ migrations.AlterModelOptions( name='model', options={'verbose_name': 'model', 'verbose_name_plural': 'models'}, ), ] }) writer = MigrationWriter(migration) output = writer.as_string() self.assertIn("from django.db import migrations\n", output)
Example #19
Source File: 0074_fix_duplicate_attachments.py From zulip with Apache License 2.0 | 5 votes |
def fix_duplicate_attachments(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None: """Migration 0041 had a bug, where if multiple messages referenced the same attachment, rather than creating a single attachment object for all of them, we would incorrectly create one for each message. This results in exceptions looking up the Attachment object corresponding to a file that was used in multiple messages that predate migration 0041. This migration fixes this by removing the duplicates, moving their messages onto a single canonical Attachment object (per path_id). """ Attachment = apps.get_model('zerver', 'Attachment') # Loop through all groups of Attachment objects with the same `path_id` for group in Attachment.objects.values('path_id').annotate(Count('id')).order_by().filter(id__count__gt=1): # Sort by the minimum message ID, to find the first attachment attachments = sorted(list(Attachment.objects.filter(path_id=group['path_id']).order_by("id")), key = lambda x: min(x.messages.all().values_list('id')[0])) surviving = attachments[0] to_cleanup = attachments[1:] for a in to_cleanup: # For each duplicate attachment, we transfer its messages # to the canonical attachment object for that path, and # then delete the original attachment. for msg in a.messages.all(): surviving.messages.add(msg) surviving.is_realm_public = surviving.is_realm_public or a.is_realm_public surviving.save() a.delete()
Example #20
Source File: 0004__v3_0__adding_rrules.py From django-radio with GNU General Public License v3.0 | 5 votes |
def migrate_daily_recurrences(apps, schema_editor): """ Migration to convert weekly recurrences into new complex recurrences """ Schedule = apps.get_model("schedules", "Schedule") for schedule in Schedule.objects.all(): schedule.recurrences = recurrence.Recurrence(rrules=[recurrence.Rule(recurrence.WEEKLY, byday=(int(schedule.day), ))]) schedule.save()
Example #21
Source File: test_writer.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_migration_path(self): test_apps = [ 'migrations.migrations_test_apps.normal', 'migrations.migrations_test_apps.with_package_model', 'migrations.migrations_test_apps.without_init_file', ] base_dir = os.path.dirname(os.path.dirname(__file__)) for app in test_apps: with self.modify_settings(INSTALLED_APPS={'append': app}): migration = migrations.Migration('0001_initial', app.split('.')[-1]) expected_path = os.path.join(base_dir, *(app.split('.') + ['migrations', '0001_initial.py'])) writer = MigrationWriter(migration) self.assertEqual(writer.path, expected_path)
Example #22
Source File: 0032_recipe_type_link_tables.py From scale with Apache License 2.0 | 5 votes |
def populate_recipe_type_link_tables(apps, schema_editor): # Go through all of the recipe type models and create links for their sub recipes and job types JobType = apps.get_model('job', 'JobType') RecipeType = apps.get_model('recipe', 'RecipeType') total_count = RecipeType.objects.all().count() if not total_count: return print('\nCreating new recipe link table rows: %i' % total_count) recipe_types = RecipeType.objects.all() done_count = 0 fail_count = 0 for rt in recipe_types: try: create_recipe_type_job_links_from_definition(apps, rt) create_recipe_type_sub_links_from_definition(apps, rt) except (JobType.DoesNotExist, RecipeType.DoesNotExist) as ex: fail_count += 1 print ('Failed creating links for recipe type %i: %s' % (rt.id, ex)) done_count += 1 percent = (float(done_count) / float(total_count)) * 100.00 print('Progress: %i/%i (%.2f%%)' % (done_count, total_count, percent)) print ('Migration finished. Failed: %i' % fail_count)
Example #23
Source File: 0023_add_price.py From micromasters with BSD 3-Clause "New" or "Revised" License | 5 votes |
def copy_prices(apps, schema_editor): """ Validate CoursePrice then iterate over CoursePrice and populate Program.price """ Program = apps.get_model('courses', 'Program') CourseRun = apps.get_model('courses', 'CourseRun') program_prices = {} # Note: we are not filtering on live here because all Programs will require prices and liveness won't matter # Validate that there is only one price per program and each run had a price. Else, fail the migration for run in CourseRun.objects.all(): program = run.course.program # There must be one and only one price per course run try: price = run.courseprice_set.get(is_valid=True).price except ObjectDoesNotExist as ex: raise Exception("Migration failed due to a price missing for run {}".format( run.edx_course_key )) from ex if program.id not in program_prices: program_prices[program.id] = price elif program_prices[program.id] != price: raise Exception("One run in program {program} had price {price1} but another had price {price2}".format( program=program, price1=program_prices[program.id], price2=price, )) # Verify that all programs have prices at this point (might be false if a Program doesn't have any runs) for program in Program.objects.all(): if program.id not in program_prices: raise Exception("Program {} does not have a price (probably due to not having any runs)".format(program)) # Now, copy the prices for program_id, price in program_prices.items(): program = Program.objects.get(id=program_id) program.price = price program.save()
Example #24
Source File: test_writer.py From django-sqlserver with MIT License | 5 votes |
def test_models_import_omitted(self): """ django.db.models shouldn't be imported if unused. """ migration = type(str("Migration"), (migrations.Migration,), { "operations": [ migrations.AlterModelOptions( name='model', options={'verbose_name': 'model', 'verbose_name_plural': 'models'}, ), ] }) writer = MigrationWriter(migration) output = writer.as_string() self.assertIn("from django.db import migrations\n", output)
Example #25
Source File: test_linter.py From django-migration-linter with Apache License 2.0 | 5 votes |
def test_has_errors(self): linter = MigrationLinter(database="mysql") self.assertFalse(linter.has_errors) m = Migration("0001_create_table", "app_add_not_null_column") linter.lint_migration(m) self.assertFalse(linter.has_errors) m = Migration("0002_add_new_not_null_field", "app_add_not_null_column") linter.lint_migration(m) self.assertTrue(linter.has_errors) m = Migration("0001_create_table", "app_add_not_null_column") linter.lint_migration(m) self.assertTrue(linter.has_errors)
Example #26
Source File: 0114_introduce_base_playbook_name.py From opencraft with GNU Affero General Public License v3.0 | 5 votes |
def rollback_base_playbook_name(apps, schema_editor): """ On rollback, warn that all custom playbook names will be lost. The defaults, based on the openedx_release, can be recalculated using the function above when the migration is performed. """ print('\nReverting this migration causes data loss...') print('All customized playbook_names set will be lost!') # Migration ###################################################################
Example #27
Source File: test_writer.py From django-sqlserver with MIT License | 5 votes |
def test_migration_path(self): test_apps = [ 'migrations.migrations_test_apps.normal', 'migrations.migrations_test_apps.with_package_model', 'migrations.migrations_test_apps.without_init_file', ] base_dir = os.path.dirname(os.path.dirname(upath(__file__))) for app in test_apps: with self.modify_settings(INSTALLED_APPS={'append': app}): migration = migrations.Migration('0001_initial', app.split('.')[-1]) expected_path = os.path.join(base_dir, *(app.split('.') + ['migrations', '0001_initial.py'])) writer = MigrationWriter(migration) self.assertEqual(writer.path, expected_path)
Example #28
Source File: test_operations.py From django-mysql with BSD 3-Clause "New" or "Revised" License | 5 votes |
def apply_operations(self, app_label, project_state, operations): migration = migrations.Migration("name", app_label) migration.operations = operations with connection.schema_editor() as editor: return migration.apply(project_state, editor)
Example #29
Source File: 0017_populate_data_type_tags.py From scale with Apache License 2.0 | 5 votes |
def populate_data_type_tags(apps, schema_editor): # Go through all of the Ingest models and convert the data_type string into an array of tags update = 'UPDATE ingest SET data_type_tags = string_to_array(data_type,\',\') WHERE data_type <> \'\'' with connection.cursor() as cursor: cursor.execute(update) count = cursor.rowcount if count: print('%d entries updated with data type tags' % count) print ('Migration finished.')
Example #30
Source File: 0016_populate_data_type_tags.py From scale with Apache License 2.0 | 5 votes |
def populate_data_type_tags(apps, schema_editor): # Go through all of the ScaleFile models and convert the data_type string into an array of tags update = 'UPDATE scale_file SET data_type_tags = string_to_array(data_type,\',\') WHERE data_type <> \'\'' with connection.cursor() as cursor: cursor.execute(update) count = cursor.rowcount if count: print('%d entries updated with data type tags' % count) print ('Migration finished.')