Skip to content

Commit 0d5c2da

Browse files
committed
added create_index and drop_index methods to Table
1 parent 0c82354 commit 0d5c2da

File tree

12 files changed

+134
-22
lines changed

12 files changed

+134
-22
lines changed

dev-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ipdb==0.12.2
22
ipython==7.8.0
33
twine==3.1.1
4-
mypy==0.750
4+
mypy==0.782
55
pip-upgrader==1.4.15

piccolo/columns/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ def engine_type(self) -> str:
121121
else:
122122
raise ValueError("The table has no engine defined.")
123123

124+
@property
125+
def index_name(self) -> str:
126+
"""
127+
If an index is added for this column, this is the name which is used.
128+
"""
129+
return f"{self.table._meta.tablename}_{self.name}"
130+
124131
def get_full_name(self, just_alias=False) -> str:
125132
"""
126133
Returns the full column name, taking into account joins.

piccolo/query/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .base import Query
1+
from .base import Query # noqa
22
from .methods import (
33
Alter,
44
Select,
@@ -11,4 +11,6 @@
1111
TableExists,
1212
Exists,
1313
Count,
14+
CreateIndex,
15+
DropIndex,
1416
)

piccolo/query/methods/__init__.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
from .alter import Alter
2-
from .count import Count
3-
from .create import Create
4-
from .delete import Delete
5-
from .exists import Exists
6-
from .insert import Insert
7-
from .objects import Objects
8-
from .raw import Raw
9-
from .select import Select
10-
from .table_exists import TableExists
11-
from .update import Update
1+
from .alter import Alter # noqa
2+
from .count import Count # noqa
3+
from .create import Create # noqa
4+
from .create_index import CreateIndex # noqa
5+
from .delete import Delete # noqa
6+
from .drop_index import DropIndex # noqa
7+
from .exists import Exists # noqa
8+
from .insert import Insert # noqa
9+
from .objects import Objects # noqa
10+
from .raw import Raw # noqa
11+
from .select import Select # noqa
12+
from .table_exists import TableExists # noqa
13+
from .update import Update # noqa
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from __future__ import annotations
2+
from enum import Enum
3+
import typing as t
4+
5+
from piccolo.columns import Column
6+
from piccolo.query.base import Query
7+
from piccolo.querystring import QueryString
8+
9+
if t.TYPE_CHECKING:
10+
from piccolo.table import Table
11+
12+
13+
class IndexMethod(str, Enum):
14+
btree = "btree"
15+
hash = "hash"
16+
gist = "gist"
17+
gin = "gin"
18+
19+
20+
class CreateIndex(Query):
21+
def __init__(
22+
self,
23+
table: t.Type[Table],
24+
column: Column,
25+
method: IndexMethod = IndexMethod.btree,
26+
):
27+
self.column = column
28+
self.method = method
29+
super().__init__(table)
30+
31+
@property
32+
def querystrings(self) -> t.Sequence[QueryString]:
33+
index_name = self.column._meta.index_name
34+
tablename = self.table._meta.tablename
35+
method_name = self.method.value
36+
return [
37+
QueryString(
38+
f"CREATE INDEX {index_name} ON {tablename} USING {method_name}"
39+
f" ({self.column._meta.name})"
40+
)
41+
]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from __future__ import annotations
2+
import typing as t
3+
4+
from piccolo.query.base import Query
5+
from piccolo.querystring import QueryString
6+
7+
if t.TYPE_CHECKING:
8+
from piccolo.columns.base import Column
9+
from piccolo.table import Table
10+
11+
12+
class DropIndex(Query):
13+
def __init__(self, table: t.Type[Table], column: Column):
14+
self.column = column
15+
super().__init__(table)
16+
17+
@property
18+
def querystrings(self) -> t.Sequence[QueryString]:
19+
index_name = self.column._meta.index_name
20+
return [QueryString(f"DROP INDEX {index_name}")]

piccolo/table.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
from piccolo.columns.defaults.base import Default
1313
from piccolo.query import (
1414
Alter,
15-
Create,
1615
Count,
16+
Create,
1717
Delete,
18+
DropIndex,
1819
Exists,
1920
Insert,
2021
Objects,
@@ -23,6 +24,7 @@
2324
TableExists,
2425
Update,
2526
)
27+
from piccolo.query.methods.create_index import CreateIndex, IndexMethod
2628
from piccolo.querystring import QueryString, Unquoted
2729
from piccolo.utils import _camel_to_snake
2830

@@ -399,7 +401,7 @@ def raw(cls, sql: str, *args: t.Any) -> Raw:
399401
"""
400402
Execute raw SQL queries on the underlying engine - use with caution!
401403
402-
await Band.raw('select * from band')
404+
await Band.raw('select * from band').run()
403405
404406
Or passing in parameters:
405407
@@ -478,7 +480,7 @@ def alter(cls) -> Alter:
478480
"""
479481
Used to modify existing tables and columns.
480482
481-
await Band.alter().rename_column(Band.popularity, 'rating')
483+
await Band.alter().rename_column(Band.popularity, 'rating').run()
482484
"""
483485
return Alter(table=cls)
484486

@@ -535,10 +537,32 @@ def update(cls, values: t.Dict[Column, t.Any] = {}) -> Update:
535537
536538
await Band.update().values(
537539
{Band.name: "Spamalot"}
538-
).where(Band.name=="Pythonistas")
540+
).where(
541+
Band.name=="Pythonistas"
542+
).run()
539543
"""
540544
return Update(table=cls).values(values)
541545

546+
@classmethod
547+
def create_index(
548+
cls, column: Column, method: IndexMethod = IndexMethod.btree,
549+
) -> CreateIndex:
550+
"""
551+
Create a table index.
552+
553+
await Band.create_index(Band.name).run()
554+
"""
555+
return CreateIndex(table=cls, column=column, method=method)
556+
557+
@classmethod
558+
def drop_index(cls, column: Column) -> DropIndex:
559+
"""
560+
Drop a table index.
561+
562+
await Band.drop_index(Band.name).run()
563+
"""
564+
return DropIndex(table=cls, column=column)
565+
542566
###########################################################################
543567

544568
@classmethod

tests/table/test_alter.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import decimal
21
from unittest import TestCase
32

43
from piccolo.columns import Integer, Numeric

tests/table/test_batch.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import asyncio
22
import math
33

4-
from piccolo.engine.postgres import AsyncBatch, PostgresEngine
5-
from piccolo.querystring import QueryString
6-
74
from ..base import DBTestCase
85
from ..example_project.tables import Manager
96

tests/table/test_create.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from unittest import TestCase
2+
23
from ..example_project.tables import Manager
34

45

0 commit comments

Comments
 (0)