WIP Prepare postgres for migration by replication
This commit is contained in:
35
site-cookbooks/kosmos_postgresql/files/fix_sequences.sh
Normal file
35
site-cookbooks/kosmos_postgresql/files/fix_sequences.sh
Normal file
@@ -0,0 +1,35 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
DB="$1"
|
||||
|
||||
if [ -z "$DB" ]; then
|
||||
echo "Usage: $0 <database>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "== Fixing sequences in database: $DB =="
|
||||
|
||||
SQL=$(psql -d "$DB" -Atqc "
|
||||
SELECT
|
||||
'SELECT setval(' ||
|
||||
quote_literal(pg_get_serial_sequence(quote_ident(n.nspname)||'.'||quote_ident(c.relname), a.attname)) ||
|
||||
', COALESCE(MAX(' || quote_ident(a.attname) || '), 0) + 1, false) FROM ' ||
|
||||
quote_ident(n.nspname)||'.'||quote_ident(c.relname) || ';'
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON n.oid = c.relnamespace
|
||||
JOIN pg_attribute a ON a.attrelid = c.oid
|
||||
WHERE c.relkind = 'r'
|
||||
AND a.attnum > 0
|
||||
AND NOT a.attisdropped
|
||||
AND pg_get_serial_sequence(quote_ident(n.nspname)||'.'||quote_ident(c.relname), a.attname) IS NOT NULL;
|
||||
")
|
||||
|
||||
if [ -z "$SQL" ]; then
|
||||
echo "No sequences found in $DB"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "$SQL" | psql -d "$DB"
|
||||
|
||||
echo "== Done =="
|
||||
Reference in New Issue
Block a user