Created
December 15, 2016 21:58
-
-
Save grncdr/3d1990e891d875347d4ac0708a721386 to your computer and use it in GitHub Desktop.
Found myself needing this today to fix a bunch of broken ID sequences in a Rails DB that it been incorrectly restored from a backup.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO $$ | |
var rows = plv8.execute("select tablename, pg_get_serial_sequence(tablename, 'id') seqname from " + | |
"(select tablename from pg_tables where schemaname = 'public' and tablename != 'schema_migrations') blah" | |
) | |
rows.forEach(function (row) { | |
plv8.execute("select setval('" + row.seqname + "', (select max(id) from " + row.tablename + ")+1, false)") | |
}) | |
$$ LANGUAGE plv8; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment