Skip to content

Instantly share code, notes, and snippets.

@checco
Created August 16, 2024 12:46
Show Gist options
  • Save checco/95516f2e134b95f396ab76ed457f496a to your computer and use it in GitHub Desktop.
Save checco/95516f2e134b95f396ab76ed457f496a to your computer and use it in GitHub Desktop.
On Nexus 3.71.0, after the migration from OrientDB to H2 all "auto increment" columns in the H2 database have been reset to start at "1"
#!/bin/bash
NEXUS_BIN=/opt/sonatype/nexus
NEXUS_DATA=/opt/sonatype/sonatype-work/nexus3
h2sql() {
echo "> $@" >&2
java -cp "$NEXUS_BIN"/system/com/h2database/h2/*/h2-*.jar org.h2.tools.Shell -url jdbc:h2:"$NEXUS_DATA"/db/nexus -sql "$@"
}
schema=$(h2sql "SCRIPT NODATA")
echo "$schema" | while read line; do
if [[ $line =~ ^CREATE\ CACHED\ TABLE\ \"PUBLIC\"\.\"([^\"]+)\" ]]; then
tbl="${BASH_REMATCH[1]}"
elif [[ $line =~ ^\"([^\"]+)\"\ .*\ GENERATED\ BY\ DEFAULT ]]; then
col="${BASH_REMATCH[1]}"
h2sql "ALTER TABLE $tbl ALTER COLUMN $col RESTART WITH SELECT max($col) + 1 FROM $tbl"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment