forked from ansible/awx
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow pytest --migrations to succeed (ansible#14663)
* allow pytest --migrations to succeed * We actually subvert migrations from running in test via pytest.ini --no-migrations option. This has led to bit rot for the sqlite migrations happy path. This changeset pays off that tech debt and allows for an sqlite migration happy path. * This paves the way for programatic invocation of individual migrations and weaving of the creation of resources (i.e. Instance, Job Template, etc). With this, a developer can instantiate various database states, trigger a migration, assert the state of the db, and then have pytest rollback all of that. * I will note that in practice, running these migrations is dog shit slow BUT this work also opens up the possibility of saving and re-using sqlite3 database files. Normally, caching is not THE answer and causes more harm than good. But in this case, our migrations are mostly write-once (I say mostly because this change set violates that :) so cache invalidation isn't a major issue. * functional test for migrations on sqlite * We commonly subvert running migrations in test land. Test land uses sqlite. By not constantly exercising this code path it atrophies. The smoke test here is to continuously exercise that code path. * Add ci test to run migration tests separately, they take =~ 2-3 minutes each on my laptop. * The smoke tests also serves as an example of how to write migration tests. * run migration tests in ci
- Loading branch information
1 parent
3e5851f
commit 2ac304d
Showing
11 changed files
with
177 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from django.db import migrations | ||
|
||
|
||
class RunSQL(migrations.operations.special.RunSQL): | ||
""" | ||
Bit of a hack here. Django actually wants this decision made in the router | ||
and we can pass **hints. | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
if 'sqlite_sql' not in kwargs: | ||
raise ValueError("sqlite_sql parameter required") | ||
sqlite_sql = kwargs.pop('sqlite_sql') | ||
|
||
self.sqlite_sql = sqlite_sql | ||
self.sqlite_reverse_sql = kwargs.pop('sqlite_reverse_sql', None) | ||
super().__init__(*args, **kwargs) | ||
|
||
def database_forwards(self, app_label, schema_editor, from_state, to_state): | ||
if not schema_editor.connection.vendor.startswith('postgres'): | ||
self.sql = self.sqlite_sql or migrations.RunSQL.noop | ||
super().database_forwards(app_label, schema_editor, from_state, to_state) | ||
|
||
def database_backwards(self, app_label, schema_editor, from_state, to_state): | ||
if not schema_editor.connection.vendor.startswith('postgres'): | ||
self.reverse_sql = self.sqlite_reverse_sql or migrations.RunSQL.noop | ||
super().database_backwards(app_label, schema_editor, from_state, to_state) | ||
|
||
|
||
class RunPython(migrations.operations.special.RunPython): | ||
""" | ||
Bit of a hack here. Django actually wants this decision made in the router | ||
and we can pass **hints. | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
if 'sqlite_code' not in kwargs: | ||
raise ValueError("sqlite_code parameter required") | ||
sqlite_code = kwargs.pop('sqlite_code') | ||
|
||
self.sqlite_code = sqlite_code | ||
self.sqlite_reverse_code = kwargs.pop('sqlite_reverse_code', None) | ||
super().__init__(*args, **kwargs) | ||
|
||
def database_forwards(self, app_label, schema_editor, from_state, to_state): | ||
if not schema_editor.connection.vendor.startswith('postgres'): | ||
self.code = self.sqlite_code or migrations.RunPython.noop | ||
super().database_forwards(app_label, schema_editor, from_state, to_state) | ||
|
||
def database_backwards(self, app_label, schema_editor, from_state, to_state): | ||
if not schema_editor.connection.vendor.startswith('postgres'): | ||
self.reverse_code = self.sqlite_reverse_code or migrations.RunPython.noop | ||
super().database_backwards(app_label, schema_editor, from_state, to_state) | ||
|
||
|
||
class _sqlitemigrations: | ||
RunPython = RunPython | ||
RunSQL = RunSQL | ||
|
||
|
||
dbawaremigrations = _sqlitemigrations() |
Oops, something went wrong.