MyBatis3Migrations.docx

上传人:b****3 文档编号:10556295 上传时间:2023-05-26 格式:DOCX 页数:18 大小:32.52KB
下载 相关 举报
MyBatis3Migrations.docx_第1页
第1页 / 共18页
MyBatis3Migrations.docx_第2页
第2页 / 共18页
MyBatis3Migrations.docx_第3页
第3页 / 共18页
MyBatis3Migrations.docx_第4页
第4页 / 共18页
MyBatis3Migrations.docx_第5页
第5页 / 共18页
MyBatis3Migrations.docx_第6页
第6页 / 共18页
MyBatis3Migrations.docx_第7页
第7页 / 共18页
MyBatis3Migrations.docx_第8页
第8页 / 共18页
MyBatis3Migrations.docx_第9页
第9页 / 共18页
MyBatis3Migrations.docx_第10页
第10页 / 共18页
MyBatis3Migrations.docx_第11页
第11页 / 共18页
MyBatis3Migrations.docx_第12页
第12页 / 共18页
MyBatis3Migrations.docx_第13页
第13页 / 共18页
MyBatis3Migrations.docx_第14页
第14页 / 共18页
MyBatis3Migrations.docx_第15页
第15页 / 共18页
MyBatis3Migrations.docx_第16页
第16页 / 共18页
MyBatis3Migrations.docx_第17页
第17页 / 共18页
MyBatis3Migrations.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

MyBatis3Migrations.docx

《MyBatis3Migrations.docx》由会员分享,可在线阅读,更多相关《MyBatis3Migrations.docx(18页珍藏版)》请在冰点文库上搜索。

MyBatis3Migrations.docx

MyBatis3Migrations

MyBatis3–SchemaMigrationSystem

UserGuide

⇨NOTE:

ThisguideisnotaboutmigratingfromolderversionsofMyBatis.Itisamanualaboutatoolthatwillchangethewayyoumanagechangestoyourdatabase.

Introduction

Evolvingdatabaseshasbeenoneofthemajorchallengesforsoftwaredevelopment.Oftentimes,regardlessofoursoftwaredevelopmentmethodology,thedatabasefollowsadifferentchangemanagementprocess.Despiteourbestefforts,fewtoolsandpracticeshavebeenabletochangethat.ThetoolsofthepasthavebeenGUIcentric,proprietaryforaparticulardatabaseand/orcarriedasteeplicensecost.Yet,attheendofthedaytheysufferedfromthesamechallenges.

Recently,afewtoolsarrivedandchangedallofthat.Theydidsobyembracingsimplicityandafewsimplerulesfordatabaseevolutiontofollow.AcoupleofgoodexamplesareRailsMigrationsanddbdeploy.Bothtoolsaresimilarinpurpose,butquitedifferentinimplementation.TheMyBatisSchemaMigrationSystemdrawsfrombothandseekstobethebestmigrationtoolofitskind.

Goals

Toachieveagooddatabasechangemanagementpractice,weneedtoidentifyafewkeygoals.

Thus,theMyBatisSchemaMigrationSystem(orMyBatisMigrationsforshort)seeksto:

∙Workwithanydatabase,neworexisting

∙Leveragethesourcecontrolsystem(e.g.Subversion)

∙Enableconcurrentdevelopersorteamstoworkindependently

∙Allowconflictsveryvisibleandeasilymanageable

∙Allowforforwardandbackwardmigration(evolve,devolverespectively)

∙Makethecurrentstatusofthedatabaseeasilyaccessibleandcomprehensible

∙Enablemigrationsdespiteaccessprivilegesorbureaucracy

∙Workwithanymethodology

∙Encouragesgood,consistentpractices

Installation

Installationissimplyamatterofunzippingthepackagetoadirectoryofyourchoosing.Therearegenerallytwowaystoworkwiththistool:

∙Unzipittoacentrallocation.AddMIGRATIONS_HOMEtoyourenvironmentvariables,andaddMIGRATIONS_HOMEtoyourpath.Thisisacommonoption,popularamongsimilartoolslikeAntorMaven.

