diff --git a/spug_api/apps/account/management/commands/update.py b/spug_api/apps/account/management/commands/update.py index ce7c226..4527d0b 100644 --- a/spug_api/apps/account/management/commands/update.py +++ b/spug_api/apps/account/management/commands/update.py @@ -60,7 +60,8 @@ class Command(BaseCommand): commands = [ f'cd {settings.BASE_DIR}', f'python3 ./manage.py makemigrations ' + ' '.join(apps), - f'python3 ./manage.py migrate' + f'python3 ./manage.py migrate', + f'python3 ./tools/migrate.py {version}' ] task = subprocess.Popen(' && '.join(commands), shell=True) if task.wait() != 0: diff --git a/spug_api/apps/app/views.py b/spug_api/apps/app/views.py index e60cef5..141400b 100644 --- a/spug_api/apps/app/views.py +++ b/spug_api/apps/app/views.py @@ -12,13 +12,6 @@ import subprocess import json import os -# v2.3.14 临时数据初始化 -ap = App.objects.first() -if ap and hasattr(ap, 'sort_id') and ap.sort_id == 0: - for ap in App.objects.all(): - ap.sort_id = ap.id - ap.save() - class AppView(View): def get(self, request): diff --git a/spug_api/tools/migrate.py b/spug_api/tools/migrate.py new file mode 100644 index 0000000..2c208a9 --- /dev/null +++ b/spug_api/tools/migrate.py @@ -0,0 +1,42 @@ +import django +import sys +import os + +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +sys.path.append(BASE_DIR) + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "spug.settings") +django.setup() + +from django.conf import settings +from apps.app.models import App +import sys + + +class Version: + def __init__(self, version): + self.version = version.lstrip('vV').split('.') + + def __gt__(self, other): + if not isinstance(other, Version): + raise TypeError('required type Version') + for v1, v2 in zip(self.version, other.version): + if int(v1) == int(v2): + continue + elif int(v1) > int(v2): + return True + else: + return False + return False + + +if __name__ == '__main__': + old_version = Version(sys.argv[1]) + now_version = Version(settings.SPUG_VERSION) + if old_version < Version('v2.3.14'): + app = App.objects.first() + if app and hasattr(app, 'sort_id') and app.sort_id == 0: + print('执行v2.3.14数据初始化') + for app in App.objects.all(): + app.sort_id = app.id + app.save()