在使用mysql数据库的时候,A方使用一个版本,B方在使用一个版本数据库进行开发使用,B方在开发的时候,有新的需求,需要添加表字段和所需要的表。但是A方已经在使用之前的版本数据库并且数据库里面有真实的数据,这个时候B方添加的数据如何更新在A方上面,所以写个数据库更新脚本,A方数据库版本与B方更新添加的数据库结构进行对比。添加了哪些字段,哪些表格。
# 在项目根目录下创建一个脚本文件
updata_database.py
from web.settings import DATABASES
import pymysql
host = DATABASES["default"]["HOST"]
port = DATABASES["default"]['PORT']
user = DATABASES["default"]['USER']
password = DATABASES["default"]['PASSWORD']
name = DATABASES["default"]["NAME"]
db=pymysql.connect(host= host, port=int(port), user=user, passwd=password, db=name)
cur=db.cursor()
sqls=[
"SET FOREIGN_KEY_CHECKS=0",
"alter table info_person_basic add real_estate text", #新添加的字段
"alter table auth_user rename to user_profile_user", #修改表名
"alter table user_profile_user add (bind_addr varchar(30) DEFAULT NULL,last_login_addr varchar(30) DEFAULT NULL)", #新添加的字段
"alter table auth_user_groups rename to user_profile_user_groups", #修改表名
"alter table auth_user_user_permissions rename to user_profile_user_user_permissions", #修改表名
'''
CREATE TABLE `login_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`login_status` varchar(50) NOT NULL,
`user_agent` varchar(500) DEFAULT NULL,
`remote_addr` varchar(30) DEFAULT NULL,
`login_date` datetime(6) NOT NULL,
`user_id_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `login_log_user_id_id_2117abc3_fk_user_profile_user_id` (`user_id_id`),
CONSTRAINT `login_log_user_id_id_2117abc3_fk_user_profile_user_id` FOREIGN KEY (`user_id_id`) REFERENCES `user_profile_user` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; #创建新的表
''',
'''
CREATE TABLE `schedulers_task` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Task` varchar(200) NOT NULL,
`State` int(11) NOT NULL DEFAULT '1',
`CreateUserId` varchar(200) DEFAULT NULL,
`CreateTime` datetime DEFAULT NULL,
`Args` longtext,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
''',
'''
CREATE TABLE `statistics_analyze` (
`key` varchar(200) NOT NULL,
`value` longtext,
PRIMARY KEY (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; #创建新的表
''',
"ALTER TABLE `info_person_basic` ADD INDEX info_person_basic_uid (`uid`)", #为搜索字段创建索引
"ALTER TABLE `info_person_basic` ADD INDEX info_person_basic_id (`person_id`)", #为搜索字段创建索引
"ALTER TABLE `info_person_basic` ADD INDEX info_person_basic_name (`person_name`)", #为搜索字段创建索引
"ALTER TABLE `info_person_basic` ADD INDEX info_person_basic_sex (`person_sex`)", #为搜索字段创建索引
"ALTER TABLE `info_person_basic` ADD INDEX info_person_basic_contact_number #为搜索字段创建索引(`person_contact_number`)",
"ALTER TABLE `info_person_basic` ADD INDEX info_person_basic_brithday (`person_birthday`)", #为搜索字段创建索引
"ALTER TABLE `info_person_basic` ADD INDEX info_person_basic_email (`person_email`)", #为搜索字段创建索引
"ALTER TABLE `info_person_basic` ADD INDEX info_person_basic_org_name (`org_name`)", #为搜索字段创建索引
"ALTER TABLE `info_person_basic` ADD INDEX info_person_basic_org_address (`org_address`)", #为搜索字段创建索引
"SET FOREIGN_KEY_CHECKS=1"
]
idx = 0
for sql in sqls:
idx += 1
try:
cur.execute(sql)
print "Successfully run sql %d." % idx
except Exception,e:
print "Warning: run sql failed:", str(e)
cur.close()
更新脚本文件 updata_database.sh
#备份数据库
mysqldump -uroot -B mysql > update_database.sql