∙Unzipitintoadirectoryinyourworkspaceforaprojectthatyou’recurrentlyworkingon,thuskeepingallofthedependenciesandthetoolversionisolatedwithintheproject.It’sasmallframework,andthisoptionhastheadvantageofportabilityandzerosetupfordevelopersnewtotheproject.

What’sIncluded?

TheMyBatisMigrationspackageissmallandsimple.Thefollowingisthecontentsoftheunzippedpackage:

./lib/mybatis-3-core-3.0.0.188.jar

./migrate

.migrate.cmd

ThesingleMyBatisJARfileistheonlydependencythatMyBatisMigrationshas.Thetwoscriptfilesdothesamething,butasyoucansee,oneisfor*nixshellsandtheotherisforWindows(Note:

cygwinusersshouldstillcallthe.cmdversion).

The‘migrate’Command

TheentireMigrationssystemcanbeaccessedthroughthisonesimplecommand.Youcanaccessthebuilt-inhelpbytyping:

migrate--help

Callingthemigratecommandwithnooptionsorinvalidoptionsalsoproducesthehelpmessage.Here’stheoutputofthehelpcommand:

Usage:

migratecommand[parameter][--path=][--env=]

--path=Pathtorepository.Defaultcurrentworkingdirectory.

--env=Environmenttoconfigure.Defaultenvironmentis'development'.

--forceForcesscripttocontinueevenifSQLerrorsareencountered.

--helpDisplaysthisusagemessage.

--traceShowsadditionalerrordetails(ifany).

Commands:

initCreates(ifnecessary)andinitializesamigrationpath.

bootstrapRunsthebootstrapSQLscript(seescripts/bootstrap.sqlformore).

newCreatesanewmigrationwiththeprovideddescription.

upRunallunappliedmigrations.

downUndoesthelastmigrationappliedtothedatabase.

versionMigratesthedatabaseupordowntothespecifiedversion.

pendingForceexecutespendingmigrationsoutoforder(notrecommended).

statusPrintsthechangelogfromthedatabaseifthechangelogtableexists.

scriptGeneratesadeltamigrationscriptfromversionv1tov2(undoifv1>v2).

We’llgothrougheachofthesecommandsindetail,butfirst,let’stalkaboutlifecycle.

TheMyBatisMigrationsLifecycle

Databasechangemanagementisdifficultatthebestoftimes,sotomakethesituationbetter,it’simportanttohaveagooddatabaseevolutionstrategy.ThatemployedbyMyBatisMigrationstargetsafewkeygoals:

∙Consistent–Theschemashouldbepredictableoneverymachineit’screatedon.

∙Repeatable–Theschemacanbedestroyedandrecreatedapredictableway.

∙Reversible–Changesshouldbeabletoberolledback,orundone.

∙Versioned–Theversionoftheschemashouldbeidentifiable(viaqueryortool).

∙Auditable–Theschemaevolutionshouldbeauditableandthechangestoitlogged.

∙Automated–Theevolution(ordevolution)oftheschemashouldbefullyautomated.

∙Serial–Theevolutioninthedatabaseshouldneverbranchorevolveconditionally.

∙ImmutableChanges–Nopastappliedalterorevolutionofthedatabaseshouldbemodified,insteadanewchangeshouldbecreated.

∙ConcurrentlyModifiable–Theschemashouldbesafelymodifiablebymultiplepeopleorteamsinawaythatencouragesteamwork,communicationandeasyidentificationofconflicts,withoutdependingontextcomparisons(diff)oranyparticularsourcecontrolfeature(conflicts),butshouldworkverywellwithsourcecontrolsystems.

Thus,thelifecycleofaschemamanagedwithMyBatisMigrationsisasfollows:

1.InitializeRepository

2.Bootstrapdatabaseschema

3.Createanewmigration(ormanymigrations)

4.Applymigrations

Optionalstepsinclude:

∙Revertmigrationsifnecessarytoresolveconflicts

∙Applypendingmigrationsoutoforderifit’ssafetodoso

∙Generatemigrationscriptstoberun“offline”inenvironmentsthatarebeyondyourcontrol

∙Getthestatusofthesystematanytime

