From 8d4d1582a3ac5b56691f1b75cd1a57d28e02e15e Mon Sep 17 00:00:00 2001 From: wangyan Date: Sun, 10 Jun 2018 20:15:55 -0700 Subject: [PATCH] add checking for clair DB migration --- tools/migration/db/util/mysql_pgsql_1_5_0.sh | 35 ++++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/tools/migration/db/util/mysql_pgsql_1_5_0.sh b/tools/migration/db/util/mysql_pgsql_1_5_0.sh index efff377e7..5fecdca0a 100644 --- a/tools/migration/db/util/mysql_pgsql_1_5_0.sh +++ b/tools/migration/db/util/mysql_pgsql_1_5_0.sh @@ -83,11 +83,34 @@ EOF function up_clair { # clair DB info: user: 'postgres' database: 'postgres' - pg_dump -U postgres postgres > /harbor-migration/db/schema/clair.pgsql - stop_pgsql postgres "/clair-db" - # it's harbor DB on pgsql. - launch_pgsql $1 - psql -U $1 -f /harbor-migration/db/schema/clair.pgsql - stop_pgsql $1 + set +e + if [[ $(psql -U $1 -d postgres -t -c "select count(*) from vulnerability;") -eq 0 ]]; then + echo "no vulnerability data needs to be updated." + return 0 + else + + ## it's not a clean clair db, so cannot execute the import step. + ## fail at here to call user to clean DB, then to run clair db migration. + if [[ $(psql -U $1 -d postgres -t -c "select count(*) from pg_tables where schemaname='public';") -ne 0 ]]; then + cat >&2 <<-EOF + ******************************************************************************* + WARNING: Clair migration will only allow anyone haven't migrated clair or + launched harbor yet. + If you want to migrate clair data, please delete all the clair DB tables + in pgsql manually fistly. + ******************************************************************************* +EOF + exit 0 + fi + + set -e + pg_dump -U postgres postgres > /harbor-migration/db/schema/clair.pgsql + stop_pgsql postgres "/clair-db" + + # it's harbor DB on pgsql. + launch_pgsql $1 + psql -U $1 -f /harbor-migration/db/schema/clair.pgsql + stop_pgsql $1 + fi } \ No newline at end of file