Skip to content

Instantly share code, notes, and snippets.

@grncdr
Created December 15, 2016 21:58
Show Gist options
  • Save grncdr/3d1990e891d875347d4ac0708a721386 to your computer and use it in GitHub Desktop.
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.
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