diff options
| author | Étienne Loks <etienne.loks@iggdrasil.net> | 2026-02-25 11:05:41 +0100 |
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2026-02-25 12:31:27 +0100 |
| commit | 15619786d41df09e9701d23842724ac0a30fd85d (patch) | |
| tree | 9c139d65a2fc0b750542f23642d214f83a4bf310 /ishtar_common/management | |
| parent | 53b408380649f39880763b35a2a9d24eb7cc014a (diff) | |
| download | Ishtar-15619786d41df09e9701d23842724ac0a30fd85d.tar.bz2 Ishtar-15619786d41df09e9701d23842724ac0a30fd85d.zip | |
🗃️ refactoring migrations
Diffstat (limited to 'ishtar_common/management')
| -rw-r--r-- | ishtar_common/management/commands/process_initialize_item_keys.py | 39 |
1 files changed, 28 insertions, 11 deletions
diff --git a/ishtar_common/management/commands/process_initialize_item_keys.py b/ishtar_common/management/commands/process_initialize_item_keys.py index e493066f1..000a526d9 100644 --- a/ishtar_common/management/commands/process_initialize_item_keys.py +++ b/ishtar_common/management/commands/process_initialize_item_keys.py @@ -1,27 +1,31 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -import datetime import sys from django.apps import apps +from django.db import connection from django.conf import settings from django.contrib.contenttypes.models import ContentType from django.core.management.base import BaseCommand from django.utils.text import slugify from ishtar_common.models import ItemKey -from ishtar_common.utils import get_log_time, get_percent, get_eta, BColors +from ishtar_common.utils import get_log_time, get_percent, BColors -def write_output(base_lbl, idx, total, ref_time): +def write_output(base_lbl, idx, total): lbl = f"\r{BColors.OKBLUE}[{get_percent(idx, total)}] {base_lbl} {idx + 1}/{total}" - lbl += f" ({get_eta(idx, total, ref_time, datetime.datetime.now())} left)" - lbl += "{BColors.ENDC}" + lbl += f"{BColors.ENDC}" sys.stdout.write(lbl) sys.stdout.flush() +SQL_CHECK_DATABASE_EXISTS = """SELECT 1 FROM information_schema.tables + WHERE table_type='BASE TABLE' AND table_schema='public' + AND table_catalog='{database}' AND table_name='{table_name}';""" + + def migrate_item_key(clean_old=False, quiet=False): """ clean_old=False: set to True to migrate from non unicode to unicode @@ -31,26 +35,39 @@ def migrate_item_key(clean_old=False, quiet=False): "archaeological_finds", "archaeological_operations", "archaeological_warehouse"): app_models = apps.get_app_config(app).get_models() + is_init = False for model in app_models: + if is_init: # no need to initialize + break if any(1 for attr in ("available", "txt_idx", "comment", "available") if not hasattr(model, attr)): continue # not a general type content_type, __ = ContentType.objects.get_or_create( app_label=app, model=model._meta.model_name) - ref_time = datetime.datetime.now() + sql = SQL_CHECK_DATABASE_EXISTS.format( + database=settings.DATABASES["default"]["NAME"], + table_name=model._meta.db_table) + res = None + with connection.cursor() as cursor: + cursor.execute(sql) + res = cursor.fetchall() + if not res: # table is not created yet (database initialisation or migration) + is_init = True + break q = model.objects nb = q.count() - for idx, item in enumerate(q.all()): + for idx, item in enumerate(q.values_list("id", "label")): + pk, label = item if not quiet: - write_output(model._meta.verbose_name, idx, nb, ref_time) + write_output(model._meta.verbose_name, idx, nb) if clean_old: ItemKey.objects.filter( - key=slugify(item.label), + key=slugify(label), content_type=content_type, importer_type=None, ishtar_import=None, user=None, group=None).delete() ItemKey.objects.get_or_create( - key=slugify(item.label, allow_unicode=True), - content_type=content_type, object_id=item.pk, + key=slugify(label, allow_unicode=True), + content_type=content_type, object_id=pk, importer_type=None, ishtar_import=None, user=None, group=None) lbl = f"\r{BColors.OKGREEN}* {model._meta.verbose_name} - OK{SPACE}{BColors.ENDC}\n" |
