Skip to content

Instantly share code, notes, and snippets.

@ichtrojan
Created July 8, 2020 13:31
Show Gist options
  • Save ichtrojan/f9fe2b9e65af75348e7a2e50e096bd52 to your computer and use it in GitHub Desktop.
Save ichtrojan/f9fe2b9e65af75348e7a2e50e096bd52 to your computer and use it in GitHub Desktop.
Script to fix postgres migration issues with Laravel
DO $$
DECLARE
rec RECORD;
LAST_ID integer;
BEGIN
FOR rec IN SELECT
table_name
FROM information_schema.columns WHERE table_schema='public' and column_name='id' and data_type='integer'
LOOP
execute 'SELECT (id + 1) as id FROM ' || rec.table_name || ' ORDER BY id DESC LIMIT 1' into LAST_ID;
execute 'ALTER SEQUENCE public.' || rec.table_name || '_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 START 1 RESTART ' || COALESCE(LAST_ID, 1) || ' CACHE 1 NO CYCLE;';
END LOOP;
END; $$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment