Skip to content

Add make_reader(migrate=False) and matching CLI option - #415

Open
divyanshi555 wants to merge 1 commit into
lemon24:masterfrom
divyanshi555:migrate-flag
Open

Add make_reader(migrate=False) and matching CLI option #415
divyanshi555 wants to merge 1 commit into
lemon24:masterfrom
divyanshi555:migrate-flag

Conversation

@divyanshi555

Copy link
Copy Markdown

Fixes #403

Adds a migrate flag to make_reader() (default True) and a matching --migrate/--no-migrate CLI flag.When
migrate=False and a migration is needed, StorageError is raised instead of migrating, done by passing setup_db() a copy of MIGRATION with an empty migrations dict and a custom missing_suffix, as suggested in the issue.

Changes :

  • _storage/_base.py: StorageBase.setup_db() accepts migrate; builds
    an empty-migrations copy of MIGRATION with a custom error suffix
    when migrate=False.
  • _storage/__init__.py: Storage.__init__ threads migrate through.
  • core.py: make_reader() gains the migrate parameter + docstring.
  • _cli.py: adds --migrate/--no-migrate (default True).
  • tests/test_reader_lifecycle.py: new test_migrate, verifying
    StorageError is raised when migrate=False and a migration is
    needed, and that migrate=True still migrates normally.

Verification
Ran the full test suite, coverage, mypy strict, and the docs build all passing locally.

@divyanshi555

Copy link
Copy Markdown
Author

Hi @lemon24 !
Thanks for such a well-scoped "good first issue" to work on. This PR is among my early contributions, and I’m happy to make any changes you’d suggest.

@codecov

codecov Bot commented Sep 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.05%. Comparing base (f567c36) to head (bf80475).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #415   +/-   ##
=======================================
  Coverage   94.04%   94.05%           
=======================================
  Files         110      110           
  Lines       14043    14065   +22     
  Branches     1040     1041    +1     
=======================================
+ Hits        13207    13229   +22     
  Misses        756      756           
  Partials       80       80           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@lemon24 lemon24 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR, looks good! (and thanks for testing everything locally)

I added a few small comments, after they're addressed we're good to merge.

Comment on lines +397 to +411
# Build a database stuck at version 1
old_db_path = str(pathlib.Path(db_path).parent / 'old.sqlite')
db = sqlite3.connect(old_db_path)
try:
HeavyMigration(create=lambda db: None, version=1, migrations={}).migrate(db)
finally:
db.close()

# Define a new schema (version 2) with trivial migrations from version 1 to 2
new_migration = HeavyMigration(
create=lambda db: None,
version=2,
migrations={1: lambda db: None},
)
monkeypatch.setattr('reader._storage._schema.MIGRATION', new_migration)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think increasing the version can be simplified a bit by patching MIGRATION from the beginning:

migration = HeavyMigration(create=lambda db: None, version=1, migrations={})
monkeypatch.setattr('reader._storage._schema.MIGRATION', migration)

# ... test empty case
with make_reader(db_path): pass

migration.version = 2
migration.migrations[1] = lambda _: None

# ... test make_reader(db_path, migrate=False)
# ... test make_reader(db_path)

# migrate=False, refuse and raise StorageError.
with pytest.raises(StorageError) as excinfo:
make_reader(old_db_path, migrate=False)
assert 'migrate=False' in str(excinfo.value)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides the error (or lack thereof), it's a good idea to check if the database actually changed or not:

with make_reader(db_path, ...) as reader:
   assert migration.get_version(reader._storage.get_db()) == ...

path: str,
read_only: bool = False,
timeout: float | None = None,
migrate: bool = True,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Might be better to put migrate after read_only, to keep "how to create this" flags together, and for symmetry with make_reader().

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.

make_reader(migrate=False)

2 participants