From 7d6cdefae63211189ee6ce34f7340142dad3d06a Mon Sep 17 00:00:00 2001
From: wang yan <wangyan@vmware.com>
Date: Fri, 8 Mar 2019 19:08:38 +0800
Subject: [PATCH] add script to fix migration issue on notary

The script is to fix the issue #7091, notary upgrade failure from mysql to pqsl.

Signed-off-by: wang yan <wangyan@vmware.com>
---
 tools/notary-migration-fix.sh | 61 +++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 tools/notary-migration-fix.sh

diff --git a/tools/notary-migration-fix.sh b/tools/notary-migration-fix.sh
new file mode 100644
index 000000000..e7614e2a2
--- /dev/null
+++ b/tools/notary-migration-fix.sh
@@ -0,0 +1,61 @@
+#!/bin/bash
+# Copyright Project Harbor Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+set -ex
+
+harbor_db_image=$(docker images goharbor/harbor-db --format "{{.Repository}}:{{.Tag}}")
+harbor_db_path="/data/database"
+
+launch_db() {
+    docker run -d --name fix-notary-migration -v ${harbor_db_path}:/var/lib/postgresql/data ${harbor_db_image} "postgres"
+}
+
+clean_db() {
+    docker stop fix-notary-migration
+    docker rm fix-notary-migration
+}
+
+wait_for_db_ready() {
+    TIMEOUT=12
+    while [ $TIMEOUT -gt 0 ]; do
+        docker exec fix-notary-migration pg_isready | grep "accepting connections"
+        if [ $? -eq 0 ]; then
+                break
+        fi
+        TIMEOUT=$((TIMEOUT - 1))
+        sleep 5
+    done
+    if [ $TIMEOUT -eq 0 ]; then
+        echo "Harbor DB cannot reach within one minute."
+        clean_db
+        exit 1
+    fi
+}
+
+fix_notary_server() {
+    docker exec fix-notary-migration psql -U postgres -d notaryserver -c "delete from schema_migrations where version > 2 and not exists(select column_name from information_schema.columns where table_name = 'schema_migrations' and column_name = 'dirty');"
+}
+
+fix_notary_signer() {
+    docker exec fix-notary-migration psql -U postgres -d notarysigner -c "delete from schema_migrations where version > 1 and not exists(select column_name from information_schema.columns where table_name = 'schema_migrations' and column_name = 'dirty');"
+}
+
+
+launch_db
+wait_for_db_ready
+fix_notary_server
+fix_notary_signer
+clean_db
\ No newline at end of file