3

I have a Spring based project where we turn of features using database flags.

This approach works great when turn on/off new logic. We run into issues when making database changes. In our testing environment we made have different columns in a given database table, than the table in the production environment.

For example: Let say we have a column FIELD_IN_TEST_ENV_ONLY in our testing env only.

@Column(name = "FIELD_IN_TEST_ENV_ONLY")
private String myField;

When using a JPA repo.

this.repo.findAll()

The line above will fail if run in an enviroment with out the FIELD_IN_TEST_ENV_ONLY column since hibernate will have a column in the select statement that does not exist

So basically I would like to implment something like the following...

if(MY_FLAG)
     @Column(name = "FIELD_IN_TEST_ENV_ONLY")
else 
     @Transient
private String myField;

I'm just not sure how to implment something like this.

Ross Sullivan
  • 396
  • 1
  • 3
  • 13
  • 2
    "In our testing environment we made have different columns than the table in the production environment" <- that's just wrong. What's the point of testing on environment that doesn't represent your prod? – M. Prokhorov Dec 18 '19 at 17:27
  • Try to see this answer: https://stackoverflow.com/questions/19098541/jpa-optional-columns – Allan Braga Dec 18 '19 at 17:32
  • @M.Prokhorov sorry maybe I should have explained that better. in our development environment, where we may add a new feature that is not yet ready to be moved to production. We would like to move it to prod behind a flag without making a DDL change unit a go live date where we would add the column. – Ross Sullivan Dec 18 '19 at 18:05
  • That's also strange to say the least. Your go-live should be as atomic as possible, and deploying code without deploying schema is not atomic at all. Also, conditionally applied annotation still means you'll have to redeploy your code to enable the annotation, because it's in your class files. – M. Prokhorov Dec 18 '19 at 18:09
  • [This link](https://stackoverflow.com/questions/15280789/hibernate-ignore-column-if-not-exist) may help, but I advise you to rethink your approach. Generally speaking, if your app uses JPA, it's a DB-driven app, meaning database should be migrated first, then you app. Not the opposite. – M. Prokhorov Dec 18 '19 at 18:11
  • @M.Prokhorov Understood, thank you for the advice. – Ross Sullivan Dec 18 '19 at 18:28

0 Answers0