Add a script to reset PostgreSQL sequences.

This commit is contained in:
Andrew Ekstedt 2012-06-01 18:49:45 -07:00
parent 3511bf3076
commit d33ea7b6da

40
bin/reset-postgresql-sequences Executable file
View file

@ -0,0 +1,40 @@
#!/usr/bin/env python2
import sys
from sqlalchemy.sql import func
from sqlalchemy.types import Integer
from pokedex.db import connect
from pokedex.db.tables import mapped_classes
import optparse
parser = optparse.OptionParser()
parser.add_option('-v', '--verbose', action='store_true', default=False,
help="verbose output")
parser.add_option('-e', '--engine', dest='engine_uri', default=None,
help="database engine uri")
options, _ = parser.parse_args(sys.argv[1:])
session = connect(options.engine_uri)
assert session.connection().dialect.name == 'postgresql'
def sequence_columns(table):
for c in table.__table__.primary_key:
if c.autoincrement and not c.foreign_keys and \
isinstance(c.type, Integer):
yield c
for table in sorted(mapped_classes):
table_name = table.__tablename__
for c in sequence_columns(table):
max_value, = session.query(func.max(c)).one()
if options.verbose:
print "%s.%s <- %s" % (table_name, c.name, max_value)
session.execute(func.setval(
func.pg_get_serial_sequence(table_name, c.name),
max_value,
))