搜尋
首頁後端開發Python教學Python產生腳本--實作資料庫更新

Python產生腳本--實作資料庫更新

Jul 24, 2017 pm 01:49 PM
python資料庫腳本

  我在工作的時候,在測試環境下使用的資料庫跟生產環境的資料庫不一致,當我們的測試環境下的資料庫完成測試準備更新到生產環境上的資料庫時候,需要準備更新腳本,真是一不小心沒記下來就會忘了改了哪裡,哪裡加了什麼,這真是非常讓人頭痛。因此我就試著用Python來實現自動的生成更新腳本,以免我這爛記性,記不住事。

  主要操作如下:

  1.在原先basedao.py 中加入如下方法,這樣舊能很方便的獲取資料庫的數據,為測試資料庫和生產資料庫做比較打下了基礎。

 1     def select_database_struts(self): 2         ''' 3         查找当前连接配置中的数据库结构以字典集合 4         ''' 5         sql = '''SELECT COLUMN_NAME, IS_NULLABLE, COLUMN_TYPE, COLUMN_KEY, COLUMN_COMMENT 6                 FROM information_schema.`COLUMNS` 
 7                 WHERE TABLE_SCHEMA="%s" AND TABLE_NAME="{0}" '''%(self.__database) 8         struts = {} 9         for k in self.__primaryKey_dict.keys():10             self.__cursor.execute(sql.format(k))11             results = self.__cursor.fetchall()12             struts[k] = {}13             for result in results:14                 struts[k][result[0]] = {}15                 struts[k][result[0]]["COLUMN_NAME"] = result[0]16                 struts[k][result[0]]["IS_NULLABLE"] = result[1]17                 struts[k][result[0]]["COLUMN_TYPE"] = result[2]18                 struts[k][result[0]]["COLUMN_KEY"] = result[3]19                 struts[k][result[0]]["COLUMN_COMMENT"] = result[4]20         return self.__config, struts
View Code

#  2.寫對比的Python腳本

