Skip to content

Commit

Permalink
WIP: Fix dblocation defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
mugikhan committed Oct 2, 2024
1 parent d6a5c49 commit d0e8e72
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
1 change: 0 additions & 1 deletion packages/powersync-op-sqlite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
},
"peerDependencies": {
"@op-engineering/op-sqlite": "8.0.3",
"@powersync/react-native": "workspace:^1.9.0",
"@powersync/common": "workspace:^1.15.0",
"react": "*",
"react-native": "*"
Expand Down
80 changes: 66 additions & 14 deletions packages/powersync-op-sqlite/src/db/OPSqliteDBOpenFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { open, DB } from '@op-engineering/op-sqlite';
import {
ANDROID_DATABASE_PATH,
IOS_LIBRARY_PATH,
open,
OPSQLite,
type DB,
} from '@op-engineering/op-sqlite';
import { DBAdapter, SQLOpenFactory, SQLOpenOptions } from '@powersync/common';
import { NativeModules, Platform } from 'react-native';
import { OPSQLiteDBAdapter } from './OPSqliteAdapter';
Expand All @@ -25,12 +31,27 @@ export class OPSqliteOpenFactory implements SQLOpenFactory {
const { lockTimeoutMs, journalMode, journalSizeLimit, synchronous } =
this.sqliteOptions;
const { dbFilename, dbLocation } = this.options;
//This is needed because an undefined dbLocation will cause the open function to fail
const location = this.getDbLocation(dbLocation);
console.log('opening', dbFilename);

const DB = open({
name: dbFilename,
// location: dbLocation fails when undefined
});
let DB: DB;
try {
DB = open({
name: dbFilename,
location: location,
});
console.log('opened', dbFilename);
} catch (ex) {
if (ex.message.includes('one JS connection per database')) {
console.log('Error opening database', ex);
DB.close();
console.log('reopening', dbFilename);
DB = open({
name: dbFilename,
location: location,
});
}
}

const statements: string[] = [
`PRAGMA busy_timeout = ${lockTimeoutMs}`,
Expand Down Expand Up @@ -65,7 +86,7 @@ export class OPSqliteOpenFactory implements SQLOpenFactory {
// Workaround to create read-only connections
let baseName = dbFilename.slice(0, dbFilename.lastIndexOf('.'));
let dbName = './'.repeat(i + 1) + baseName + `.db`;
const conn = this.openConnection(dbName);
const conn = this.openConnection(location, dbName);
conn.execute('PRAGMA query_only = true');
readConnections.push(conn);
}
Expand All @@ -81,13 +102,28 @@ export class OPSqliteOpenFactory implements SQLOpenFactory {
});
}

protected openConnection(filenameOverride?: string) {
const { dbFilename, dbLocation } = this.options;
const openOptions = { location: dbLocation };
const DB = open({
name: filenameOverride ?? dbFilename,
// location: dbLocation fails when undefined
});
protected openConnection(
dbLocation: string,
filenameOverride?: string
): OPSQLiteConnection {
const { dbFilename } = this.options;
let DB: DB;
try {
DB = open({
name: filenameOverride ?? dbFilename,
location: dbLocation,
});
} catch (ex) {
if (ex.message.includes('one JS connection per database')) {
console.log('Error opening connection', ex);
OPSQLite.open;
console.log('reopening connection', filenameOverride ?? dbFilename);
DB = open({
name: filenameOverride ?? dbFilename,
location: dbLocation,
});
}
}

//Load extension for all connections
this.loadExtension(DB);
Expand All @@ -99,6 +135,22 @@ export class OPSqliteOpenFactory implements SQLOpenFactory {
});
}

private getDbLocation(dbLocation?: string): string {
if (Platform.OS === 'ios') {
return dbLocation ?? IOS_LIBRARY_PATH;
} else {
return dbLocation ?? ANDROID_DATABASE_PATH;
}
}

private openDatabase(dbFilename: string): DB {
const DB = open({
name: dbFilename,
// location: dbLocation fails when undefined
});
return DB;
}

private loadExtension(DB: DB) {
if (Platform.OS === 'ios') {
const bundlePath: string =
Expand Down

0 comments on commit d0e8e72

Please sign in to comment.