Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixtures app #229

Merged
merged 17 commits into from Sep 14, 2021
Merged

Fixtures app #229

merged 17 commits into from Sep 14, 2021

Conversation

dantownsend
Copy link
Member

Adding a fixtures app which can be used to dump data from the database to a JSON file, and then reload it again.

To create a fixture:

piccolo fixture dump --apps=blog --tables=Post,PostComment > fixture.json

To load a fixture:

piccolo fixture load fixture.json

Reasons for adding it

Sharing data with colleagues

A Piccolo project can contain a fixture with all of the data a colleague needs to setup a project with. For example, if it's an app about selling cars, the fixture could contain all of the makes and models of cars.

Deploying to a new environment

When you want to deploy your application to production for the first time, you can load the fixture to populate the database with essential data.

Why not use pg_dump and pg_restore?

Whilst these are great tools, and achieve similar goals to fixtures, they're locked to a single Postgres version. With fixtures, you should be able to dump from say Postgres 12 and load into Postgres 13. This means you don't have to rebuild your fixtures when you update your Postgres version.

Also, having your fixtures in JSON is better for version control, and makes them easier to modify by hand.

Side effects

In order to implement this change, some fairly large changes had to be made to the wider codebase (though no breaking changes).

Test refactoring

The tests were refactored slightly - in the past there was a single example Piccolo app which the tests use (containing the Band, Manager etc tables). There are now multiple example apps. The new one is called mega, and contains a table called MegaTable which has every column type, and is very useful for testing serialisation.

Moving Pydantic into core

All of the Pydantic functionality is currently in Piccolo API. It was been discussed in the past about moving this into core (#10 (comment)).

The downside of doing so, is a tighter coupling to external dependencies. But on the other hand, it's so heavily used elsewhere in the Piccolo ecosystem, that practically it makes little difference.

So this PR moves the Pydantic code into the main Piccolo repo. In a future PR I'll remove the Pydantic code from Piccolo API, and will replace it with proxies to the code in Piccolo core.

@codecov-commenter
Copy link

codecov-commenter commented Sep 13, 2021

Codecov Report

Merging #229 (c79a523) into master (104c1a5) will decrease coverage by 0.39%.
The diff coverage is 75.41%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #229      +/-   ##
==========================================
- Coverage   90.50%   90.11%   -0.40%     
==========================================
  Files         119      124       +5     
  Lines        5740     5916     +176     
==========================================
+ Hits         5195     5331     +136     
- Misses        545      585      +40     
Impacted Files Coverage Δ
piccolo/utils/encoding.py 68.18% <44.44%> (-13.07%) ⬇️
piccolo/apps/fixture/commands/dump.py 50.00% <50.00%> (ø)
piccolo/engine/base.py 71.11% <50.00%> (-0.99%) ⬇️
piccolo/apps/fixture/commands/load.py 84.00% <84.00%> (ø)
piccolo/utils/pydantic.py 86.76% <86.76%> (ø)
piccolo/apps/fixture/commands/shared.py 95.65% <95.65%> (ø)
piccolo/apps/fixture/piccolo_app.py 100.00% <100.00%> (ø)
piccolo/main.py 77.96% <100.00%> (+0.37%) ⬆️
... and 4 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 104c1a5...c79a523. Read the comment docs.

@dantownsend
Copy link
Member Author

@sinisaos @aminalaee any thoughts on this change? In particular, moving Pydantic support into the main Piccolo repo.

@sinisaos
Copy link
Member

@dantownsend More or less all orm's (Tortoise, especially Ormar etc.) have a strong integration with Pydantic in their core. I think moving a serializers from Piccolo API to Piccolo ORM is a good idea that has more pros than cons. Unlike other libraries, the Piccolo API is specific because it allows you to easily create a web API-s, and I think there is no difference if we use Pydantic serialization from Piccolo ORM or from Piccolo API.

@aminalaee
Copy link
Member

I think that's ok. Pydantic is pretty stable now. My only concern is that there was a discussion that Python 3.10 will cause some issues with Pydantic, not sure how serious that is.

@dantownsend
Copy link
Member Author

@sinisaos @aminalaee Thanks for your input.

There's a giant thread here about Pydantic and Python 3.10: pydantic/pydantic#2678

It seems like they've solved the issue (at least temporarily) 🤞

@dantownsend dantownsend merged commit 48768ba into master Sep 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants