Skip to content

feat(duckdb): improve attach(), detach(), and attach_sqlite() #11198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

NickCrews
Copy link
Contributor

@NickCrews NickCrews commented May 9, 2025

The sqlite_attach() approach is deprecated per
https://duckdb.org/docs/stable/extensions/sqlite.html,
and the newer syntax opens up read only flag, specifiying a name for the attachment, and not overwriting tables that already exist.

This is breaking:

  • some params are now keyword-only
  • the semantics for attach_sqlite(overwrite: bool) have changed, now it is on_exists: Literal["ignore", "error"] = "error"

This is prep for work I want to do to add attach_postgres(), but I want to get the API up to date so that the two APIs can be consistent. In fact, after this PR I don't even need another attach_postgres() method, since now the plain attach() is flexible enough to support this.

@NickCrews NickCrews added the breaking change Changes that introduce an API break at any level label May 9, 2025
@github-actions github-actions bot added tests Issues or PRs related to tests duckdb The DuckDB backend and removed breaking change Changes that introduce an API break at any level labels May 9, 2025
@NickCrews NickCrews added feature Features or general enhancements breaking change Changes that introduce an API break at any level sqlite The SQLite backend duckdb The DuckDB backend and removed tests Issues or PRs related to tests duckdb The DuckDB backend labels May 9, 2025
@NickCrews NickCrews force-pushed the duckdb-attach-sqlite branch from 9cc5741 to 80bf360 Compare May 9, 2025 02:19
@github-actions github-actions bot added the tests Issues or PRs related to tests label May 9, 2025
The name to attach the database as.
If `None`, use the default behavior of DuckDB
(as of duckdb==1.2.0 this is the basename of the path).
skip_if_exists
Copy link
Contributor Author

Choose a reason for hiding this comment

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

very explicitly choosing this language of "skip_if_exists" instead of "exist_ok" because I don't want users to think this has "CREATE OR REPLACE" semantics.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could also do on_exists: Literal["ignore", "replace", "error"]="error" and then this would be more inline with how I want to change the create_table() and create_view() APIs to be in the future

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I went with on_exists: Literal["ignore", "error"] = "error" so that it would be consistent with on_missing: Literal["ignore", "error"]="error" of detach()

@NickCrews NickCrews force-pushed the duckdb-attach-sqlite branch from 80bf360 to a1345c2 Compare June 6, 2025 14:27
@NickCrews NickCrews changed the title feat(duckdb): switch to newer ATTACH syntax for attach_sqlite() feat(duckdb): improve attach(), detach(), and attach_sqlite() Jun 6, 2025
cur.execute(
f"CALL sqlite_attach('{path}', overwrite={overwrite})"
).fetchall()
self._safe_raw_sql(f"SET GLOBAL sqlite_all_varchar={all_varchar}")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't THINK it's needed to use this as a context manager here (or in fact most places within the duckdb backend), but double check me.

@NickCrews NickCrews force-pushed the duckdb-attach-sqlite branch from a1345c2 to 100149e Compare June 6, 2025 20:17
NickCrews added 2 commits June 9, 2025 13:08
the sqlite_attach() approach is deprecated per
https://duckdb.org/docs/stable/extensions/sqlite.html,
and the newer syntax opens up read only flag, specifiying a name for the attachment, and not overwriting tables that already exist.
This is breaking in that some params are now keyword only.
@NickCrews NickCrews force-pushed the duckdb-attach-sqlite branch from 100149e to 0c1d64d Compare June 9, 2025 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Changes that introduce an API break at any level duckdb The DuckDB backend feature Features or general enhancements sqlite The SQLite backend tests Issues or PRs related to tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant