Database Upgrade Code learning and Database Upgrade Code

Source: Internet
Author: User

Database Upgrade Code learning and Database Upgrade Code

Business scenarios and requirements:After the product is launched at each banking outlet [in cooperation with the Company] in China, you must upgrade your business to meet new functional requirements, it is inconvenient to execute the upgrade script in the database management tool every time the database is updated. In this case, you need to set the switch in the business system code. When you need to upgrade the switch, the system automatically completes the upgrade.


Idea: 1) The database has a corresponding version number, and the service has a version file named after the version number to be upgraded ].

2) during the upgrade, check the version in the Service and the version in the database. For comparison, if the version is the same, do not upgrade. if the version is different, execute the upgrade script in the service.


Let's take a look at the main idea code as a note. You can refer to it later.


1) Get the names of all script version files in the service:In the form of an array, it is convenient to expand multiple versions later.

/*** Get all script version file names */private ArrayList <String> getVersionsForFilePath () {String filePath = ""; arrayList <String> fileName = new ArrayList <String> (); try {// filePath is the directory where the Database Upgrade script is placed. filePath = PropertiesHander. class. getResource ("/assp/evoucher/SQL /"). toString (); filePath = filePath. replace ("file:", ""); // obtain all File names in filePath: File dir = new file (filePath ); // return the File [] files = dir. listFiles (); <strong> /<Span style = "color: # cc0000;">/expand the file [With an array, this file may contain many folders or files.] </span> </strong> for (int I = 0; I <files. length; I ++) {// determines whether the file is a file if (files [I]. isDirectory () {fileName. add (files [I]. getName () ;}}// String templte = ""; for (int I = 0; I <fileName. size ()-1; I ++) {for (int j = 0; j <fileName. size ()-I-1; j ++) {if (fileName. get (j ). compareTo (fileName. get (j + 1)> 0) {templte = fileName. Get (j); fileName. set (j, fileName. get (j + 1); fileName. set (j + 1, templte) ;}}} catch (Exception e) {throw new EVoucherException ("getting the automatic upgrade script file [" + filePath + "] failed, check whether the configuration is correct. ", E) ;}return fileName ;}


2) obtain the corresponding upgrade script based on the current version number and the database type used by the current service.

/*** Obtain the upgrade script * @ param version * @ param userBbType */public Map <String according to the current version number and the database type used by the current service, string []> getSqlScriptsForDBType (String version, String userBbType) {Map <String, String []> map = new HashMap <String, String []> (); try {String filePath = DataBaseUPdateThread. class. getResource ("/assp/evoucher/SQL/" + version + "/" + userBbType ). toString (); filePath = filePath. replace ("file:", ""); // obtain All File names: dir = new File (filePath); // returns the File [] files = dir. listFiles (); for (int I = 0; I <files. length; I ++) {// determines whether this file is a file if (! Files [I]. isDirectory () {map. put (files [I]. getName (), this. getResourceString (files [I]. getPath (); logger.info ("Get version number [" + version + "], the database type is ["+ userBbType +"] corresponding upgrade script ["+ files [I]. getName () + "] is complete. ") ;}} Catch (Exception e) {throw new EVoucherException (" Get version number ["+ version + "], the upgrade script corresponding to the Database Type ["+ userBbType +"] failed. ", E);} return map ;}

3) Obtain database scripts by path

/*** Obtain the database script by path * @ param sqlAbsolutePath * @ return * @ throws Exception */private String [] getResourceString (String sqlAbsolutePath) throws Exception {// return the input stream InputStream that reads the specified resource is = new FileInputStream (sqlAbsolutePath); BufferedReader br = new BufferedReader (new InputStreamReader (is, "UTF-8 ")); string s = ""; StringBuffer sb = new StringBuffer (); while (s = br. readLine ())! = Null) sb. append (s). append ("\ n"); if (br! = Null) {br. close () ;}if (is! = Null) {is. close ();} return sb. toString (). split (";");}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.