-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathdatamove.py
229 lines (181 loc) · 8.53 KB
/
datamove.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
import logging
import sys
from tortoise import Model, fields, Tortoise, run_async
class LoggedMessage(Model):
messageid = fields.BigIntField(pk=True, generated=False)
content = fields.CharField(max_length=2000, collation="utf8mb4_general_ci", null=True)
author = fields.BigIntField()
channel = fields.BigIntField()
server = fields.BigIntField()
type = fields.IntField(null=True)
pinned = fields.BooleanField(default=False)
class Meta:
app = "models"
class NewLoggedMessage(Model):
messageid = fields.BigIntField(pk=True, generated=False)
content = fields.CharField(max_length=2000, collation="utf8mb4_general_ci", null=True)
author = fields.BigIntField()
channel = fields.BigIntField()
server = fields.BigIntField()
type = fields.IntField(null=True)
pinned = fields.BooleanField(default=False)
class Meta:
app = "new"
class LoggedAttachment(Model):
id = fields.BigIntField(pk=True, generated=False)
name = fields.CharField(max_length=100)
isImage = fields.BooleanField()
message = fields.ForeignKeyField("models.LoggedMessage", related_name='attachments', source_field='message_id')
class Meta:
app = "models"
class NewLoggedAttachment(Model):
id = fields.BigIntField(pk=True, generated=False)
name = fields.CharField(max_length=100)
isImage = fields.BooleanField()
message = fields.ForeignKeyField("models.LoggedMessage", related_name='attachments', source_field='message_id')
class Meta:
app = "new"
class CustomCommand(Model):
id = fields.IntField(pk=True, generated=True)
serverid = fields.BigIntField()
trigger = fields.CharField(max_length=20, collation="utf8mb4_general_ci")
response = fields.CharField(max_length=2000, collation="utf8mb4_general_ci")
created_by = fields.BigIntField(null=True)
class Meta:
app = "models"
class NewCustomCommand(Model):
id = fields.IntField(pk=True, generated=True)
serverid = fields.BigIntField()
trigger = fields.CharField(max_length=20, collation="utf8mb4_general_ci")
response = fields.CharField(max_length=2000, collation="utf8mb4_general_ci")
created_by = fields.BigIntField(null=True)
class Meta:
app = "new"
class Infraction(Model):
id = fields.IntField(pk=True, generated=True)
guild_id = fields.BigIntField()
user_id = fields.BigIntField()
mod_id = fields.BigIntField()
type = fields.CharField(max_length=10, collation="utf8mb4_general_ci")
reason = fields.CharField(max_length=2000, collation="utf8mb4_general_ci")
start = fields.BigIntField()
end = fields.BigIntField(null=True)
active = fields.BooleanField(default=True)
class Meta:
app = "models"
class NewInfraction(Model):
id = fields.IntField(pk=True, generated=True)
guild_id = fields.BigIntField()
user_id = fields.BigIntField()
mod_id = fields.BigIntField()
type = fields.CharField(max_length=10, collation="utf8mb4_general_ci")
reason = fields.CharField(max_length=2000, collation="utf8mb4_general_ci")
start = fields.BigIntField()
end = fields.BigIntField(null=True)
active = fields.BooleanField(default=True)
class Meta:
app = "new"
class Reminder(Model):
id = fields.IntField(pk=True, generated=True)
user_id = fields.BigIntField()
channel_id = fields.BigIntField()
guild_id = fields.CharField(max_length=20, null=True)
message_id = fields.BigIntField()
dm = fields.BooleanField()
to_remind = fields.CharField(max_length=1800, collation="utf8mb4_general_ci")
send = fields.BigIntField(null=True)
time = fields.BigIntField()
status = fields.IntField()
class Meta:
app = "models"
class NewReminder(Model):
id = fields.IntField(pk=True, generated=True)
user_id = fields.BigIntField()
channel_id = fields.BigIntField()
guild_id = fields.CharField(max_length=20, null=True)
message_id = fields.BigIntField(null=True)
dm = fields.BooleanField()
to_remind = fields.CharField(max_length=1800, collation="utf8mb4_general_ci")
send = fields.BigIntField(null=True)
time = fields.BigIntField()
status = fields.IntField()
class Meta:
app = "new"
LOGGER = logging.getLogger('datamover')
LOGGER.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s')
handler = logging.StreamHandler(stream=sys.stdout)
handler.setLevel(logging.INFO)
handler.setFormatter(formatter)
LOGGER.addHandler(handler)
async def run():
old_url = input("Source database url: ")
new_url = input("Destination database url: ")
await Tortoise.init(
config={
'connections': {
'old': old_url,
'new': new_url
},
'apps': {
'my_app': {
'models': ['__main__'],
# If no default_connection specified, defaults to 'default'
'default_connection': 'old',
},
'models': {'models': ['__main__'], 'default_connection': 'old'}
}
}
)
new = Tortoise.get_connection('new')
LOGGER.info("Database connections established")
LOGGER.info("Staring custom commands migration")
ccs = await CustomCommand.filter()
LOGGER.info(f"Found {len(ccs)} custom commands, migrating...")
await CustomCommand.bulk_create([NewCustomCommand(id=cc.id, serverid=cc.serverid, trigger=cc.trigger, response=cc.response,created_by=cc.created_by) for cc in ccs], new)
await new.execute_script("select setval('customcommand_id_seq', (select max(id) from customcommand))")
LOGGER.info("Custom commands migrated")
LOGGER.info("Starting reminders migration")
reminders = await Reminder.filter()
LOGGER.info(f"Found {len(reminders)} reminders, migrating...")
await Reminder.bulk_create([NewReminder(id=reminder.id, user_id=reminder.user_id, channel_id=reminder.channel_id, guild_id=reminder.guild_id, message_id=reminder.message_id, dm=reminder.dm, to_remind=reminder.to_remind, send=reminder.send, time=reminder.time, status=reminder.status) for reminder in reminders], new)
await new.execute_script("select setval('reminder_id_seq', (select max(id) from reminder))")
LOGGER.info("Reminders migrated")
LOGGER.info("Starting infractions migration")
todo = await Infraction.filter().count()
LOGGER.info(f"Found {todo} infractions to migrate")
done = 0
while done < todo:
chunk = await Infraction.filter().offset(done).limit(10000)
new_infractions=[NewInfraction(id=infraction.id, guild_id=infraction.guild_id, user_id=infraction.user_id, mod_id=infraction.mod_id, type=infraction.type, reason=infraction.reason, start=infraction.start, end=infraction.end, active=infraction.active) for infraction in chunk]
await Infraction.bulk_create(new_infractions, new)
done += len(new_infractions)
LOGGER.info(f"{done}/{todo} infractions migrated...")
await new.execute_script("select setval('infraction_id_seq', (select max(id) from infraction))")
LOGGER.info("Infractions migrated")
LOGGER.info("Starting logged messages migration")
todo = await LoggedMessage.filter().count()
LOGGER.info(f"Found {todo} logged messages to migrate")
done = 0
while done < todo:
chunk = await LoggedMessage.filter().order_by("-messageid").offset(done).limit(10000)
new_messages=[NewLoggedMessage(messageid=message.messageid, content=message.content, author=message.author, channel=message.channel, server=message.server, type=message.type, pinned=message.pinned) for message in chunk]
try:
await LoggedMessage.bulk_create(new_messages, new)
except:
LOGGER.error("Logged message chunk failed!")
done += len(new_messages)
LOGGER.info(f"{done}/{todo} logged messages migrated...")
LOGGER.info("Logged messages migrated")
LOGGER.info("Starting logged attachments migration")
todo = await LoggedAttachment.filter().count()
LOGGER.info(f"Found {todo} logged attachments to migrate")
done = 0
while done < todo:
chunk = await LoggedAttachment.filter().order_by("-id").offset(done).limit(10000)
new_attachments=[NewLoggedAttachment(id=attachment.id, name=attachment.name, message=attachment.message, isImage=attachment.isImage) for attachment in chunk]
await LoggedAttachment.bulk_create(new_attachments, new)
done += len(new_attachments)
LOGGER.info(f"{done}/{todo} logged attachments migrated...")
LOGGER.info("Logged attachments migrated")
run_async(run())