Skip to content
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

Table renaming: db.rename_table() and sqlite-utils rename-table #565

Closed
simonw opened this issue Jul 3, 2023 · 6 comments
Closed

Table renaming: db.rename_table() and sqlite-utils rename-table #565

simonw opened this issue Jul 3, 2023 · 6 comments
Labels

Comments

@simonw
Copy link
Owner

simonw commented Jul 3, 2023

I find myself wanting two new features in sqlite-utils:

  • The ability to have the new transformed table set to a specific name, while keeping the old table around
  • The ability to rename a table (sqlite-utils doesn't have a table rename function at all right now)

Originally posted by @simonw in simonw/llm#65 (comment)

@simonw
Copy link
Owner Author

simonw commented Jul 3, 2023

For the CLI:

sqlite-utils rename-table data.db old_table_name new_table_name

For the Python code, should it go on Table or on Database?

db["foo"].rename_table("bar")

db.rename_table("foo", "bar")

I think I like the second better, it's slightly more clear.

Also need a design for an option for the .transform() method to indicate that the new table should be created with a new name without dropping the old one.

@simonw
Copy link
Owner Author

simonw commented Jul 22, 2023

Trying out a simple first implementation:

>>> from sqlite_utils import Database
>>> db = Database(memory=True, tracer=print)
PRAGMA recursive_triggers=on; None
>>> db["foo"].insert({"id": 1})
select name from sqlite_master where type = 'view' None
select name from sqlite_master where type = 'table' None
select name from sqlite_master where type = 'view' None
select name from sqlite_master where type = 'table' None
select name from sqlite_master where type = 'view' None
CREATE TABLE [foo] (
   [id] INTEGER
);
         None
select name from sqlite_master where type = 'view' None
INSERT INTO [foo] ([id]) VALUES (?); [1]
select name from sqlite_master where type = 'table' None
select name from sqlite_master where type = 'table' None
PRAGMA table_info([foo]) None
<Table foo (id)>
>>> db.rename_table("foo", "baz")
ALTER TABLE [foo] RENAME TO [baz] None
>>> print(db.schema)
select sql from sqlite_master where sql is not null None
CREATE TABLE "baz" (
   [id] INTEGER
);

simonw added a commit that referenced this issue Jul 22, 2023
@simonw
Copy link
Owner Author

simonw commented Jul 22, 2023

Also need a design for an option for the .transform() method to indicate that the new table should be created with a new name without dropping the old one.

I think keep_table="name_of_table" is good for this.

@simonw
Copy link
Owner Author

simonw commented Jul 22, 2023

Manually testing new rename-table command:

$ sqlite-utils schema /tmp/f.db 
CREATE TABLE [foo] (
   [id] INTEGER,
   [name] TEXT
);
$ sqlite-utils rename-table /tmp/f.db bad-table hi
Error: Table "bad-table" could not be renamed. no such table: bad-table
$ sqlite-utils rename-table /tmp/f.db foo foo     
Error: Table "foo" could not be renamed. there is already another table or index with this name: foo
$ sqlite-utils rename-table /tmp/f.db foo bar
$ sqlite-utils schema /tmp/f.db                   
CREATE TABLE "bar" (
   [id] INTEGER,
   [name] TEXT
);

@simonw simonw changed the title Table renaming utilities Table renaming: db.rename_table() and sqlite-utils rename-table Jul 22, 2023
@simonw simonw closed this as completed Jul 22, 2023
simonw added a commit that referenced this issue Jul 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant