From 29bb735dc4748d4f03e30a4f8776d1063c688e80 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 4 Feb 2021 00:25:00 +1100 Subject: [PATCH] Helper functions to automatically extract migration file info --- InvenTree/InvenTree/helpers.py | 64 ++++++++++++++++++++++++++++ InvenTree/company/test_migrations.py | 27 ++++++++++++ 2 files changed, 91 insertions(+) diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py index 99dc255dac..5cb6cc18dd 100644 --- a/InvenTree/InvenTree/helpers.py +++ b/InvenTree/InvenTree/helpers.py @@ -492,3 +492,67 @@ def addUserPermissions(user, permissions): for permission in permissions: addUserPermission(user, permission) + + +def getMigrationFileNames(app): + """ + Return a list of all migration filenames for provided app + """ + + local_dir = os.path.dirname(os.path.abspath(__file__)) + + migration_dir = os.path.join(local_dir, '..', app, 'migrations') + + files = os.listdir(migration_dir) + + # Regex pattern for migration files + pattern = r"^[\d]+_.*\.py$" + + migration_files = [] + + for f in files: + if re.match(pattern, f): + migration_files.append(f) + + return migration_files + + +def getOldestMigrationFile(app, exclude_extension=True): + """ + Return the filename associated with the oldest migration + """ + + oldest_num = -1 + oldest_file = None + + for f in getMigrationFileNames(app): + num = int(f.split('_')[0]) + + if oldest_file is None or num < oldest_num: + oldest_num = num + oldest_file = f + + if exclude_extension: + oldest_file = oldest_file.replace('.py', '') + + return oldest_file + +def getNewestMigrationFile(app, exclude_extension=True): + """ + Return the filename associated with the newest migration + """ + + newest_file = None + newest_num = -1 + + for f in getMigrationFileNames(app): + num = int(f.split('_')[0]) + + if newest_file is None or num > newest_num: + newest_num = num + newest_file = f + + if exclude_extension: + newest_file = newest_file.replace('.py', '') + + return newest_file diff --git a/InvenTree/company/test_migrations.py b/InvenTree/company/test_migrations.py index 16d9059f23..8ff2cc2a8b 100644 --- a/InvenTree/company/test_migrations.py +++ b/InvenTree/company/test_migrations.py @@ -4,6 +4,33 @@ Tests for the company model database migrations from django_test_migrations.contrib.unittest_case import MigratorTestCase +from InvenTree import helpers + + +class TestForwardMigrations(MigratorTestCase): + + migrate_from = ('company', helpers.getOldestMigrationFile('company')) + migrate_to = ('company', helpers.getNewestMigrationFile('company')) + + def prepare(self): + """ + Create some simple Company data, and ensure that it migrates OK + """ + + Company = self.old_state.apps.get_model('company', 'company') + + Company.objects.create( + name='MSPC', + description='Michael Scotts Paper Company', + is_supplier=True + ) + + def test_migrations(self): + + Company = self.new_state.apps.get_model('company', 'company') + + self.assertEqual(Company.objects.count(), 1) + class TestManufacturerField(MigratorTestCase): """