@@ -240,7 +240,7 @@ def get_config(v,key):
240240 return res
241241
242242
243- def crea_nuova_versione (nuova_versione ,base ,tipo = "pg" ):
243+ def crea_nuova_versione (nuova_versione ,base ,tipo = "pg" , riserva_id = 100 ):
244244 nuova_versione_path = os .path .join (settings .KART_REPO ,nuova_versione )
245245 master_path = os .path .join (settings .KART_REPO ,base )
246246 if tipo == 'pg' :
@@ -258,7 +258,7 @@ def crea_nuova_versione(nuova_versione,base,tipo="pg"):
258258 new_wc = create_workingcopy (nuova_versione ,uri ,force = True )
259259 grant_select_schema (nuova_versione )
260260 #crea_fdw(nuova_versione)
261- serial_pk_setup (nuova_versione )
261+ serial_pk_setup (nuova_versione , aumento = riserva_id )
262262
263263
264264def get_pg_uri (v ):
@@ -603,6 +603,13 @@ def recover_uncommitted_nulls(versione):
603603 with conn .cursor () as cursor :
604604 cursor .execute (sql )
605605
606+ def get_schema (schema ):
607+ sql = """ SELECT nspname FROM pg_catalog.pg_namespace WHERE nspname like '{schema}%';""" .format (schema = schema )
608+ with get_pg_versions_connection () as conn :
609+ with conn .cursor () as cursor :
610+ cursor .execute (sql )
611+ return cursor .fetchone ()[0 ]
612+
606613
607614def get_schemas ():
608615 sql = """ SELECT nspname FROM pg_catalog.pg_namespace;"""
@@ -626,23 +633,18 @@ def serial_pk_setup(versione, aumento=100):
626633 base_path = get_remote (versione )
627634 base = os .path .split (base_path )[- 1 ]
628635 if base :
629- schemas = get_schemas ()
630- #if not base in schemas:
631- # base = base + "_pub"
632- # if not base in schemas:
633- # raise KartException
634- sequences = get_sequences (base .replace ("_pub" ,"" ))
636+ base_schema = get_schema (base )
637+ sequences = get_sequences (base_schema )
635638 for tab in list_versioned_tables (base ):
636- if not (tab + "_auto_pk_seq " ) in sequences :
639+ if not (tab + "_fid_seq " ) in sequences :
637640 continue
638- #sql = """SELECT MAX(auto_pk) from "{schema}"."{table}";""".format(schema=versione,table=tab)
639- sql = """SELECT last_value FROM "{schema}"."{table}_auto_pk_seq";""" .format (schema = base ,table = tab )
641+ sql = """SELECT last_value FROM "{schema}"."{table}_fid_seq";""" .format (schema = base_schema , table = tab )
640642 cursor .execute (sql )
641- min_pk = cursor .fetchone ()[0 ]
642- min_pk += 100
643- sql = """ALTER SEQUENCE "{schema}"."{table}_auto_pk_seq " RESTART WITH {val}""" .format (schema = versione , table = tab , val = min_pk )
643+ base_min_pk = cursor .fetchone ()[0 ]
644+ min_pk = base_min_pk + aumento
645+ sql = """ALTER SEQUENCE "{schema}"."{table}_fid_seq " RESTART WITH {val}""" .format (schema = versione , table = tab , val = min_pk )
644646 cursor .execute (sql )
645- sql = """ALTER SEQUENCE "{schema}"."{table}_auto_pk_seq " RESTART WITH {val}""" .format (schema = base , table = tab , val = min_pk )
647+ sql = """ALTER SEQUENCE "{schema}"."{table}_fid_seq " RESTART WITH {val}""" .format (schema = base_schema , table = tab , val = min_pk )
646648 cursor .execute (sql )
647649
648650
0 commit comments