From a79c2cb7a8a839d78afc983cb25897d021910129 Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Thu, 18 Jan 2018 09:27:46 -0800 Subject: [PATCH 1/4] draft tweek on seq handling --- scripts-available/CDB_CartodbfyTable.sql | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/scripts-available/CDB_CartodbfyTable.sql b/scripts-available/CDB_CartodbfyTable.sql index 9ccf47c..54e64f2 100644 --- a/scripts-available/CDB_CartodbfyTable.sql +++ b/scripts-available/CDB_CartodbfyTable.sql @@ -791,6 +791,8 @@ DECLARE relname TEXT; relschema TEXT; + relseq TEXT; + relseqtmp TEXT; destoid REGCLASS; destname TEXT; @@ -897,12 +899,23 @@ BEGIN END IF; -- We must rewrite, so here we go... + + -- Our desired PK sequence name + destseq := Format('%s_%s_seq', relname, const.pkey); + -- We are going to drop the source table when we're done anyways + -- but it's possible the source PK sequence is living in a name we would like to use + -- so we check to see if that's the case, and rename it out of the way + SELECT pg_catalog.pg_get_serial_sequence(Format('%I.%I', relschema, relname), const.pkey) + INTO relseq; + IF relseq IS NOT NULL AND relseq = Format('%I.%I', destschema, destseq) THEN + PERFORM _CDB_SQL(Format('ALTER SEQUENCE %s RENAME TO %s_tmp', relseq, destseq), '_CDB_Rewrite_Table'); + END IF; -- Put the primary key sequence in the right schema -- If the new table is not moving, better ensure the sequence name -- is unique - destseq := cartodb._CDB_Unique_Identifier(NULL, relname, '_' || const.pkey || '_seq', destschema); + destseq := cartodb._CDB_Unique_Identifier(NULL, destseq, destschema); destseq := Format('%I.%I', destschema, destseq); PERFORM _CDB_SQL(Format('CREATE SEQUENCE %s', destseq), '_CDB_Rewrite_Table'); From e4b0e7ea7abba795f199bd6e8caa1c5c1dc6d928 Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Thu, 18 Jan 2018 10:06:38 -0800 Subject: [PATCH 2/4] Make user final seq has the right desired name --- scripts-available/CDB_CartodbfyTable.sql | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts-available/CDB_CartodbfyTable.sql b/scripts-available/CDB_CartodbfyTable.sql index 54e64f2..a4d6aae 100644 --- a/scripts-available/CDB_CartodbfyTable.sql +++ b/scripts-available/CDB_CartodbfyTable.sql @@ -792,7 +792,6 @@ DECLARE relname TEXT; relschema TEXT; relseq TEXT; - relseqtmp TEXT; destoid REGCLASS; destname TEXT; @@ -915,7 +914,7 @@ BEGIN -- Put the primary key sequence in the right schema -- If the new table is not moving, better ensure the sequence name -- is unique - destseq := cartodb._CDB_Unique_Identifier(NULL, destseq, destschema); + destseq := cartodb._CDB_Unique_Identifier(NULL, relname, '_' || const.pkey || '_seq', destschema); destseq := Format('%I.%I', destschema, destseq); PERFORM _CDB_SQL(Format('CREATE SEQUENCE %s', destseq), '_CDB_Rewrite_Table'); From 405055580130cafe731f257e58df8f8308a4d635 Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Thu, 18 Jan 2018 10:26:01 -0800 Subject: [PATCH 3/4] Quiet errors where there's no existing sequence --- scripts-available/CDB_CartodbfyTable.sql | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts-available/CDB_CartodbfyTable.sql b/scripts-available/CDB_CartodbfyTable.sql index a4d6aae..a4e0d31 100644 --- a/scripts-available/CDB_CartodbfyTable.sql +++ b/scripts-available/CDB_CartodbfyTable.sql @@ -900,15 +900,19 @@ BEGIN -- We must rewrite, so here we go... -- Our desired PK sequence name - destseq := Format('%s_%s_seq', relname, const.pkey); -- We are going to drop the source table when we're done anyways -- but it's possible the source PK sequence is living in a name we would like to use -- so we check to see if that's the case, and rename it out of the way - SELECT pg_catalog.pg_get_serial_sequence(Format('%I.%I', relschema, relname), const.pkey) - INTO relseq; - IF relseq IS NOT NULL AND relseq = Format('%I.%I', destschema, destseq) THEN - PERFORM _CDB_SQL(Format('ALTER SEQUENCE %s RENAME TO %s_tmp', relseq, destseq), '_CDB_Rewrite_Table'); + IF has_usable_primary_key AND has_usable_pk_sequence THEN + -- See if the existing sequence is squatting on our preferred name + destseq := Format('%s_%s_seq', relname, const.pkey); + SELECT pg_catalog.pg_get_serial_sequence(Format('%I.%I', relschema, relname), const.pkey) + INTO relseq; + -- If it's the name we want, then rename it + IF relseq IS NOT NULL AND relseq = Format('%I.%I', destschema, destseq) THEN + PERFORM _CDB_SQL(Format('ALTER SEQUENCE %s RENAME TO %s_tmp', relseq, destseq), '_CDB_Rewrite_Table'); + END IF; END IF; -- Put the primary key sequence in the right schema From 2c6273030117bb27d8c626c487bf742fad231eca Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Fri, 9 Mar 2018 10:00:10 -0800 Subject: [PATCH 4/4] News entry for this fix --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index 961e9d2..a22986a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,6 @@ +0.22.0 (yyyy-mm-dd) +* Fix: allow older ogr2ogr to work in -append mode (#319) + 0.21.0 (2018-02-15) * Add optional parameter to limit the number of cells in grid-generation functions #322 * Fix: grant usage on cartodb_id sequence when sharing read write #323