################# #####
  1 '''  2 数据库迁移脚本, 目前支持一下几种功能:  3 1.生成旧数据库中没有的数据库表执行 SQL 脚本(支持是否带表数据),生成的 SQL 脚本在 temp 目录下(表名.sql)。  4 2.生成添加列 SQL 脚本,生成的 SQL 脚本统一放在 temp 目录下的 depoyed.sql 中。  5 3.生成修改列属性 SQL 脚本,生成的 SQL 脚本统一放在 temp 目录下的 depoyed.sql 中。  6 4.生成删除列 SQL 脚本,生成的 SQL 脚本统一放在 temp 目录下的 depoyed.sql 中。  7 '''  8 import json, os, sys  9 from basedao import BaseDao 10  11 temp_path = sys.path[0] + "/temp" 12 if not os.path.exists(temp_path): 13     os.mkdir(temp_path) 14  15 def main(old, new, has_data=False): 16     ''' 17     @old 旧数据库(目标数据库) 18     @new 最新的数据库(源数据库) 19     @has_data 是否生成结构+数据的sql脚本 
 20     ''' 21     clear_temp()    # 先清理 temp 目录 22     old_config, old_struts = old 23     new_config, new_struts = new 24     for new_table, new_fields in new_struts.items(): 25         if old_struts.get(new_table) is None: 26             gc_sql(new_config["user"], new_config["password"], new_config["database"], new_table, has_data) 27         else: 28             cmp_table(old_struts[new_table], new_struts[new_table], new_table) 29  30 def cmp_table(old, new, table): 31     ''' 32     对比表结构生成 sql 33     ''' 34     old_fields = old 35     new_fields = new 36  37     sql_add_column = "ALTER TABLE `{TABLE}` ADD COLUMN `{COLUMN_NAME}` {COLUMN_TYPE} COMMENT '{COLUMN_COMMENT}';\n" 38     sql_change_column = "ALTER TABLE `{TABLE}` CHANGE `{COLUMN_NAME}` `{COLUMN_NAME}` {COLUMN_TYPE} COMMENT '{COLUMN_COMMENT}';\n" 39     sql_del_column = "ALTER TABLE `{TABLE}` DROP {COLUMN_NAME};" 40  41     if old_fields != new_fields: 42         f = open(sys.path[0] + "/temp/deploy.sql", "a", encoding="utf8") 43         content = "" 44         for new_field, new_field_dict in new_fields.items(): 45             old_filed_dict = old_fields.get(new_field) 46             if old_filed_dict is None: 47                 # 生成添加列 sql 48                 content += sql_add_column.format(TABLE=table, **new_field_dict) 49             else: 50                 # 生成修改列 sql 51                 if old_filed_dict != new_field_dict: 52                     content += sql_change_column.format(TABLE=table, **new_field_dict) 53                 pass 54         # 生成删除列 sql 55         for old_field, old_field_dict in old_fields.items(): 56             if new_fields.get(old_field) is None: 57                 content += sql_del_column.format(TABLE=table, COLUMN_NAME=old_field) 58                  59         f.write(content) 60         f.close() 61  62 def gc_sql(user, pwd, db, table, has_data): 63     ''' 64     生成 sql 文件 65     ''' 66     if has_data: 67         sys_order = "mysqldump -u%s -p%s %s %s > %s/%s.sql"%(user, pwd, db, table, temp_path, table) 68     else: 69         sys_order = "mysqldump -u%s -p%s -d %s %s > %s/%s.sql"%(user, pwd, db, table, temp_path, table) 70     os.system(sys_order) 71  72 def clear_temp(): 73     ''' 74     每次执行的时候调用这个,先清理下temp目录下面的旧文件 75     ''' 76     if os.path.exists(temp_path): 77         files = os.listdir(temp_path) 78         for file in files: 79             f = os.path.join(temp_path, file) 80             if os.path.isfile(f): 81                 os.remove(f) 82     print("临时文件目录清理完成") 83  84 if __name__ == "__main__": 85     test1_config = { 86         "user" : "root", 
 87         "password" : "root", 88         "database" : "test1", 
 89     } 90     test2_config = { 91         "user" : "root", 
 92         "password" : "root", 93         "database" : "test2", 
 94     } 95      96     test1_dao = BaseDao(**test1_config) 97     test1_struts = test1_dao.select_database_struts() 98      99     test2_dao = BaseDao(**test2_config)100     test2_struts = test2_dao.select_database_struts()101 102     main(test2_struts, test1_struts)
######View Code#########  目前只支援了4種SQL腳本的生成。 ######  如果有感興趣一起學習、討論Python的可以加QQ群:626787819,有啥意見或者建議的可以發我郵箱:410093793@qq.com。 ###

以上是Python產生腳本--實作資料庫更新的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
可以在Python數組中存儲哪些數據類型?可以在Python數組中存儲哪些數據類型?Apr 27, 2025 am 12:11 AM

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

如果您嘗試將錯誤的數據類型的值存儲在Python數組中,該怎麼辦?如果您嘗試將錯誤的數據類型的值存儲在Python數組中,該怎麼辦?Apr 27, 2025 am 12:10 AM

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

Python標準庫的哪一部分是:列表或數組?Python標準庫的哪一部分是:列表或數組?Apr 27, 2025 am 12:03 AM

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

您應該檢查腳本是否使用錯誤的Python版本執行?您應該檢查腳本是否使用錯誤的Python版本執行?Apr 27, 2025 am 12:01 AM

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

在Python陣列上可以執行哪些常見操作?在Python陣列上可以執行哪些常見操作?Apr 26, 2025 am 12:22 AM

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

在哪些類型的應用程序中,Numpy數組常用?在哪些類型的應用程序中,Numpy數組常用?Apr 26, 2025 am 12:13 AM

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

您什麼時候選擇在Python中的列表上使用數組?您什麼時候選擇在Python中的列表上使用數組?Apr 26, 2025 am 12:12 AM

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

所有列表操作是否由數組支持,反之亦然?為什麼或為什麼不呢?所有列表操作是否由數組支持,反之亦然?為什麼或為什麼不呢?Apr 26, 2025 am 12:05 AM

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。