Skip to content

Commit

Permalink
ids reservation to prevent conflicts on merging new features
Browse files Browse the repository at this point in the history
  • Loading branch information
enricofer committed Sep 1, 2024
1 parent d6f208c commit aa5be7b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 20 deletions.
4 changes: 2 additions & 2 deletions webapp/djakart/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',),
Expand All @@ -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')
}),
)

Expand Down
32 changes: 17 additions & 15 deletions webapp/djakart/kart_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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):
Expand Down Expand Up @@ -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;"""
Expand All @@ -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)


Original file line number Diff line number Diff line change
@@ -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]),
),
]
6 changes: 3 additions & 3 deletions webapp/djakart/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit aa5be7b

Please sign in to comment.