欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python 實現(xiàn)數(shù)據(jù)庫更新腳本的生成方法

 更新時間:2017年07月09日 17:59:08   投稿:jingxian  
下面小編就為大家?guī)硪黄狿ython 實現(xiàn)數(shù)據(jù)庫更新腳本的生成方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

我在工作的時候,在測試環(huán)境下使用的數(shù)據(jù)庫跟生產(chǎn)環(huán)境的數(shù)據(jù)庫不一致,當我們的測試環(huán)境下的數(shù)據(jù)庫完成測試準備更新到生產(chǎn)環(huán)境上的數(shù)據(jù)庫時候,需要準備更新腳本,真是一不小心沒記下來就會忘了改了哪里,哪里添加了什么,這個真是非常讓人頭疼。因此我就試著用Python來實現(xiàn)自動的生成更新腳本,以免我這爛記性,記不住事。

主要操作如下:

1.在原先 basedao.py 中添加如下方法,這樣舊能很方便的獲取數(shù)據(jù)庫的數(shù)據(jù),為測試數(shù)據(jù)庫和生產(chǎn)數(shù)據(jù)庫做對比打下了基礎。

def select_database_struts(self):
    '''
    查找當前連接配置中的數(shù)據(jù)庫結(jié)構(gòu)以字典集合
    '''
    sql = '''SELECT COLUMN_NAME, IS_NULLABLE, COLUMN_TYPE, COLUMN_KEY, COLUMN_COMMENT
        FROM information_schema.`COLUMNS` 
        WHERE TABLE_SCHEMA="%s" AND TABLE_NAME="{0}" '''%(self.__database)
    struts = {}
    for k in self.__primaryKey_dict.keys():
      self.__cursor.execute(sql.format(k))
      results = self.__cursor.fetchall()
      struts[k] = {}
      for result in results:
        struts[k][result[0]] = {}
        struts[k][result[0]]["COLUMN_NAME"] = result[0]
        struts[k][result[0]]["IS_NULLABLE"] = result[1]
        struts[k][result[0]]["COLUMN_TYPE"] = result[2]
        struts[k][result[0]]["COLUMN_KEY"] = result[3]
        struts[k][result[0]]["COLUMN_COMMENT"] = result[4]
    return self.__config, struts

2.編寫對比的Python腳本

'''
數(shù)據(jù)庫遷移腳本, 目前支持一下幾種功能:
1.生成舊數(shù)據(jù)庫中沒有的數(shù)據(jù)庫表執(zhí)行 SQL 腳本(支持是否帶表數(shù)據(jù)),生成的 SQL 腳本在 temp 目錄下(表名.sql)。
2.生成添加列 SQL 腳本,生成的 SQL 腳本統(tǒng)一放在 temp 目錄下的 depoyed.sql 中。
3.生成修改列屬性 SQL 腳本,生成的 SQL 腳本統(tǒng)一放在 temp 目錄下的 depoyed.sql 中。
4.生成刪除列 SQL 腳本,生成的 SQL 腳本統(tǒng)一放在 temp 目錄下的 depoyed.sql 中。
'''
import json, os, sys
from basedao import BaseDao

temp_path = sys.path[0] + "/temp"
if not os.path.exists(temp_path):
  os.mkdir(temp_path)

def main(old, new, has_data=False):
  '''
  @old 舊數(shù)據(jù)庫(目標數(shù)據(jù)庫)
  @new 最新的數(shù)據(jù)庫(源數(shù)據(jù)庫)
  @has_data 是否生成結(jié)構(gòu)+數(shù)據(jù)的sql腳本 
  '''
  clear_temp()  # 先清理 temp 目錄
  old_config, old_struts = old
  new_config, new_struts = new
  for new_table, new_fields in new_struts.items():
    if old_struts.get(new_table) is None:
      gc_sql(new_config["user"], new_config["password"], new_config["database"], new_table, has_data)
    else:
      cmp_table(old_struts[new_table], new_struts[new_table], new_table)

def cmp_table(old, new, table):
  '''
  對比表結(jié)構(gòu)生成 sql
  '''
  old_fields = old
  new_fields = new

  sql_add_column = "ALTER TABLE `{TABLE}` ADD COLUMN `{COLUMN_NAME}` {COLUMN_TYPE} COMMENT '{COLUMN_COMMENT}';\n"
  sql_change_column = "ALTER TABLE `{TABLE}` CHANGE `{COLUMN_NAME}` `{COLUMN_NAME}` {COLUMN_TYPE} COMMENT '{COLUMN_COMMENT}';\n"
  sql_del_column = "ALTER TABLE `{TABLE}` DROP {COLUMN_NAME};"

  if old_fields != new_fields:
    f = open(sys.path[0] + "/temp/deploy.sql", "a", encoding="utf8")
    content = ""
    for new_field, new_field_dict in new_fields.items():
      old_filed_dict = old_fields.get(new_field)
      if old_filed_dict is None:
        # 生成添加列 sql
        content += sql_add_column.format(TABLE=table, **new_field_dict)
      else:
        # 生成修改列 sql
        if old_filed_dict != new_field_dict:
          content += sql_change_column.format(TABLE=table, **new_field_dict)
        pass
    # 生成刪除列 sql
    for old_field, old_field_dict in old_fields.items():
      if new_fields.get(old_field) is None:
        content += sql_del_column.format(TABLE=table, COLUMN_NAME=old_field)
        
    f.write(content)
    f.close()

