-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
db: use triggers and change table for reactivity on native (#4279)
* db: fix reactivity issue, use triggers on both platforms, reduce code duplication * Switch to using commitHook/callback function to process changes rather than polling * switch to using updateHook to trigger processChangs() on native * Stop syncing thread unreads on channel focus, this triggers unnecessary inserts/updates to the db. Stop adding duplicate posts to the flush queue in the change listener. Stop adding posts that were just inserted to the flush queue, we only want to invalidate if it's an update to an existing post. * Add syncChannelThreadUnreads back in, but don't insert unless we're certain we have new unreads * Fix merge artefact * update to new type for op-sqlite db connection
- Loading branch information
1 parent
f794ee1
commit 9036269
Showing
10 changed files
with
384 additions
and
382 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
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { createDevLogger } from '@tloncorp/shared'; | ||
import { handleChange } from '@tloncorp/shared/db'; | ||
import { sql } from 'drizzle-orm'; | ||
import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core'; | ||
import { useEffect, useMemo, useState } from 'react'; | ||
|
||
export const enableLogger = false; | ||
export const logger = createDevLogger('db', enableLogger); | ||
|
||
export const changeLogTable = sqliteTable('__change_log', { | ||
id: integer('id').primaryKey(), | ||
table_name: text('table_name').notNull(), | ||
operation: text('operation').notNull(), | ||
row_id: integer('row_id'), | ||
row_data: text('row_data'), | ||
timestamp: integer('timestamp').default(sql`(strftime('%s', 'now'))`), | ||
}); | ||
|
||
export function useMigrations(db: BaseDb) { | ||
const [hasSucceeded, setHasSucceeded] = useState(false); | ||
const [error, setError] = useState(null); | ||
|
||
useEffect(() => { | ||
logger.log('running migrations'); | ||
const startTime = Date.now(); | ||
db.runMigrations() | ||
.then(() => setHasSucceeded(true)) | ||
.catch((e) => { | ||
logger.log('failed to migrate database', e); | ||
setError(e); | ||
}) | ||
.finally(() => | ||
logger.log('migrations complete in', Date.now() - startTime + 'ms') | ||
); | ||
}, [db]); | ||
|
||
return useMemo( | ||
() => ({ | ||
success: hasSucceeded, | ||
error: error, | ||
}), | ||
[hasSucceeded, error] | ||
); | ||
} | ||
|
||
export abstract class BaseDb { | ||
protected client: any = null; | ||
protected isPolling = false; | ||
|
||
abstract setupDb(): Promise<void>; | ||
abstract purgeDb(): Promise<void>; | ||
abstract getDbPath(): Promise<string | undefined>; | ||
abstract runMigrations(): Promise<void>; | ||
|
||
protected async processChanges() { | ||
if (!this.client) return; | ||
|
||
try { | ||
const changes = await this.client.select().from(changeLogTable).all(); | ||
for (const change of changes) { | ||
handleChange({ | ||
table: change.table_name, | ||
operation: change.operation as 'INSERT' | 'UPDATE' | 'DELETE', | ||
row: JSON.parse(change.row_data ?? ''), | ||
}); | ||
} | ||
await this.client.delete(changeLogTable).run(); | ||
} catch (e) { | ||
logger.error('failed to process changes:', e); | ||
} | ||
} | ||
|
||
async resetDb() { | ||
await this.purgeDb(); | ||
await this.runMigrations(); | ||
} | ||
} |
Oops, something went wrong.