Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

ignore conflicst when restoring a deleted project

Tizian Rößlerf334c6efb469
diff --git a/docs/installation-and-operations/operation/restoring/restoring-a-deleted-project/restore.sql b/docs/installation-and-operations/operation/restoring/restoring-a-deleted-project/restore.sql
index d521c99dc13..1cd7e8928ac 100644
--- a/docs/installation-and-operations/operation/restoring/restoring-a-deleted-project/restore.sql
+++ b/docs/installation-and-operations/operation/restoring/restoring-a-deleted-project/restore.sql
@@ -5,6 +5,8 @@ BEGIN;
DO
$$
DECLARE table_to_copy TEXT;
+DECLARE primary_keys TEXT;
+DECLARE insert_sql TEXT;
BEGIN
FOR table_to_copy IN (
select table_name
@@ -16,8 +18,25 @@ FOR table_to_copy IN (
LOOP
raise notice 'Restoring % to %', table_to_copy, REPLACE(table_to_copy, 'missing_', '');
- EXECUTE 'INSERT INTO ' || REPLACE(table_to_copy, 'missing_', '') ||
- ' (SELECT * FROM ' || table_to_copy || ');';
+ -- get primary keys for table
+ SELECT STRING_AGG(a.attname, ',') AS TEXT INTO primary_keys
+ FROM pg_index i
+ JOIN pg_attribute a
+ ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey)
+ WHERE i.indrelid = REPLACE(table_to_copy, 'missing_', '')::regclass
+ AND i.indisprimary;
+
+
+ insert_sql := 'INSERT INTO ' || REPLACE(table_to_copy, 'missing_', '') ||
+ ' (SELECT * FROM ' || table_to_copy || ')';
+
+ -- do nothing on conflict if a primary key exists
+ IF primary_keys IS NOT NULL
+ THEN
+ insert_sql := (insert_sql || ' ON CONFLICT ('|| primary_keys ||') DO NOTHING');
+ END IF;
+
+ EXECUTE (insert_sql || ';');
END LOOP;
END;
$$;