django-migration-zero 0.1.0__py2.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 +7 -0
- django_migration_zero/exceptions.py +6 -0
- django_migration_zero/helpers/__init__.py +0 -0
- django_migration_zero/helpers/file_system.py +70 -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_reset.py +60 -0
- django_migration_zero/management/commands/reset_local_migration_files.py +21 -0
- django_migration_zero/managers.py +25 -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/local.py +39 -0
- django_migration_zero/settings.py +3 -0
- django_migration_zero-0.1.0.dist-info/LICENSE +21 -0
- django_migration_zero-0.1.0.dist-info/LICENSE.md +21 -0
- django_migration_zero-0.1.0.dist-info/METADATA +222 -0
- django_migration_zero-0.1.0.dist-info/RECORD +24 -0
- django_migration_zero-0.1.0.dist-info/WHEEL +5 -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
|
|
File without changes
|
|
@@ -0,0 +1,70 @@
|
|
|
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
|
+
|
|
8
|
+
from django_migration_zero.helpers.logger import get_logger
|
|
9
|
+
from django_migration_zero.settings import MIGRATION_ZERO_APPS_DIR
|
|
10
|
+
|
|
11
|
+
logger = get_logger()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def build_migration_directory_path(*, app_label: str) -> Path:
|
|
15
|
+
"""
|
|
16
|
+
Get directory to the migration directory of a given local Django app
|
|
17
|
+
"""
|
|
18
|
+
return MIGRATION_ZERO_APPS_DIR + app_label + "migrations"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def get_local_django_apps() -> list[str]:
|
|
22
|
+
"""
|
|
23
|
+
Iterate all installed Django apps and detect local ones.
|
|
24
|
+
"""
|
|
25
|
+
local_apps = []
|
|
26
|
+
logger.info("Getting local Django apps...")
|
|
27
|
+
for app_config in apps.get_app_configs():
|
|
28
|
+
if str(Path(MIGRATION_ZERO_APPS_DIR)) in str(Path(app_config.path)):
|
|
29
|
+
logger.info(f"Local app {app_config.label!r} discovered.")
|
|
30
|
+
local_apps.append(app_config.label)
|
|
31
|
+
else:
|
|
32
|
+
logger.debug(f"App {app_config.label!r} ignored since it's not local.")
|
|
33
|
+
|
|
34
|
+
return local_apps
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def has_migration_directory(*, app_label: str) -> bool:
|
|
38
|
+
"""
|
|
39
|
+
Determines if the given Django app has a migrations directory and therefore migrations
|
|
40
|
+
"""
|
|
41
|
+
possible_migration_dir = build_migration_directory_path(app_label=app_label)
|
|
42
|
+
return True if isdir(possible_migration_dir) else False
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def get_migration_files(*, app_label: str) -> list[str]:
|
|
46
|
+
migration_file_list = []
|
|
47
|
+
|
|
48
|
+
logger.info(f"Getting migration files from app {app_label!r}...")
|
|
49
|
+
migration_dir = build_migration_directory_path(app_label=app_label)
|
|
50
|
+
file_pattern = r"^\d{4}_\w+\.py$"
|
|
51
|
+
for filename in os.listdir(migration_dir):
|
|
52
|
+
if re.match(file_pattern, filename):
|
|
53
|
+
logger.info(f"Migration file {filename!r} detected.")
|
|
54
|
+
migration_file_list.append(filename)
|
|
55
|
+
else:
|
|
56
|
+
logger.debug(f"File {filename!r} ignored since it's not fitting the migration name pattern..")
|
|
57
|
+
|
|
58
|
+
return migration_file_list
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def delete_file(*, filename: str, app_label: str, dry_run: bool = False) -> None:
|
|
62
|
+
"""
|
|
63
|
+
Physically delete file.
|
|
64
|
+
"""
|
|
65
|
+
file_path = build_migration_directory_path(app_label=app_label) + filename
|
|
66
|
+
if not dry_run:
|
|
67
|
+
try:
|
|
68
|
+
os.unlink(file_path)
|
|
69
|
+
except OSError:
|
|
70
|
+
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,60 @@
|
|
|
1
|
+
from django.core.management import call_command
|
|
2
|
+
from django.core.management.base import BaseCommand
|
|
3
|
+
|
|
4
|
+
from django_migration_zero.exceptions import InvalidMigrationTreeError
|
|
5
|
+
from django_migration_zero.helpers.file_system import get_local_django_apps, has_migration_directory
|
|
6
|
+
from django_migration_zero.helpers.logger import get_logger
|
|
7
|
+
from django_migration_zero.models import MigrationZeroConfiguration
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Command(BaseCommand):
|
|
11
|
+
help = "Prepares the database after resetting all migrations." # noqa: A003
|
|
12
|
+
|
|
13
|
+
def handle(self, *args, **options):
|
|
14
|
+
logger = get_logger()
|
|
15
|
+
|
|
16
|
+
logger.info("Starting migration zero database adjustments...")
|
|
17
|
+
|
|
18
|
+
# Fetch configuration singleton from database
|
|
19
|
+
config_singleton = MigrationZeroConfiguration.objects.fetch_singleton()
|
|
20
|
+
|
|
21
|
+
# If we are not planning to do a migration reset, we are done here
|
|
22
|
+
if not config_singleton.is_migration_applicable:
|
|
23
|
+
return
|
|
24
|
+
|
|
25
|
+
# Reset migration history in database for all local apps
|
|
26
|
+
logger.info("Resetting migration history for local apps...")
|
|
27
|
+
|
|
28
|
+
local_app_list = get_local_django_apps()
|
|
29
|
+
|
|
30
|
+
for app_label in local_app_list:
|
|
31
|
+
# Local apps have a path which contains the path of the Django app directory
|
|
32
|
+
if not has_migration_directory(app_label=app_label):
|
|
33
|
+
logger.debug(f"Skipping app {app_label!r}. No migration package detected.")
|
|
34
|
+
continue
|
|
35
|
+
|
|
36
|
+
logger.info(f"Processing {app_label!r}...")
|
|
37
|
+
call_command("migrate", fake=True, app_label=app_label, migration_name="zero")
|
|
38
|
+
|
|
39
|
+
# Apply migrations via fake because the database is already up-to-date
|
|
40
|
+
logger.info("Populating migration history.")
|
|
41
|
+
call_command("migrate", fake=True)
|
|
42
|
+
|
|
43
|
+
# Check if migration tree is valid
|
|
44
|
+
logger.info("Checking migration integrity.")
|
|
45
|
+
migrate_check = call_command("migrate", check=True)
|
|
46
|
+
|
|
47
|
+
if not migrate_check:
|
|
48
|
+
logger.info("All good.")
|
|
49
|
+
else:
|
|
50
|
+
raise InvalidMigrationTreeError(
|
|
51
|
+
'The command "migrate --check" returned a non-zero error code. '
|
|
52
|
+
"Your migration structure seems to be invalid."
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
# Process finished, deactivate migration zero switch
|
|
56
|
+
logger.info("Deactivating migration zero switch in database.")
|
|
57
|
+
config_singleton.migration_imminent = False
|
|
58
|
+
config_singleton.save()
|
|
59
|
+
|
|
60
|
+
logger.info("Process successfully finished.")
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
|
|
15
|
+
def handle(self, *args, **options):
|
|
16
|
+
if not settings.DEBUG:
|
|
17
|
+
print("Don't run this command in production!")
|
|
18
|
+
return
|
|
19
|
+
|
|
20
|
+
service = ResetMigrationFiles(dry_run=options.get("dry_run", False))
|
|
21
|
+
service.process()
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from django.db import models
|
|
2
|
+
|
|
3
|
+
from django_migration_zero.exceptions import MissingMigrationZeroConfigRecordError
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class MigrationZeroConfigurationQuerySet(models.QuerySet):
|
|
7
|
+
pass
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class MigrationZeroConfigurationManager(models.Manager):
|
|
11
|
+
def fetch_singleton(self) -> None:
|
|
12
|
+
number_records = self.all().count()
|
|
13
|
+
if number_records > 1:
|
|
14
|
+
raise MissingMigrationZeroConfigRecordError(
|
|
15
|
+
"Too many configuration records detected. There can only be one."
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
config_singleton = self.all().first()
|
|
19
|
+
if not config_singleton:
|
|
20
|
+
raise MissingMigrationZeroConfigRecordError("No configuration record found in the database.")
|
|
21
|
+
|
|
22
|
+
return config_singleton
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
MigrationZeroConfigurationManager = MigrationZeroConfigurationManager.from_queryset(MigrationZeroConfigurationQuerySet)
|
|
@@ -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,39 @@
|
|
|
1
|
+
from django.core.management import call_command
|
|
2
|
+
|
|
3
|
+
from django_migration_zero.helpers.file_system import (
|
|
4
|
+
delete_file,
|
|
5
|
+
get_local_django_apps,
|
|
6
|
+
get_migration_files,
|
|
7
|
+
has_migration_directory,
|
|
8
|
+
)
|
|
9
|
+
from django_migration_zero.helpers.logger import get_logger
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ResetMigrationFiles:
|
|
13
|
+
help = "Remove all local migrations files and create new initial ones." # noqa: A003
|
|
14
|
+
|
|
15
|
+
dry_run: bool
|
|
16
|
+
|
|
17
|
+
def __init__(self, dry_run: bool = False):
|
|
18
|
+
super().__init__()
|
|
19
|
+
|
|
20
|
+
self.dry_run = dry_run
|
|
21
|
+
|
|
22
|
+
def process(self):
|
|
23
|
+
logger = get_logger()
|
|
24
|
+
local_app_list = get_local_django_apps()
|
|
25
|
+
|
|
26
|
+
for app_label in local_app_list:
|
|
27
|
+
if not has_migration_directory(app_label=app_label):
|
|
28
|
+
logger.debug(f"Skipping app {app_label!r}. No migration package detected.")
|
|
29
|
+
continue
|
|
30
|
+
|
|
31
|
+
migration_file_list = get_migration_files(app_label=app_label)
|
|
32
|
+
|
|
33
|
+
for migration_file in migration_file_list:
|
|
34
|
+
delete_file(filename=migration_file, app_label=app_label, dry_run=self.dry_run)
|
|
35
|
+
|
|
36
|
+
logger.info("\nRecreating new initial migration files...\n")
|
|
37
|
+
call_command("makemigrations")
|
|
38
|
+
|
|
39
|
+
logger.info("Process finished.")
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Ambient Digital
|
|
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.
|
|
@@ -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.
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: django-migration-zero
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Holistic implementation of "migration zero" pattern for Django covering local changes and in-production database
|
|
5
|
+
Author-email: Ambient Digital <hello@ambient.digital>
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
Classifier: Development Status :: 4 - Beta
|
|
8
|
+
Classifier: Environment :: Web Environment
|
|
9
|
+
Classifier: Framework :: Django
|
|
10
|
+
Classifier: Framework :: Django :: 3.2
|
|
11
|
+
Classifier: Framework :: Django :: 4.1
|
|
12
|
+
Classifier: Framework :: Django :: 4.2
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Natural Language :: English
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
24
|
+
Classifier: Topic :: Utilities
|
|
25
|
+
Requires-Dist: Django>=3.2
|
|
26
|
+
Requires-Dist: freezegun~=1.2 ; extra == "dev"
|
|
27
|
+
Requires-Dist: pytest-django~=4.5 ; extra == "dev"
|
|
28
|
+
Requires-Dist: pytest-mock~=3.10 ; extra == "dev"
|
|
29
|
+
Requires-Dist: pre-commit~=3.2 ; extra == "dev"
|
|
30
|
+
Requires-Dist: black~=23.3 ; extra == "dev"
|
|
31
|
+
Requires-Dist: Django~=3.2 ; extra == "dev"
|
|
32
|
+
Requires-Dist: sphinx==4.2.0 ; extra == "dev"
|
|
33
|
+
Requires-Dist: sphinx-rtd-theme==1.0.0 ; extra == "dev"
|
|
34
|
+
Requires-Dist: m2r2==0.3.1 ; extra == "dev"
|
|
35
|
+
Requires-Dist: mistune<2.0.0 ; extra == "dev"
|
|
36
|
+
Requires-Dist: ambient-package-update~=23.10.1 ; extra == "dev"
|
|
37
|
+
Project-URL: Bugtracker, https://github.com/ambient-innovation/django-migration-zero/issues
|
|
38
|
+
Project-URL: Changelog, https://django-migration-zero.readthedocs.io/en/latest/features/changelog.html
|
|
39
|
+
Project-URL: Documentation, https://django-migration-zero.readthedocs.io/en/latest/index.html
|
|
40
|
+
Project-URL: Homepage, https://github.com/ambient-innovation/django-migration-zero/
|
|
41
|
+
Project-URL: Maintained by, https://ambient.digital/
|
|
42
|
+
Provides-Extra: dev
|
|
43
|
+
|
|
44
|
+
[](https://pypi.org/project/django-migration-zero/)
|
|
45
|
+
[](https://pepy.tech/project/django-migration-zero)
|
|
46
|
+
[](https://github.com/astral-sh/ruff)
|
|
47
|
+
[](https://github.com/python/black)
|
|
48
|
+
[](https://django-migration-zero.readthedocs.io/en/latest/?badge=latest)
|
|
49
|
+
|
|
50
|
+
Welcome to **django-migration-zero** - the holistic implementation of "migration zero" pattern for
|
|
51
|
+
Django covering local changes and CI/CD pipeline adjustments.
|
|
52
|
+
|
|
53
|
+
This package implements the "migration zero" pattern to clean up your local migrations and provides convenient
|
|
54
|
+
management commands to recreate your migration files and updating your migration history on your environments
|
|
55
|
+
(like test or production systems).
|
|
56
|
+
|
|
57
|
+
* [PyPI](https://pypi.org/project/django-migration-zero/)
|
|
58
|
+
* [GitHub](https://github.com/ambient-innovation/django-migration-zero)
|
|
59
|
+
* [Full documentation](https://django-migration-zero.readthedocs.io/en/latest/index.html)
|
|
60
|
+
* Creator & Maintainer: [Ambient Digital](https://ambient.digital)
|
|
61
|
+
|
|
62
|
+
## Features
|
|
63
|
+
|
|
64
|
+
* Remove all existing local migration files and recreate them as initial migrations
|
|
65
|
+
* Configuration singleton in Django admin to prepare your clean-up deployment
|
|
66
|
+
* Management command for your pipeline to update Django's migration history table to reflect the changed migrations
|
|
67
|
+
|
|
68
|
+
## Motivation
|
|
69
|
+
|
|
70
|
+
Working with any proper ORM will result in database changes which are reflected in migration files to update your
|
|
71
|
+
different environment's database structure. These files are versioned in your repository and if you follow any of the
|
|
72
|
+
most popular deployment approaches, they won't be needed when they are deployed on production. This means, they clutter
|
|
73
|
+
your repo, might lead to merge conflicts in the future and will slow down your test setup.
|
|
74
|
+
|
|
75
|
+
Django's default way of handling this is called "squashing". This approach is covered broadly in the
|
|
76
|
+
(official documentation)[https://docs.djangoproject.com/en/dev/topics/migrations/#migration-squashing. The main
|
|
77
|
+
drawback here is, that you have to take care of circular dependencies between models. Depending on your project's
|
|
78
|
+
size, this can take a fair amount of time.
|
|
79
|
+
|
|
80
|
+
The main benefit of squashing migrations is, that the history stays intact, therefore it can be used for example in
|
|
81
|
+
package which can be installed by anybody and you don't have control over their database.
|
|
82
|
+
|
|
83
|
+
If you are working on a "regular" application, you have full control over your data(bases) and once everything has
|
|
84
|
+
been applied on the "last" system, typically production, the migrations are obsolete. To avoid spending much time on
|
|
85
|
+
fixing squashed migrations you won't need, you can use the "migration zero" pattern. In a nutshell, this means:
|
|
86
|
+
|
|
87
|
+
* Delete all your local migration files
|
|
88
|
+
* Recreate initial migration files containing your current model state
|
|
89
|
+
* Fix the migration history on every of your environments
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
## Installation
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
- Install the package via pip:
|
|
98
|
+
|
|
99
|
+
`pip install django-migration-zero`
|
|
100
|
+
|
|
101
|
+
or via pipenv:
|
|
102
|
+
|
|
103
|
+
`pipenv install django-migration-zero`
|
|
104
|
+
|
|
105
|
+
- Add module to `INSTALLED_APPS` within the main django `settings.py`:
|
|
106
|
+
|
|
107
|
+
````
|
|
108
|
+
INSTALLED_APPS = (
|
|
109
|
+
...
|
|
110
|
+
'django_migration_zero',
|
|
111
|
+
)
|
|
112
|
+
````
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
## Contribute
|
|
116
|
+
|
|
117
|
+
### Setup package for development
|
|
118
|
+
|
|
119
|
+
- Create a Python virtualenv and activate it
|
|
120
|
+
- Install "pip-tools" with `pip install pip-tools`
|
|
121
|
+
- Compile the requirements with `pip-compile --extra dev, -o requirements.txt pyproject.toml --resolver=backtracking`
|
|
122
|
+
- Sync the dependencies with your virtualenv with `pip-sync`
|
|
123
|
+
|
|
124
|
+
### Add functionality
|
|
125
|
+
|
|
126
|
+
- Create a new branch for your feature
|
|
127
|
+
- Change the dependency in your requirements.txt to a local (editable) one that points to your local file system:
|
|
128
|
+
`-e /Users/workspace/django-migration-zero` or via pip `pip install -e /Users/workspace/django-migration-zero`
|
|
129
|
+
- Ensure the code passes the tests
|
|
130
|
+
- Create a pull request
|
|
131
|
+
|
|
132
|
+
### Run tests
|
|
133
|
+
|
|
134
|
+
- Run tests
|
|
135
|
+
````
|
|
136
|
+
pytest --ds settings tests
|
|
137
|
+
````
|
|
138
|
+
|
|
139
|
+
### Git hooks (via pre-commit)
|
|
140
|
+
|
|
141
|
+
We use pre-push hooks to ensure that only linted code reaches our remote repository and pipelines aren't triggered in
|
|
142
|
+
vain.
|
|
143
|
+
|
|
144
|
+
To enable the configured pre-push hooks, you need to [install](https://pre-commit.com/) pre-commit and run once:
|
|
145
|
+
|
|
146
|
+
pre-commit install -t pre-push -t pre-commit --install-hooks
|
|
147
|
+
|
|
148
|
+
This will permanently install the git hooks for both, frontend and backend, in your local
|
|
149
|
+
[`.git/hooks`](./.git/hooks) folder.
|
|
150
|
+
The hooks are configured in the [`.pre-commit-config.yaml`](templates/.pre-commit-config.yaml.tpl).
|
|
151
|
+
|
|
152
|
+
You can check whether hooks work as intended using the [run](https://pre-commit.com/#pre-commit-run) command:
|
|
153
|
+
|
|
154
|
+
pre-commit run [hook-id] [options]
|
|
155
|
+
|
|
156
|
+
Example: run single hook
|
|
157
|
+
|
|
158
|
+
pre-commit run ruff --all-files --hook-stage push
|
|
159
|
+
|
|
160
|
+
Example: run all hooks of pre-push stage
|
|
161
|
+
|
|
162
|
+
pre-commit run --all-files --hook-stage push
|
|
163
|
+
|
|
164
|
+
### Update documentation
|
|
165
|
+
|
|
166
|
+
- To build the documentation run: `sphinx-build docs/ docs/_build/html/`.
|
|
167
|
+
- Open `docs/_build/html/index.html` to see the documentation.
|
|
168
|
+
|
|
169
|
+
### Translation files
|
|
170
|
+
|
|
171
|
+
If you have added custom text, make sure to wrap it in `_()` where `_` is
|
|
172
|
+
gettext_lazy (`from django.utils.translation import gettext_lazy as _`).
|
|
173
|
+
|
|
174
|
+
How to create translation file:
|
|
175
|
+
|
|
176
|
+
* Navigate to `django-migration-zero`
|
|
177
|
+
* `python manage.py makemessages -l de`
|
|
178
|
+
* Have a look at the new/changed files within `django_migration_zero/locale`
|
|
179
|
+
|
|
180
|
+
How to compile translation files:
|
|
181
|
+
|
|
182
|
+
* Navigate to `django-migration-zero`
|
|
183
|
+
* `python manage.py compilemessages`
|
|
184
|
+
* Have a look at the new/changed files within `django_migration_zero/locale`
|
|
185
|
+
|
|
186
|
+
### Publish to ReadTheDocs.io
|
|
187
|
+
|
|
188
|
+
- Fetch the latest changes in GitHub mirror and push them
|
|
189
|
+
- Trigger new build at ReadTheDocs.io (follow instructions in admin panel at RTD) if the GitHub webhook is not yet set
|
|
190
|
+
up.
|
|
191
|
+
|
|
192
|
+
### Publish to PyPi
|
|
193
|
+
|
|
194
|
+
- Update documentation about new/changed functionality
|
|
195
|
+
|
|
196
|
+
- Update the `Changelog`
|
|
197
|
+
|
|
198
|
+
- Increment version in main `__init__.py`
|
|
199
|
+
|
|
200
|
+
- Create pull request / merge to master
|
|
201
|
+
|
|
202
|
+
- This project uses the flit package to publish to PyPI. Thus publishing should be as easy as running:
|
|
203
|
+
```
|
|
204
|
+
flit publish
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
To publish to TestPyPI use the following ensure that you have set up your .pypirc as
|
|
208
|
+
shown [here](https://flit.readthedocs.io/en/latest/upload.html#using-pypirc) and use the following command:
|
|
209
|
+
|
|
210
|
+
```
|
|
211
|
+
flit publish --repository testpypi
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### Maintenance
|
|
215
|
+
|
|
216
|
+
Please note that this package supports the [ambient-package-update](https://pypi.org/project/ambient-package-update/).
|
|
217
|
+
So you don't have to worry about the maintenance of this package. All important configuration and setup files are
|
|
218
|
+
being rendered by this updater. It works similar to well-known updaters like `pyupgrade` or `django-upgrade`.
|
|
219
|
+
|
|
220
|
+
To run an update, refer to the [documentation page](https://pypi.org/project/ambient-package-update/)
|
|
221
|
+
of the "ambient-package-update".
|
|
222
|
+
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
django_migration_zero/__init__.py,sha256=YVzUolBFgXRYvB74gpWNUpH2JeiA33wKlaYvGkY196A,157
|
|
2
|
+
django_migration_zero/admin.py,sha256=hBPJa5aCzLYsuYpWHdby05PmpgEu1wnoodnILiUSF1M,455
|
|
3
|
+
django_migration_zero/apps.py,sha256=J53IO-q03b9pQRoZn58sRWjOyE4Nsk989EDya0EdmbM,217
|
|
4
|
+
django_migration_zero/exceptions.py,sha256=6eG6zpFCtGR2uhHIH85q7g0pzrN4YKlX8RXGgOPxz6w,132
|
|
5
|
+
django_migration_zero/managers.py,sha256=jL1UYuXCs3RKxSXxHPpNU5YUxr2vHDlRb0kTf7m2bLI,850
|
|
6
|
+
django_migration_zero/models.py,sha256=4UlVGG-Fx70R25fGUnVDPOSxbKDBxCzKbF1KRnytNX8,1413
|
|
7
|
+
django_migration_zero/settings.py,sha256=q08l5M3-vInnroBP95r5wTFWele4aPDYbLZl0xcOCo4,114
|
|
8
|
+
django_migration_zero/helpers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
|
+
django_migration_zero/helpers/file_system.py,sha256=v-K9AltvUUpBNnKuTX3YnrwiGKjAWsMkDlXvFE18QDI,2320
|
|
10
|
+
django_migration_zero/helpers/logger.py,sha256=eV6KSYieawO9-ZOWCHulyU0bDjfZsSyYH0q9g-vgvUE,185
|
|
11
|
+
django_migration_zero/locale/de/LC_MESSAGES/django.po,sha256=YFTqX_xF1HvKR-QLOHPQCirJGGSPbLjsQWivsSQOjqI,1396
|
|
12
|
+
django_migration_zero/management/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
13
|
+
django_migration_zero/management/commands/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
14
|
+
django_migration_zero/management/commands/handle_migration_reset.py,sha256=T1eTWOvAb_JmOEJGezXnyE3_oQ-rUoJSsVkgSDXWhFc,2539
|
|
15
|
+
django_migration_zero/management/commands/reset_local_migration_files.py,sha256=b9yr9WC_YbvBLGQwp8zpfz4EomioHyH3qKYa-oz8h8w,667
|
|
16
|
+
django_migration_zero/migrations/0001_initial.py,sha256=bxCX-xNhOPyeLMOXl3Rc_3n0pP8w0T8TqJbgdXMl1hQ,1465
|
|
17
|
+
django_migration_zero/migrations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
18
|
+
django_migration_zero/services/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
19
|
+
django_migration_zero/services/local.py,sha256=I5QLCorxMFkNnCRBr5dsSrSkhWwdVsURvohGACET9ws,1255
|
|
20
|
+
django_migration_zero-0.1.0.dist-info/LICENSE,sha256=vNAJpTGgMHR0pV-zW_DBfhu2LFO4hSlB5K3MeieW_TY,1093
|
|
21
|
+
django_migration_zero-0.1.0.dist-info/LICENSE.md,sha256=wXE3v9f_9fGeTqpGEfPoHbLVFTtx1pQ-bLlQ8v2or4c,1102
|
|
22
|
+
django_migration_zero-0.1.0.dist-info/WHEEL,sha256=Sgu64hAMa6g5FdzHxXv9Xdse9yxpGGMeagVtPMWpJQY,99
|
|
23
|
+
django_migration_zero-0.1.0.dist-info/METADATA,sha256=ZhV7MfRUY76LTHYIUXlfgP5ltGVzzzd949j8og0w0kM,9279
|
|
24
|
+
django_migration_zero-0.1.0.dist-info/RECORD,,
|