django-migration-zero 2.3.11__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- django_migration_zero/__init__.py +6 -0
- django_migration_zero/admin.py +18 -0
- django_migration_zero/apps.py +8 -0
- django_migration_zero/exceptions.py +10 -0
- django_migration_zero/helpers/__init__.py +0 -0
- django_migration_zero/helpers/file_system.py +83 -0
- django_migration_zero/helpers/logger.py +8 -0
- django_migration_zero/locale/de/LC_MESSAGES/django.po +45 -0
- django_migration_zero/management/__init__.py +0 -0
- django_migration_zero/management/commands/__init__.py +0 -0
- django_migration_zero/management/commands/handle_migration_zero_reset.py +11 -0
- django_migration_zero/management/commands/reset_local_migration_files.py +28 -0
- django_migration_zero/managers.py +26 -0
- django_migration_zero/migrations/0001_initial.py +32 -0
- django_migration_zero/migrations/__init__.py +0 -0
- django_migration_zero/models.py +40 -0
- django_migration_zero/services/__init__.py +0 -0
- django_migration_zero/services/deployment.py +62 -0
- django_migration_zero/services/local.py +49 -0
- django_migration_zero-2.3.11.dist-info/METADATA +195 -0
- django_migration_zero-2.3.11.dist-info/RECORD +23 -0
- django_migration_zero-2.3.11.dist-info/WHEEL +4 -0
- django_migration_zero-2.3.11.dist-info/licenses/LICENSE.md +21 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from django.contrib import admin
|
|
2
|
+
|
|
3
|
+
from django_migration_zero.models import MigrationZeroConfiguration
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@admin.register(MigrationZeroConfiguration)
|
|
7
|
+
class MigrationZeroAdmin(admin.ModelAdmin):
|
|
8
|
+
list_display = (
|
|
9
|
+
"__str__",
|
|
10
|
+
"migration_imminent",
|
|
11
|
+
"migration_date",
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
def has_add_permission(self, request):
|
|
15
|
+
return False
|
|
16
|
+
|
|
17
|
+
def has_delete_permission(self, request, obj=None):
|
|
18
|
+
return False
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
from django.apps import AppConfig
|
|
2
|
+
from django.utils.translation import gettext_lazy as _
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class MigrationZeroConfig(AppConfig):
|
|
6
|
+
name = "django_migration_zero"
|
|
7
|
+
verbose_name = _("Migration Zero Configuration")
|
|
8
|
+
default_auto_field = "django.db.models.AutoField"
|
|
File without changes
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import re
|
|
3
|
+
from os.path import isdir
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from django.apps import apps
|
|
7
|
+
from django.apps.config import AppConfig
|
|
8
|
+
from django.conf import settings
|
|
9
|
+
|
|
10
|
+
from django_migration_zero.helpers.logger import get_logger
|
|
11
|
+
|
|
12
|
+
logger = get_logger()
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def build_migration_directory_path(*, app_path: Path) -> Path:
|
|
16
|
+
"""
|
|
17
|
+
Get directory to the migration directory of a given local Django app
|
|
18
|
+
"""
|
|
19
|
+
return app_path / "migrations"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def get_local_django_apps() -> list[AppConfig]:
|
|
23
|
+
"""
|
|
24
|
+
Iterate all installed Django apps and detect local ones.
|
|
25
|
+
"""
|
|
26
|
+
local_apps = []
|
|
27
|
+
local_path = str(settings.BASE_DIR).replace("\\", "/")
|
|
28
|
+
logger.info("Getting local Django apps...")
|
|
29
|
+
for app_config in apps.get_app_configs():
|
|
30
|
+
app_path = str(app_config.path).replace("\\", "/")
|
|
31
|
+
if app_path.startswith(local_path) and "site-packages" not in app_path:
|
|
32
|
+
logger.info(f"Local app {app_config.label!r} discovered.")
|
|
33
|
+
local_apps.append(app_config)
|
|
34
|
+
else:
|
|
35
|
+
logger.debug(f"App {app_config.label!r} ignored since it's not local.")
|
|
36
|
+
|
|
37
|
+
return local_apps
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def has_migration_directory(*, app_path: Path) -> bool:
|
|
41
|
+
"""
|
|
42
|
+
Determines if the given Django app has a migrations directory and therefore migrations
|
|
43
|
+
"""
|
|
44
|
+
possible_migration_dir = build_migration_directory_path(app_path=app_path)
|
|
45
|
+
return True if isdir(possible_migration_dir) else False
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def get_migration_files(*, app_label: str, app_path: Path, exclude_initials: bool = False) -> list[str]:
|
|
49
|
+
"""
|
|
50
|
+
Returns a list of all migration files detected in the given Django app.
|
|
51
|
+
"""
|
|
52
|
+
migration_file_list = []
|
|
53
|
+
|
|
54
|
+
logger.info(f"Getting migration files from app {app_label!r}...")
|
|
55
|
+
migration_dir = build_migration_directory_path(app_path=app_path)
|
|
56
|
+
file_pattern = r"^\d{4,}_\w+\.py$"
|
|
57
|
+
for filename in os.listdir(migration_dir):
|
|
58
|
+
if re.match(file_pattern, filename):
|
|
59
|
+
if exclude_initials:
|
|
60
|
+
initial_pattern = r"^\d{4,}_initial.py$"
|
|
61
|
+
if re.match(initial_pattern, filename):
|
|
62
|
+
logger.debug(f"File {filename!r} ignored since it's an initial migration.")
|
|
63
|
+
continue
|
|
64
|
+
|
|
65
|
+
logger.info(f"Migration file {filename!r} detected.")
|
|
66
|
+
migration_file_list.append(filename)
|
|
67
|
+
else:
|
|
68
|
+
logger.debug(f"File {filename!r} ignored since it's not fitting the migration name pattern.")
|
|
69
|
+
|
|
70
|
+
return migration_file_list
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def delete_file(*, filename: str, app_path: Path, dry_run: bool = False) -> None:
|
|
74
|
+
"""
|
|
75
|
+
Physically delete the given file
|
|
76
|
+
"""
|
|
77
|
+
file_path = build_migration_directory_path(app_path=app_path) / filename
|
|
78
|
+
if not dry_run:
|
|
79
|
+
try:
|
|
80
|
+
os.unlink(file_path)
|
|
81
|
+
logger.info(f"Deleted file {filename!r}.")
|
|
82
|
+
except OSError:
|
|
83
|
+
logger.warning(f"Unable to delete file {file_path!r}.")
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# SOME DESCRIPTIVE TITLE.
|
|
2
|
+
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
|
|
3
|
+
# This file is distributed under the same license as the PACKAGE package.
|
|
4
|
+
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
|
|
5
|
+
#
|
|
6
|
+
#, fuzzy
|
|
7
|
+
msgid ""
|
|
8
|
+
msgstr ""
|
|
9
|
+
"Project-Id-Version: PACKAGE VERSION\n"
|
|
10
|
+
"Report-Msgid-Bugs-To: \n"
|
|
11
|
+
"POT-Creation-Date: 2023-10-19 13:51+0200\n"
|
|
12
|
+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
|
13
|
+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
|
14
|
+
"Language-Team: LANGUAGE <LL@li.org>\n"
|
|
15
|
+
"Language: \n"
|
|
16
|
+
"MIME-Version: 1.0\n"
|
|
17
|
+
"Content-Type: text/plain; charset=UTF-8\n"
|
|
18
|
+
"Content-Transfer-Encoding: 8bit\n"
|
|
19
|
+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
|
|
20
|
+
#: .\migration_zero\apps.py:7
|
|
21
|
+
msgid "Migration Zero Configuration"
|
|
22
|
+
msgstr "Migration Zero-Konfiguration"
|
|
23
|
+
|
|
24
|
+
#: .\migration_zero\models.py:11
|
|
25
|
+
msgid "Migration imminent"
|
|
26
|
+
msgstr "Bevorstehende Migration"
|
|
27
|
+
|
|
28
|
+
#: .\migration_zero\models.py:13
|
|
29
|
+
msgid ""
|
|
30
|
+
"Enable this checkbox to prepare the database for a migration zero reset on "
|
|
31
|
+
"the next deployment."
|
|
32
|
+
msgstr "Aktivieren Sie diese Checkbox um für das nächste Deployment die Datenbank für Migration Zero-Datenbankreset "
|
|
33
|
+
"vorzubereiten."
|
|
34
|
+
|
|
35
|
+
#: .\migration_zero\models.py:15
|
|
36
|
+
msgid "Migration date"
|
|
37
|
+
msgstr "Migrationsdatum"
|
|
38
|
+
|
|
39
|
+
#: .\migration_zero\models.py:20
|
|
40
|
+
msgid "Configuration"
|
|
41
|
+
msgstr "Konfiguration"
|
|
42
|
+
|
|
43
|
+
#: .\migration_zero\models.py:21
|
|
44
|
+
msgid "Configurations"
|
|
45
|
+
msgstr "Konfigurationen"
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from django.core.management.base import BaseCommand
|
|
2
|
+
|
|
3
|
+
from django_migration_zero.services.deployment import DatabasePreparationService
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Command(BaseCommand):
|
|
7
|
+
help = "Prepares the database after resetting all migrations."
|
|
8
|
+
|
|
9
|
+
def handle(self, *args, **options):
|
|
10
|
+
service = DatabasePreparationService()
|
|
11
|
+
service.process()
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
from django.conf import settings
|
|
2
|
+
from django.core.management.base import BaseCommand
|
|
3
|
+
|
|
4
|
+
from django_migration_zero.services.local import ResetMigrationFiles
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Command(BaseCommand):
|
|
8
|
+
def add_arguments(self, parser):
|
|
9
|
+
parser.add_argument(
|
|
10
|
+
"--dry-run",
|
|
11
|
+
action="store_true",
|
|
12
|
+
help="Shows affected files without actually deleting them.",
|
|
13
|
+
)
|
|
14
|
+
parser.add_argument(
|
|
15
|
+
"--exclude-initials",
|
|
16
|
+
action="store_true",
|
|
17
|
+
help="Won't delete initial migration files.",
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
def handle(self, *args, **options):
|
|
21
|
+
if not settings.DEBUG:
|
|
22
|
+
print("Don't run this command in production!")
|
|
23
|
+
return
|
|
24
|
+
|
|
25
|
+
service = ResetMigrationFiles(
|
|
26
|
+
dry_run=options.get("dry_run", False), exclude_initials=options.get("exclude_initials", False)
|
|
27
|
+
)
|
|
28
|
+
service.process()
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
|
|
2
|
+
from django.db import ProgrammingError, models
|
|
3
|
+
|
|
4
|
+
from django_migration_zero.exceptions import MissingMigrationZeroConfigRecordError
|
|
5
|
+
from django_migration_zero.helpers.logger import get_logger
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class MigrationZeroConfigurationManager(models.Manager):
|
|
9
|
+
def fetch_singleton(self) -> None:
|
|
10
|
+
logger = get_logger()
|
|
11
|
+
try:
|
|
12
|
+
config_singleton = self.select_for_update().get()
|
|
13
|
+
except ProgrammingError:
|
|
14
|
+
logger.warning(
|
|
15
|
+
"The migration zero table is missing. This might be ok for the first installation of "
|
|
16
|
+
'"django-migration-zero" but if you see this warning after that point, something went sideways.'
|
|
17
|
+
)
|
|
18
|
+
config_singleton = None
|
|
19
|
+
except MultipleObjectsReturned as e:
|
|
20
|
+
raise MissingMigrationZeroConfigRecordError(
|
|
21
|
+
"Too many configuration records detected. There can only be one."
|
|
22
|
+
) from e
|
|
23
|
+
except ObjectDoesNotExist as e:
|
|
24
|
+
raise MissingMigrationZeroConfigRecordError("No configuration record found in the database.") from e
|
|
25
|
+
|
|
26
|
+
return config_singleton
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Generated by Django 4.2.5 on 2023-10-18 07:14
|
|
2
|
+
import datetime
|
|
3
|
+
|
|
4
|
+
from django.db import migrations, models
|
|
5
|
+
from django.db.migrations import RunPython
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Migration(migrations.Migration):
|
|
9
|
+
initial = True
|
|
10
|
+
|
|
11
|
+
dependencies = [
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
def create_initial_record(apps, schema_editor):
|
|
15
|
+
print("Creating configuration record for migration zero.")
|
|
16
|
+
MigrationZeroConfiguration = apps.get_model("django_migration_zero", "MigrationZeroConfiguration")
|
|
17
|
+
MigrationZeroConfiguration.objects.create(migration_imminent=False, migration_date=datetime.date(1970, 1, 1))
|
|
18
|
+
|
|
19
|
+
operations = [
|
|
20
|
+
migrations.CreateModel(
|
|
21
|
+
name='MigrationZeroConfiguration',
|
|
22
|
+
fields=[
|
|
23
|
+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
|
24
|
+
('migration_imminent', models.BooleanField(default=False,
|
|
25
|
+
help_text='Enable this checkbox to prepare the database for a migration zero reset on the next deployment.',
|
|
26
|
+
verbose_name='Migration imminent')),
|
|
27
|
+
('migration_date', models.DateField(blank=True, null=True, verbose_name='Migration date')),
|
|
28
|
+
],
|
|
29
|
+
options={'verbose_name': 'Configuration', 'verbose_name_plural': 'Configurations'},
|
|
30
|
+
),
|
|
31
|
+
RunPython(create_initial_record),
|
|
32
|
+
]
|
|
File without changes
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from django.db import models
|
|
2
|
+
from django.utils import timezone
|
|
3
|
+
from django.utils.translation import gettext_lazy as _
|
|
4
|
+
|
|
5
|
+
from django_migration_zero.helpers.logger import get_logger
|
|
6
|
+
from django_migration_zero.managers import MigrationZeroConfigurationManager
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class MigrationZeroConfiguration(models.Model):
|
|
10
|
+
migration_imminent = models.BooleanField(
|
|
11
|
+
_("Migration imminent"),
|
|
12
|
+
default=False,
|
|
13
|
+
help_text=_("Enable this checkbox to prepare the database for a migration zero reset on the next deployment."),
|
|
14
|
+
)
|
|
15
|
+
migration_date = models.DateField(_("Migration date"), null=True, blank=True)
|
|
16
|
+
|
|
17
|
+
objects = MigrationZeroConfigurationManager()
|
|
18
|
+
|
|
19
|
+
class Meta:
|
|
20
|
+
verbose_name = _("Configuration")
|
|
21
|
+
verbose_name_plural = _("Configurations")
|
|
22
|
+
|
|
23
|
+
def __str__(self):
|
|
24
|
+
return "Configuration"
|
|
25
|
+
|
|
26
|
+
@property
|
|
27
|
+
def is_migration_applicable(self) -> bool:
|
|
28
|
+
"""
|
|
29
|
+
Checks if we are currently preparing for a "migration zero"-deployment
|
|
30
|
+
"""
|
|
31
|
+
logger = get_logger()
|
|
32
|
+
if not self.migration_imminent:
|
|
33
|
+
logger.info("Switch not active. Skipping migration zero process.")
|
|
34
|
+
return False
|
|
35
|
+
|
|
36
|
+
if not self.migration_date == timezone.now().date():
|
|
37
|
+
logger.info("Security date doesn't match today. Skipping migration zero process.")
|
|
38
|
+
return False
|
|
39
|
+
|
|
40
|
+
return True
|
|
File without changes
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
from logging import Logger
|
|
2
|
+
|
|
3
|
+
from django.core.management import call_command
|
|
4
|
+
from django.db import transaction
|
|
5
|
+
from django.db.migrations.recorder import MigrationRecorder
|
|
6
|
+
|
|
7
|
+
from django_migration_zero.exceptions import InvalidMigrationTreeError
|
|
8
|
+
from django_migration_zero.helpers.logger import get_logger
|
|
9
|
+
from django_migration_zero.models import MigrationZeroConfiguration
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class DatabasePreparationService:
|
|
13
|
+
"""
|
|
14
|
+
Service to prepare the database for an upcoming commit in the CI/CD pipeline.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
logger: Logger
|
|
18
|
+
|
|
19
|
+
def __init__(self):
|
|
20
|
+
super().__init__()
|
|
21
|
+
|
|
22
|
+
self.logger = get_logger()
|
|
23
|
+
|
|
24
|
+
@transaction.atomic
|
|
25
|
+
def process(self):
|
|
26
|
+
self.logger.info("Starting migration zero database adjustments...")
|
|
27
|
+
|
|
28
|
+
# Fetch configuration singleton from database
|
|
29
|
+
config_singleton = MigrationZeroConfiguration.objects.fetch_singleton()
|
|
30
|
+
|
|
31
|
+
# If we encountered a problem or are not planning to do a migration reset, we are done here
|
|
32
|
+
if not (config_singleton and config_singleton.is_migration_applicable):
|
|
33
|
+
return
|
|
34
|
+
|
|
35
|
+
# Reset migration history in database for all apps because there might be dependency issues if we keep the
|
|
36
|
+
# records of the other ones
|
|
37
|
+
self.logger.info("Resetting migration history for all apps...")
|
|
38
|
+
|
|
39
|
+
MigrationRecorder.Migration.objects.all().delete()
|
|
40
|
+
|
|
41
|
+
# Apply migrations via fake because the database is already up-to-date
|
|
42
|
+
self.logger.info("Populating migration history.")
|
|
43
|
+
call_command("migrate", fake=True)
|
|
44
|
+
|
|
45
|
+
# Check if migration tree is valid
|
|
46
|
+
self.logger.info("Checking migration integrity.")
|
|
47
|
+
migrate_check = call_command("migrate", check=True)
|
|
48
|
+
|
|
49
|
+
if not migrate_check:
|
|
50
|
+
self.logger.info("All good.")
|
|
51
|
+
else:
|
|
52
|
+
raise InvalidMigrationTreeError(
|
|
53
|
+
'The command "migrate --check" returned a non-zero error code. '
|
|
54
|
+
"Your migration structure seems to be invalid."
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
# Process finished, deactivate migration zero switch
|
|
58
|
+
self.logger.info("Deactivating migration zero switch in database.")
|
|
59
|
+
config_singleton.migration_imminent = False
|
|
60
|
+
config_singleton.save()
|
|
61
|
+
|
|
62
|
+
self.logger.info("Process successfully finished.")
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from django.core.management import call_command
|
|
4
|
+
|
|
5
|
+
from django_migration_zero.helpers.file_system import (
|
|
6
|
+
delete_file,
|
|
7
|
+
get_local_django_apps,
|
|
8
|
+
get_migration_files,
|
|
9
|
+
has_migration_directory,
|
|
10
|
+
)
|
|
11
|
+
from django_migration_zero.helpers.logger import get_logger
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ResetMigrationFiles:
|
|
15
|
+
help = "Remove all local migrations files and create new initial ones."
|
|
16
|
+
|
|
17
|
+
dry_run: bool
|
|
18
|
+
exclude_initials: bool
|
|
19
|
+
|
|
20
|
+
def __init__(self, dry_run: bool = False, exclude_initials: bool = False):
|
|
21
|
+
super().__init__()
|
|
22
|
+
|
|
23
|
+
self.dry_run = dry_run
|
|
24
|
+
self.exclude_initials = exclude_initials
|
|
25
|
+
|
|
26
|
+
def process(self):
|
|
27
|
+
logger = get_logger()
|
|
28
|
+
local_apps = get_local_django_apps()
|
|
29
|
+
|
|
30
|
+
for app_config in local_apps:
|
|
31
|
+
app_path = Path(app_config.path)
|
|
32
|
+
|
|
33
|
+
if not has_migration_directory(app_path=app_path):
|
|
34
|
+
logger.debug(f"Skipping app {app_config.label!r}. No migration package detected.")
|
|
35
|
+
continue
|
|
36
|
+
|
|
37
|
+
migration_file_list = get_migration_files(
|
|
38
|
+
app_label=app_config.label,
|
|
39
|
+
app_path=app_path,
|
|
40
|
+
exclude_initials=self.exclude_initials,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
for migration_file in migration_file_list:
|
|
44
|
+
delete_file(filename=migration_file, app_path=app_path, dry_run=self.dry_run)
|
|
45
|
+
|
|
46
|
+
logger.info("Recreating new initial migration files...")
|
|
47
|
+
call_command("makemigrations")
|
|
48
|
+
|
|
49
|
+
logger.info("Process finished.")
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: django-migration-zero
|
|
3
|
+
Version: 2.3.11
|
|
4
|
+
Summary: Holistic implementation of 'migration zero' pattern for Django covering local changes and in-production database adjustments.
|
|
5
|
+
Project-URL: Homepage, https://github.com/ambient-innovation/django-migration-zero/
|
|
6
|
+
Project-URL: Documentation, https://django-migration-zero.readthedocs.io/en/latest/index.html
|
|
7
|
+
Project-URL: Maintained by, https://ambient.digital/
|
|
8
|
+
Project-URL: Bugtracker, https://github.com/ambient-innovation/django-migration-zero/issues
|
|
9
|
+
Project-URL: Changelog, https://django-migration-zero.readthedocs.io/en/latest/features/changelog.html
|
|
10
|
+
Author-email: Ambient Digital <hello@ambient.digital>
|
|
11
|
+
License: MIT License
|
|
12
|
+
|
|
13
|
+
Copyright (c) 2023 Ambient Innovation: GmbH
|
|
14
|
+
|
|
15
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
16
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
17
|
+
in the Software without restriction, including without limitation the rights
|
|
18
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
19
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
20
|
+
furnished to do so, subject to the following conditions:
|
|
21
|
+
|
|
22
|
+
The above copyright notice and this permission notice shall be included in all
|
|
23
|
+
copies or substantial portions of the Software.
|
|
24
|
+
|
|
25
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
26
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
27
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
28
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
29
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
30
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
31
|
+
SOFTWARE.
|
|
32
|
+
License-File: LICENSE.md
|
|
33
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
34
|
+
Classifier: Environment :: Web Environment
|
|
35
|
+
Classifier: Framework :: Django
|
|
36
|
+
Classifier: Framework :: Django :: 4.2
|
|
37
|
+
Classifier: Framework :: Django :: 5.1
|
|
38
|
+
Classifier: Framework :: Django :: 5.2
|
|
39
|
+
Classifier: Intended Audience :: Developers
|
|
40
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
41
|
+
Classifier: Natural Language :: English
|
|
42
|
+
Classifier: Operating System :: OS Independent
|
|
43
|
+
Classifier: Programming Language :: Python
|
|
44
|
+
Classifier: Programming Language :: Python :: 3
|
|
45
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
46
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
47
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
48
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
49
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
50
|
+
Classifier: Topic :: Utilities
|
|
51
|
+
Requires-Python: >=3.10
|
|
52
|
+
Requires-Dist: django>=4.2
|
|
53
|
+
Description-Content-Type: text/markdown
|
|
54
|
+
|
|
55
|
+
[](https://pypi.org/project/django-migration-zero/)
|
|
56
|
+
[](https://pepy.tech/project/django-migration-zero)
|
|
57
|
+
[](https://github.com/ambient-innovation/django-migration-zero/actions?workflow=CI)
|
|
58
|
+
[](https://github.com/astral-sh/ruff)
|
|
59
|
+
[](https://github.com/astral-sh/ruff)
|
|
60
|
+
[](https://django-migration-zero.readthedocs.io/en/latest/?badge=latest)
|
|
61
|
+
|
|
62
|
+
Welcome to **django-migration-zero** - the holistic implementation of "migration zero" pattern for
|
|
63
|
+
Django covering local changes and CI/CD pipeline adjustments.
|
|
64
|
+
|
|
65
|
+
This package implements the "migration zero" pattern to clean up your local migrations and provides convenient
|
|
66
|
+
management commands to recreate your migration files and updating your migration history on your environments
|
|
67
|
+
(like test or production systems).
|
|
68
|
+
|
|
69
|
+
[PyPI](https://pypi.org/project/django-migration-zero/) | [GitHub](https://github.com/ambient-innovation/django-migration-zero) | [Full documentation](https://django-migration-zero.readthedocs.io/en/latest/index.html)
|
|
70
|
+
|
|
71
|
+
Creator & Maintainer: [Ambient Digital](https://ambient.digital/)
|
|
72
|
+
|
|
73
|
+
## Features
|
|
74
|
+
|
|
75
|
+
* Remove all existing local migration files and recreate them as initial migrations
|
|
76
|
+
* Configuration singleton in Django admin to prepare your clean-up deployment
|
|
77
|
+
* Management command for your pipeline to update Django's migration history table to reflect the changed migrations
|
|
78
|
+
|
|
79
|
+
## Motivation
|
|
80
|
+
|
|
81
|
+
Working with any proper ORM will result in database changes which are reflected in migration files to update your
|
|
82
|
+
different environment's database structure. These files are versioned in your repository and if you follow any of the
|
|
83
|
+
most popular deployment approaches, they won't be needed when they are deployed on production. This means, they clutter
|
|
84
|
+
your repo, might lead to merge conflicts in the future and will slow down your test setup.
|
|
85
|
+
|
|
86
|
+
Django's default way of handling this is called "squashing". This approach is covered broadly in the
|
|
87
|
+
[official documentation](https://docs.djangoproject.com/en/dev/topics/migrations/#migration-squashing). The main
|
|
88
|
+
drawback here is, that you have to take care of circular dependencies between models. Depending on your project's
|
|
89
|
+
size, this can take a fair amount of time.
|
|
90
|
+
|
|
91
|
+
The main benefit of squashing migrations is, that the history stays intact, therefore it can be used for example in
|
|
92
|
+
package which can be installed by anybody and you don't have control over their database.
|
|
93
|
+
|
|
94
|
+
If you are working on a "regular" application, you have full control over your data(bases) and once everything has
|
|
95
|
+
been applied on the "last" system, typically production, the migrations are obsolete. To avoid spending much time on
|
|
96
|
+
fixing squashed migrations you won't need, you can use the "migration zero" pattern. In a nutshell, this means:
|
|
97
|
+
|
|
98
|
+
* Delete all your local migration files
|
|
99
|
+
* Recreate initial migration files containing your current model state
|
|
100
|
+
* Fix the migration history on every of your environments
|
|
101
|
+
|
|
102
|
+
## Installation
|
|
103
|
+
|
|
104
|
+
- Install the package via pip:
|
|
105
|
+
|
|
106
|
+
`pip install django-migration-zero`
|
|
107
|
+
|
|
108
|
+
or via pipenv:
|
|
109
|
+
|
|
110
|
+
`pipenv install django-migration-zero`
|
|
111
|
+
|
|
112
|
+
- Add module to `INSTALLED_APPS` within the main django `settings.py`:
|
|
113
|
+
|
|
114
|
+
````
|
|
115
|
+
INSTALLED_APPS = (
|
|
116
|
+
...
|
|
117
|
+
'django_migration_zero',
|
|
118
|
+
)
|
|
119
|
+
````
|
|
120
|
+
|
|
121
|
+
- Apply migrations by running:
|
|
122
|
+
|
|
123
|
+
`python ./manage.py migrate`
|
|
124
|
+
|
|
125
|
+
- Add this block to your loggers in your main Django `settings.py` to show logs in your console.
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
LOGGING = {
|
|
129
|
+
"handlers": {
|
|
130
|
+
"console": {
|
|
131
|
+
"class": "logging.StreamHandler",
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
"loggers": {
|
|
135
|
+
"django_migration_zero": {
|
|
136
|
+
"handlers": ["console"],
|
|
137
|
+
"level": "INFO",
|
|
138
|
+
"propagate": True,
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Publish to ReadTheDocs.io
|
|
145
|
+
|
|
146
|
+
- Fetch the latest changes in GitHub mirror and push them
|
|
147
|
+
- Trigger new build at ReadTheDocs.io (follow instructions in admin panel at RTD) if the GitHub webhook is not yet set
|
|
148
|
+
up.
|
|
149
|
+
|
|
150
|
+
### Preparation and building
|
|
151
|
+
|
|
152
|
+
This package uses [uv](https://github.com/astral-sh/uv) for dependency management and building.
|
|
153
|
+
|
|
154
|
+
- Update documentation about new/changed functionality
|
|
155
|
+
|
|
156
|
+
- Update the `CHANGES.md`
|
|
157
|
+
|
|
158
|
+
- Increment version in main `__init__.py`
|
|
159
|
+
|
|
160
|
+
- Create pull request / merge to "master"
|
|
161
|
+
|
|
162
|
+
- This project uses uv to publish to PyPI. This will create distribution files in the `dist/` directory.
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
uv build
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Publishing to PyPI
|
|
169
|
+
|
|
170
|
+
To publish to the production PyPI:
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
uv publish
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
To publish to TestPyPI first (recommended for testing):
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
uv publish --publish-url https://test.pypi.org/legacy/
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
You can then test the installation from TestPyPI:
|
|
183
|
+
|
|
184
|
+
```bash
|
|
185
|
+
uv pip install --index-url https://test.pypi.org/simple/ ambient-package-update
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Maintenance
|
|
189
|
+
|
|
190
|
+
Please note that this package supports the [ambient-package-update](https://pypi.org/project/ambient-package-update/).
|
|
191
|
+
So you don't have to worry about the maintenance of this package. This updater is rendering all important
|
|
192
|
+
configuration and setup files. It works similar to well-known updaters like `pyupgrade` or `django-upgrade`.
|
|
193
|
+
|
|
194
|
+
To run an update, refer to the [documentation page](https://pypi.org/project/ambient-package-update/)
|
|
195
|
+
of the "ambient-package-update".
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
django_migration_zero/__init__.py,sha256=OFcnPjqhvI93h2shcDBzspXyPGPEwJ2ldZH8JkCr6X4,183
|
|
2
|
+
django_migration_zero/admin.py,sha256=hBPJa5aCzLYsuYpWHdby05PmpgEu1wnoodnILiUSF1M,455
|
|
3
|
+
django_migration_zero/apps.py,sha256=vLGaiWSZn8Hyg69hqA-d69GiLGqnzeY5yk8MipH3eWQ,279
|
|
4
|
+
django_migration_zero/exceptions.py,sha256=C1YMMuzPt8vqAz0-quEKBKxuVqfG0aV1dUvuumwBxik,199
|
|
5
|
+
django_migration_zero/managers.py,sha256=ZmCJmadkkzctJJfcZr4VfWF5_4mOFg3bv8b6Ff53dOc,1217
|
|
6
|
+
django_migration_zero/models.py,sha256=4UlVGG-Fx70R25fGUnVDPOSxbKDBxCzKbF1KRnytNX8,1413
|
|
7
|
+
django_migration_zero/helpers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
django_migration_zero/helpers/file_system.py,sha256=VZpADqrcaFT377koLvO1P0jGX-d6D37KtWGBT-FCro4,2974
|
|
9
|
+
django_migration_zero/helpers/logger.py,sha256=RiOzM4747NYlzAcc-cUnHPn_DA_sL-Uj-bG3JbjbPiU,193
|
|
10
|
+
django_migration_zero/locale/de/LC_MESSAGES/django.po,sha256=YFTqX_xF1HvKR-QLOHPQCirJGGSPbLjsQWivsSQOjqI,1396
|
|
11
|
+
django_migration_zero/management/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
|
+
django_migration_zero/management/commands/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
13
|
+
django_migration_zero/management/commands/handle_migration_zero_reset.py,sha256=ONq1FbScjb1N9H3pJ3iW1KoFUsMDAUop-vI6Lo05IWE,356
|
|
14
|
+
django_migration_zero/management/commands/reset_local_migration_files.py,sha256=f46MlgCCtq-txMMBCq8GjjKDgejY3Sua1HA7QaAHp10,917
|
|
15
|
+
django_migration_zero/migrations/0001_initial.py,sha256=bxCX-xNhOPyeLMOXl3Rc_3n0pP8w0T8TqJbgdXMl1hQ,1465
|
|
16
|
+
django_migration_zero/migrations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
17
|
+
django_migration_zero/services/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
18
|
+
django_migration_zero/services/deployment.py,sha256=kMQHGFAy3tDrxR6lIvfYzfUc8Wbo9yOT_OtDPL_tcmQ,2333
|
|
19
|
+
django_migration_zero/services/local.py,sha256=HwEYWua_QkusraPeYFJ8s63Sf8FRQo2QWkH2qqx2Ag8,1552
|
|
20
|
+
django_migration_zero-2.3.11.dist-info/METADATA,sha256=72v_MMU5hcLmVx1o38HOFUXpmxefYPPZwLs78mX5wzw,8505
|
|
21
|
+
django_migration_zero-2.3.11.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
22
|
+
django_migration_zero-2.3.11.dist-info/licenses/LICENSE.md,sha256=wXE3v9f_9fGeTqpGEfPoHbLVFTtx1pQ-bLlQ8v2or4c,1102
|
|
23
|
+
django_migration_zero-2.3.11.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Ambient Innovation: GmbH
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|