[SOLVED] How to populate seed data using flyway

Issue

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)

Solution

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_talble.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 Reporsitory with throw new UnsupportedOperationException("country table is read only")

Answered By – ezer

Answer Checked By – Katrina (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.