Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2201,6 +2201,8 @@ The ``duplicate`` command duplicates a table - creating a new table with the sam

sqlite-utils duplicate books.db authors authors_copy

The copy keeps the original table's column types, constraints, foreign keys, ``STRICT`` and ``WITHOUT ROWID`` options, ``rowid`` values and indexes (recreated under new names). Triggers are not copied. See :ref:`table.duplicate() <python_api_duplicate>` for details.

.. note::
In Python: :ref:`table.duplicate() <python_api_duplicate>` CLI reference: :ref:`sqlite-utils duplicate <cli_ref_duplicate>`

Expand Down
11 changes: 11 additions & 0 deletions docs/python-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,17 @@ The ``table.duplicate()`` method creates a copy of the table, copying both the t

The new ``authors_copy`` table will now contain a duplicate copy of the data from ``authors``.

The new table is created from the original table's ``CREATE TABLE`` statement with only the table name changed, so it keeps the same column types, primary key, ``NOT NULL``, ``UNIQUE``, ``CHECK`` and ``DEFAULT`` constraints, foreign keys, ``STRICT`` and ``WITHOUT ROWID`` options. Those constraints are enforced on the copy. Row ``rowid`` values and the ``AUTOINCREMENT`` counter are preserved too.

A few parts of the schema are adjusted for the new table name:

- References to the table itself, such as a self-referencing foreign key or an ``authors.name`` column reference in a ``CHECK`` constraint, are updated to point at the copy.
- Indexes are recreated under new names, because index names must be unique within a database. If the index name contains the original table name that part is replaced, so ``idx_authors_name`` becomes ``idx_authors_copy_name``. Other index names are prefixed with the new table name, so ``by_name`` becomes ``authors_copy_by_name``. A numeric suffix is added if that name is already in use.

Triggers are not copied.

Duplicating a virtual table, such as a full-text search table, creates a regular table containing a copy of its rows.

This method raises ``sqlite_utils.db.NoTable`` if the table does not exist.

.. note::
Expand Down
84 changes: 84 additions & 0 deletions sqlite_utils/create_table_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,90 @@ def parse_uniques(create_sql: str) -> list[Unique]:
return uniques


def parse_without_rowid(create_sql: str) -> bool:
"""Return True if a CREATE TABLE statement ends with WITHOUT ROWID."""
if _table_body(create_sql) is None:
return False
tokens = _meaningful(_lex(create_sql))
close = max(i for i, token in enumerate(tokens) if token.text == ")")
return any(token.is_keyword("WITHOUT") for token in tokens[close + 1 :])


def rename_create_statement(
create_sql: str, new_name: str, table: str | None = None
) -> str:
"""
Rename the table or index created by a CREATE TABLE or CREATE INDEX statement.

For CREATE INDEX, ``table`` replaces the name of the indexed table. Everything
else in the statement, including comments and whitespace, is left unchanged.
"""
tokens = _meaningful(_lex(create_sql))
if not tokens or not tokens[0].is_keyword("CREATE"):
raise ParseError("Expected CREATE TABLE or CREATE INDEX")
index = 1
while index < len(tokens) and tokens[index].text.upper() in (
"TEMP",
"TEMPORARY",
"UNIQUE",
):
index += 1
if index >= len(tokens) or not (
tokens[index].is_keyword("TABLE") or tokens[index].is_keyword("INDEX")
):
raise ParseError("Expected CREATE TABLE or CREATE INDEX")
index += 1
if (
index + 2 < len(tokens)
and tokens[index].is_keyword("IF")
and tokens[index + 1].is_keyword("NOT")
and tokens[index + 2].is_keyword("EXISTS")
):
index += 3
if index + 1 < len(tokens) and tokens[index + 1].text == ".":
index += 2
if index >= len(tokens):
raise ParseError("CREATE statement is missing its name")
replacements = [(tokens[index], new_name)]
if table is not None:
if index + 2 >= len(tokens) or not tokens[index + 1].is_keyword("ON"):
raise ParseError("CREATE INDEX is missing ON")
replacements.append((tokens[index + 2], table))
for token, replacement in reversed(replacements):
create_sql = (
create_sql[: token.start]
+ _quote_replacement(token.text, replacement)
+ create_sql[token.end :]
)
return create_sql


def rewrite_table_references(sql: str, old_table: str, new_table: str) -> str:
"""
Rename ``old_table`` where it qualifies a column (``table.column``) or is the
target of ``REFERENCES``. Strings and comments are left unchanged.
"""
tokens = _meaningful(_lex(sql))
folded = _ascii_fold(old_table)
replacements = [
token
for i, token in enumerate(tokens)
if token.kind in ("word", "identifier")
and _ascii_fold(_unquote(token.text)) == folded
and (
(i + 1 < len(tokens) and tokens[i + 1].text == ".")
or (i > 0 and tokens[i - 1].is_keyword("REFERENCES"))
)
]
for token in reversed(replacements):
sql = (
sql[: token.start]
+ _quote_replacement(token.text, new_table)
+ sql[token.end :]
)
return sql


