Introducing a new way to review your PRs.

Read the announcement

junkfood02 / seal

🦭 Video/Audio Downloader for Android, based on yt-dlp

View on GitHub

13 documents · Updated Nov 16, 2026

docs / database-persistence-and-backup

Database Persistence and Backup

Overview

The database layer is a Room database centered on app/…/AppDatabase.kt. AppDatabase registers four entities at schema version 5 and exposes a single DAO, VideoInfoDao, through videoInfoDao(). The concrete database instance is created in app/…/DatabaseUtil.kt with Room.databaseBuilder(context, AppDatabase::class.java, "app_database"), and that utility object is the façade other app code should use for persistence rather than constructing DAOs directly.

The persisted model lives under app/src/main/java/com/junkfood/seal/database/objects/:

  • DownloadedVideoInfo.kt stores download history: generated integer id, title/author/source URL/thumbnail/path fields, and extractor, whose Room column default is "Unknown" for migration compatibility. It is also @Serializable, so the same shape is used for JSON download-history backup.
  • CommandTemplate.kt stores custom command templates with generated integer id, display name, and template string. It is serializable and participates in template backup/import.
  • CookieProfile.kt stores multiple cookie profiles with generated integer id, url, and cookie content. It is serializable for consistency, but it is not included in the current Backup payload.
  • OptionShortcut.kt stores command shortcut options with generated long id and an option string. It is serializable and is backed up together with command templates.

app/…/VideoInfoDao.kt is the only DAO. It provides Flow-based readers for download history, templates, cookie profiles, and shortcuts, plus suspend functions for one-shot reads and writes. Download history supports lookup/deletion by id and by videoPath; insertInfoDistinctByPath() wraps a path lookup plus insert in a transaction so repeated inserts for the same path are ignored. Template, cookie, and shortcut APIs are straightforward insert/update/delete/list operations, with bulk import methods for templates and shortcuts.

app/…/DatabaseUtil.kt wraps the DAO and owns app-level persistence behavior. insertInfo() launches DAO writes on applicationScope with Dispatchers.IO, while most other operations are suspend wrappers. Its initializer collects getTemplateFlow() and calls PreferenceUtil.initializeTemplateSample() when the template table is empty, so an empty CommandTemplate table is treated as a signal to seed sample templates. deleteInfoList() deletes rows and, when deleteFile is true, also calls FileUtil.deleteFile(info.videoPath), making download-history deletion optionally affect files on disk.

Backup/export code is under app/src/main/java/com/junkfood/seal/database/backup/. Backup.kt defines the serialized payload with nullable lists for templates, shortcuts, and downloadHistory. BackupUtil.kt configures Json with prettyPrint = true and ignoreUnknownKeys = true, exports templates plus shortcuts via DatabaseUtil.getTemplateList() and DatabaseUtil.getShortcutList(), exports download history either as JSON (List<DownloadedVideoInfo>.toJsonString()) or newline-separated URLs (toURLListString()), and decodes backup JSON with String.decodeToBackup(). BackupUtil.BackupType is the selector used by import flows: DownloadHistory, URLList, CommandTemplate, and CommandShortcut; only the database-backed types are imported by DatabaseUtil.importBackup().

Restore/import is implemented in DatabaseUtil.importBackup(). For download history, it reads current rows, filters incoming rows with filterNot { itemList.contains(it) }, resets imported IDs with copy(id = 0), and bulk inserts the remaining rows. For shortcuts it follows the same pattern with copy(id = 0). For templates, the intended path is also to compare against existing templates, reset IDs, and bulk insert, but the implementation currently filters templateList against itself rather than filtering backup.templates; see Risks. importTemplatesFromJson() decodes JSON through BackupUtil.decodeToBackup() and imports only CommandTemplate and CommandShortcut types.

Room schema snapshots are checked in under app/schemas/com.junkfood.seal.database.AppDatabase/. Version 1 contains only DownloadedVideoInfo; version 2 adds the extractor column with default Unknown; version 3 adds CommandTemplate; version 4 adds CookieProfile; version 5 adds OptionShortcut. AppDatabase.kt declares auto-migrations for every step from 1 through 5, so entity changes that are not auto-migration-safe need explicit migration work and matching schema snapshots.

Routing notes for this area:

Coupling

  • If any entity in app/src/main/java/com/junkfood/seal/database/objects/ gains, removes, or renames a persisted field, update app/…/AppDatabase.kt version/migrations and regenerate the matching app/…/*.json; Room validates against these exported schemas and the current database relies on auto-migrations from versions 1 through 5.
  • If a @Serializable entity field changes in DownloadedVideoInfo.kt, CommandTemplate.kt, or OptionShortcut.kt, also check app/…/Backup.kt and BackupUtil.kt; backup JSON uses the entity classes directly, so Room column compatibility and external backup-file compatibility drift together.
  • If template seeding behavior changes, verify app/…/DatabaseUtil.kt; it treats an empty CommandTemplate Flow as a trigger to call PreferenceUtil.initializeTemplateSample(), so bulk deletion or failed import can cause samples to be recreated.
  • If download-history deletion semantics change in VideoInfoDao.kt, also verify DatabaseUtil.deleteInfoList() in app/…/DatabaseUtil.kt; database row deletion can be coupled to filesystem deletion through the deleteFile parameter.
  • If adding a backup type in app/…/BackupUtil.kt, also add its serialized field to Backup.kt and its import branch to DatabaseUtil.importBackup(); the enum alone does not make the type exportable or restorable.

Risks

  • app/…/DatabaseUtil.kt has a likely template import bug: inside the BackupType.CommandTemplate branch, it checks backup.templates != null but then imports from templateList.filterNot { templateList.contains(it) }, which is always empty. Template JSON imports through importBackup()/importTemplatesFromJson() can therefore report no imported templates even when the backup contains templates. History for these paths includes prior import/id-conflict fixes (81289cce Fix: ID conflicts when importing custom templates), so import duplicate handling is a fragile area.
  • DatabaseUtil.importBackup() deduplicates DownloadedVideoInfo, CommandTemplate, and OptionShortcut using data-class equality before resetting IDs. Because equality includes generated IDs in DownloadedVideoInfo.kt, CommandTemplate.kt, and OptionShortcut.kt, the same logical item with a different database ID may be imported as a duplicate row. The path history includes multiple backup/import changes (47e5b434 Feature Update: Export/import command templates, a9e64835 feat(backup): import download history, bbd4de95 feat, refactor: backup download history), so changes here should be tested with backups created on another install.
  • VideoInfoDao.insertInfoDistinctByPath() in app/…/VideoInfoDao.kt prevents duplicate download-history rows only by videoPath, and DownloadedVideoInfo.videoPath has no unique index in app/…/5.json. Concurrent inserts for the same path can still race if they bypass the wrapper or interleave before the lookup sees the new row.