-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
* 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.
- Loading branch information
1 parent
873b1fb
commit 7517411
Showing
7 changed files
with
126 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import sys | ||
|
||
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_forwards(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_forwards(app_label, schema_editor, from_state, to_state) | ||
|
||
|
||
class _sqlitemigrations: | ||
RunPython = RunPython | ||
RunSQL = RunSQL | ||
|
||
|
||
sqlitemigrations = _sqlitemigrations() |