-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmigrate.js
211 lines (183 loc) · 5.72 KB
/
migrate.js
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
const Database = require("better-sqlite3");
const { Pool } = require("pg");
const db = new Database("./data/dev.db");
const pool = new Pool({
user: "fluid",
host: "localhost",
database: "fluid_calendar",
password: "fluid",
port: 5432,
});
// Helper function to convert SQLite timestamps to PostgreSQL timestamps
function convertValue(value, columnName) {
if (value === null) {
return null;
}
// Handle boolean values
if (typeof value === "boolean") {
return value ? 1 : 0;
}
// Handle string booleans
if (value === "true" || value === "false") {
return value === "true" ? 1 : 0;
}
// Handle timestamp fields
const timestampFields = [
"dueDate",
"scheduledStart",
"scheduledEnd",
"lastCompletedDate",
"createdAt",
"updatedAt",
"lastSyncedAt",
"start",
"end",
"created",
"lastModified",
"lastImported",
"expiresAt",
"expires",
"emailVerified",
"lastSync",
"channelExpiration",
];
if (timestampFields.includes(columnName)) {
try {
let timestamp = value;
if (typeof timestamp === "string") {
timestamp = new Date(timestamp).getTime();
}
if (typeof timestamp === "number") {
// Convert seconds to milliseconds if needed
if (timestamp < 1e12) {
timestamp *= 1000;
}
// Cap timestamps to a safe range
const minDate = new Date("2000-01-01T00:00:00.000Z").getTime();
const maxDate = new Date("2037-12-31T23:59:59.999Z").getTime();
timestamp = Math.min(Math.max(timestamp, minDate), maxDate);
return new Date(timestamp).toISOString();
}
} catch (error) {
console.error(`Error converting timestamp for ${columnName}:`, error);
return new Date("2000-01-01T00:00:00.000Z").toISOString();
}
}
return value;
}
async function insertRow(client, tableName, columns, values) {
const placeholders = values.map((_, i) => `$${i + 1}`).join(", ");
const query = `
INSERT INTO "${tableName}" (${columns.map((c) => `"${c}"`).join(", ")})
VALUES (${placeholders})
ON CONFLICT DO NOTHING
`;
try {
await client.query(query, values);
return true;
} catch (error) {
console.error(`Error inserting row in ${tableName}:`, error.message);
console.error(
"Problematic row:",
JSON.stringify(
Object.fromEntries(columns.map((col, i) => [col, values[i]])),
null,
2
)
);
console.error("Converted values:", values);
return false;
}
}
async function migrateTable(client, tableName, query = null) {
console.log(`Migrating table: ${tableName}`);
const sqliteRows = await db
.prepare(query || `SELECT * FROM "${tableName}"`)
.all();
if (sqliteRows.length === 0) {
console.log(`No data in table ${tableName}`);
return;
}
let successCount = 0;
const columns = Object.keys(sqliteRows[0]);
for (const row of sqliteRows) {
const values = columns.map((column) => convertValue(row[column], column));
if (await insertRow(client, tableName, columns, values)) {
successCount++;
}
}
console.log(
`Successfully migrated ${successCount} out of ${sqliteRows.length} rows from ${tableName}`
);
}
async function ensureCalendarFeedExists(client, feedId) {
// Check if feed exists in SQLite
const feed = db
.prepare('SELECT * FROM "CalendarFeed" WHERE id = ?')
.get(feedId);
if (!feed) {
console.log(`Feed ${feedId} not found in SQLite database`);
return false;
}
// Insert feed into PostgreSQL if it doesn't exist
const columns = Object.keys(feed);
const values = columns.map((column) => convertValue(feed[column], column));
return await insertRow(client, "CalendarFeed", columns, values);
}
async function migrate() {
const client = await pool.connect();
try {
// Start with independent tables
await migrateTable(client, "Tag");
await migrateTable(client, "User");
await migrateTable(client, "SystemSettings");
// Migrate tables that depend on User
await migrateTable(client, "Account");
await migrateTable(client, "Session");
await migrateTable(client, "AutoScheduleSettings");
await migrateTable(client, "ConnectedAccount");
// Migrate Project before Task and OutlookTaskListMapping
await migrateTable(client, "Project");
// Migrate Task and its relationships
await migrateTable(client, "Task");
await migrateTable(client, "_TagToTask");
// Migrate CalendarFeed before CalendarEvent
await migrateTable(client, "CalendarFeed");
// For CalendarEvent, ensure feeds exist before inserting events
const events = db.prepare('SELECT * FROM "CalendarEvent"').all();
let successCount = 0;
for (const event of events) {
// Ensure feed exists
if (
event.feedId &&
!(await ensureCalendarFeedExists(client, event.feedId))
) {
console.log(
`Skipping event ${event.id} due to missing feed ${event.feedId}`
);
continue;
}
const columns = Object.keys(event);
const values = columns.map((column) =>
convertValue(event[column], column)
);
if (await insertRow(client, "CalendarEvent", columns, values)) {
successCount++;
}
}
console.log(
`Successfully migrated ${successCount} out of ${events.length} rows from CalendarEvent`
);
// Migrate OutlookTaskListMapping last since it depends on Project
await migrateTable(client, "OutlookTaskListMapping");
await migrateTable(client, "VerificationToken");
console.log("Migration completed successfully");
} catch (error) {
console.error("Migration failed:", error);
} finally {
client.release();
await pool.end();
await db.close();
}
}
migrate().catch(console.error);