Repository navigation
Expand file tree
/
Copy pathmigrate_users.py
More file actions
113 lines (91 loc) · 5.32 KB
/
Copy pathmigrate_users.py
File metadata and controls
113 lines (91 loc) · 5.32 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
from utils.db import PostgresORM, SupabaseInterface
from datetime import datetime
from shared_migrations.db.server import ServerQueries
class MigrateContributors:
def __init__(self):
self.postgres_client = ServerQueries()
self.supabase_client = SupabaseInterface.get_instance()
return
async def migration(self):
try:
#migrate contributors discord
await self.migrate_contributors_discord()
#migrate contributors registraiton
await self.migrate_contributors_registration()
return 'success'
except Exception as e:
print('Exception occured while migrating users ', e)
return 'failed'
def convert_to_datetime(self, date_str):
dt = datetime.strptime(date_str, '%Y-%m-%dT%H:%M:%S.%f%z')
return dt.replace(tzinfo=None)
async def migrate_contributors_discord(self):
try:
supabase_contributors_discord = self.supabase_client.readAll("contributors_discord")
postgres_contributors_discord = await self.postgres_client.readAll("contributors_discord")
print('length of supabase_contributors_discord ', len(supabase_contributors_discord))
print('length of postgres_contributors_discord ', len(postgres_contributors_discord))
if len(supabase_contributors_discord) == len(postgres_contributors_discord):
print('skipping migration to contributors discord as both lens are equal')
return
for contributor in supabase_contributors_discord:
# Ensure the correct key is used to compare 'discord_id' values.
discord_id = contributor['discord_id']
present = next((pcd for pcd in postgres_contributors_discord if pcd['discord_id'] == discord_id), None)
if present:
continue
else:
#insert the record in postgres
# format = ''
print('inserting contributors discord for ', contributor['discord_id'])
contributors_data = {
'github_url': contributor['github_url'],
'discord_id': contributor['discord_id'],
'discord_username': contributor['discord_username'],
'joined_at': self.convert_to_datetime(contributor['joined_at']),
'email': contributor['email'],
'chapter': contributor['chapter'],
'gender': contributor['gender'],
'is_active': contributor['is_active'],
'country': contributor['country'],
'city': contributor['city'],
'experience': contributor['experience'],
'field_name': contributor['name']
}
await self.postgres_client.add_data(contributors_data, "contributors_discord")
return 'success'
except Exception as e:
print('Exception occured while migrate contributors discord ', e)
return 'failed'
async def migrate_contributors_registration(self):
try:
supabase_contributors_registration = self.supabase_client.readAll("contributors_registration")
postgres_contributors_registration = await self.postgres_client.readAll("contributors_registration")
print('length of supabase_contributors_registration ', len(supabase_contributors_registration))
print('length of postgres_contributors_registration ', len(postgres_contributors_registration))
if len(supabase_contributors_registration) == len(postgres_contributors_registration):
print('skipping migration to contributors discord as both lens are equal')
return
for contributor in supabase_contributors_registration:
# Ensure the correct key is used to compare 'discord_id' values.
discord_id = contributor['discord_id']
present = next((pcd for pcd in postgres_contributors_registration if pcd['discord_id'] == discord_id), None)
if present:
continue
else:
#insert the record in postgres
print('inserting contributors discord for ', contributor['discord_id'])
contributors_data = {
'discord_id': contributor['discord_id'],
'github_id': contributor['github_id'],
'github_url': contributor['github_url'],
'discord_username': contributor['discord_username'],
'joined_at': self.convert_to_datetime(contributor['joined_at']),
'email': contributor['email'],
'name': contributor['name']
}
await self.postgres_client.add_data(contributors_data, "contributors_registration")
return 'success'
except Exception as e:
print('Exception occured while migrate contributors discord ', e)
return 'failed'