-
Notifications
You must be signed in to change notification settings - Fork 18
/
DatabasePort.py
258 lines (188 loc) · 8.09 KB
/
DatabasePort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
from twisted.internet import reactor
from twisted.internet.task import LoopingCall
from twisted.internet.defer import inlineCallbacks, returnValue, Deferred
import logging
import os, sys, signal
import json
from twisted.enterprise import adbapi
from twisted.python import log
from twisted.logger import Logger
CONFIG = \
{
"MySQL" : {
'user': 'root',
'pass': '',
'dbname': 'timelin',
'oldname': 'times-cp'
}
}
def InitiateColorLogger(name='twisted'):
from colorlog import ColoredFormatter
logger = logging.getLogger(name)
stream = logging.StreamHandler()
LogFormat = " %(reset)s%(log_color)s%(levelname)-8s%(reset)s | %(log_color)s%(message)s"
stream.setFormatter(ColoredFormatter(LogFormat, log_colors={
'DEBUG': 'white',
'INFO': 'cyan',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'black,bg_red',
}))
logger.addHandler(stream)
logger.setLevel(logging.DEBUG)
logger.info("Porter running")
return logger
InitiateColorLogger()
logger = Logger()
TEObserver = log.PythonLoggingObserver()
TEObserver.start()
logger.info('DatabasePort running. Initializing values...')
logger.info("Checking existance of module: mysql.connector")
try:
import mysql.connector
except:
logger.info("Module mysql.connector not found. Trying to install it")
import pip
pip.main(['install', 'mysql-connector-python'])
logger.info("Done. Trying to import again.")
import mysql.connector
# get values
logger.debug('Enter MySQL username:')
CONFIG['MySQL']['user'] = raw_input()
logger.debug('Enter MySQL Password:')
CONFIG['MySQL']['pass'] = raw_input()
logger.debug('Enter old MySQL database name (the one from which you are going to port):')
CONFIG['MySQL']['oldname'] = raw_input()
logger.debug('Enter new MySQL database name (the one to which new data is transferred):')
CONFIG['MySQL']['dbname'] = raw_input()
logger.info('Checking DB Name conventions, and rules')
if not CONFIG['MySQL']['dbname'].endswith('line'):
logger.error('Database name should end with line (eg. Timeline), a strictly enforced nomenclature.')
sys.exit(0)
logger.info('DB naming rules check - clear')
logger.info('Checking MySQL Credentials')
try:
MYSQL_CONN = mysql.connector.connect(user=CONFIG['MySQL']['user'], password=CONFIG['MySQL']['pass'], database=CONFIG['MySQL']['oldname'])
except Exception, e:
print 'Error MySQL', e
os._exit(1)
@inlineCallbacks
def setupDatabase():
cursor = MYSQL_CONN.cursor()
a = cursor.execute("SELECT 1")
list(cursor)
logger.info("MySQL Connection - good")
logger.info('Creating database {db}, if not exist', db=CONFIG['MySQL']['dbname'])
cursor.execute("CREATE DATABASE IF NOT EXISTS `%s`" % (CONFIG['MySQL']['dbname'],))
logger.info("Connecting to database - {db}", db=CONFIG['MySQL']['dbname'])
cursor.close()
MYSQL_CONN.close()
MYSQL_CONN.connect(database=CONFIG['MySQL']['dbname'])
cursor = MYSQL_CONN.cursor()
cursor.execute('SELECT DATABASE();')
list(cursor)
logger.info("Checking connection to new database...")
cursor.execute('SELECT 1')
list(cursor)
logger.info("Connection status - Good")
logger.debug("You sure you wanna continue? All data in database - {db} will be wiped.", db=CONFIG['MySQL']['dbname'])
confirmation = raw_input('Y/n? ')
if confirmation not in ('Y', 'y'):
print 'Bye! :~('
os._exit(1)
logger.info('Creating tables if not exists')
import time
oldname = '`{}`'.format(CONFIG['MySQL']['oldname'])
newname = '`{}`'.format(CONFIG['MySQL']['dbname'])
DBSSQL = open('timeline.sql', 'r')\
.read().replace("`times-cp`", oldname).replace('`timeline`', newname)
DBStructureSQL = DBSSQL.split('------- PROCEDURES ------------- ')[0].split(';')
DBProcedureSQL = DBSSQL.split('------- PROCEDURES ------------- ')[1]\
.split('-- --------------------------------------------------------')
DBSQL = DBStructureSQL + DBProcedureSQL
for sql in DBSQL:
print 'executing:', sql
try:
r = cursor.execute(sql)
except:
# try with multi
try:
r = cursor.execute(sql, multi=True)
except:
print 'skipping it...'
list(cursor)
cursor.close()
MYSQL_CONN.close()
MYSQL_CONN.connect(database=CONFIG['MySQL']['dbname'])
cursor = MYSQL_CONN.cursor()
logger.info("Done creating tables")
logger.info("Done executing procedures. Ready to port old data set to new one!")
logger.info("Chilling for a bit...")
time.sleep(1)
logger.info("Commiting to MySQL")
# bitches stop irking me, this procedure is done once for all
'''
logger.info('About to start import old-db set to new one...')
PROCEDURES = ['IMPORT_INVENTORY_FROM_CP_STRUCT' , 'IMPORT_PENGUIN_FROM_CP_STRUCT', 'IMPORT_ASSETS_FROM_CP_STRUCT',
'IMPORT_STAMP_FROM_CP_STRUCT', 'IMPORT_PUFFLE_FROM_CP_STRUCT', 'IMPORT_FRIENDS_FROM_CP_STRUCT']
for procedure in PROCEDURES:
logger.info("Executing {prod}", prod=procedure)
stats = cursor.callproc(procedure)
logger.debug("{prod} - Done", prod=procedure)
print list(stats)
MYSQL_CONN.commit()
cursor.close()
logger.info("Done...!")
logger.info("Successfully imported all penguin data from old-db structure.")
logger.info("At last, importing your igloo likes into new DB Structure.")
logger.info("Fetching igloo likes...")
cursor = MYSQL_CONN.cursor()
cursor.execute('SELECT `id`, `likes` FROM `{}`.`igloos` WHERE 1'.format(CONFIG['MySQL']['oldname']))
# [{"count": 1, "id": "{882977da-bf7d-11e7-ac97-a02bb82e593b}", "time": 1545461242}]
# NULL, igloo id, swid, likes, last like timestamp
logger.info("Fetched igloo likes. Parsing it...")
LIKE_SQL = []
for (igloo_id, igloo_like_json) in cursor:
igloo_likes_list = json.loads(igloo_like_json) if igloo_like_json != '' and igloo_like_json is not None else []
if len(igloo_likes_list) < 1:
continue
for like_data in igloo_likes_list:
LIKE_SQL.append('(NULL, {}, "{}", {}, TIMESTAMP({}))'
.format(igloo_id, like_data['id'], like_data['count'], like_data['time']))
logger.info("Parsed like data. Inserting it into new database...")
if len(LIKE_SQL) > 0:
LIKE_SQL_RAW = 'INSERT IGNORE INTO `{}`.`igloo_likes` VALUES {};'\
.format(CONFIG['MySQL']['dbname'], ', '.join(LIKE_SQL))
print 'executing:', LIKE_SQL_RAW
r = (cursor.execute(LIKE_SQL_RAW))
MYSQL_CONN.commit()
cursor.close()
MYSQL_CONN.close()
logger.info("Done inserting igloo like data.")
logger.info("One last thing, setting your new db in your Timeline configs, :-D")
Start_PY = file('./Start.py', 'r+')
Start_RAW = Start_PY.readlines()
for i in range(len(Start_RAW)):
line = Start_RAW[i]
if not line.startswith('DBMS = DBM'):
continue
break # found, modified, now exit :P
Start_PY.seek(0)
Start_PY.truncate()
Start_RAW[i] = 'DBMS = DBM(user = "{}", passd = "{}", db = "{}")\n'\
.format(CONFIG['MySQL']['user'], CONFIG['MySQL']['pass'], CONFIG['MySQL']['dbname'])
Start_PY.write(''.join(Start_RAW))
Start_PY.close()
'''
logger.info("Done :D")
logger.info("All done, all clear, all set!")
logger.info("Successfully imported your old `-cp` styled data structure to new one!")
logger.info("Thank you! Enjoy the new version of timeline!!!")
print \
'''
- Script by dote. Exiting DatabasePort.py
- BYE ! :~D
'''
os._exit(0)
setupDatabase()
reactor.run()