simonw / sqlite-utils

Python CLI utility and library for manipulating SQLite databases
https://sqlite-utils.datasette.io
Apache License 2.0
1.62k stars 109 forks source link

`.transform(keep_table=...)` option #571

Closed simonw closed 1 year ago

simonw commented 1 year ago

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.

Originally posted by @simonw in https://github.com/simonw/sqlite-utils/issues/565#issuecomment-1646657324

simonw commented 1 year ago

Demo:

>>> from sqlite_utils import Database
>>> db = Database(memory=True)
>>> db["foo"].insert({"name": "Cleo"})
<Table foo (name)>
>>> print(db["foo"].schema)
CREATE TABLE [foo] (
   [name] TEXT
)
>>> db["foo"].transform(not_null={"name"})
<Table foo (name)>
>>> print(db["foo"].schema)
CREATE TABLE "foo" (
   [name] TEXT NOT NULL
)
>>> db["foo"].transform(types={"name": int}, keep_table="kept")
<Table foo (name)>
>>> print(db.schema)
CREATE TABLE "kept" (
   [name] TEXT NOT NULL
);
CREATE TABLE "foo" (
   [name] INTEGER NOT NULL
);