From aa5be7b95fcb7d31aca71eede9126ee89f1bdfad Mon Sep 17 00:00:00 2001 From: enrico ferreguti Date: Sun, 1 Sep 2024 17:12:38 +0200 Subject: [PATCH] ids reservation to prevent conflicts on merging new features --- webapp/djakart/admin.py | 4 +-- webapp/djakart/kart_api.py | 32 ++++++++++--------- ...reserved_ids_alter_version_crs_and_more.py | 28 ++++++++++++++++ webapp/djakart/models.py | 6 ++-- 4 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 webapp/djakart/migrations/0006_version_reserved_ids_alter_version_crs_and_more.py diff --git a/webapp/djakart/admin.py b/webapp/djakart/admin.py index 71d49d1..80dcabf 100755 --- a/webapp/djakart/admin.py +++ b/webapp/djakart/admin.py @@ -247,7 +247,7 @@ def get_fieldsets(self, request, obj=None): return ( ("intestazione", { 'classes': ('grp-collapse grp-open',), - 'fields': ('nome', ('base','merged','clean',),'template_qgis','note','get_project','mapping_service_url',('referente','riservato'),'mapa',('crs','extent','apply_map_extent')) + 'fields': ('nome', ('base','merged','clean',),'reserved_ids','template_qgis','note','get_project','mapping_service_url',('referente','riservato'),'mapa',('crs','extent','apply_map_extent')) }), ("rapporti", { 'classes': ('grp-collapse grp-open',), @@ -258,7 +258,7 @@ def get_fieldsets(self, request, obj=None): return ( ("intestazione", { 'classes': ('grp-collapse grp-open',), - 'fields': ('nome', 'base','template_qgis','crs','note','referente','riservato') + 'fields': ('nome', 'base','reserved_ids','template_qgis','crs','note','referente','riservato') }), ) diff --git a/webapp/djakart/kart_api.py b/webapp/djakart/kart_api.py index 62b97ac..c41eabb 100755 --- a/webapp/djakart/kart_api.py +++ b/webapp/djakart/kart_api.py @@ -240,7 +240,7 @@ def get_config(v,key): return res -def crea_nuova_versione(nuova_versione,base,tipo="pg"): +def crea_nuova_versione(nuova_versione,base,tipo="pg",riserva_id=100): nuova_versione_path = os.path.join(settings.KART_REPO,nuova_versione) master_path = os.path.join(settings.KART_REPO,base) if tipo == 'pg': @@ -258,7 +258,7 @@ def crea_nuova_versione(nuova_versione,base,tipo="pg"): new_wc = create_workingcopy(nuova_versione,uri,force=True) grant_select_schema(nuova_versione) #crea_fdw(nuova_versione) - serial_pk_setup(nuova_versione) + serial_pk_setup(nuova_versione, aumento=riserva_id) def get_pg_uri(v): @@ -603,6 +603,13 @@ def recover_uncommitted_nulls(versione): with conn.cursor() as cursor: cursor.execute(sql) +def get_schema(schema): + sql = """ SELECT nspname FROM pg_catalog.pg_namespace WHERE nspname like '{schema}%';""".format(schema=schema) + with get_pg_versions_connection() as conn: + with conn.cursor() as cursor: + cursor.execute(sql) + return cursor.fetchone()[0] + def get_schemas(): sql = """ SELECT nspname FROM pg_catalog.pg_namespace;""" @@ -626,23 +633,18 @@ def serial_pk_setup(versione, aumento=100): base_path = get_remote(versione) base = os.path.split(base_path)[-1] if base: - schemas = get_schemas() - #if not base in schemas: - # base = base + "_pub" - # if not base in schemas: - # raise KartException - sequences = get_sequences(base.replace("_pub","")) + base_schema = get_schema(base) + sequences = get_sequences(base_schema) for tab in list_versioned_tables(base): - if not (tab + "_auto_pk_seq") in sequences: + if not (tab + "_fid_seq") in sequences: continue - #sql = """SELECT MAX(auto_pk) from "{schema}"."{table}";""".format(schema=versione,table=tab) - sql = """SELECT last_value FROM "{schema}"."{table}_auto_pk_seq";""".format(schema=base,table=tab) + sql = """SELECT last_value FROM "{schema}"."{table}_fid_seq";""".format(schema=base_schema, table=tab) cursor.execute(sql) - min_pk = cursor.fetchone()[0] - min_pk += 100 - sql = """ALTER SEQUENCE "{schema}"."{table}_auto_pk_seq" RESTART WITH {val}""".format(schema=versione, table=tab, val=min_pk) + base_min_pk = cursor.fetchone()[0] + min_pk = base_min_pk + aumento + sql = """ALTER SEQUENCE "{schema}"."{table}_fid_seq" RESTART WITH {val}""".format(schema=versione, table=tab, val=min_pk) cursor.execute(sql) - sql = """ALTER SEQUENCE "{schema}"."{table}_auto_pk_seq" RESTART WITH {val}""".format(schema=base, table=tab, val=min_pk) + sql = """ALTER SEQUENCE "{schema}"."{table}_fid_seq" RESTART WITH {val}""".format(schema=base_schema, table=tab, val=min_pk) cursor.execute(sql) diff --git a/webapp/djakart/migrations/0006_version_reserved_ids_alter_version_crs_and_more.py b/webapp/djakart/migrations/0006_version_reserved_ids_alter_version_crs_and_more.py new file mode 100644 index 0000000..ea070fe --- /dev/null +++ b/webapp/djakart/migrations/0006_version_reserved_ids_alter_version_crs_and_more.py @@ -0,0 +1,28 @@ +# Generated by Django 5.0.6 on 2024-09-01 13:47 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('djakart', '0005_version_crs_alter_basemap_request_params_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='version', + name='reserved_ids', + field=models.IntegerField(default=100), + ), + migrations.AlterField( + model_name='version', + name='crs', + field=models.CharField(default='EPSG:3003', max_length=20, verbose_name='Coordinate system epsg code'), + ), + migrations.AlterField( + model_name='version', + name='extent', + field=models.JSONField(default=[99999999, 99999999, -999999, -9999999]), + ), + ] diff --git a/webapp/djakart/models.py b/webapp/djakart/models.py index f23a677..38c0e2f 100755 --- a/webapp/djakart/models.py +++ b/webapp/djakart/models.py @@ -491,13 +491,13 @@ def importa(self, dspath): def save(self, *args, **kwargs): self.nome = slugify(self.nome).upper() if not self.pk is None: - kwargs["update_fields"] = ['note','template_qgis','referente','riservato','extent'] + kwargs["update_fields"] = ['note','template_qgis','referente','riservato','extent','reserved_ids'] else: if self.riservato and not self.referente: self.riservato = False if self.base: - crea_nuova_versione(self.nome,self.base.nome) - #crea progetto + self.reserved_ids = self.base.reserved_ids + crea_nuova_versione(self.nome,self.base.nome,riserva_id=self.reserved_ids) self.progetto = get_qgs_filename(self.nome) self.extent = self.base.extent else: