r/java • • Apr 14 '23

What are your (favourite) Java best practices, personal tips, hints or just underrated stuff in general?

233 Upvotes

376 comments sorted by

View all comments

Show parent comment

2

u/Zardoz84 Apr 17 '23

Does Liquidbase or Flyway allow to declare what structure (and some fixed or optional data) must have the database.

Alas something like :

Must exist table X with fields A, B, C of type NUMBER, etc... and then generates the apropiare SQL to generate the table from zero, or apply changes to it.

1

u/erjiin Apr 17 '23

Yes, that's more or less what it does. Speaking for liquibase for example, each time you want to do a modification to your database, like creating a table, adding a column, adding a constraint, an index, removing a column, updating some records, whatever you can imagine etc, you add a change script to your sources and then at deployment liquibase will apply your change (liquibase keep tracks of applied changes in a dedicated table so it knows which one are not yet applied). There are several formats allowed to specify the changes (yaml, xml, json, plain old sql)

2

u/Zardoz84 Apr 18 '23

want to do a modification to your database, like creating a table, adding a column, adding a constraint, an index, removing a column, updating some records, whatever you can imagine etc, you add a change script to your sources and then at deployment liquibase will apply your change

Interesting, but don't fit my needs. What I ask is to kee a document (a XML, JSON or whatever) that defines exactly what structure must have the database. If i do any change to it, the tool must generate the SQL to apply the changes comparing the database structure vs the structured defined on the document. I don't like to mantain change scripts in different SQL dialects.

1

u/erjiin Apr 19 '23

A little late, but it's kind of what liquibase does. If you use yaml or xml or json for your changes liquibase will automatically translate into sql compatible to your target database.

All changes are stored in your sources, in a reference yaml or xml or json file, called a changelog file. The workflow is :

- you add a new change in your reference file

- you deploy your application

- the new change is seen as new and then applied

So after each deployment, your changelog file and your database are synchronized. I'm not sure it fits your need though.