-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
117 lines (88 loc) · 3.47 KB
/
index.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
import 'dotenv/config'
import schema from './database/schema.js';
import initialTableCreation from './database/initial-table-creation.js'
import sqlite3 from 'sqlite3'
import { open } from 'sqlite'
import { createClient } from '@libsql/client'
import { log } from './logger/index.js'
import { truncate } from './helpers/index.js'
sqlite3.verbose();
const sourceDbPath = process.env.SOURCE_DATABASE_PATH;
const tursoConnectionString = process.env.TURSO_DATABASE_URL;
const tursoConnectAuthToken = process.env.TURSO_AUTH_TOKEN;
const sourceDb = await open({
filename: sourceDbPath,
driver: sqlite3.Database,
});
const tursoDb = createClient({
url: tursoConnectionString,
authToken: tursoConnectAuthToken,
});
async function migrateTable(table, primaryColumn, columns = []) {
log(`\nMigrating table ${table}...`);
if (!await tableExistsOnTursoDb(table)) {
log(`Table ${table} does not exist in Turso database. Creating...`);
await createTableOnTursoDb(table);
} else {
log(`Table ${table} already exists in Turso database. Skipping creation...`);
}
const sourceQuery = `SELECT * FROM ${table}`;
const result = await sourceDb.all(sourceQuery);
for (const row of result) {
const commonIdentifier = `${table}[${primaryColumn}] = ${row[primaryColumn]}`;
log(`Migrating row with ${commonIdentifier}`);
const exists = await keyExistsInTursoDb(table, primaryColumn, row[primaryColumn]);
if (exists) {
log(`Row with ${commonIdentifier} already exists in Turso database. Updating...`)
await updateRowOnTursoDb(table, columns, Object.values(row));
continue;
}
log(`Row with ${commonIdentifier} does not exist in Turso database. Inserting...`)
await insertRowOnTursoDb(table, columns, Object.values(row));
}
}
async function keyExistsInTursoDb(table, primaryColumn, key) {
const query = `SELECT * FROM ${table} WHERE ${primaryColumn} = '${key}'`;
const result = await tursoDb.execute(query);
return result.rows.length > 0;
}
async function tableExistsOnTursoDb(tableName) {
const query = `SELECT name FROM sqlite_master WHERE type='table' AND name='${tableName}'`;
const exists = await tursoDb.execute(query);
return exists.rows.length > 0;
}
async function createTableOnTursoDb(tableName) {
const query = initialTableCreation[tableName];
await tursoDb.execute(query);
}
async function updateRowOnTursoDb(table, columns = [], values = []) {
const updateStatements = columns.map((column, index) => `${column} = '${values[index]}'`);
const query = `UPDATE ${table} SET ${updateStatements.join(', ')} WHERE ${columns[0]} = '${values[0]}'`;
log(`Update query: ${truncate(query)}`);
await tursoDb.execute(query);
}
async function insertRowOnTursoDb(table, columns = [], values = []) {
const columnsString = columns.join(', ');
values = values.map(value => `'${value}'`);
const query = `INSERT INTO ${table} (${columnsString}) VALUES (${values.join(', ')})`;
log(`Insert query: ${truncate(query)}`);
await tursoDb.execute(query);
}
(async function main() {
try {
console.time('migration-time');
const migrationPromises = [];
for (const tableIndex in schema) {
migrationPromises.push(
migrateTable(tableIndex, schema[tableIndex][0], schema[tableIndex])
);
}
await Promise.all(migrationPromises);
} catch (error) {
console.error('Error during data transfer:', error);
} finally {
await sourceDb.close();
tursoDb.close();
console.timeEnd('migration-time');
}
})();