File tree Expand file tree Collapse file tree 8 files changed +41
-53
lines changed
piccolo/commands/migration Expand file tree Collapse file tree 8 files changed +41
-53
lines changed Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ underlying SQL - making the ORM feel less magic.
2020
2121.. code-block :: python
2222
23- query = Band.select().columns( Band.name).where(Band.popularity >= 100 )
23+ query = Band.select(Band.name).where(Band.popularity >= 100 )
2424
2525 print (query)
2626 ' SELECT name from band where popularity > 100'
Original file line number Diff line number Diff line change @@ -121,26 +121,4 @@ Altering tables
121121---------------
122122
123123To alter tables, you'll use mostly use alter queries (see :ref: `alter `), and
124- occassionally raw queries (see :ref: `raw `).
125-
126- Auto populating migrations
127- --------------------------
128-
129- Instead of manually populating your migrations each time, Piccolo has helpers
130- for common use cases.
131-
132- Creating tables
133- ~~~~~~~~~~~~~~~
134-
135- Rather than having to copy in your table definitions manually, you can ask
136- Piccolo to do it for you using the ``-c `` flag and passing in the import path
137- for the table. Multiple ``-c `` flags can be used.
138-
139- .. code-block :: bash
140-
141- piccolo migration new -c ..tables.Band -c ..tables.Manager
142-
143- Piccolo needs to be able to import these files using ``importlib ``, so make
144- sure the paths are correct.
145-
146- Piccolo will then add the table definitions the migration.
124+ occasionally raw queries (see :ref: `raw `).
Original file line number Diff line number Diff line change @@ -8,8 +8,8 @@ You can use ``limit`` clauses with the following queries:
88* :ref: `Objects `
99* :ref: `Select `
1010
11- Rather than returning a list of results, it will only return the number you ask
12- for.
11+ Rather than returning all of the matching results, it will only return the
12+ number you ask for.
1313
1414.. code-block :: python
1515
Original file line number Diff line number Diff line change @@ -8,8 +8,7 @@ You can use ``offset`` clauses with the following queries:
88* :ref: `Objects `
99* :ref: `Select `
1010
11- This will omit the first X rows from the response. It's useful for things like
12- pagination.
11+ This will omit the first X rows from the response.
1312
1413It's highly recommended to use it along with an :ref: `order_by ` clause,
1514otherwise the results returned could be different each time.
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ You can use ``where`` clauses with the following queries:
1414It allows powerful filtering of your data.
1515
1616Equal / Not Equal
17- -----
17+ -----------------
1818
1919.. code-block :: python
2020
@@ -132,3 +132,22 @@ Using multiple ``where`` clauses is equivalent to an AND.
132132 ).where(
133133 b.popularity < 1000
134134 ).run_sync()
135+
136+ Using And / Or directly
137+ ~~~~~~~~~~~~~~~~~~~~~~~
138+
139+ Rather than using the ``| `` and ``& `` characters, you can use the ``And `` and
140+ ``Or `` classes, which are what's used under the hood.
141+
142+ .. code-block :: python
143+
144+ from piccolo.columns.combination import And, Or
145+
146+ b = Band
147+
148+ b.select().where(
149+ Or(
150+ And(b.popularity >= 100 , b.popularity < 1000 ),
151+ b.name == ' Pythonistas'
152+ )
153+ ).run_sync()
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ This is used to update any rows in the table which match the criteria.
77
88.. code-block :: python
99
10- >> > Band.update().values( {
10+ >> > Band.update({
1111 >> > Band.name: ' Pythonistas 2'
1212 >> > }).where(
1313 >> > Band.name == ' Pythonistas'
Original file line number Diff line number Diff line change 33Column Types
44============
55
6- .. hint :: You'll notice that all of the column names match their SQL equivalents.
6+ .. hint :: You'll notice that the column names tend to match their SQL
7+ equivalents.
78
89Column
910------
@@ -123,3 +124,16 @@ UUID
123124
124125 class Band (Table ):
125126 uuid = UUID()
127+
128+ Secret
129+ ------
130+
131+ The database treats it the same as a Varchar, but Piccolo may treat it
132+ differently internally - for example, allowing a user to automatically omit any
133+ secret fields when doing a select query, to help prevent inadvertant leakage.
134+ A common use for a Secret field is a password.
135+
136+ .. code-block :: python
137+
138+ class Band (Table ):
139+ password = Secret(length = 100 )
Original file line number Diff line number Diff line change 11from __future__ import annotations
22import datetime
3- import importlib
4- import importlib .util
53import os
6- import sys
74import typing as t
85from types import ModuleType
96
107import click
118
129from piccolo .migrations .template import TEMPLATE
13- from piccolo .table import Table
1410
1511
1612MIGRATION_MODULES : t .Dict [str , ModuleType ] = {}
@@ -44,12 +40,6 @@ def _create_new_migration(migrations_path) -> None:
4440###############################################################################
4541
4642
47- @click .option (
48- "-c" ,
49- "--create" ,
50- multiple = True ,
51- help = "Path to a table class which you want to create, e.g. tables.Band" ,
52- )
5343@click .option (
5444 "-p" ,
5545 "--path" ,
@@ -66,17 +56,5 @@ def new(create: t.Tuple[str], path: str):
6656 root_dir = path if path else os .getcwd ()
6757 migrations_path = os .path .join (root_dir , "migrations" )
6858
69- # TODO - might remove this. Does nothing currently.
70- for path in create :
71- module_path , table_name = path .rsplit ("." , maxsplit = 1 )
72- module = importlib .import_module (module_path )
73- try :
74- table_class : Table = getattr (module , table_name )
75- except AttributeError :
76- print (f"Unable to find { path } - aborting" )
77- sys .exit (1 )
78-
79- print (table_class ._table_str ())
80-
8159 _create_migrations_folder (migrations_path )
8260 _create_new_migration (migrations_path )
You can’t perform that action at this time.
0 commit comments