資料庫升級代碼學習,資料庫升級代碼

來源:互聯網
上載者:User

資料庫升級代碼學習,資料庫升級代碼

      業務情境及需求:全國每個銀行營業網點【指跟公司合作的】產品上線後,肯定需要升級業務以應對新的功能需求,那麼資料庫每次升級更新時都是在資料庫管理工具中執行升級指令碼,非常不方便。此時需求:能夠在業務系統代碼中設定開關,需要升級的時候開啟開關係統自動直接完成升級.


思路:1)資料庫裡面有個相應的版本號碼,服務裡面有個版本檔案【以要升級到的版本號碼命名】。

         2)升級的時候查出服務裡面的版本和資料庫裡面的版本,比對:如果版本一樣不升級,如果版本不一樣就執行服務裡面的升級指令碼.


下面看看主要的思路代碼作為筆記,以後可以參考。


一)擷取服務裡面的所有指令碼版本檔案名稱:以一個數組形式存在方便以後多個版本的時候擴充用。

/** * 擷取所有指令碼版本檔案名稱 */private ArrayList<String> getVersionsForFilePath(){String filePath = "";ArrayList<String> fileName = new ArrayList<String>();try {// filePath為放置資料庫升級指令碼的目錄filePath = PropertiesHander.class.getResource("/assp/evoucher/sql/").toString();filePath = filePath.replace("file:", "");// 擷取filePath下的所有檔案名稱File dir = new File(filePath);// 返回此抽象路徑下的檔案File[] files = dir.listFiles();       <strong> /<span style="color:#cc0000;">/展開該檔案【以一個數組,因為該檔案下可能有很多個檔案夾或者檔案】</span></strong>for (int i = 0; i < files.length; i++) {// 判斷此檔案是否是一個檔案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("擷取自動升級指令檔【 " +filePath+ " 】失敗,檢查配置是否正確。", e);}return fileName;}


二)根據最新分支號及當前服務使用的資料庫類型擷取對應升級指令碼

/** * 根據最新分支號及當前服務使用的資料庫類型擷取對應升級指令碼 * @param version * @param userBbType */public Map<String, 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:", "");// 擷取filePath下的所有檔案名稱File dir = new File(filePath);// 返回此抽象路徑下的檔案File[] files = dir.listFiles();for (int i = 0; i < files.length; i++) {// 判斷此檔案是否是一個檔案if (!files[i].isDirectory()) {map.put(files[i].getName(), this.getResourceString(files[i].getPath()));logger.info("擷取版本號碼【 " + version + " 】,資料庫類型為【 " + userBbType + " 】對應的升級指令碼【 " + files[i].getName() + " 】完成。");}}} catch (Exception e) {throw new EVoucherException("擷取版本號碼【 " + version + " 】,資料庫類型為【 " + userBbType + " 】對應的升級指令碼失敗。", e);}return map;}

三)根據路徑擷取資料庫指令碼

/** * 根據路徑擷取資料庫指令碼 * @param sqlAbsolutePath * @return * @throws Exception */private String[] getResourceString(String sqlAbsolutePath) throws Exception {// 返回讀取指定資源的輸入資料流InputStream 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(";");}

相關文章

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.