mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Helper functions to automatically extract migration file info
This commit is contained in:
parent
5c8e65c285
commit
29bb735dc4
@ -492,3 +492,67 @@ def addUserPermissions(user, permissions):
|
|||||||
|
|
||||||
for permission in permissions:
|
for permission in permissions:
|
||||||
addUserPermission(user, permission)
|
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
|
||||||
|
@ -4,6 +4,33 @@ Tests for the company model database migrations
|
|||||||
|
|
||||||
from django_test_migrations.contrib.unittest_case import MigratorTestCase
|
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):
|
class TestManufacturerField(MigratorTestCase):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user