Skip to content

Commit

Permalink
table.create_index(columns, index_name) method
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Willison committed Aug 1, 2018
1 parent 70e1f83 commit 4427d2d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
18 changes: 18 additions & 0 deletions docs/table.rst
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,21 @@ If you insert additional records into the table you will need to refresh the sea
"is_good_dog": True,
}, pk="id")
dogs.populate_fts(["name", "twitter"])
Creating indexes
================

You can create an index on a table using the ``.create_index(columns)`` method. The method takes a list of columns:

.. code-block:: python
dogs.create_index(["is_good_dog"])
By default the index will be named ``idx_{table-name}_{columns}`` - if you want to customize the name of the created index you can pass the ``index_name`` parameter::]

.. code-block:: python
dogs.create_index(
["is_good_dog", "age"],
index_name="good_dogs_by_age"
)
13 changes: 13 additions & 0 deletions sqlite_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ def create(self, columns, pk=None, foreign_keys=None):
self.db.create_table(self.name, columns, pk=pk, foreign_keys=foreign_keys)
self.exists = True

def create_index(self, columns, index_name=None):
if index_name is None:
index_name = "idx_{}_{}".format(
self.name.replace(" ", "_"), "_".join(columns)
)
sql = """
CREATE INDEX {index_name}
ON {table_name} ({columns});
""".format(
index_name=index_name, table_name=self.name, columns=", ".join(columns)
)
self.db.conn.execute(sql)

def drop(self):
return self.db.conn.execute("DROP TABLE {}".format(self.name))

Expand Down
50 changes: 50 additions & 0 deletions tests/test_create.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .fixtures import fresh_db
from sqlite_utils.db import Index
import pytest
import json

Expand Down Expand Up @@ -68,6 +69,55 @@ def test_create_table_works_for_m2m_with_only_foreign_keys(fresh_db):
)


@pytest.mark.parametrize(
"columns,index_name,expected_index",
(
(
["is_good_dog"],
None,
Index(
seq=0,
name="idx_dogs_is_good_dog",
unique=0,
origin="c",
partial=0,
columns=["is_good_dog"],
),
),
(
["is_good_dog", "age"],
None,
Index(
seq=0,
name="idx_dogs_is_good_dog_age",
unique=0,
origin="c",
partial=0,
columns=["is_good_dog", "age"],
),
),
(
["age"],
"age_index",
Index(
seq=0,
name="age_index",
unique=0,
origin="c",
partial=0,
columns=["age"],
),
),
),
)
def test_create_index(fresh_db, columns, index_name, expected_index):
dogs = fresh_db["dogs"]
dogs.insert({"name": "Cleo", "twitter": "cleopaws", "age": 3, "is_good_dog": True})
assert [] == dogs.indexes
dogs.create_index(columns, index_name)
assert expected_index == dogs.indexes[0]


@pytest.mark.parametrize(
"data_structure",
(
Expand Down

0 comments on commit 4427d2d

Please sign in to comment.