How to populate seed data using flyway

Viewed 908

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)

3 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_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")

It somewhat depends on your database structure but you can have the insert statements for your country names as a migration script with set primary keys.

If you have Flyway as part of your Java application, you would have Flyway Migrate run on application start up. This would allow the application to deploy the country names to a new environment when run and since its a migration script it would not run again if the environment has already had it run prior as it would be in the schema history table. You could also add to the table if more names are required and alter names as well via subsequent migration scripts.

The only issue with this would be making sure nothing else adds to this table outside of the migration scripts you have created as primary key clashes would cause the migration to fail.

In a SQL database, you should use views to do this. These are considered to be DDL and will be included in all build scripts, whereas data inserts aren’t. They are read-only, and so a hacker cannot add a spurious currency etc. Here is a rather impractical example that allows you to lookup the words used in the Brythonic language for counting up to twenty.

CREATE VIEW BrythonicCounting
AS
  SELECT TheName, TheValue
    FROM
      (VALUES ('oinos', 1), ('dewou', 2), ('trīs ', 3), ('petwār', 4),
              ('pimpe', 5), ('swexs', 6), ('sextam', 7), ('oxtū', 8),
              ('nawam', 9), ('dekam', 10), ('oindekam', 11),
              ('deudekam', 12), ('trīdekam', 13), ('petwārdekam', 14),
              ('penpedekam', 15), ('swedekam', 16), ('sextandekam', 17),
              ('oxtūdekam', 18), ('nawandekam', 19), ('ukintī', 20)
      ) Brythonic ( TheName, TheValue);
go
Select TheName from BrythonicCounting where Thevalue=12;

As you see, the data is in the definition of the view and is part of the DDL, so will be part of the schema, not the data.

You don't say what relational database system you're using, but just about every relational database will support this syntax. for a view with static read-only data in it.

go
CREATE VIEW AltBrythonicCounting
AS
  SELECT TheName, TheValue
    FROM
      (select 'oinos', 1
      UNION
       SELECT 'dewou', 2
      UNION 
        SELECT 'trīs ', 3
      UNION 
       SELECT 'petwār', 4
      ) Brythonic ( TheName, TheValue);
go
Select * from AltBrythonicCounting
Related