-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* introduce project settings in case you're interested have a read here[^1] - [^1] https://wiki.godesteem.de/wiki/configure-settings-for-a-django-package/ * only set default config for django < 3.2 * restructure file test case and share some code * use new setting in tests * add new command to allow replacing graphs * bump django version for dev * extend readme * drop unused/untested code * build more test coverage
- Loading branch information
1 parent
c96b039
commit 3ff351c
Showing
15 changed files
with
291 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
default_apps_config = 'data_migration.apps.DataMigrationsConfig' | ||
import django | ||
|
||
|
||
if django.VERSION < (3, 2): | ||
default_app_config = 'data_migration.apps.DataMigrationsConfig' |
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,12 @@ | ||
from django.conf import settings | ||
from django.apps import AppConfig | ||
|
||
from data_migration.settings import internal_settings | ||
|
||
|
||
class DataMigrationsConfig(AppConfig): | ||
name = 'data_migration' | ||
verbose_name = 'Django data migrations' | ||
|
||
def ready(self): | ||
internal_settings.update(settings) |
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,31 @@ | ||
from django.core.management import BaseCommand | ||
|
||
from data_migration.services.squasher import MigrationSquash | ||
from data_migration.settings import internal_settings as data_migration_settings | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Extended migrate command. | ||
Allows forward and backward migration of data/regular migrations | ||
""" | ||
|
||
def add_arguments(self, parser): # noqa D102 | ||
parser.add_argument( | ||
'--app_labels', nargs='?', dest='app_labels', | ||
help='App label of an application to synchronize the state.', | ||
) | ||
parser.add_argument( | ||
'--all', '-a', action='store_true', dest='squash_all', | ||
help='Squash all apps.', | ||
) | ||
super().add_arguments(parser) | ||
|
||
def handle(self, *args, **options): # noqa D102 | ||
# extract parameters | ||
apps_to_squash = options.get('app_labels') | ||
if apps_to_squash is None and options['squash_all']: | ||
apps_to_squash = data_migration_settings.SQUASHABLE_APPS | ||
|
||
MigrationSquash(apps_to_squash).squash() |
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,44 @@ | ||
from typing import Dict, Optional | ||
|
||
from django.core.signals import setting_changed | ||
|
||
DATA_MIGRATION_DEFAULTS = { | ||
"SQUASHABLE_APPS": [] | ||
} | ||
|
||
|
||
class DataMigrationSettings(object): | ||
def __init__(self, user_settings=None, defaults: Optional[Dict] = None): | ||
if defaults is None: | ||
defaults = DATA_MIGRATION_DEFAULTS | ||
|
||
self.settings = defaults.copy() | ||
if user_settings: | ||
self.update(user_settings) | ||
|
||
def update(self, settings): | ||
try: | ||
self.settings.update(getattr(settings, 'DATA_MIGRATION')) | ||
except AttributeError: | ||
self.settings.update(settings.get('DATA_MIGRATION', {})) | ||
|
||
def reload(self, settings): | ||
try: | ||
_user_settings = getattr(settings, 'DATA_MIGRATION') | ||
self.settings = _user_settings | ||
except AttributeError: | ||
pass | ||
|
||
def __getattr__(self, item): | ||
return self.settings[item] | ||
|
||
|
||
internal_settings = DataMigrationSettings(None) | ||
|
||
|
||
def reload(sender, setting, value, *args, **kwargs): | ||
if setting == 'DATA_MIGRATION': | ||
internal_settings.update(value) | ||
|
||
|
||
setting_changed.connect(reload) |
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,35 @@ | ||
import os | ||
from unittest import TestCase, mock | ||
|
||
from data_migration.settings import internal_settings | ||
from tests.unittests.test_app_2.helper import ResetDirectory2Context | ||
from tests.utils import FileTestCase | ||
|
||
from django.core.management import call_command | ||
|
||
this_dir = os.path.dirname(__file__) | ||
|
||
|
||
class DataMigrateCommandTestCase(FileTestCase): | ||
internal_target = os.path.join(this_dir, '../test_app_2/') | ||
needs_cleanup = False | ||
|
||
def test_explicit_app(self): | ||
with ResetDirectory2Context(): | ||
call_command('data_migrate', app_labels=['test_app_2']) | ||
prev_target = self.target | ||
self.target = os.path.join(prev_target, 'data_migrations/') | ||
self.assertTrue(self.has_file('0001_0002_split_name.py')) | ||
self.target = os.path.join(prev_target, 'migrations/') | ||
self.assertTrue(self.has_file('0001_squashed_0008.py')) | ||
|
||
def test_all_apps(self): | ||
self.assertIsNotNone(internal_settings.SQUASHABLE_APPS) | ||
with ResetDirectory2Context(): | ||
call_command('data_migrate', squash_all=True) | ||
prev_target = self.target | ||
self.target = os.path.join(prev_target, 'data_migrations/') | ||
self.assertTrue(self.has_file('0001_0002_split_name.py')) | ||
self.assertTrue(self.has_file('0002_0006_address_line_split.py')) | ||
self.target = os.path.join(prev_target, 'migrations/') | ||
self.assertTrue(self.has_file('0001_squashed_0008.py')) |
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 |
---|---|---|
@@ -1,27 +1,11 @@ | ||
import os.path | ||
|
||
from tests.utils import ResetDirectoryMixin | ||
|
||
this_dir = os.path.dirname(__file__) | ||
|
||
|
||
class ResetDirectoryContext: | ||
class ResetDirectoryContext(ResetDirectoryMixin): | ||
targets = ['migrations', 'data_migrations'] | ||
protected_files = ['__init__.py', '0001_first.py', '0002_add_name.py'] | ||
|
||
def __enter__(self): | ||
return True | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
for target in self.targets: | ||
dir_path = os.path.join(this_dir, target) | ||
try: | ||
files = [ | ||
os.path.join(dir_path, f) | ||
for f in os.listdir(dir_path) | ||
if f not in self.protected_files | ||
and os.path.isfile(os.path.join(dir_path, f)) | ||
] | ||
except FileNotFoundError: | ||
continue | ||
for file in files: | ||
os.remove(file) | ||
this_dir = this_dir |
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
Oops, something went wrong.