MS SQL ServerããŒã¿ããŒã¹ã®ããŒãžã§ã³ç®¡çã®ç°¡åãªã¢ãããŒã
ãŸããã
å€ãã®å Žåãã¢ããªã±ãŒã·ã§ã³ã§ã¯ãããŒã¿ã¬ãã«ã ãã§ãªããã¹ãã¢ãããã·ãŒãžã£ãããªã¬ãŒãé¢æ°ãä»ããŠãã®ããŒã¿ãžã®ã¢ã¯ã»ã¹ãæäŸããç¬èªã®APIã®äœæã«åºã¥ããŠãéçºè ã«éåžžã«å€§ããªæ©èœãæäŸããææ°ã®ããŒã¿ããŒã¹ã䜿çšããå¿ èŠããããŸãã æããã«ããã®æ§é å šäœã§ã¯ãç¶æ³ã«å¿ããŠãããã€ãã®å€æŽãå¿ èŠã«ãªãå ŽåããããŸãã ãããŠãæãç¡å®³ãªã±ãŒã¹ã§ã¯ãéçºè ã1ã€ã®ã¯ã©ã€ã¢ã³ããš1ã€ã®å€åããããŒã¿ããŒã¹ïŒããšãã°ãå°èŠæš¡äŒæ¥ïŒãæ±ãå ŽåãæŽæ°ããã»ã¹ã¯ã·ã³ãã«ã«èŠããŸã-æ§é ã«å¿ èŠãªå€æŽãå ããSQL Examinerãæ°æ§ã®ç¹å¥ãªãŠãŒãã£ãªãã£ã®å©ããåããŠæ¯èŒããŸãçæãããsqlã¹ã¯ãªãããããŒãžã§ã³ç®¡çããæ¢åã®ããŒã¿ããŒã¹ã«ããŒã«ããŸãã åè¿°ã®ã±ãŒã¹ã§ãããããã«ãããŒã¿ããŒã¹æ§é ãæŽæ°ããããã³ã«ããŒã¿ç§»è¡ãçºçããŸãã ããããæ®å¿µãªããšã«ãèšèŒãããŠããç¶æ³ã¯éåžžã«ãŸãã§ãããããé »ç¹ã«-顧客ããã³éçºè åãã®è£œåã®å¯Ÿå¿ããããŒã¿ããŒã¹ã¯ãæ°çŸã®ããããã¯ãã以äžã§ã¯ãããŸããã ãããã£ãŠãããŒã¿ããŒã¹ã®éåžžã®ã©ã€ããµã€ã¯ã«ã«ã¯ãããŒãžã§ã³ç®¡çã·ã¹ãã ãå¿ èŠã§ãïŒSubversionãªã©ã®ãœãŒã¹ããŒãžã§ã³ç®¡çã·ã¹ãã ãšæ··åããªãã§ãã ããïŒã
ã¢ãããŒã
ãã®ã·ã¹ãã ã®ç®çãããããç解ããããã«ã次ã®äŸãèããŠã¿ãŸãããïŒç¹å®ã®ãã¹ã¯ãããã¢ããªã±ãŒã·ã§ã³ããããŸã-顧客ã®èŠæ±ã«å¿ããŠããã®ããã°ã©ã ã¯æ¬¡ã®ããŒã¿ãä¿åããå¿ èŠããããŸãïŒãå§ãããåãããããã«ããŒã ãã圹è·ãããã³ãã®ããã°ã©ã ãã€ã³ã¹ããŒã«ãããŸãäŒç€Ÿã®åãªãã£ã¹ã«ã¯ãåžå ã«æ£ãã°ã£ãŠããŸãã åŸè ã®äºå®ã¯ãå°æ¥ããœãããŠã§ã¢ããŒãžã§ã³ã®æŽæ°ãåæçã«è¡ãããªãç¶æ³ãçºçããå¯èœæ§ãããããšã瀺åããŠããŸããããšãã°ãããã°ã©ã ãããŒãžã§ã³10ããªãªãŒã¹ãããªãã£ã¹ã«ãã£ãŠã¯æåã®ããŒãžã§ã³ããŸã ã€ã³ã¹ããŒã«ãããå ŽåããããŸããããããããã«10ã«ã¢ããã°ã¬ãŒãããå Žåããã®æŽæ°ãå®è¡ãã管çè ã¯10åãã¹ãŠã®æŽæ°ãã€ã³ã¹ããŒã«ããå¿ èŠã¯ãããŸãããã10åã®æŽæ°ãäžåºŠã«æäŸããå¿ èŠããããŸããããã«ãããæ§é å šäœãåå¥ã«æŽæ°ãããæ°ããã¢ããªã±ãŒã·ã§ã³ã®ããŒã¿ã移è¡ãããŸãã ãããã£ãŠãããŒã¿ããŒã¹ããŒãžã§ã³ç®¡çã·ã¹ãã ã®äž»ãªèŠä»¶ãçå®ããŸãã
- 移è¡æã«ããŒã¿ãç¶æããå¿ èŠããããŸã
- ããŒãžã§ã³ã«é¢ä¿ãªãããŒã¿ã移è¡ããå¿ èŠããããŸã
- 移è¡ããŒã«ã¯ã§ããã ãã·ã³ãã«ã«ããå¿ èŠããããŸãã
移è¡ã³ãã³ãã®ã¿ã¹ã¯ã®æãç°¡åãªããŒãžã§ã³ã¯ãä»»æã®èšèªã§ç§»è¡ã¹ã¯ãªãããéçºããããšã§ãããã®äŸã§ã¯ãWindowsçšã®æšæºã®batãã¡ã€ã«ã䜿çšãããŸãã
次ã«ããšã³ãã£ãã£ãä¿åããããã®ã«ãŒã«ãäœæããå¿ èŠããããŸãã
- ããŒã¿ããŒã¹ãäœæããããŸãã¯æ¢åã®æ§é ãå€æŽããDMLå®çŸ©ãå«ãã¹ã¯ãªããã¯ãååãalter.sqlã§ããåããã¡ã€ã«ã«ååšããå¿ èŠããããŸãã
- åã¹ãã¢ãããã·ãŒãžã£ã«ã¯ãäœæïŒæåã®ããŒãžã§ã³ïŒãšå€æŽïŒåŸç¶ïŒã®äž¡æ¹ã§ç¬èªã®ãã¡ã€ã«ãå¿ èŠã§ãã
- ããŒã¿ããŒã¹å ã®åãšã³ãã£ãã£ã«å¯ŸããŠãå¯èœãªéã次ã®ã«ãŒã«ã䜿çšã§ããŸãã[ã¢ã¯ã·ã§ã³ãšã³ãã£ãã£]ã«ãŒã«ã«åŸã£ãŠååãä»ããå¿ èŠããããšã³ãã£ãã£ãéžæãè¿œå ãæŽæ°ãåé€ããããã®4ã€ã®ããã·ãŒãžã£ãäœæããŸãã ããšãã°ãæ¬ã®æ¬è³ªãæã£ãŠããå Žå-Bookã次ã«ããã«ã¢ã¯ã»ã¹ããããã«ãSelectBookãInsertBookãUpdateBookãDeleteBookã®4ã€ã®ããã·ãŒãžã£ãäœæããŸãã æ°ããã«ãŒã«ã®å°å ¥ã¯èš±å¯ãããŠããŸãããå¯èœãªéãçµ±äžããå¿ èŠããããŸãã
- åããšã³ãã£ãã£ãæäœããæé ã¯ããšã³ãã£ãã£ã®ååãä»ãã1ã€ã®ãã©ã«ããŒã«ã°ã«ãŒãåããå¿ èŠããããŸãã
- ããŒãžã§ã³ããšã«ãå®è£ ããããŒãžã§ã³ã®çªå·ãæã€ãã©ã«ããŒãäœæããã¹ããã4ã®ãã©ã«ããŒãšãã¹ããã1ã®ããŒã¿ããŒã¹æ§é ãåæåãŸãã¯å€æŽããããã®ã¹ã¯ãªãããå«ããå¿ èŠããããŸãã
äžèšã®ãã¹ãŠã®ã«ãŒã«ã«åŸãããšã§ãå³å¯ãªã«ã¿ãã°åãå®çŸã§ããŸããããã«ãããå¿ èŠãªããŒãžã§ã³ç®¡çãå¯èœã«ãªããŸãã
äŸã«æ»ããŸãããã æåã®ããŒãžã§ã³ã§ã¯ãFirstNameãMiddleNameãLastNameã®3ã€ã®ãã£ãŒã«ããæã€1ã€ã®PersonããŒãã«ããããŸãã ãŸããVersionãã©ã«ããŒãäœæããŸã-ããŒãžã§ã³ãå«ãŸããŸããããŒãžã§ã³1ã«ãªãããã®äžã«Personãã©ã«ããŒãšåæåã¹ã¯ãªãããå«ãŸããŸãã
次ã«ããã©ã°ã©ãïŒ3ïŒã®ã«ãŒã«ã«åŸã£ãŠã4ã€ã®ã¹ãã¢ãããã·ãŒãžã£ãå人ã®ãã©ã«ããŒã«äœæããããããã«SelectPersonãInsertPersonãUpdatePersonãDeletePersonãšããååãä»ããPersonãã©ã«ããŒå ã«é 眮ããŸããååŸïŒCREATE TABLE Person
(
FirstName varchar (50),
MiddleName varchar (50),
LastName varchar (50)
)
* This source code was highlighted with Source Code Highlighter .
åºæ¬å±éã·ã¹ãã ã®äœæ
ãã¹ãŠã®ãã¡ã€ã«ãšãã£ã¬ã¯ããªãç¹å®ã®æ§é ã§åéãããã®ã§ãããŒã¿ããŒã¹ãå±éããã¡ã«ããºã ã®äœæãéå§ã§ããŸãã MS SQLãµãŒããŒã«ã¯ãã³ãã³ãã©ã€ã³ããã¢ã¯ã»ã¹ããããã®ãŠãŒãã£ãªãã£ããããŸããsqlcmd.exeã¯ãsqlã³ãã³ãããã»ããµãšããŠäœ¿çšãããã®ã§ãã å®è¡æã«ã¯ããã®ãŠãŒãã£ãªãã£ãžã®ãã¹ãPathç°å¢å€æ°ã«ç»é²ãããŠãããšæ³å®ãããŸãã ãŸããsqlcmd.exeã«é¢ããããã€ãã®æ å ±ïŒ
- ãã®ãŠãŒãã£ãªãã£ã¯ãSQLã³ãã³ããå«ããã¡ã€ã«ãå®è¡ã§ããŸããããã¯ã-i filenameã¹ã€ããã䜿çšããŠè¡ãããŸã
- ãŸããSQLã³ãŒãã§äœ¿çšã§ããå€æ°ãå®çŸ©ããæ¹æ³ãç¥ã£ãŠããŸããå€æ°ã¯âv param = valueããŒã䜿çšããŠå®çŸ©ãããæ¢ã«ã³ãŒãå ã§$ïŒparamïŒæ§æã䜿çšããŠã¢ã¯ã»ã¹ã§ããå®è¡å¯èœããã»ããµã¯ãããã®ããŒã¯ã³ãå€ã«åçŽã«çœ®ãæããŸã-vãªãã·ã§ã³ã䜿çšããåŒã³åºãã§å®çŸ©ããããã®
- ãã®ãŠãŒãã£ãªãã£ã䜿çšãããšãSQLã¹ã¯ãªããå ã®ä»ã®SQLã¹ã¯ãªãããåŒã³åºãããšãã§ããŸããããã¯ããã£ã¬ã¯ãã£ãã䜿çšããŠè¡ããŸããr filename
瀺ãããæ©èœã¯ãç§ãã¡ã®ç®çã®ããã«æè»ãªã·ã¹ãã ãäœæããã®ã«åœ¹ç«ã¡ãŸãã ãŸããããŒã¿ããŒã¹å±éã¢ã«ãŽãªãºã ãå®çŸ©ããŸãã
- ããŒã¿ããŒã¹ãååšãããã©ãããå€æããååšããªãå Žåã¯ãæ°ããããŒã¿ããŒã¹ãäœæããŸãã
- ããŒã¿ã®ããã¯ã¢ãããäœæããŸãã
- ããŒãžã§ã³ç®¡ççšã®ããŒã¿ããŒã¹ã®åææ§æãäœæãããŠãããã©ããã確èªããäœæãããŠããªãå Žåã¯äœæããŸãã
- çŸåšã®ããŒãžã§ã³ã確èªãã移è¡ã¹ã¯ãªãããåŒã³åºããŸãã
ããŒã¿ããŒã¹ãçŸåšã®ããŒãžã§ã³ãä¿åã§ããããã«ïŒæé 3ã®å ŽåïŒããã®ããããã®ããŒãžã§ã³ãä¿åããããã£ãŒã«ããæã€ããŒãã«ãäœæããå¿ èŠããããŸãã
CREATE TABLE Settings
(
DbVersion int
)
go
INSERT INTO Settings(DbVersion) VALUES (1)
go
* This source code was highlighted with Source Code Highlighter .
ããŒã¿ããŒã¹ã®ããŒãžã§ã³ãå€æŽãããšãäœæãããããŒãã«ã®DbVersionãã£ãŒã«ãã®å¯Ÿå¿ããå€ãå€æŽãããŸãã
次ã«ãå±éã¹ã¯ãªãããåãåããã©ã¡ãŒã¿ãŒã決å®ããŸãã
- ããŒã¿ããŒã¹åã¯å€æ°workdbnameã§ãã
- SQLãµãŒãã¹ã®ã€ã³ã¹ã¿ã³ã¹åã
- ããŒã¿ããŒã¹ãã¡ã€ã«ãžã®ãã¹ã¯ãdatabasepathå€æ°ã§ãã
- ããã¯ã¢ãããã¡ã€ã«ãžã®ãã¹ã¯ãbackuppathå€æ°ã§ãã
æºå段é
ããã§ã¯ãããŒã¹ã®ååšããã§ãã¯ããŠæ°ããããŒã¹ãäœæããããžãã¯ãèšè¿°ããå¿ èŠããããŸãã ãŒãé€ç®ãªã©ã®ã¹ã¯ãªããã®å®è¡äžã«é倧ãªãšã©ãŒãçºçããå Žåãsqlcmd.exeãŠãŒãã£ãªãã£ãæ°Žææ¥ã«1ã®ãšã©ãŒã³ãŒããè¿ãããšãããã«èšåãã䟡å€ããããŸããããã¯ãbatã¹ã¯ãªããã®å®è¡æã«äœ¿çšã§ããŸãã äžèšã®æ€èšŒã®ããã«ãCheckDbExists.sqlãã¡ã€ã«ã«æ¬¡ã®SQLã³ãã³ããäœæããŸãã
use master;
IF NOT EXISTS ( SELECT * FROM sysdatabases WHERE [Name] = $(workdbname)')
BEGIN
SELECT 1/0;
END
* This source code was highlighted with Source Code Highlighter .
次ã«ãdeploy.batãã¡ã€ã«ã§æåã®ãã§ãã¯ãæŽçããŸãã
"sqlcmd.exe" -S %2 -V 1 -v workdbname=%1 -i CheckDbExists.sql
IF ERRORLEVEL 1 GOTO :CREATENEW
ããŒã¿ããŒã¹ãååšããªãå ŽåããŠãŒãã£ãªãã£ã¯å®äºã³ãŒã1ãå®è¡ç°å¢ã«è¿ããããžãã¯ã¯ããŒã¿ããŒã¹ã®ç©ççãªäœæãå®è¡ããŸãã ãããè¡ãã«ã¯ã次ã®å 容ã®sqlã¹ã¯ãªãããäœæããŸãã
use master;
go
CREATE DATABASE $(workdbname) ON PRIMARY
(Name = N '$(workdbname)' , FILENAME = N '$(databasepath)' );
go
use $(workdbname);
go
CREATE TABLE Settings
(
DbVersion int
)
go
INSERT INTO Settings(DbVersion) VALUES (1)
Go
* This source code was highlighted with Source Code Highlighter .
ã芧ã®ãšãããã¹ã¯ãªããã¯sqlcmdãåŒã³åºããããšãã«æž¡ãå¿ èŠããã2ã€ã®å€æ°ã䜿çšããŸãã
"sqlcmd.exe" -S %2 -v workdbname=%1 -v databasepath=%3 -i InitDatabase.sql
ããŒã¿ããŒã¹ã®ååšã確èªããã¹ãããã§ã¯ãããŒã¿ããŒã¹ãæ¢ã«ååšããå¯èœæ§ããããããæŽæ°ã®ããã«ããŒã¿ããŒã¹ãæºåããå¿ èŠããããŸããã€ãŸããããŒã¿ã®ããã¯ã¢ããã匷å¶ããã«ã¯ã次ã®ã¹ã¯ãªãããåŒã³åºããŠãããå®è¡ã§ããŸãã
BACKUP DATABASE $(workdbname) TO DISK = N '$(backuppath)' WITH INIT,
SKIP, NOREWIND, NOUNLOAD
* This source code was highlighted with Source Code Highlighter .
çæãããbatãã¡ã€ã«ããïŒ
"sqlcmd.exe" -S %2 -v workdbname=%1 -v backuppath=%4 -i BackupDatabase.sql
ããã§ãã·ã¹ãã ã§æãéèŠãªããšãã€ãŸãçŸåšã®ããŒãžã§ã³ã確èªãããšããæ¥ãŸãããããã¯ãç¹å®ã®ããŒãžã§ã³ã®ç°¡åãªç¢ºèªã«ãã£ãŠè¡ãããŸãã ã©ãããããããããŒãžã§ã³çªå·ããå€ã®äžçãã«éç¥ããå¿ èŠããããããããã§ããŒãé€ç®ãšã©ãŒãé©çšããCheckVersion.sqlãã¡ã€ã«ã§ãã®ã³ãŒããçºè¡ããŸãã
use $(workdbname);
go
SELECT [DbVersion]/($(checkedVersion) - [DbVersion]) FROM Settings
Go
* This source code was highlighted with Source Code Highlighter .
ãããŠãæåã®ããŒãžã§ã³ã®æ€èšŒã䌎ãåŒã³åºããäœæããŸãã
"sqlcmd.exe" -S %2 -V 1 -v workdbname=%1 -v checkedVersion=1 -i CheckVersion.sql
æåã®åŒã³åºãã§ã¯ãäºæ³ã©ãããŒãé€ç®ãšã©ãŒãçºçããã³ãŒã1ãã©ã³ã¿ã€ã ã«æ»ããŸãããããã£ãŠãããŒã¿ããŒã¹ã®æåã®ããŒãžã§ã³ã®æ§é ãåæåããããã«å¿ èŠãªãã¹ãŠã®å€æŽãè¡ãå¿ èŠãããã¹ã¯ãªãããåŒã³åºãããšãã§ããŸãã
IF ERRORLEVEL 1 "sqlcmd.exe" -S %2 -v workdbname=%1 -i Version\1\alter.sql
åããŒãžã§ã³ã®åŒã³åºãããalter.sqlãã¡ã€ã«ã¯ã次ã®ååã«åŸã£ãŠæ©èœããŸãã
- å€æ°workdbnameã§ååãæž¡ãããããŒã¿ããŒã¹ã®ã³ã³ããã¹ãã確ç«ããå¿ èŠããããŸãã
- ãã£ã¬ã¯ãã£ãã䜿çšããŠåŒã³åºããŸããr sql DMLã³ãŒããå«ããã¹ãŠã®å¿ èŠãªãã¡ã€ã«ã
ãã®äŸã®æåã®ããŒãžã§ã³ã§ã¯ããã®ãã¡ã€ã«ã®ã¹ã¯ãªããã¯æ¬¡ã®è¡ã§æ§æãããæåŸã«å¿ é ã®ããŒãžã§ã³æŽæ°ããããŸãã
use $(workdbname);
go
:r Version\1\script. sql
go
:r Version\1\Person\DeletePerson. sql
go
:r Version\1\Person\InsertPerson. sql
go
:r Version\1\Person\SelectPerson. sql
go
:r Version\1\Person\UpdatePerson. sql
go
UPDATE Settings SET DbVersion = 2
Go
* This source code was highlighted with Source Code Highlighter .
ããã§ãæåã®ããŒãžã§ã³ã§äœæ¥ããããã®ãã¹ãŠã®ã»ããã¢ãããå®äºãããã¡ã€ã«æ§é ã¯ç»åã«ç€ºããã圢åŒããšãã¯ãã§ãã
æ°ããããŒãžã§ã³ãäœæãã
次ã®ãªãªãŒã¹ã®ææãæ¥ãããäœæ¥äžã«èç©ããããã¹ãŠã®å€æŽããã¡ã€ã³ã®ããŒãžã§ã³ãã©ã³ãã®ãµããã©ã«ããŒã«åéããå¿ èŠããããŸãã ããããããããããã«ãããŒã¿ããŒã¹ã®æ¬¡ã®ããŒãžã§ã³ã«æ°ããéœåžã®ããŒãã«ã®å°å ¥ãšã人ãšçãŸããéœåžã®é¢ä¿ã®äœæã®å€æŽãå«ãŸãããšä»®å®ããŸããscript.sqlãšããååã®æ§é 移è¡ã¹ã¯ãªããã¯æ¬¡ã®ããã«ãªããŸãã
CREATE TABLE City
(
CityId int identity NOT NULL ,
Title varchar (255) NOT NULL ,
PRIMARY KEY (CityID)
)
go
ALTER TABLE Person ADD CityId int
go
ALTER TABLE Person ADD FOREIGN KEY (CityID) REFERENCES City (CityID)
go
* This source code was highlighted with Source Code Highlighter .
åæã«ãCityãšã³ãã£ãã£ãæäœããããã®4ã€ã®æé ãäœæããPersonãšã³ãã£ãã£ã®ã¹ãã¬ãŒãžãå€æŽããå¿ èŠããããŸãã 次ã«ãalter.sqlãã¡ã€ã«ã«æŽæ°ã¹ã¯ãªãããèšè¿°ããŸãã
use $(workdbname);
go
:r Version\2\script. sql
go
:r Version\2\Person\InsertPerson. sql
go
:r Version\2\Person\SelectPerson. sql
go
:r Version\2\Person\UpdatePerson. sql
go
:r Version\2\City\SelectCity. sql
go
:r Version\2\City\InsertCity. sql
go
:r Version\2\City\UpdateCity. sql
go
:r Version\2\City\DeleteCity. sql
go
UPDATE Settings SET DbVersion = 3
Go
* This source code was highlighted with Source Code Highlighter .
å®äºããããã¡ã€ã³ã®deploy.batã®ããŒãžã§ã³æ€èšŒã»ã¯ã·ã§ã³ã«æ¬¡ã®è¡ãè¿œå ããŸãã
"sqlcmd.exe" -S %2 -V 1 -v workdbname=%1 -v checkedVersion=2 -i CheckVersion.sql
IF ERRORLEVEL 1 "sqlcmd.exe" -S %2 -v workdbname=%1 -i Version\2\alter.sql
2çªç®ã®ããŒãžã§ã³ã§ã¯ããã¡ã€ã«ãšãã©ã«ããŒã®æ§é ã¯æ¬¡ã®åœ¢åŒã«ãªããŸãã
ãããã«
説æããã¢ãããŒãã¯ãMS SQLããŒã¿ããŒã¹ã ãã§ãªããä»ã®DBMSã«ãé©çšã§ããŸãã ããšãã°ããã®ã¡ãœããã¯ãDBMSãPostgresã§ãããããžã§ã¯ãã®ããŒã¿ã¹ããŒã ã®ããŒãžã§ã³ç®¡çã·ã¹ãã ãäœæããããã»ã¹ã§éçºãããŸããã ãã¹ãŠã®ãœãŒã¹ã¹ã¯ãªããã¯ããããå ¥æã§ããŸãã