How can I manage database evolutions when I use JPA?

I learned the game by following the tutorial on my website to create a small blogging mechanism.

It uses JPA, and in it bootstrap calls Fixtures.Deletemodels (), (or something like that).

It basically destroys all the tables every time it starts, and I lose all the data.

I have deployed a production system such as (without nuke instruction).

Now I need to deploy a big update to the production system. Many classes have changed, been added and removed. In my testing locally, without decorating the tables every time I started, I ran into synchronization problems. When I try to write or read from tables, the game will cause errors. I opened mysql and, of course, the tables were partially modified and incorrectly changed in some cases. Even if I have the DDL mode set to โ€œcreateโ€ in my configuration, JPA cannot โ€œdetermineโ€ how to reconcile the changes and change my schema accordingly.

Therefore, I need to return to the bootstrap statement, which destroys all my tables.

So, I began to study the evolution of the database in Play and read an article on the website of the playback platform about the development of databases. The article talked about version scripts, but he said: "If you work with JPA, Hibernate can automatically process database changes. Evolutions are useful if you do not use JPA."

So, if the JPA is supposed to take care of this for me, how do I deploy large updates to the large Play application? So far, JPA has not been able to correctly modify the schema, and the application will throw errors. I do not want to lose all my data, so the fix on dev "Fixtures.deleteModels ()" cannot really be used in prod.

Thanks in advance, Josh

+6
source share
4 answers

No, the JPA should not take care of it for you. This is not a magic tool. If you decide to rename the client table to client, the street column is row1 and switch the client type column values โ€‹โ€‹from 1, 2, 3 to bronze, silver, gold, JPA cannot read in your mind and figure out all the changes that need to be done automatically.

To migrate from one schema to another, you use the same tools as if you weren't using JPA scripts: SQL or better tools for migrating schemas and data, or even custom JDBC code for migrating.

+7
source

Take a look at the flyway . You can initiate database migration from your code or maven.

+4
source

There is a property called hbm2ddl.auto=update that will update your schema. I would strongly suggest not using this option in production, since it introduces a whole new level of problems if something goes wrong. This is great for development though.

0
source

When the JPA container starts (say, EclipseLink or whatever), it expects to find a database that matches the @Entity classes that you defined in your code. If the database is already migrated, everything will work smoothly; otherwise: perhaps this will not work.

So, a short story, you need to do a database migration (or evolution, if you want) before launching the JPA container. Apparently, Play is migrating for you before Play exits the database manager that you configured. So, theoretically, no matter which ORM you use, Play decides when the time comes for ORM to get started. So conceptually this should work.

For a good presentation on this subject, please watch the second video at: http://flywaydb.org/documentation/videos.html

0
source

Source: https://habr.com/ru/post/909567/


All Articles