Skip to content

Add support for COPY FROM DATABASE statement #9765

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

Merged
merged 13 commits into from
Nov 23, 2023

Conversation

Mytherin
Copy link
Collaborator

@Mytherin Mytherin commented Nov 22, 2023

This PR adds support for a new COPY syntax that allows an entire database to be copied from one attached database to another attached database.

Syntax
COPY FROM DATABASE db1 TO db2;
-- copy only the catalog elements and not any data
COPY FROM DATABASE db1 TO db2 (SCHEMA);
Full example
ATTACH ':memory:' AS db1;
CREATE TABLE db1.integers AS SELECT 42 AS i;
ATTACH ':memory:' AS db2;

COPY FROM DATABASE db1 TO db2;
SELECT * FROM db2.integers;
┌───────┐
│   i   │
│ int32 │
├───────┤
│    42 │
└───────┘
Description

All catalog entries are copied from the source database to the target database. This includes schemas, tables, views, macros, sequences and types. The copy happens in two parts. First all schema elements are copied over. Then, if (SCHEMA) is not specified, all data is copied over from the source database to the target database. Data is copied over internally by essentially running queries in the form of INSERT INTO db2.tbl SELECT * FROM db1.tbl.

Limitations

@github-actions github-actions bot marked this pull request as draft November 22, 2023 16:11
@Mytherin Mytherin marked this pull request as ready for review November 22, 2023 16:25
@carlopi
Copy link
Contributor

carlopi commented Nov 23, 2023

Great feature!
I gave this a spin, here some corner cases that could make it into a test / I am not sure what the semantic should be.

  • ./build/release/duckdb other.db -c "CREATE TABLE t(i int); COPY FROM DATABASE other TO other;"
    Error: Catalog Error: Table with name "t" already exists!
    Self copy should likely be an error

  • ./build/release/duckdb empty.db -c "COPY FROM DATABASE empty TO empty"
    Error: INTERNAL Error: Attempted to dereference unique_ptr that is NULL!

  • ./build/release/duckdb empty.db -c "ATTACH 'other.db'; COPY FROM DATABASE empty TO other;"
    Error: INTERNAL Error: Attempted to dereference unique_ptr that is NULL!

  • ./build/release/duckdb other.db -c "ATTACH 'empty.db' (READ_ONLY); COPY FROM DATABASE other TO empty (SCHEMA);"
    This succeeds (even though it should not), but schema is not moved.

  • ./build/release/duckdb a.db -c "ATTACH 'b.db' (READ_ONLY); COPY FROM DATABASE a TO b ;"
    This fails, good, but either with:
    Error: Invalid Error: Cannot execute statement of type "COPY_DATABASE" on database "empty" which is attached in read-only mode!
    or
    Error: Catalog Error: Table with name "t" already exists!
    depending on the content of b.db

  • Should performing copy twice work? Basically, if all tables are there (and with the same content), it should still be OK?

  • Override option (unsure of the meaning, maybe not then)?

  • Should the syntax allow some way to get the default database? Like COPY FROM DEFAULT DATABASE TO otheror even COPY FROM DATABASE TO other defaults to currently attached DB.

@github-actions github-actions bot marked this pull request as draft November 23, 2023 14:01
@Mytherin Mytherin marked this pull request as ready for review November 23, 2023 14:08
@Mytherin
Copy link
Collaborator Author

Thanks for having a look - I've addressed the comments. Should work more as expected now

@Mytherin Mytherin merged commit 9819b14 into duckdb:main Nov 23, 2023
@szarnyasg
Copy link
Collaborator

szarnyasg commented Nov 29, 2023

I read through the CLI dot commands today. Almost all of the SQLite commands are now supported, except .clone:

.clone NEWDB         Clone data into NEWDB from the existing database

It seems .clone is doing something very similar to COPY FROM DATABASE.

@Mytherin Mytherin deleted the copydatabase branch December 4, 2023 11:44
krlmlr added a commit to duckdb/duckdb-r that referenced this pull request Dec 14, 2023
Merge pull request duckdb/duckdb#9765 from Mytherin/copydatabase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Documentation Use for issues or PRs that require changes in the documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants