PK!zzdjango_safemigrate/__init__.pyfrom enum import Enum class Safe(Enum): always = "always" before_deploy = "before_deploy" after_deploy = "after_deploy" PK!mdjango_safemigrate/apps.py"""Add a safemigrate command.""" from django.apps import AppConfig from django.utils.translation import ugettext_lazy as _ class SafeMigrateConfig(AppConfig): """Safe migrate Django app config.""" name = "django_safemigrate" verbose_name = _("Safe Migrate") PK!)django_safemigrate/management/__init__.pyPK!2django_safemigrate/management/commands/__init__.pyPK! ^EE5django_safemigrate/management/commands/safemigrate.py"""Hook into the migrate command to add safety. Migration safety is enforced by a pre_migrate signal receiver. """ from django.conf import settings from django.db.models.signals import pre_migrate from django.core.management.base import CommandError from django.core.management.commands import migrate from django_safemigrate import Safe from django_safemigrate.apps import SafeMigrateConfig def safety(migration): """Determine the safety status of a migration.""" return getattr(migration, "safe", Safe.after_deploy) class Command(migrate.Command): """Run database migrations that are safe to run before deployment.""" help = "Run database migrations that are safe to run before deployment." receiver_has_run = False def handle(self, *args, **options): # Only connect the handler when this command is run to # avoid running for tests. pre_migrate.connect( self.pre_migrate_receiver, dispatch_uid="django_safemigrate" ) super().handle(*args, **options) def pre_migrate_receiver(self, *, plan, **_): """Handle the pre_migrate receiver for all apps.""" if self.receiver_has_run: # Can't just look for the run for the current app, # because it only sends the signal to apps with models. return # Only run once self.receiver_has_run = True # strict by default strict = getattr(settings, "SAFEMIGRATE", None) != "nonstrict" if any(backward for mig, backward in plan): raise CommandError("Backward migrations are not supported.") # Pull the migrations into a new list migrations = [migration for migration, backward in plan] # Check for invalid safe properties invalid = [ migration for migration in migrations if safety(migration) not in Safe ] if invalid: self.stdout.write(self.style.MIGRATE_HEADING("Invalid migrations:")) for migration in invalid: self.stdout.write(f" {migration.app_label}.{migration.name}") raise CommandError( "Aborting due to migrations with invalid safe properties." ) protected = [ migration for migration in migrations if safety(migration) == Safe.after_deploy ] if not protected: return # No migrations to protect. # Display the migrations that are protected self.stdout.write(self.style.MIGRATE_HEADING("Protected migrations:")) for migration in protected: self.stdout.write(f" {migration.app_label}.{migration.name}") ready = [ migration for migration in migrations if safety(migration) != Safe.after_deploy ] delayed = [] blocked = [] while True: blockers = protected + delayed + blocked blockers_deps = [(m.app_label, m.name) for m in blockers] to_block_deps = [dep for mig in blockers for dep in mig.run_before] block = [ migration for migration in ready if any(dep in blockers_deps for dep in migration.dependencies) or (migration.app_label, migration.name) in to_block_deps ] if not block: break for migration in block: ready.remove(migration) if safety(migration) == Safe.before_deploy: blocked.append(migration) else: delayed.append(migration) # Order the migrations in the order of the original plan. delayed = [m for m in migrations if m in delayed] blocked = [m for m in migrations if m in blocked] # Display delayed migrations if they exist: if delayed: self.stdout.write(self.style.MIGRATE_HEADING("Delayed migrations:")) for migration in delayed: self.stdout.write(f" {migration.app_label}.{migration.name}") # Display blocked migrations if they exist. if blocked: self.stdout.write(self.style.MIGRATE_HEADING("Blocked migrations:")) for migration in blocked: self.stdout.write(f" {migration.app_label}.{migration.name}") if blocked and strict: raise CommandError("Aborting due to blocked migrations.") # Swap out the items in the plan with the safe migrations. # None are backward, so we can always set backward to False. plan[:] = [(migration, False) for migration in ready] PK!&//(django_safemigrate-2.0.dist-info/LICENSEMIT License Copyright (c) 2018 AspirEDU, Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!HnHTU&django_safemigrate-2.0.dist-info/WHEEL A н#Z;/"d&F[xzw@Zpy3Fv]\fi4WZ^EgM_-]#0(q7PK!H%aDz)django_safemigrate-2.0.dist-info/METADATAWn7}W)Z$r"8'Q1by( i[+YE?gHу3g̜(B 8W5 eA{5ZO $v˶%vTsZ#FlrDcHTv^ ⵭ߨ l;sqs2 g'W uCv|iD.,T+] ë_or[w'߿c#=?|==_T{= Or8o.OQ8;{qYDSL)h'O]gO|Uڇ./CIv@ϝn k_6 n!GV'[,2]O2_j i;hL3?)0(72'2&ÞxPU8uG"*hZG\@VQb ʱ XP9<_f\|0\¶ϒ^'BjD216Y9Eʪ+.n AOr%YR J4G2LzR7Dtؐ`/|$؂]1}S* 2\vZX 1T$6tby ~dHr:ڸ~'?P*c]M^{CZ!zlMЦ<tꦢ^Jm!h]QE 7%K8JtfWm5sƐ"C.$԰ T0o‹푌C8ytDҦ}QTU-\04 7/V`!H-rN(FKtVKdeXg6Wrf&e"*aZƘgw} ^[rSU[ kHu09c.2S2_Qyɑ(r틳T{J|"őoJE:BVR\5t5΃<*}Uq]2OIPð,ULj44~_+$@g0SHTl~6|ku2|뽟ViteTlK (\E _Fm);+GHqH `l2*}NMrvTنr9!"WNq1KmÌhik SԨNg\,Ah\ag&蚺9l?}"mחu"؝ݍkKszƳÂ4NMc]YI&"2RF-too^~7/ܙ`[N{k{wCٚbc>/c fkzdˎt:KF+k6Vyߝ[pPK!H l:''django_safemigrate-2.0.dist-info/RECORDˎ@}? ,@DkDaC{A~zcb&zyr?9q &z<&A@j2!,/YUjT~9"W@7Ã˾~(O$on`=a8)vll  :L"JpJ@C[Hbc:pD:UV~'I=R7|TSp 4PK!zzdjango_safemigrate/__init__.pyPK!mdjango_safemigrate/apps.pyPK!) django_safemigrate/management/__init__.pyPK!2Rdjango_safemigrate/management/commands/__init__.pyPK! ^EE5django_safemigrate/management/commands/safemigrate.pyPK!&//(:django_safemigrate-2.0.dist-info/LICENSEPK!HnHTU&django_safemigrate-2.0.dist-info/WHEELPK!H%aDz)Gdjango_safemigrate-2.0.dist-info/METADATAPK!H l:'' django_safemigrate-2.0.dist-info/RECORDPK "