def gc_sql(user, pwd, db, table, has_data):
  '''
  生成 sql 文件
  '''
  if has_data:
    sys_order = "mysqldump -u%s -p%s %s %s > %s/%s.sql"%(user, pwd, db, table, temp_path, table)
  else:
    sys_order = "mysqldump -u%s -p%s -d %s %s > %s/%s.sql"%(user, pwd, db, table, temp_path, table)
  os.system(sys_order)

def clear_temp():
  '''
  每次執(zhí)行的時候調(diào)用這個,先清理下temp目錄下面的舊文件
  '''
  if os.path.exists(temp_path):
    files = os.listdir(temp_path)
    for file in files:
      f = os.path.join(temp_path, file)
      if os.path.isfile(f):
        os.remove(f)
  print("臨時文件目錄清理完成")

if __name__ == "__main__":
  test1_config = {
    "user" : "root", 
    "password" : "root",
    "database" : "test1", 
  }
  test2_config = {
    "user" : "root", 
    "password" : "root",
    "database" : "test2", 
  }
  
  test1_dao = BaseDao(**test1_config)
  test1_struts = test1_dao.select_database_struts()
  
  test2_dao = BaseDao(**test2_config)
  test2_struts = test2_dao.select_database_struts()

  main(test2_struts, test1_struts)

目前只支持了4種SQL腳本的生成。

以上這篇Python 實現(xiàn)數(shù)據(jù)庫更新腳本的生成方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python優(yōu)雅實現(xiàn)二分查找的示例詳解

    Python優(yōu)雅實現(xiàn)二分查找的示例詳解

    二分查找是一種高效的搜索算法,用于在有序數(shù)組中查找特定元素,本文將介紹二分查找的基本原理,并通過Python代碼進行詳細講解,需要的可以參考一下
    2023-07-07
  • 好的Python培訓機構(gòu)應該具備哪些條件

    好的Python培訓機構(gòu)應該具備哪些條件

    python是現(xiàn)在開發(fā)的熱潮,大家應該如何學習呢?許多人選擇自學,還有人會選擇去培訓結(jié)構(gòu)學習,那么好的培訓機構(gòu)的標準是什么樣的呢?下面跟隨腳本之家小編一起通過本文學習吧
    2018-05-05
  • python 實現(xiàn)倒計時功能(gui界面)

    python 實現(xiàn)倒計時功能(gui界面)

    這篇文章主要介紹了python 實現(xiàn)倒計時功能(gui界面),幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-11-11
  • Python學習入門之區(qū)塊鏈詳解

    Python學習入門之區(qū)塊鏈詳解

    區(qū)塊鏈的基礎概念很簡單:一個分布式數(shù)據(jù)庫,存儲一個不斷加長的 list,list 中包含著許多有序的記錄。下面這篇文章主要給大家介紹了關(guān)于Python學習入門之區(qū)塊鏈的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友們下面來一起看看吧。
    2017-07-07
  • Django返回HTML文件的實現(xiàn)方法

    Django返回HTML文件的實現(xiàn)方法

    這篇文章主要介紹了Django返回HTML文件的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • 通俗易懂詳解Python基礎五種下劃線作用

    通俗易懂詳解Python基礎五種下劃線作用

    本來而言,這個問題網(wǎng)上很多資料,但是網(wǎng)上資料都是復制來復制去,很多話大家其實都不是很明白的,或者拿著官方文檔翻譯過來的,讓人看的非常迷糊。今天用通俗好懂表述解釋下這幾種情況
    2021-10-10
  • python中的插入排序的簡單用法

    python中的插入排序的簡單用法

    在本篇內(nèi)容里小編給各位分享的是一篇關(guān)于python中的插入排序的簡單用法,有興趣的朋友們可以參考學習下。
    2021-01-01
  • python編程PyAutoGUI庫使用與安裝簡介

    python編程PyAutoGUI庫使用與安裝簡介

    這篇文章主要為大家介紹了python編程中PyAutoGUI庫的使用與安裝簡單介紹,文中含有視頻詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-10-10
  • Selenium+Python自動化腳本環(huán)境搭建的全過程

    Selenium+Python自動化腳本環(huán)境搭建的全過程

    說到自動化測試,就不得不提大名鼎鼎的Selenium,Selenium 是如今最常用的自動化測試工具之一,支持快速開發(fā)自動化測試框架,且支持在多種瀏覽器上執(zhí)行測試,下面這篇文章主要給大家介紹了關(guān)于Selenium+Python自動化腳本環(huán)境搭建的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • python算法深入理解風控中的KS原理

    python算法深入理解風控中的KS原理

    這篇文章主要為大家介紹了python算法深入理解風控中的KS原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2021-11-11

最新評論