V1__create_database.sql
:
CREATE TABLE persons (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
age INT NOT NULL
);
V1.1__initial_inserts.sql
:
INSERT INTO persons (name, age) VALUES ('John Foden', 30), ('Anna Deyoe', 25), ('David Sanchez', 35);
V2__alter_table_persons.sql
:
ALTER TABLE persons ADD COLUMN email VARCHAR(150) UNIQUE;
ALTER TABLE persons ADD COLUMN birth_date DATE;
UPDATE persons SET birth_date = '1990-01-01' WHERE name = 'John Foden';
UPDATE persons SET birth_date = '1995-06-15' WHERE name = 'Anna Deyoe';
UPDATE persons SET birth_date = '1985-03-10' WHERE name = 'David Sanchez';
V3__create_table_address.sql
:
CREATE TABLE addresses (
id SERIAL PRIMARY KEY,
street VARCHAR(150) NOT NULL,
city VARCHAR(100) NOT NULL,
person_id INT NOT NULL,
CONSTRAINT fk_person FOREIGN KEY (person_id) REFERENCES persons(id)
);
INSERT INTO addresses (street, city, person_id) VALUES ('Calle Falsa 123', 'Madrid', 1),
('Avenida Siempre Viva 742', 'Barcelona', 2), ('Calle Luna 456', 'Sevilla', 3);
V4__rename_column_name.sql
:
ALTER TABLE persons RENAME COLUMN name TO full_name;
V5__split_full_name.sql
:
ALTER TABLE persons ADD COLUMN name VARCHAR(100);
ALTER TABLE persons ADD COLUMN surname VARCHAR(100);
UPDATE persons SET name = split_part(full_name, ' ', 1);
UPDATE persons SET surname = split_part(full_name, ' ', 2);
ALTER TABLE persons DROP COLUMN full_name;
Example:
# Flyway properties
quarkus.flyway.migrate-at-start=true
quarkus.flyway.schemas=public # Comment or remove this line after base line creation
quarkus.flyway.baseline-on-migrate=true # Comment or remove this line after base line creation
quarkus.flyway.baseline-version=1 # Comment or remove this line after base line creation
# Database connection properties
quarkus.datasource.db-kind=postgresql
quarkus.datasource.username=myuser
quarkus.datasource.password=mypassword
quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/mydb
Flyway is set current database as version 1
and does not try to execute V1__initial.sql
(empty file). Then, just create new script migration, i.e. V2__add_new_colum.sql
.