Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate seed data using flyway

Tags:

java

mysql

flyway

We have a countries table, which contains the country names which will never change.

How to populate country specific seed data using flyway on application start preferably with the same primary keys, so that it doesn't change across environments (say dev, qa, prod)

like image 496
Rpj Avatar asked Nov 24 '25 22:11

Rpj


1 Answers

you can use flyway migrate assuming this is the first scheme modification (otherwise start from consecutive V#)

V1__create_countries_table.sql:

create table COUNTRY (
    ID int not null,
    NAME varchar(100) not null
    -- additional fields and constraints
);

V2__add_values_to_country_table.sql:

insert into COUNTRY (ID, NAME) values (1, 'Afghanistan');
insert into COUNTRY (ID, NAME) values (2, 'Albania');
... 

since this is data is static and immutable you can protect it by overwriting create/update/delete on your java Country JPA Repository with throw new UnsupportedOperationException("country table is read only")

like image 152
ezer Avatar answered Nov 26 '25 13:11

ezer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!