16.3. 分發設計變更內容

在您的開發主機上面裏的dev5資料庫設計、更改、與測試應用系統後,接下來的工作就是分發變更內容到下列兩類資料庫:

分發過程必須遵循下列步驟進行:

  1. 鎖住app5資料庫。

    鎖住期間,不允許任何客戶承租Section 16.2.4應用系統編號5』。

    開發者應執行這道指令,以鎖住app5資料庫:

    wget -q --auth-no-challenge --http-user=john@example.biz --http-password=MyYyPPassSwwordD -O - https://www.terarows.com/pdbs/lock/5
    				

    curl -u john@example.biz:MyYyPPassSwwordD https://www.terarows.com/pdbs/lock/5
    				

    上述指令,如果執行成功,則回傳Y;如果執行失敗,則回傳N

  2. app5資料庫以及每一個承租應用系統編號5』的客戶使用的資料庫上面執行您的分發腳本。

    開發者應執行這道指令,以下載你的app5以及您全部客戶的主機IP地址以及資料庫名:

    wget -q --auth-no-challenge --http-user=john@example.biz --http-password=MyYyPPassSwwordD -O /tmp/hosts.txt https://www.terarows.com/pdbs/hosts/5
    				

    curl -u john@example.biz:MyYyPPassSwwordD -o /tmp/hosts.txt https://www.terarows.com/pdbs/hosts/5
    				

    上述指令,如果執行成功,/tmp/hosts.txt有資料;如果執行失敗,則沒有/tmp/hosts.txt這個檔案或該檔案無內容。

    假設/tmp/hosts.txt含有下面三個目標資料庫:

    192.168.1.200	app5
    192.168.2.15	udb456
    10.0.2.2	udb789
    				
  3. 解鎖app5

    解鎖之後,重新開放客戶承租Section 16.2.4應用系統編號5』。

    開發者應執行這道指令,以解鎖app5資料庫:

    wget -q --auth-no-challenge --http-user=john@example.biz --http-password=MyYyPPassSwwordD -O - https://www.terarows.com/pdbs/unlock/5
    				

    curl -u john@example.biz:MyYyPPassSwwordD https://www.terarows.com/pdbs/unlock/5
    				

    上述指令,如果執行成功,則回傳Y;如果執行失敗,則回傳N

上述john@example.biz是您的電子郵件地址,MyYyPPassSwwordD是您在的登入密碼。請以真實資料替換!

下面是完整的分發腳本例子。

在您當地機器裏準備/tmp/patch-db.sql檔案。它存放全部PostgreSQL指令,將在每一個目標資料庫上面執行:

BEGIN;

CREATE TABLE x_y_ (
	c1 TEXT PRIMARY KEY,
	c2 INTEGER;
);

COPY x_y_ FROM stdin;
AA	1
BB	2
\.

CREATE FUNCTION my_func(param TEXT) AS $$
BEGIN
	INSERT INTO x_y_ VALUES (param,3);
END $$ LANGUAGE PLPGSQL VOLATILE;

SELECT my_func('CC');

DROP TABLE x_y_;

DROP FUNCTION my_func();

COMMIT;
		

在您當地機器裏準備/tmp/patch-db.sh這個檔案。它存放下面內容:

#!/bin/bash

APPLICATION=5
SQL_FILE=/tmp/patch-db.sql
HOST_DB_FILE=/tmp/hosts.txt
USER_PASSWORD=john@example.biz:MyYyPPassSwwordD
URL_PREFIX=https://www.terarows.com/pdbs

#Lock template database.
RESULT=`curl -s -u $USER_PASSWORD $URL_PREFIX/lock/$APPLICATION`
if [ "$RESULT" != "Y" ]; then
	echo "Failed to lock template database app${APPLICATION}."
	exit 1
fi

function unlock_template_database {
	RESULT=`curl -s -u $USER_PASSWORD $URL_PREFIX/unlock/$APPLICATION`
	if [ "$RESULT" != "Y" ]; then
		echo "Failed to unlock template database. Customers can't subscribe application# $APPLICATION now!"
	fi
}

#Download list of target hosts and databases.
rm -f $HOST_DB_FILE
curl -s -u $USER_PASSWORD -o $HOST_DB_FILE $URL_PREFIX/hosts/$APPLICATION
if [ ! -f "$HOST_DB_FILE" ]; then
	unlock_template_database
	echo "Failed to download list of target hosts and databases."
	exit 1
fi

#Apply this patch on all target databases in all target servers.
while read -a fields; do
	echo "===${fields[0]}	${fields[1]}==="
	cat $SQL_FILE |
		ssh dba@${fields[0]} "psql -U postgres --set AUTOCOMMIT=off --set ON_ERROR_STOP=on ${fields[1]}"
	if [ "$?" -ne 0 ]; then
		unlock_template_database
		exit 1
	fi
done < $HOST_DB_FILE

unlock_template_database
exit 0
		
		

在您當地機器裏下chmod u+x /tmp/patch-db.sh這個指令,把該檔案變成可執行檔。然後下/tmp/patch-db.sh這個指令,執行這個模擬版的「分發設計變更內容」工作。