Skip to content

Commit

Permalink
Create index fix (#433)
Browse files Browse the repository at this point in the history
* update table docstrings - remove `run`

* quote column names when creating an index
  • Loading branch information
dantownsend authored Feb 13, 2022
1 parent 8500c81 commit 5f59841
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 31 deletions.
4 changes: 2 additions & 2 deletions piccolo/query/methods/create_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def postgres_ddl(self) -> t.Sequence[str]:
index_name = self.table._get_index_name(column_names)
tablename = self.table._meta.tablename
method_name = self.method.value
column_names_str = ", ".join(column_names)
column_names_str = ", ".join([f'"{i}"' for i in self.column_names])
return [
(
f"{self.prefix} {index_name} ON {tablename} USING "
Expand All @@ -62,7 +62,7 @@ def sqlite_ddl(self) -> t.Sequence[str]:
if method_name != "btree":
raise ValueError("SQLite only support btree indexes.")

column_names_str = ", ".join(column_names)
column_names_str = ", ".join([f'"{i}"' for i in self.column_names])
return [
(
f"{self.prefix} {index_name} ON {tablename} "
Expand Down
54 changes: 27 additions & 27 deletions piccolo/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,8 @@ def get_related(self, foreign_key: t.Union[ForeignKey, str]) -> Objects:
.. code-block:: python
band = await Band.objects().first().run()
manager = await band.get_related(Band.manager).run()
band = await Band.objects().first()
manager = await band.get_related(Band.manager)
>>> print(manager.name)
'Guido'
Expand Down Expand Up @@ -534,7 +534,7 @@ def to_dict(self, *columns: Column) -> t.Dict[str, t.Any]:
instance = await Manager.objects().get(
Manager.name == 'Guido'
).run()
)
>>> instance.to_dict()
{'id': 1, 'name': 'Guido'}
Expand Down Expand Up @@ -677,7 +677,7 @@ def all_related(
concert = await Concert.objects(
Concert.all_related()
).run()
)
>>> concert.band_1
<Band: 1>
Expand All @@ -695,7 +695,7 @@ def all_related(
Concert.venue,
Concert.band_1,
Concert.band_2
).run()
)
:param exclude:
You can request all columns, except these.
Expand Down Expand Up @@ -726,7 +726,7 @@ def all_columns(
await Band.select(
Band.all_columns(),
Band.manager.all_columns()
).run()
)
This is mostly useful when the table has a lot of columns, and typing
them out by hand would be tedious.
Expand Down Expand Up @@ -785,7 +785,7 @@ def insert(cls, *rows: "Table") -> Insert:
await Band.insert(
Band(name="Pythonistas", popularity=500, manager=1)
).run()
)
"""
query = Insert(table=cls)
Expand All @@ -800,7 +800,7 @@ def raw(cls, sql: str, *args: t.Any) -> Raw:
.. code-block:: python
await Band.raw('select * from band').run()
await Band.raw('select * from band')
Or passing in parameters:
Expand Down Expand Up @@ -839,9 +839,9 @@ def select(
.. code-block:: python
await Band.select().columns(Band.name).run()
await Band.select(Band.name).run()
await Band.select('name').run()
await Band.select().columns(Band.name)
await Band.select(Band.name)
await Band.select('name')
:param exclude_secrets:
If ``True``, any password fields are omitted from the response.
Expand All @@ -861,7 +861,7 @@ def delete(cls, force=False) -> Delete:
.. code-block:: python
await Band.delete().where(Band.name == 'Pythonistas').run()
await Band.delete().where(Band.name == 'Pythonistas')
:param force:
Unless set to ``True``, deletions aren't allowed without a
Expand All @@ -879,7 +879,7 @@ def create_table(
.. code-block:: python
await Band.create_table().run()
await Band.create_table()
"""
return Create(
Expand All @@ -895,7 +895,7 @@ def alter(cls) -> Alter:
.. code-block:: python
await Band.alter().rename_column(Band.popularity, 'rating').run()
await Band.alter().rename_column(Band.popularity, 'rating')
"""
return Alter(table=cls)
Expand All @@ -912,11 +912,11 @@ def objects(
pythonistas = await Band.objects().where(
Band.name == 'Pythonistas'
).first().run()
).first()
pythonistas.name = 'Pythonistas Reborn'
await pythonistas.save().run()
await pythonistas.save()
# Or to remove it from the database:
await pythonistas.remove()
Expand All @@ -928,12 +928,12 @@ def objects(
.. code-block:: python
# Without nested
band = await Band.objects().first().run()
band = await Band.objects().first()
>>> band.manager
1
# With nested
band = await Band.objects(Band.manager).first().run()
band = await Band.objects(Band.manager).first()
>>> band.manager
<Band 1>
Expand All @@ -947,7 +947,7 @@ def count(cls) -> Count:
.. code-block:: python
await Band.count().where(Band.popularity > 1000).run()
await Band.count().where(Band.popularity > 1000)
"""
return Count(table=cls)
Expand All @@ -959,7 +959,7 @@ def exists(cls) -> Exists:
.. code-block:: python
await Band.exists().where(Band.name == 'Pythonistas').run()
await Band.exists().where(Band.name == 'Pythonistas')
"""
return Exists(table=cls)
Expand All @@ -971,7 +971,7 @@ def table_exists(cls) -> TableExists:
.. code-block:: python
await Band.table_exists().run()
await Band.table_exists()
"""
return TableExists(table=cls)
Expand All @@ -994,19 +994,19 @@ def update(
{Band.name: "Spamalot"}
).where(
Band.name == "Pythonistas"
).run()
)
await Band.update(
{"name": "Spamalot"}
).where(
Band.name == "Pythonistas"
).run()
)
await Band.update(
name="Spamalot"
).where(
Band.name == "Pythonistas"
).run()
)
:param force:
Unless set to ``True``, updates aren't allowed without a
Expand All @@ -1023,7 +1023,7 @@ def indexes(cls) -> Indexes:
.. code-block:: python
await Band.indexes().run()
await Band.indexes()
"""
return Indexes(table=cls)
Expand All @@ -1041,7 +1041,7 @@ def create_index(
.. code-block:: python
await Band.create_index([Band.name]).run()
await Band.create_index([Band.name])
"""
return CreateIndex(
Expand All @@ -1061,7 +1061,7 @@ def drop_index(
.. code-block:: python
await Band.drop_index([Band.name]).run()
await Band.drop_index([Band.name])
"""
return DropIndex(table=cls, columns=columns, if_exists=if_exists)
Expand Down
39 changes: 37 additions & 2 deletions tests/table/test_indexes.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from unittest import TestCase

from tests.base import DBTestCase
from piccolo.columns.column_types import Integer
from piccolo.table import Table
from tests.example_apps.music.tables import Manager


class TestIndexes(DBTestCase):
class TestIndexes(TestCase):
def setUp(self):
Manager.create_table().run_sync()

def tearDown(self):
Manager.alter().drop_table().run_sync()

def test_create_index(self):
"""
Test single column and multi column indexes.
Expand All @@ -27,6 +34,34 @@ def test_create_index(self):
self.assertTrue(index_name not in index_names)


class Concert(Table):
order = Integer()


class TestProblematicColumnName(TestCase):
def setUp(self):
Concert.create_table().run_sync()

def tearDown(self):
Concert.alter().drop_table().run_sync()

def test_problematic_name(self):
"""
Make sure we can add an index to a column with a problematic name
(which clashes with a SQL keyword).
"""
columns = [Concert.order]
Concert.create_index(columns=columns).run_sync()
index_name = Concert._get_index_name([i._meta.name for i in columns])

index_names = Concert.indexes().run_sync()
self.assertTrue(index_name in index_names)

Concert.drop_index(columns).run_sync()
index_names = Concert.indexes().run_sync()
self.assertTrue(index_name not in index_names)


class TestIndexName(TestCase):
def test_index_name(self):
self.assertEqual(
Expand Down

0 comments on commit 5f59841

Please sign in to comment.