Skip to content

Commit

Permalink
db.rename_table() method, refs #565
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jul 22, 2023
1 parent 86a352f commit 82e8cd3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
22 changes: 20 additions & 2 deletions docs/python-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ You can leave off the third item in the tuple to have the referenced column auto
.. _python_api_table_configuration:

Table configuration options
===========================
---------------------------

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.

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

Setting defaults and not null constraints
=========================================
-----------------------------------------

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:

Expand Down Expand Up @@ -699,6 +699,24 @@ Here's an example that uses these features:
# [score] INTEGER NOT NULL DEFAULT 1
# )
.. _python_api_rename_table:

Renaming a table
================

The ``db.rename_table(old_name, new_name)`` method can be used to rename a table:

.. code-block:: python
db.rename_table("my_table", "new_name_for_my_table")
This executes the following SQL:

.. code-block:: sql
ALTER TABLE [my_table] RENAME TO [new_name_for_my_table]
.. _python_api_duplicate:

Duplicating tables
Expand Down
13 changes: 13 additions & 0 deletions sqlite_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,19 @@ def create_table(
)
return cast(Table, created_table)

def rename_table(self, name: str, new_name: str):
"""
Rename a table.
:param name: Current table name
:param new_name: Name to rename it to
"""
self.execute(
"ALTER TABLE [{name}] RENAME TO [{new_name}]".format(
name=name, new_name=new_name
)
)

def create_view(
self, name: str, sql: str, ignore: bool = False, replace: bool = False
):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -1279,3 +1279,14 @@ def test_create_transform(fresh_db, cols, kwargs, expected_schema, should_transf
new_schema = fresh_db["demo"].schema
assert new_schema == expected_schema, repr(new_schema)
assert fresh_db["demo"].count == 1


def test_rename_table(fresh_db):
fresh_db["t"].insert({"foo": "bar"})
assert ["t"] == fresh_db.table_names()
fresh_db.rename_table("t", "renamed")
assert ["renamed"] == fresh_db.table_names()
assert [{"foo": "bar"}] == list(fresh_db["renamed"].rows)
# Should error if table does not exist:
with pytest.raises(sqlite3.OperationalError):
fresh_db.rename_table("does_not_exist", "renamed")

0 comments on commit 82e8cd3

Please sign in to comment.