Thefollowingcommanddiscussionswillprovidemoredetailabouthowthislifecycleworks.

init

Theinitcommandinitializesanew‘migrationpath’,alsocalleda‘repository’(ofmigrationscripts).Regardlessofwhetheryourworkingwithanewdatabaseoranexistingone,you’llruninittocreatetheworkspaceinwhichyou’llplaceeverythingyouneedtomanagedatabasechange.Runningthiscommandwillcreatethedirectoryspecifiedbythe--pathoption(whichisthecurrentworkingdirectorybydefault).

Here’sanexampleofrunningtheinitcommand:

/$migrate--path=/home/cbegin/testdbinit

IfIwasalreadyinthe/home/cbegin/testdbdirectory,Icouldsimplyrun:

/home/cbegin/testdb$migrateinit

Whenthecommandiscompleted,thedirectorywillcontainthefollowingsub-directories:

./drivers

PlaceyourJDBCdriver.jaror.zipfilesinthisdirectory.Uponrunningamigration,thedriverswillbedynamicallyloaded.

./environments

Intheenvironmentsfolderyouwillfind.propertiesfilesthatrepresentyourdatabaseinstances.Bydefaultadevelopment.propertiesfileiscreatedforyoutoconfigureyourdevelopmenttimedatabaseproperties.Youcanalsocreatetest.propertiesandproduction.propertiesfiles.Detailsaboutthepropertiesthemselvesfollowlaterinthisdocument.Theenvironmentcanbespecifiedwhenrunningamigrationbyusingthe--env=option(withoutthepathor".properties"part).

Thedefaultenvironmentis"development".Thepropertiesfileisselfdocumented,buthereitisforreference:

##Basetimezonetoensuretimesareconsistentacrossmachines

time_zone=GMT+0:

00

##Thecharactersetthatscriptsareencodedwith

#script_char_set=UTF-8

##JDBCconnectionproperties.

driver=

url=

username=

password=

#Nameofthetablethattrackschangestothedatabase

changelog=CHANGELOG

#Ifsettotrue,eachstatementisisolated

#initsowntransaction.Otherwisetheentire

#scriptisexecutedinonetransaction.

auto_commit=false

#Thiscontrolshowstatementsaredelimited.

#Bydefaultstatementsaredelimitedbyan

#endoflinesemicolon.Somedatabasesmay

#(e.g.MSSQLServer)mayrequireafullline

#delimitersuchasGO.

delimiter=;

full_line_delimiter=false

#Thisignoresthelinedelimitersand

#simplysendstheentirescriptatonce.

#UsewithJDBCdriversthatcanacceptlarge

#blocksofdelimitedtextatonce.

send_full_script=false

#Customdriverpathtoavoidcopyingyourdrivers

#driver_path=

./scripts

ThisdirectorycontainsyourmigrationSQLfiles.ThesearethefilesthatcontainyourDDLtobothupgradeanddowngradeyourdatabasestructure.Bydefault,thedirectorywillcontainthescripttocreatethechangelogtable,plusoneemptyexamplemigrationscript.Tocreateanewmigrationscript,usethe"new"command.Torunallpendingmigrations,usethe"up"command.Toundothelastmigrationapplied,usethe"down"commandetc.

bootstrap

Ifyou’reworkingfromanexistingdatabase,youneedtostartfromaknownstate.There’snopointintryingtorewindtimeandshoehornyourexistingdatabaseintoaseriesofmigrationscripts.It’smorepracticaltojustacceptthecurrentstateofyourdatabaseschemaandidentifythisasthestartingpoint.Thebootstrapscriptandcommandexistforthisreason.Inthescriptsdirectoryyou’llfindbootstrap.sql.YoucanputyourexistingDDLscriptinthisfile.Ifyoudon’thaveaDDLscript,youcanexportyourexistingdatabaseschemaandputitinthebootstrapfile.You’llwanttocleanitupsothatitdoesn’tcontainanythingspecifictoanyoneenvironment,butotherwisealmostanyscriptshouldwork.WatchoutforDDLthatcontainsconditionalelementsorbranchinglogicthatcouldgeneratemultipleschemas.Whilethisissome

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试认证 > IT认证

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2