diff --git a/src/background-script/constants.ts b/src/background-script/constants.ts index 5cc3fe2de7..6fb1cc2f7e 100644 --- a/src/background-script/constants.ts +++ b/src/background-script/constants.ts @@ -6,3 +6,5 @@ export const UNINSTALL_URL = `${API_HOST}/uninstall` * Percentage of quota usage where we want to warn users with a notification. */ export const QUOTA_USAGE_WARN_PERC = 80 + +export const DB_DATA_LOSS_FLAG = { name: 'STORAGE_CHECK_FLAG', postId: -1 } diff --git a/src/background-script/index.ts b/src/background-script/index.ts index c386cb0dfe..b41882a64e 100644 --- a/src/background-script/index.ts +++ b/src/background-script/index.ts @@ -42,6 +42,8 @@ import { checkStripePlan } from '@worldbrain/memex-common/lib/subscriptions/stor import type { AnalyticsCoreInterface } from '@worldbrain/memex-common/lib/analytics/types' import { CLOUDFLARE_WORKER_URLS } from '@worldbrain/memex-common/lib/content-sharing/storage/constants' import checkBrowser from 'src/util/check-browser' +import type Dexie from 'dexie' +import { DB_DATA_LOSS_FLAG } from './constants' interface Dependencies { localExtSettingStore: BrowserSettingsStore @@ -131,6 +133,11 @@ class BackgroundScript { // Store the timestamp of when the extension was installed await this.deps.localExtSettingStore.set('installTimestamp', Date.now()) + // Add this to an unused table to periodically check and see if all data has ever been evicted + await (this.deps.storageManager.backend['dexie'] as Dexie) + .table('socialTags') + .add(DB_DATA_LOSS_FLAG) + // Disable PDF integration by default await this.deps.syncSettingsStore.pdfIntegration.set( 'shouldAutoOpen', diff --git a/src/background-script/quick-and-dirty-migrations/index.ts b/src/background-script/quick-and-dirty-migrations/index.ts index d2d9f3a78f..cc6e5263f6 100644 --- a/src/background-script/quick-and-dirty-migrations/index.ts +++ b/src/background-script/quick-and-dirty-migrations/index.ts @@ -35,6 +35,7 @@ import type { SyncSettingsByFeature } from 'src/sync-settings/background/types' import { HIGHLIGHT_COLORS_DEFAULT } from '@worldbrain/memex-common/lib/common-ui/components/highlightColorPicker/constants' import type { CustomListTree } from '@worldbrain/memex-common/lib/types/core-data-types/client' import type { Template } from 'src/copy-paster/types' +import { DB_DATA_LOSS_FLAG } from '../constants' export interface MigrationProps { db: Dexie @@ -69,6 +70,20 @@ export const MIGRATION_PREFIX = '@QnDMigration-' // __IMPORTANT NOTE__ export const migrations: Migrations = { + /* + * This exists as we had a suspicion that sometimes browsers would forcefully evict all extension data. + * To in/validate that we put this flag and periodically check it to see if it's still there. In + * the case of a full data eviction, it should be gone. + */ + [MIGRATION_PREFIX + 'add-db-data-loss-check-flag-01']: async ({ db }) => { + let [existing] = await db + .table('socialTags') + .where({ postId: DB_DATA_LOSS_FLAG.postId }) + .primaryKeys() + if (!existing) { + await db.table('socialTags').add(DB_DATA_LOSS_FLAG) + } + }, /* * This exists as we released the templates with a new order field, same as cuomstListTrees, * though we set the values for the static default templates to be less than the default space between.