def parse_column_comments(create_sql: str) -> dict[str, ColumnComments]:
"""Return comments immediately before and after each column definition."""
body_info = _table_body(create_sql)
Expand Down
71 changes: 68 additions & 3 deletions sqlite_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
parse_checks,
parse_column_comments,
parse_uniques,
parse_without_rowid,
rename_create_statement,
rewrite_check_expression,
rewrite_table_references,
sql_ends_in_line_comment,
)
from .utils import (
Expand Down Expand Up @@ -2629,15 +2632,77 @@ def create(

def duplicate(self, new_name: str) -> "Table":
"""
Create a duplicate of this table, copying across the schema and all row data.
Create a duplicate of this table, copying across the schema, indexes, rowids
and all row data. Triggers are not copied.

:param new_name: Name of the new table
"""
if not self.exists():
raise NoTable(f"Table {self.name} does not exist")
with self.db.atomic():
sql = f"CREATE TABLE {quote_identifier(new_name)} AS SELECT * FROM {quote_identifier(self.name)};"
self.db.execute(sql)
if self.virtual_table_using is not None:
sql = f"CREATE TABLE {quote_identifier(new_name)} AS SELECT * FROM {quote_identifier(self.name)};"
self.db.execute(sql)
return self.db.table(new_name)
self.db.execute(
rewrite_table_references(
rename_create_statement(self.schema, new_name), self.name, new_name
)
)
columns = [column.name for column in self.columns]
if not parse_without_rowid(self.schema) and "rowid" not in {
c.lower() for c in columns
}:
columns.insert(0, "rowid")
columns_sql = ", ".join(quote_identifier(c) for c in columns)
self.db.execute(
f"INSERT INTO {quote_identifier(new_name)} ({columns_sql}) "
f"SELECT {columns_sql} FROM {quote_identifier(self.name)};"
)
if parse_autoincrement(self.schema):
sequence_row = self.db.execute(
"SELECT seq FROM sqlite_sequence WHERE name = ?", [self.name]
).fetchone()
if sequence_row is not None:
self.db.execute(
"UPDATE sqlite_sequence SET seq = MAX(seq, ?) WHERE name = ?",
[sequence_row[0], new_name],
)
self.db.execute(
"INSERT INTO sqlite_sequence (name, seq) SELECT ?, ? "
"WHERE NOT EXISTS (SELECT 1 FROM sqlite_sequence WHERE name = ?)",
[new_name, sequence_row[0], new_name],
)
# Index names must be unique across the database
existing_names = {
row[0].lower()
for row in self.db.execute("SELECT name FROM sqlite_master")
}
for index in self.indexes:
row = self.db.execute(
"SELECT sql FROM sqlite_master WHERE type = 'index' AND name = ?",
[index.name],
).fetchone()
if row is None or row[0] is None:
# Automatic index for a PRIMARY KEY or UNIQUE constraint
continue
if self.name in index.name:
base_name = index.name.replace(self.name, new_name, 1)
else:
base_name = f"{new_name}_{index.name}"
index_name = base_name
suffix = 2
while index_name.lower() in existing_names:
index_name = f"{base_name}_{suffix}"
suffix += 1
existing_names.add(index_name.lower())
self.db.execute(
rewrite_table_references(
rename_create_statement(row[0], index_name, table=new_name),
self.name,
new_name,
)
)
return self.db.table(new_name)

def transform(
Expand Down
16 changes: 16 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2847,6 +2847,22 @@ def test_duplicate_table(tmpdir):
assert list(db.table("one").rows) == list(db.table("two").rows)


def test_duplicate_table_copies_schema_and_indexes(db_path):
db = Database(db_path)
db.execute("CREATE TABLE one (id INTEGER PRIMARY KEY, name TEXT NOT NULL)")
db.execute("CREATE INDEX idx_one_name ON one (name)")
db.table("one").insert({"id": 1, "name": "Cleo"})

result = CliRunner().invoke(cli.cli, ["duplicate", db_path, "one", "two"])

assert result.exit_code == 0, result.output
assert db.table("two").schema == (
"CREATE TABLE two (id INTEGER PRIMARY KEY, name TEXT NOT NULL)"
)
assert [index.name for index in db.table("two").indexes] == ["idx_two_name"]
assert list(db.table("two").rows) == [{"id": 1, "name": "Cleo"}]


@pytest.mark.skipif(not _has_compiled_ext(), reason="Requires compiled ext.c")
@pytest.mark.parametrize(
"entrypoint,should_pass,should_fail",
Expand Down
Loading
Loading