Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 25, 2025

On fresh database initialization, automatically populate the database from an existing files/ directory structure. This enables recovering file metadata when only the files directory remains.

Changes

  • repository::mod::initialize_db: Added should_gen_database_from_files flag, set when database is newly created, triggers generation after migration

  • repository::mod::generate_database_from_files: New function that:

    • Returns early if files/ doesn't exist or is empty
    • Walks directory tree depth-first
    • Creates folders before files at each level
    • Uses existing folder_repository::create_folder and file_repository::create_file
    • Properly links files to parent folders
    • Logs progress with colored status messages (magenta for start, grey for folder start, cyan for folder finish, green for completion)
    • Queues each created file for icon generation
  • Tests: 17 new tests covering:

    • Empty/missing directory edge cases
    • Single and multi-level structures
    • 6-level deep recursion
    • File type and size detection
    • Prevention of regeneration when database already exists
pub fn generate_database_from_files(
    parent_folder: Option<u32>,
    con: &Connection,
) -> Result<()>
Original prompt

This section details on the original issue you should resolve

<issue_title>ability to generate database from just a root folder of files</issue_title>
<issue_description>on boot, repository::mod::initialize_db should check if the files directory exists.

  • create a new variable in the function named should_gen_database_from_files set to false
  • in the Err block when checking for table_version, set should_gen_database_from_files to true
  • after the call to migrate_db, call a new function named generate_database_from_files

Db Generation

Create a new function repository::mod::generate_database_from_files(parent_folder: Option<u32>, con: &Connection) -> rusqlite::Result<()>

  • if the ./files directory doesn't exist or is empty, return Ok(()) immediately
  • if the ./files directory has contents
    • recursively walk the directory tree
    • for each level, create the folders first before creating files, but don't walk the tree breadth-first - walk it depth-first (the straightforward way)
    • when creating folders and files, don't create them in the file system, use repository::folder_repository::create_folder. For the folder param, pass None as the id. For the parent_id field, pass the parent_folder parameter declared in the method header.
    • when creating files, use repository::file_repository::create_file. For the file parameter, pass None as the id. For the parent_id field, pass the parent_folder parameter declared in the method header.
  • this function should be called recursively, but keep in mind that there will be an arbitrary number of files and folders, and resources might be constrained in prod

Testing

The point of these tests is to ensure that having just a file directory structure under the test file directory (determined at test-time with service::file_service::file_dir) is enough to properly generate that file structure in the database when repository::mod::initialize_db is called.

  • when testing, call test::cleanup at both the first and last lines of each test function.
  • use test::create_folder_disk and test::create_file_disk. Do not use create_file_db_entry or create_folder_db_entry, as those will fail / defeat the purpose of tests.
  • do not use test::init_db_folder or test::set_password to create the database, as that defeats the point of the tests
  • prefer breadth over depth when replicating a file structure - go at max 6 folders deep just to prove that recursion is working properly
  • verify all files you create are in the database properly

Tests should only focus on recreating the file and folder structure in the database. There will be no tags or exif data to worry about

Special Tests

  • there should be a few special tests that break those rules - and all of them should be testing that this database generation does not take place if the database already exists. This is to prevent the database from being wiped out accidentally.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Add functionality to generate database from files directory Add database generation from existing files directory on boot Nov 25, 2025
Copilot AI requested a review from ploiu November 25, 2025 19:37
Copilot finished work on behalf of ploiu November 25, 2025 19:37
Copilot AI and others added 3 commits November 25, 2025 20:38
Co-authored-by: ploiu <43047560+ploiu@users.noreply.github.com>
…ction

Co-authored-by: ploiu <43047560+ploiu@users.noreply.github.com>
@ploiu ploiu force-pushed the copilot/add-db-generation-from-files branch from e653635 to c578646 Compare November 25, 2025 20:38
Co-authored-by: ploiu <43047560+ploiu@users.noreply.github.com>
Copilot finished work on behalf of ploiu November 25, 2025 21:07
Copilot AI requested a review from ploiu November 25, 2025 21:07
@ploiu ploiu marked this pull request as ready for review November 26, 2025 01:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ability to generate database from just a root folder of files

2 participants