143 lines
4.7 KiB
Python
143 lines
4.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
#!/usr/bin/python
|
|
|
|
import os
|
|
import sys
|
|
import time
|
|
import json
|
|
import redis
|
|
import pymysql
|
|
import datetime
|
|
import pprint
|
|
from string import Template
|
|
|
|
def readCombineRule():
|
|
json_data = json.loads(open('combine_rule.json', 'r').read())
|
|
return json_data
|
|
|
|
def readDBConf():
|
|
json_data = json.loads(open('db_conf.json', 'r').read())
|
|
assert len(json_data) > 1
|
|
return json_data
|
|
|
|
def checkTables(rule_conf, rows):
|
|
assert(len(rule_conf) == len(rows))
|
|
for row in rows:
|
|
if row[0] not in rule_conf:
|
|
print(row[0])
|
|
assert False
|
|
|
|
def checkColumns(table_name, a_columns, b_columans):
|
|
assert len(a_columns) > 0 and len(a_columns) == len(b_columans)
|
|
for a_col in a_columns:
|
|
found = False
|
|
for b_col in b_columans:
|
|
if a_col == b_col:
|
|
found = True
|
|
break
|
|
#end for b_col
|
|
assert found
|
|
|
|
def checkDB(db_conf, rule_conf):
|
|
table_columns = {}
|
|
rolename_hash = {}
|
|
for conf in db_conf:
|
|
conn = pymysql.connect(host = conf['db_host'],
|
|
port = conf['db_port'],
|
|
user = conf['db_user'],
|
|
passwd = conf['db_passwd'],
|
|
db = conf['db_database'],
|
|
charset = 'utf8'
|
|
)
|
|
assert conn
|
|
cursor = conn.cursor()
|
|
cursor.execute('SHOW TABLES;')
|
|
rows = cursor.fetchall()
|
|
checkTables(rule_conf, rows)
|
|
|
|
cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)
|
|
for rule in rule_conf.values():
|
|
cursor.execute('SHOW COLUMNS FROM %s;' % (rule['table_name']) )
|
|
rows = cursor.fetchall()
|
|
if rule['table_name'] in table_columns:
|
|
checkColumns(rule['table_name'], table_columns[rule['table_name']], rows)
|
|
else:
|
|
table_columns[rule['table_name']] = rows
|
|
|
|
|
|
cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)
|
|
cursor.execute('SELECT * FROM role;')
|
|
rows = cursor.fetchall()
|
|
for row in rows:
|
|
assert row['name'] not in rolename_hash
|
|
rolename_hash[row['name']] = row
|
|
# pprint.pprint(row)
|
|
|
|
conn.close()
|
|
|
|
class Combine:
|
|
|
|
def __init__(self):
|
|
self._rule_conf = readCombineRule()
|
|
self._db_conf = readDBConf()
|
|
self._main_server_id = self._db_conf[0]['server_id']
|
|
self._curr_server_id = 0
|
|
|
|
def run(self):
|
|
checkDB(self._db_conf, self._rule_conf)
|
|
self._convertDB()
|
|
|
|
def _convertDB(self):
|
|
for conf in self._db_conf:
|
|
conn = pymysql.connect(host = conf['db_host'],
|
|
port = conf['db_port'],
|
|
user = conf['db_user'],
|
|
passwd = conf['db_passwd'],
|
|
db = conf['db_database'],
|
|
charset = 'utf8'
|
|
)
|
|
assert conn
|
|
self._curr_server_id = conf['server_id']
|
|
for rule in self._rule_conf.values():
|
|
fetch_sql = self._replaceSqlTemplate(rule['fetch_sql'])
|
|
cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)
|
|
cursor.execute(fetch_sql)
|
|
rows = cursor.fetchall()
|
|
self._writeSql(conn, rule, rows)
|
|
self._appendSql()
|
|
|
|
def _writeSql(self, conn, rule, rows):
|
|
# file_name = 'out/' + rule['table_name'] + '.sql'
|
|
file_name = 'out/all.sql'
|
|
with open(file_name, 'a+') as out_file:
|
|
for row in rows:
|
|
assert len(row) > 0
|
|
field_names = ''
|
|
field_values = ''
|
|
for key in row.keys():
|
|
field_names += '`%s`,' % key
|
|
if row[key] == None:
|
|
field_values += 'null,'
|
|
else:
|
|
field_values += '"%s",' % pymysql.escape_string(str(row[key]))
|
|
#end for key
|
|
insert_sql = 'INSERT INTO ' + rule['table_name'] + '(' + field_names[0:-1] + ')' + \
|
|
' VALUES (' + field_values[0:-1] + ");\n"
|
|
out_file.write(insert_sql)
|
|
#end for row
|
|
|
|
def _appendSql(self):
|
|
file_name = 'out/all.sql'
|
|
with open(file_name, 'a+') as out_file:
|
|
out_file.write("UPDATE tribe_member SET position=5;\n")
|
|
|
|
def _replaceSqlTemplate(self, sql):
|
|
str_tpl = Template(sql)
|
|
return str_tpl.safe_substitute({
|
|
'main_server_id': self._main_server_id,
|
|
'curr_server_id': self._curr_server_id
|
|
})
|
|
|
|
combine = Combine()
|
|
combine.run()
|