Skip to content

Commit 82e8cd3

Browse files
committed
db.rename_table() method, refs #565
1 parent 86a352f commit 82e8cd3

3 files changed

Lines changed: 44 additions & 2 deletions

File tree

docs/python-api.rst

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ You can leave off the third item in the tuple to have the referenced column auto
638638
.. _python_api_table_configuration:
639639

640640
Table configuration options
641-
===========================
641+
---------------------------
642642

643643
The ``.insert()``, ``.upsert()``, ``.insert_all()`` and ``.upsert_all()`` methods each take a number of keyword arguments, some of which influence what happens should they cause a table to be created and some of which affect the behavior of those methods.
644644

@@ -660,7 +660,7 @@ The configuration options that can be specified in this way are ``pk``, ``foreig
660660
.. _python_api_defaults_not_null:
661661

662662
Setting defaults and not null constraints
663-
=========================================
663+
-----------------------------------------
664664

665665
Each of the methods that can cause a table to be created take optional arguments ``not_null=set()`` and ``defaults=dict()``. The methods that take these optional arguments are:
666666

@@ -699,6 +699,24 @@ Here's an example that uses these features:
699699
# [score] INTEGER NOT NULL DEFAULT 1
700700
# )
701701
702+
703+
.. _python_api_rename_table:
704+
705+
Renaming a table
706+
================
707+
708+
The ``db.rename_table(old_name, new_name)`` method can be used to rename a table:
709+
710+
.. code-block:: python
711+
712+
db.rename_table("my_table", "new_name_for_my_table")
713+
714+
This executes the following SQL:
715+
716+
.. code-block:: sql
717+
718+
ALTER TABLE [my_table] RENAME TO [new_name_for_my_table]
719+
702720
.. _python_api_duplicate:
703721

704722
Duplicating tables

sqlite_utils/db.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,19 @@ def create_table(
10401040
)
10411041
return cast(Table, created_table)
10421042

1043+
def rename_table(self, name: str, new_name: str):
1044+
"""
1045+
Rename a table.
1046+
1047+
:param name: Current table name
1048+
:param new_name: Name to rename it to
1049+
"""
1050+
self.execute(
1051+
"ALTER TABLE [{name}] RENAME TO [{new_name}]".format(
1052+
name=name, new_name=new_name
1053+
)
1054+
)
1055+
10431056
def create_view(
10441057
self, name: str, sql: str, ignore: bool = False, replace: bool = False
10451058
):

tests/test_create.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,3 +1279,14 @@ def test_create_transform(fresh_db, cols, kwargs, expected_schema, should_transf
12791279
new_schema = fresh_db["demo"].schema
12801280
assert new_schema == expected_schema, repr(new_schema)
12811281
assert fresh_db["demo"].count == 1
1282+
1283+
1284+
def test_rename_table(fresh_db):
1285+
fresh_db["t"].insert({"foo": "bar"})
1286+
assert ["t"] == fresh_db.table_names()
1287+
fresh_db.rename_table("t", "renamed")
1288+
assert ["renamed"] == fresh_db.table_names()
1289+
assert [{"foo": "bar"}] == list(fresh_db["renamed"].rows)
1290+
# Should error if table does not exist:
1291+
with pytest.raises(sqlite3.OperationalError):
1292+
fresh_db.rename_table("does_not_exist", "renamed")

0 commit comments

Comments
 (0)