Skip to content

Instantly share code, notes, and snippets.

@david-caro
Last active October 29, 2017 14:37
Show Gist options
  • Save david-caro/f73300176c774e7e2ae5618773511a30 to your computer and use it in GitHub Desktop.
Save david-caro/f73300176c774e7e2ae5618773511a30 to your computer and use it in GitHub Desktop.
from invenio_db import db
seqs = [
res[0]
for res in db.engine.execute(
"select relname from pg_class where relkind='S';"
)
]
for seq in seqs:
if seq.endswith('put_code_seq'):
id_name = 'put_code'
else:
id_name = seq.rsplit('_', 2)[-2]
tbl_name = seq.rsplit('_' + id_name + '_seq', 1)[0]
seq_cur_value = list(db.engine.execute("select last_value from %s;" % seq))[0][0]
max_value = list(db.engine.execute("select MAX(%s) from %s" % (id_name, tbl_name)))[0][0]
if max_value is not None and max_value > seq_cur_value:
print (
"Fixing mismatched sequence %s, has curval %s but there "
"highest %s is %s"
) % (seq, seq_cur_value, id_name, max_value)
action = "select setval('%s', %s);" % (seq, max_value)
print "running: %s" % action
list(db.engine.execute(action))
elif max_value is not None and max_value != seq_cur_value:
print (
"Something fancy is going on, sequence %s, has curval "
"%s but there highest %s is %s"
) % (seq, seq_cur_value, id_name, max_value)
else:
print (
"Skipping coherent sequence %s, has curval %s, and the "
"highest %s is %s"
) % (seq, seq_cur_value, id_name, max_value)
# fix the pidstore_pid sequence from the production_records (the safest place we have to get a recid from)
pidstore_recid_cur_seq = list(
db.engine.execute("select last_value from pidstore_recid_recid_seq;")
)[0]
max_recid = list(db.engine.execute("select MAX(recid) from production_records;"))[0]
if max_recid >= pidstore_recid_cur_seq:
print "Fixing pidstore_recid_recid_seq to the greatest know recid"
list(db.engine.execute("select setval('pidstore_recid', %s)" % (max_recid + 1))
else:
print (
"No need to tweak the pidstore_recid_recid_seq sequence, current value "
"is %s and the max known recid is %s"
) % (pidstore_recid_cur_seq, max_recid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment