-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
correctly convert CouchDB to SQLite (first tree of given user)
- Loading branch information
1 parent
4b60842
commit cd20de8
Showing
2 changed files
with
74 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,49 @@ | ||
import * as data from '../shared/data.js'; | ||
import * as hiddenConfig from '../../hidden-config.js'; | ||
import { Database } from 'bun:sqlite'; | ||
import PouchDB from "pouchdb"; | ||
|
||
const db = new Database(hiddenConfig.SQLITE_DB_PATH); | ||
console.log(db); | ||
const couchdbTreesByUser = db.query(`SELECT * FROM trees WHERE location='couchdb' AND owner = ?`); | ||
const insertCard = db.query(`INSERT INTO cards (id, treeId, content, parentId, position, updatedAt, deleted) VALUES ($id, $treeId, $content, $parentId, $position, $updatedAt, $deleted)`); | ||
const insertCards = db.transaction( cards => { | ||
for (const card of cards) { | ||
insertCard.run(card); | ||
} | ||
}) | ||
const setTreeLocation = db.query(`UPDATE trees SET location='cardbased' WHERE id = ?`); | ||
|
||
/* ==== Elm Setup ==== */ | ||
|
||
const {Elm} = require('./MigrationWorker.js') | ||
|
||
const app = Elm.MigrationWorker.init(); | ||
|
||
app.ports.output.subscribe(function(data) { | ||
console.log(data); | ||
app.ports.output.subscribe(function([docId, data]) { | ||
try { | ||
console.log('Received conversion data for ', docId); | ||
insertCards(data.map(cardToQuery)); | ||
setTreeLocation.run(docId); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}); | ||
|
||
|
||
/* ==== Run Migrations ==== */ | ||
|
||
import * as data from '../shared/data.js'; | ||
import * as hiddenConfig from '../../hidden-config.js'; | ||
import { Database } from 'bun:sqlite'; | ||
import PouchDB from "pouchdb"; | ||
|
||
let remoteDB; | ||
const sqlite = new Database(hiddenConfig.SQLITE_DB_PATH); | ||
const couchdbTreesByUser = sqlite.query(`SELECT * FROM trees WHERE location='couchdb' AND owner = ?`); | ||
|
||
async function setupDb(email) { | ||
const userDbName = `userdb-${toHex(email)}`; | ||
const userDbUrl = "http://localhost:5984/" + userDbName; | ||
const remoteOpts = { skip_setup: true, auth: { | ||
const dbOpts = { skip_setup: true, auth: { | ||
username: hiddenConfig.COUCHDB_ADMIN_USERNAME, | ||
password: hiddenConfig.COUCHDB_ADMIN_PASSWORD | ||
} }; | ||
const remoteDB = new PouchDB(userDbUrl, remoteOpts); | ||
const couchdb = new PouchDB(userDbUrl, dbOpts); | ||
|
||
// Check remoteDB exists and accessible before continuing | ||
let remoteDBinfo = await remoteDB.info().catch((e) => {console.error(e)}); | ||
// Check couchdb exists and accessible before continuing | ||
let remoteDBinfo = await couchdb.info().catch((e) => {console.error(e)}); | ||
|
||
if (!remoteDBinfo) { | ||
console.error('Remote DB not found or not accessible'); | ||
|
@@ -40,17 +53,28 @@ async function setupDb(email) { | |
} | ||
|
||
// Get list of trees from sqlite for user | ||
const treesToConvert = await couchdbTreesByUser.all(email).map((row) => row.id); | ||
const treesToConvert = couchdbTreesByUser.all(email).map((row) => row.id); | ||
|
||
if (treesToConvert.length === 0) { | ||
console.error('No trees to convert'); | ||
return; | ||
} | ||
|
||
// get first treeId | ||
const treeId = treesToConvert[0]; | ||
|
||
// Load tree from CouchDB | ||
const [treeData, rest] = await data.load(remoteDB, treesToConvert[0]); | ||
const [treeData, rest] = await data.load(couchdb, treeId); | ||
|
||
console.log(treeData); | ||
app.ports.input.send(treeData); | ||
app.ports.input.send([treeId, treeData]); | ||
} | ||
|
||
|
||
setupDb("[email protected]"); | ||
if (Bun.argv.length == 3) { | ||
console.log('Running migration for', Bun.argv[2]); | ||
setupDb(Bun.argv[2]); | ||
} else { | ||
console.error('Usage: bun couchdb-to-sqlite.ts <email>'); | ||
} | ||
|
||
|
||
|
||
|
@@ -59,4 +83,16 @@ setupDb("[email protected]"); | |
|
||
function toHex (str: string) { | ||
return Array.from(str).map(c => c.charCodeAt(0).toString(16)).join(''); | ||
} | ||
|
||
function cardToQuery(card) { | ||
return { | ||
$id: card.id, | ||
$treeId: card.treeId, | ||
$content: card.content, | ||
$parentId: card.parentId, | ||
$position: card.position, | ||
$updatedAt: card.updatedAt, | ||
$deleted: false | ||
} | ||
} |