Skip to content

Commit 8478c77

Browse files
committed
moved drop table under alter - to help prevent accidental drops
1 parent cc8b2c9 commit 8478c77

File tree

20 files changed

+44
-61
lines changed

20 files changed

+44
-61
lines changed

docs/src/piccolo/migrations/create.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ forwards and backwards functions.
5757
5858
5959
async def backwards():
60-
await Band.drop().run()
60+
await Band.alter().drop_table().run()
6161
6262
Running migrations
6363
------------------

docs/src/piccolo/query_types/alter.rst

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ This is used to modify an existing table.
77

88
.. hint:: It is typically used in conjunction with migrations - see :ref:`Migrations`.
99

10+
1011
add_column
1112
----------
1213

1314
Used to add a column to an existing table.
1415

1516
.. code-block:: python
1617
17-
Band.alter().add_column(‘members’, Integer()).run_sync()
18+
Band.alter().add_column('members', Integer()).run_sync()
19+
1820
1921
drop_column
2022
-----------
@@ -23,7 +25,18 @@ Used to drop an existing column.
2325

2426
.. code-block:: python
2527
26-
Band.alter().drop_column('popularity')
28+
Band.alter().drop_column('popularity').run_sync()
29+
30+
31+
drop_table
32+
----------
33+
34+
Used to drop the table - use with caution!
35+
36+
.. code-block:: python
37+
38+
Band.alter().drop_table().run_sync()
39+
2740
2841
rename_column
2942
-------------
@@ -32,7 +45,8 @@ Used to rename an existing column.
3245

3346
.. code-block:: python
3447
35-
Band.alter().rename_column(Band.popularity, ‘rating’).run_sync()
48+
Band.alter().rename_column(Band.popularity, 'rating').run_sync()
49+
3650
3751
set_null
3852
--------

docs/src/piccolo/query_types/drop.rst

Lines changed: 0 additions & 13 deletions
This file was deleted.

docs/src/piccolo/query_types/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ focus on `select`, `insert` and `update`.
1212
./count
1313
./create
1414
./delete
15-
./drop
1615
./exists
1716
./insert
1817
./objects

piccolo/commands/playground.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def populate():
4242
for _table in reversed(TABLES):
4343
try:
4444
if _table.table_exists().run_sync():
45-
_table.drop().run_sync()
45+
_table.alter().drop_table().run_sync()
4646
except Exception as e:
4747
print(e)
4848

piccolo/query/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
Create,
99
Update,
1010
Raw,
11-
Drop,
1211
TableExists,
1312
Exists,
1413
Count,

piccolo/query/methods/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from .count import Count
33
from .create import Create
44
from .delete import Delete
5-
from .drop import Drop
65
from .exists import Exists
76
from .insert import Insert
87
from .objects import Objects

piccolo/query/methods/alter.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ def querystring(self) -> QueryString:
8585

8686

8787
class Alter(Query):
88-
__slots__ = ("_add", "_drop", "_rename", "_unique", "_null")
88+
89+
__slots__ = ("_add", "_drop", "_rename", "_unique", "_null", "_drop_table")
8990

9091
def __init__(self, *args, **kwargs):
9192
super().__init__(*args, **kwargs)
@@ -94,6 +95,7 @@ def __init__(self, *args, **kwargs):
9495
self._rename: t.List[Rename] = []
9596
self._unique: t.List[Unique] = []
9697
self._null: t.List[Null] = []
98+
self._drop_table = False
9799

98100
def add_column(self, name: str, column: Column) -> Alter:
99101
"""
@@ -109,6 +111,13 @@ def drop_column(self, column: Column) -> Alter:
109111
self._drop.append(Drop(column))
110112
return self
111113

114+
def drop_table(self) -> Alter:
115+
"""
116+
Band.alter().drop_table()
117+
"""
118+
self._drop_table = True
119+
return self
120+
112121
def rename_column(self, column: Column, new_name: str) -> Alter:
113122
"""
114123
Band.alter().rename_column(Band.popularity, ‘rating’)
@@ -132,6 +141,9 @@ def set_unique(self, column: Column, boolean: bool = True) -> Alter:
132141

133142
@property
134143
def querystring(self) -> QueryString:
144+
if self._drop_table:
145+
return QueryString(f'DROP TABLE "{self.table._meta.tablename}"')
146+
135147
query = f"ALTER TABLE {self.table._meta.tablename}"
136148

137149
alterations = [

piccolo/query/methods/drop.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

piccolo/table.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
Create,
1111
Count,
1212
Delete,
13-
Drop,
1413
Exists,
1514
Insert,
1615
Objects,
@@ -327,15 +326,6 @@ def create_without_columns(cls) -> Raw:
327326
base=QueryString(f'CREATE TABLE "{cls._meta.tablename}"()'),
328327
)
329328

330-
@classmethod
331-
def drop(cls) -> Drop:
332-
"""
333-
Drops the table.
334-
335-
await Band.drop().run()
336-
"""
337-
return Drop(table=cls)
338-
339329
@classmethod
340330
def alter(cls) -> Alter:
341331
"""

0 commit comments

Comments
 (0)