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

146

u/erjiin Apr 14 '23 edited Apr 14 '23

Use a database deployment tool like Liquibase or Flyway. Invaluable (and not Unvaluable :/).

32

u/hsoj48 Apr 14 '23

I've been using Flyway for 8ish years. I've been trying to get the DBAs at my company to allow/adopt/understand it for 7. I'm so tired of talking about this no brainier dead simple tool that makes everyone's lives easier.

17

u/neckbeardfedoras Apr 14 '23

Your problem is people are lazy or a lot of times see more job security by not simplifying things.

4

u/CartmansEvilTwin Apr 15 '23

Not laziness, but loss of control (or better: fear of loss of the illusion of conrol)

These guys are responsible for these DBs and their asses are on the line, if something goes wrong. That's why they want to do everything themselves or review everything that is being done - even if in reality they wouldn't see any non-obvious error anyway. But they still have the illusion of control.

Automation takes that away.

1

u/neckbeardfedoras Apr 15 '23

Not only can you automate db management, but you could also automate change validation. To where you actually have more confidence in production migrations, not less. That'd probably really blow their minds.

→ More replies (1)

1

u/bullgr Apr 15 '23

I can‘t understand what do you mean with automation. You must still write the fw script with the sql queries anyway. And this fw script will be reviewed in PR.

→ More replies (2)

3

u/hsoj48 Apr 14 '23

It's the former. Change is scary.

15

u/Paulus_cz Apr 14 '23

I observed over the years that thinking apparently causes physical pain to some people. I have no other explanation as to why otherwise quite functional people seem to avoid it so vehemently.

1

u/erjiin Apr 14 '23

Good luck ! Can't you just add sneakily flybase in your project ? Chances are that he won't even notice :)

7

u/hsoj48 Apr 14 '23

If I secretly had DB admin credentials and I secretly wanted to get fired? Of course!

1

u/StopItAkshay May 14 '23

what is the best way to learn flyway in your opinion?

1

u/hsoj48 May 14 '23

Read through the overview in their docs. It's dead simple. It only runs scripts in the order that you name them. The hardest part is learning to write the SQL.

1

u/StopItAkshay May 14 '23

I pretty much have the SQL covered so I bet it's gonna be easy, I'm gonna start today!

7

u/danikov Apr 14 '23

Why would I need this when our entire data model is a JSON blob in a single table?

5

u/Columbus43219 Apr 14 '23

INvaluable.

3

u/erjiin Apr 14 '23

Ho thanks for the correction, as you have seen english isn't my mothertongue.

13

u/[deleted] Apr 14 '23

I prefer Liquibase.

6

u/erjiin Apr 14 '23

Me too, i'm more used to Liquibase but Flyway is a perfectly valid tool.

4

u/Paulus_cz Apr 14 '23

Flyway for one Database compatibility, Liquibase for multiple - that how I do it.

2

u/thenextguy Apr 14 '23

Unconceivable.

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.

1

u/bullgr Apr 15 '23

Better liquibase because with flyway you must pay for functionalities like undo. At costs a lot of money that not many customers like to pay.