-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmigrate.py
More file actions
286 lines (227 loc) · 8.18 KB
/
Copy pathmigrate.py
File metadata and controls
286 lines (227 loc) · 8.18 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python3
"""
Migration script to populate SQLite database from JSON files in the 'data migrate' folder.
Creates all necessary tables and populates them with data.
Keeps PortLogNotification table empty as requested.
"""
import json
import os
from datetime import datetime
from pathlib import Path
from models import (
db,
Port,
User,
Vessel,
PortSubscription,
VesselSubscription,
PortLog,
PortLogNotification,
)
def load_json_file(filename):
"""Load JSON data from file in the 'data migrate' folder."""
filepath = Path(__file__).parent / "data migrate" / filename
if not filepath.exists():
print(f"Warning: {filepath} not found")
return {}
with open(filepath, 'r', encoding='utf-8') as f:
data = json.load(f)
return data
def migrate_ports():
"""Migrate Port data from port.json"""
print("Migrating Ports...")
data = load_json_file("port.json")
ports = data.get("port", [])
for port_data in ports:
Port.get_or_create(
id=port_data["id"],
defaults={"name": port_data["name"]}
)
print(f" ✓ Migrated {len(ports)} ports")
def migrate_vessels():
"""Migrate Vessel data from vessel.json"""
print("Migrating Vessels...")
data = load_json_file("vessel.json")
vessels = data.get("vessel", [])
for vessel_data in vessels:
last_port_id = vessel_data.get("last_port_id")
# Only set last_port if it exists in the database
last_port = None
if last_port_id:
try:
last_port = Port.get_by_id(last_port_id)
except:
pass
Vessel.get_or_create(
id=vessel_data["id"],
defaults={
"name": vessel_data["name"],
"vessel_type": vessel_data.get("vessel_type"),
"contact": vessel_data.get("contact"),
"last_port": last_port,
"last_port_log_id": vessel_data.get("last_port_log_id"),
}
)
print(f" ✓ Migrated {len(vessels)} vessels")
def migrate_users():
"""Migrate User data from user.json"""
print("Migrating Users...")
data = load_json_file("user.json")
users = data.get("user", [])
for user_data in users:
main_port_id = user_data.get("main_port_id")
main_port = None
# Only set main_port if it exists in the database
if main_port_id:
try:
main_port = Port.get_by_id(main_port_id)
except:
pass
# Parse date_joined if it's a string
date_joined = user_data.get("date_joined")
if isinstance(date_joined, str):
try:
date_joined = datetime.fromisoformat(date_joined.replace('Z', '+00:00'))
except:
date_joined = datetime.now()
User.get_or_create(
chat_id=user_data["chat_id"],
defaults={
"chat_type": user_data["chat_type"],
"username": user_data.get("username"),
"first_name": user_data["first_name"],
"last_name": user_data.get("last_name"),
"date_joined": date_joined,
"main_port": main_port,
"notify_on_departure": user_data.get("notify_on_departure", True),
}
)
print(f" ✓ Migrated {len(users)} users")
def migrate_port_subscriptions():
"""Migrate PortSubscription data from portsubscription.json"""
print("Migrating Port Subscriptions...")
data = load_json_file("portsubscription.json")
subscriptions = data.get("portsubscription", [])
created_count = 0
for sub_data in subscriptions:
user_id = sub_data["user_id"]
port_id = sub_data["port_id"]
try:
user = User.get_by_id(user_id)
port = Port.get_by_id(port_id)
PortSubscription.get_or_create(
user=user,
port=port
)
created_count += 1
except:
# Skip if user or port doesn't exist
pass
print(f" ✓ Migrated {created_count} port subscriptions")
def migrate_vessel_subscriptions():
"""Migrate VesselSubscription data from vesselsubscription.json"""
print("Migrating Vessel Subscriptions...")
data = load_json_file("vesselsubscription.json")
subscriptions = data.get("vesselsubscription", [])
created_count = 0
for sub_data in subscriptions:
user_id = sub_data["user_id"]
vessel_id = sub_data["vessel_id"]
try:
user = User.get_by_id(user_id)
vessel = Vessel.get_by_id(vessel_id)
VesselSubscription.get_or_create(
user=user,
vessel=vessel
)
created_count += 1
except:
# Skip if user or vessel doesn't exist
pass
print(f" ✓ Migrated {created_count} vessel subscriptions")
def migrate_portlogs():
"""Migrate PortLog data from portlog.json using batch inserts"""
print("Migrating Port Logs...")
data = load_json_file("portlog.json")
portlogs = data.get("portlog", [])
batch_size = 1000
created_count = 0
skipped_count = 0
# Prepare batch of records
batch = []
for log_data in portlogs:
try:
vessel_id = log_data["vessel_id"]
port_id = log_data["port_id"]
# Verify both vessel and port exist
vessel = Vessel.get_by_id(vessel_id)
port = Port.get_by_id(port_id)
# Parse timestamp if it's a string
timestamp = log_data.get("timestamp")
if isinstance(timestamp, str):
try:
timestamp = datetime.fromisoformat(timestamp.replace('Z', '+00:00'))
except:
timestamp = datetime.now()
# Create PortLog object (not saved yet)
port_log = PortLog(
timestamp=timestamp,
vessel=vessel,
port=port,
event=log_data["event"],
notified=log_data.get("notified", False),
)
batch.append(port_log)
# Insert batch when it reaches batch_size
if len(batch) >= batch_size:
PortLog.bulk_create(batch, batch_size=batch_size)
created_count += len(batch)
print(f" ✓ Inserted {created_count} port logs so far...")
batch = []
except Exception as e:
# Skip if vessel or port doesn't exist
skipped_count += 1
# Insert remaining records
if batch:
PortLog.bulk_create(batch, batch_size=batch_size)
created_count += len(batch)
print(f" ✓ Migrated {created_count} port logs (skipped {skipped_count})")
def create_tables():
"""Create all necessary tables."""
print("Creating database tables...")
db.create_tables([
Port,
User,
Vessel,
PortSubscription,
VesselSubscription,
PortLog,
PortLogNotification,
])
print(" ✓ Database tables created")
def main():
"""Run the complete migration."""
print("\n" + "="*50)
print("STARTING DATABASE MIGRATION")
print("="*50 + "\n")
try:
# Create tables first
create_tables()
# Migrate data in order of dependencies
migrate_ports()
migrate_vessels()
migrate_users()
migrate_port_subscriptions()
migrate_vessel_subscriptions()
migrate_portlogs()
# PortLogNotification table is kept empty as requested
print("\nMigration Notifications:")
print(" ℹ PortLogNotification table created but left empty (as requested)")
print("\n" + "="*50)
print("✓ MIGRATION COMPLETED SUCCESSFULLY")
print("="*50 + "\n")
except Exception as e:
print(f"\n✗ MIGRATION FAILED: {e}")
raise
if __name__ == "__main__":
main()