Skip to content

Commit 93cc9ce

Browse files
adds CockroachDB as an option to the Piccolo playground (#1229)
* adds CockroachDB as an option to the Piccolo playground * make port optional * add @dantownsend suggestions * removed the orjson dependency for Lilya as it is no longer needed * minor tweaks to docs --------- Co-authored-by: Daniel Townsend <[email protected]>
1 parent 5124246 commit 93cc9ce

File tree

3 files changed

+77
-15
lines changed

3 files changed

+77
-15
lines changed

docs/src/piccolo/getting_started/playground.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ A ``piccolo.sqlite`` file will get created in the current directory.
3737
Advanced usage
3838
---------------
3939

40-
To see how to use the playground with Postgres, and other advanced usage, see
41-
:ref:`PlaygroundAdvanced`.
40+
To see how to use the playground with Postgres or Cockroach, and other
41+
advanced usage, see :ref:`PlaygroundAdvanced`.
4242

4343
-------------------------------------------------------------------------------
4444

docs/src/piccolo/playground/advanced.rst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,56 @@ When you have the database setup, you can connect to it as follows:
4444
4545
piccolo playground run --engine=postgres
4646
47+
CockroachDB
48+
-----------
49+
50+
If you want to use CockroachDB instead of SQLite, you need to create a database
51+
first.
52+
53+
54+
Install CockroachDB
55+
~~~~~~~~~~~~~~~~~~~
56+
57+
See the `installation guide for your OS <https://www.cockroachlabs.com/docs/v25.2/install-cockroachdb-linux/>`_.
58+
59+
Create database
60+
~~~~~~~~~~~~~~~
61+
The playground is for testing and learning purposes only, so you can start a CockroachDB
62+
`single node with the insecure flag <https://www.cockroachlabs.com/docs/v25.2/cockroach-start-single-node.html/>`_
63+
(for non-production testing only) like this:
64+
65+
.. code-block:: bash
66+
67+
cockroach start-single-node --insecure
68+
69+
After that, in a new terminal window, you can create a database like this:
70+
71+
.. code-block:: bash
72+
73+
cockroach sql --insecure --execute="DROP DATABASE IF EXISTS piccolo_playground CASCADE;CREATE DATABASE piccolo_playground;"
74+
75+
By default the playground expects a local database to exist with the following
76+
credentials:
77+
78+
79+
.. code-block:: bash
80+
81+
user: "root"
82+
password: ""
83+
host: "localhost" # or 127.0.0.1
84+
database: "piccolo_playground"
85+
port: 26257
86+
87+
88+
Connecting
89+
~~~~~~~~~~
90+
91+
When you have the database setup, you can connect to it as follows:
92+
93+
.. code-block:: bash
94+
95+
piccolo playground run --engine=cockroach
96+
4797
iPython
4898
-------
4999

piccolo/apps/playground/commands/run.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import uuid
99
from decimal import Decimal
1010
from enum import Enum
11+
from typing import Optional
1112

1213
from piccolo.columns import (
1314
JSON,
@@ -25,7 +26,7 @@
2526
Varchar,
2627
)
2728
from piccolo.columns.readable import Readable
28-
from piccolo.engine import PostgresEngine, SQLiteEngine
29+
from piccolo.engine import CockroachEngine, PostgresEngine, SQLiteEngine
2930
from piccolo.engine.base import Engine
3031
from piccolo.table import Table
3132
from piccolo.utils.warnings import colored_string
@@ -284,28 +285,29 @@ def populate():
284285

285286
def run(
286287
engine: str = "sqlite",
287-
user: str = "piccolo",
288-
password: str = "piccolo",
288+
user: Optional[str] = None,
289+
password: Optional[str] = None,
289290
database: str = "piccolo_playground",
290291
host: str = "localhost",
291-
port: int = 5432,
292+
port: Optional[int] = None,
292293
ipython_profile: bool = False,
293294
):
294295
"""
295296
Creates a test database to play with.
296297
297298
:param engine:
298-
Which database engine to use - options are sqlite or postgres
299+
Which database engine to use - options are sqlite, postgres or
300+
cockroach
299301
:param user:
300-
Postgres user
302+
Database user (ignored for SQLite)
301303
:param password:
302-
Postgres password
304+
Database password (ignored for SQLite)
303305
:param database:
304-
Postgres database
306+
Database name (ignored for SQLite)
305307
:param host:
306-
Postgres host
308+
Database host (ignored for SQLite)
307309
:param port:
308-
Postgres port
310+
Database port (ignored for SQLite)
309311
:param ipython_profile:
310312
Set to true to use your own IPython profile. Located at ~/.ipython/.
311313
For more info see the IPython docs
@@ -324,9 +326,19 @@ def run(
324326
{
325327
"host": host,
326328
"database": database,
327-
"user": user,
328-
"password": password,
329-
"port": port,
329+
"user": user or "piccolo",
330+
"password": password or "piccolo",
331+
"port": port or 5432,
332+
}
333+
)
334+
elif engine.upper() == "COCKROACH":
335+
db = CockroachEngine(
336+
{
337+
"host": host,
338+
"database": database,
339+
"user": user or "root",
340+
"password": password or "",
341+
"port": port or 26257,
330342
}
331343
)
332344
else:

0 commit comments

Comments
 (0)