#!/bin/sh [ -z "$1" -o ! -f "$1" ] && echo "No schema given or schema does not exist." && exit 1 schema="$1" for s in $(sed -n 's/^CREATE SEQUENCE \(.\+\);/\1/ p' < $schema); do for c in $(grep $s $schema | grep nextval | awk '{print $1}'); do # echo "SELECT MAX($c) FROM ${s#_seq};" # echo "SELECT nextval('$s');" echo "ALTER SEQUENCE $s MINVALUE 1;" echo "SELECT setval('$s', (SELECT MAX($c) FROM ${s%_seq})+1, false);" done done