-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate-profiles.js
More file actions
205 lines (175 loc) · 6.15 KB
/
migrate-profiles.js
File metadata and controls
205 lines (175 loc) · 6.15 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
// Targeted script to migrate profiles only
require('dotenv').config();
const https = require('https');
const Database = require('better-sqlite3');
// SQLite connection
const sqliteDb = new Database('./db/elriel.db', { verbose: console.log });
// Supabase connection details
const supabaseUrl = process.env.SUPABASE_URL.replace('https://', '');
const supabaseKey = process.env.SUPABASE_SERVICE_KEY;
// Helper to pause execution when needed
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
// Function to make a request to Supabase REST API
function supabaseRequest(method, path, data = null) {
return new Promise((resolve, reject) => {
const options = {
hostname: supabaseUrl,
path: path,
method: method,
headers: {
'Content-Type': 'application/json',
'apikey': supabaseKey,
'Authorization': `Bearer ${supabaseKey}`
}
};
if (data) {
options.headers['Content-Length'] = Buffer.byteLength(JSON.stringify(data));
}
const req = https.request(options, (res) => {
let responseData = '';
res.on('data', (chunk) => {
responseData += chunk;
});
res.on('end', () => {
try {
const parsedData = responseData ? JSON.parse(responseData) : {};
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(parsedData);
} else {
reject({
statusCode: res.statusCode,
error: parsedData
});
}
} catch (error) {
reject({
statusCode: res.statusCode,
error: error.message,
data: responseData
});
}
});
});
req.on('error', (error) => {
reject(error);
});
if (data) {
req.write(JSON.stringify(data));
}
req.end();
});
}
// Function to format data from SQLite for Supabase
const formatForSupabase = (data) => {
// Convert SQLite timestamps to ISO format for PostgreSQL
const formattedData = {...data};
for (const key in formattedData) {
if (typeof formattedData[key] === 'string' &&
(key.includes('_at') || key.includes('date') || key === 'created_at' || key === 'updated_at' || key === 'last_login')) {
try {
// Try to parse as date if it looks like a timestamp
if (formattedData[key] && !formattedData[key].includes('Z') &&
(formattedData[key].includes('-') || formattedData[key].includes(':'))) {
const date = new Date(formattedData[key]);
if (!isNaN(date.getTime())) {
formattedData[key] = date.toISOString();
}
}
} catch (e) {
// Keep original value if parsing fails
console.warn(`Failed to parse date for ${key}: ${formattedData[key]}`);
}
}
}
return formattedData;
};
// Simplify by not checking columns
async function verifyConnection() {
try {
await supabaseRequest('GET', '/rest/v1/profiles?select=id&limit=1', null);
console.log('Successfully connected to Supabase and profiles table exists');
return true;
} catch (error) {
console.error('Error connecting to Supabase or profiles table:', error);
return false;
}
}
// Migrate profiles
async function migrateProfiles() {
console.log('Migrating profiles table...');
// Get all profiles from SQLite
const profiles = sqliteDb.prepare('SELECT * FROM profiles').all();
console.log(`Found ${profiles.length} profiles to migrate.`);
if (profiles.length === 0) {
console.log('No profiles to migrate');
return;
}
// Log the structure of the first profile
console.log('Sample profile structure:', Object.keys(profiles[0]));
// Proceed with migration
for (const profile of profiles) {
const formattedProfile = formatForSupabase(profile);
// Strip out any fields that might not exist in the target table
const cleanProfile = {
id: formattedProfile.id,
user_id: formattedProfile.user_id,
status: formattedProfile.status,
background_image: formattedProfile.background_image,
custom_css: formattedProfile.custom_css,
custom_html: formattedProfile.custom_html,
blog_layout: formattedProfile.blog_layout,
glyph_id: formattedProfile.glyph_id,
district_id: formattedProfile.district_id,
created_at: formattedProfile.created_at,
updated_at: formattedProfile.updated_at
};
console.log('Migrating profile:', cleanProfile);
try {
await supabaseRequest('POST', '/rest/v1/profiles', [cleanProfile]);
console.log(`Successfully migrated profile for user_id: ${cleanProfile.user_id}`);
} catch (error) {
console.error('Error inserting profile:', error);
// If there's a primary key violation, try an upsert instead
if (error.statusCode === 409) {
try {
console.log('Attempting upsert...');
await supabaseRequest('POST', '/rest/v1/profiles?on_conflict=id', [cleanProfile]);
console.log(`Successfully upserted profile for user_id: ${cleanProfile.user_id}`);
} catch (upsertError) {
console.error('Upsert failed:', upsertError);
}
}
}
// Avoid rate limiting
await sleep(1000);
}
console.log('Profiles migration completed.');
}
// Main function
async function main() {
try {
console.log('Starting targeted profiles migration...');
// Check connection and verify profiles table exists
const connected = await verifyConnection();
if (!connected) {
console.error('Could not proceed with migration. Please check Supabase setup.');
return;
}
// Migrate profiles
await migrateProfiles();
console.log('Migration completed successfully!');
} catch (err) {
console.error('Migration failed:', err);
} finally {
// Close SQLite connection
sqliteDb.close();
}
}
// Check that we have the required environment variables
if (!supabaseUrl || !supabaseKey) {
console.error('Error: Missing Supabase credentials in environment variables.');
console.error('Please set SUPABASE_URL and SUPABASE_SERVICE_KEY in your .env file.');
process.exit(1);
}
// Execute the migration
main();