Skip to content

Commit

Permalink
correctly convert CouchDB to SQLite (first tree of given user)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianoFerrari committed Mar 12, 2024
1 parent 4b60842 commit cd20de8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 32 deletions.
28 changes: 17 additions & 11 deletions src/migration/MigrationWorker.elm
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,30 @@ init _ =


type Msg
= Incoming Dec.Value
= Incoming ( String, Dec.Value )


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Incoming json ->
Incoming ( docId, json ) ->
case Data.gitDataReceived json ( Data.empty, Tree.defaultTree ) of
Just { newData, newTree } ->
( model
, output
(Debug.toString newTree
|> Enc.string
)
)
let
converted =
Data.convert docId newData
in
case converted of
Just ( _, outvalue ) ->
( model
, output ( docId, outvalue )
)

Nothing ->
( model, output ( "", Enc.string "Conversion failed" ) )

Nothing ->
( model, output (Enc.string "Invalid JSON") )
( model, output ( "", Enc.string "Invalid JSON" ) )



Expand All @@ -62,7 +68,7 @@ subscriptions model =
input Incoming


port input : (Dec.Value -> msg) -> Sub msg
port input : (( String, Dec.Value ) -> msg) -> Sub msg


port output : Dec.Value -> Cmd msg
port output : ( String, Dec.Value ) -> Cmd msg
78 changes: 57 additions & 21 deletions src/migration/couchdb-to-sqlite.ts
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');
Expand All @@ -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>');
}



Expand All @@ -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
}
}

0 comments on commit cd20de8

Please sign in to comment.