|
| 1 | +Migrate an existing project to Piccolo |
| 2 | +====================================== |
| 3 | + |
| 4 | +Introduction |
| 5 | +------------ |
| 6 | + |
| 7 | +If you have an existing project and Postgres database, and you want to use |
| 8 | +Piccolo with it, these are the steps you need to take. |
| 9 | + |
| 10 | +Option 1 - ``piccolo asgi new`` |
| 11 | +------------------------------- |
| 12 | + |
| 13 | +This is the recommended way of creating brand new projects. If this is your |
| 14 | +first experience with Piccolo, then it's a good idea to create a test project: |
| 15 | + |
| 16 | +.. code-block:: bash |
| 17 | +
|
| 18 | + mkdir test_project |
| 19 | + cd test_project |
| 20 | + piccolo asgi new |
| 21 | +
|
| 22 | +You'll learn a lot about how Piccolo works by looking at the generated code. |
| 23 | +You can then copy over the relevant files to your existing project if you like. |
| 24 | + |
| 25 | +Alternatively, doing it from scratch, you'll need to do the following: |
| 26 | + |
| 27 | +Option 2 - from scratch |
| 28 | +----------------------- |
| 29 | + |
| 30 | +Create a Piccolo project file |
| 31 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 32 | + |
| 33 | +Create a new ``piccolo_conf.py`` file in the root of your project: |
| 34 | + |
| 35 | +.. code-block:: bash |
| 36 | +
|
| 37 | + piccolo project new |
| 38 | +
|
| 39 | +This contains your database details, and is used to register Piccolo apps. |
| 40 | + |
| 41 | +Create a new Piccolo app |
| 42 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 43 | + |
| 44 | +The app contains your ``Table`` classes and migrations. Run this command at the |
| 45 | +root of your project: |
| 46 | + |
| 47 | +.. code-block:: bash |
| 48 | +
|
| 49 | + # Replace 'my_app' with whatever you want to call your app |
| 50 | + piccolo app new my_app |
| 51 | +
|
| 52 | +Register the new Piccolo app |
| 53 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 54 | + |
| 55 | +Register this new app in ``piccolo_conf.py``. For example: |
| 56 | + |
| 57 | +.. code-block:: python |
| 58 | +
|
| 59 | + APP_REGISTRY = AppRegistry( |
| 60 | + apps=[ |
| 61 | + "my_app.piccolo_app", |
| 62 | + ] |
| 63 | + ) |
| 64 | +
|
| 65 | +While you're at it, make sure the database credentials are correct in |
| 66 | +``piccolo_conf.py``. |
| 67 | + |
| 68 | +Make ``Table`` classes for your current database |
| 69 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 70 | + |
| 71 | +Now, if you run: |
| 72 | + |
| 73 | +.. code-block:: bash |
| 74 | +
|
| 75 | + piccolo schema generate |
| 76 | +
|
| 77 | +It will output Piccolo ``Table`` classes for your current database. Copy the |
| 78 | +output into ``my_app/tables.py``. Double check that everything looks correct. |
| 79 | + |
| 80 | +In ``my_app/piccolo_app.py`` make sure it's tracking these tables for |
| 81 | +migration purposes. |
| 82 | + |
| 83 | +.. code-block:: python |
| 84 | +
|
| 85 | + from piccolo.conf.apps import AppConfig, table_finder |
| 86 | +
|
| 87 | + APP_CONFIG = AppConfig( |
| 88 | + table_classes=table_finder(["my_app.tables"], exclude_imported=True), |
| 89 | + ... |
| 90 | + ) |
| 91 | +
|
| 92 | +Create an initial migration |
| 93 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 94 | + |
| 95 | +This will create a new file in ``my_app/piccolo_migrations``: |
| 96 | + |
| 97 | +.. code-block:: bash |
| 98 | +
|
| 99 | + piccolo migrations new my_app --auto |
| 100 | +
|
| 101 | +These tables already exist in the database, as it's an existing project, so |
| 102 | +you need to fake apply this initial migration: |
| 103 | + |
| 104 | +.. code-block:: bash |
| 105 | +
|
| 106 | + piccolo migrations forwards my_app --fake |
| 107 | +
|
| 108 | +Making queries |
| 109 | +~~~~~~~~~~~~~~ |
| 110 | + |
| 111 | +Now you're basically setup - to make database queries: |
| 112 | + |
| 113 | +.. code-block:: python |
| 114 | +
|
| 115 | + from my_app.tables import MyTable |
| 116 | +
|
| 117 | + async def my_endpoint(): |
| 118 | + data = await MyTable.select() |
| 119 | + return data |
| 120 | +
|
| 121 | +Making new migrations |
| 122 | +~~~~~~~~~~~~~~~~~~~~~ |
| 123 | + |
| 124 | +Just modify the files in ``tables.py``, and then run: |
| 125 | + |
| 126 | +.. code-block:: bash |
| 127 | +
|
| 128 | + piccolo migrations new my_app --auto |
| 129 | + piccolo migrations forwards my_app |
0 commit comments