From 13378b3baf55b5ff59812e0a0d0deb67468ab579 Mon Sep 17 00:00:00 2001 From: Sagar Date: Thu, 22 Aug 2024 15:28:05 +0200 Subject: [PATCH] feat: a new script is now available to update the auto increment columns --- database/scripts/resetSequenceColumns.sql | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 database/scripts/resetSequenceColumns.sql diff --git a/database/scripts/resetSequenceColumns.sql b/database/scripts/resetSequenceColumns.sql new file mode 100644 index 00000000..447b04c0 --- /dev/null +++ b/database/scripts/resetSequenceColumns.sql @@ -0,0 +1,17 @@ +DO $$ +DECLARE + r RECORD; +BEGIN + FOR r IN + SELECT + table_name, + column_name, + substring(column_default FROM 'nextval\(''(.*)''::regclass\)') AS sequence_name + FROM + information_schema.columns + WHERE + column_default LIKE 'nextval%' + LOOP + EXECUTE 'SELECT setval(''' || r.sequence_name || ''', COALESCE((SELECT MAX(' || r.column_name || ') FROM ' || r.table_name || '), 1));'; + END LOOP; +END $$;