22
loading...
This website collects cookies to deliver better user experience
pom.xml
:<!-- Flyway -->
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
application.yml
:spring:
flyway:
url: jdbc:mysql://${DATABASE_HOST:localhost}:${DATABASE_PORT:3306}/${DATABASE:register}?createDatabaseIfNotExist=true
user: ${DATABASE_USER:root}
password: ${DATABASE_PASSWD:root}
schemas:
- ${DATABASE:register}
url
, user
, password
e o schema
que o Flyway irá usar para se conectar no banco de dados.resources
na nossa aplicação, por padrão o Flyway irá procurar pelo caminho resources/db/migration
então basta criar os diretórios e adicionar os scripts.underline
exemplo:V1_1__meu-script.sql
V1_0__create_tables.sql
:create table perfil (
id integer not null auto_increment,
name varchar(255),
primary key (id)
) engine=InnoDB;
create table user (
id integer not null auto_increment,
email varchar(255),
pass varchar(255),
primary key (id)
) engine=InnoDB;
create table user_perfis (
user_id integer not null,
perfis_id integer not null,
primary key (user_id, perfis_id)
) engine=InnoDB;
alter table user_perfis add constraint perfil_id_constraint foreign key (perfis_id) references perfil (id);
alter table user_perfis add constraint user_id_constraint foreign key (user_id) references user (id);
V1_1__init.sql
:INSERT INTO register.`user`
(email, pass)
VALUES('[email protected]', '$2a$10$JgDI7KttG8BX9AO.3mGTref9mjDxHKtx3sjqnaP3Vq88BzUNxA38S');
INSERT INTO register.perfil
(name)
VALUES('USER');
INSERT INTO register.perfil
(name)
VALUES('ADMIN');
INSERT INTO register.user_perfis
(user_id, perfis_id)
VALUES(1, 1);
INFO 77800 --- [restartedMain] o.f.c.internal.license.VersionPrinter : Flyway Community Edition 7.1.1 by Redgate
INFO 77800 --- [restartedMain] o.f.c.i.database.base.DatabaseType : Database: jdbc:mysql://localhost:3306/register (MySQL 8.0)
INFO 77800 --- [restartedMain] o.f.core.internal.command.DbValidate : Successfully validated 2 migrations (execution time 00:00.152s)
INFO 77800 --- [restartedMain] o.f.c.i.s.JdbcTableSchemaHistory : Creating Schema History table `register`.`flyway_schema_history` ...
INFO 77800 --- [restartedMain] o.f.core.internal.command.DbMigrate : Current version of schema `register`: << Empty Schema >>
INFO 77800 --- [restartedMain] o.f.core.internal.command.DbMigrate : Migrating schema `register` to version "1.0 - create tables"
INFO 77800 --- [restartedMain] o.f.core.internal.command.DbMigrate : Migrating schema `register` to version "1.1 - init"
INFO 77800 --- [restartedMain] o.f.core.internal.command.DbMigrate : Successfully applied 2 migrations to schema `register` (execution time 00:00.625s)
INFO 78443 --- [restartedMain] o.f.c.internal.license.VersionPrinter : Flyway Community Edition 7.1.1 by Redgate
INFO 78443 --- [restartedMain] o.f.c.i.database.base.DatabaseType : Database: jdbc:mysql://localhost:3306/register (MySQL 8.0)
INFO 78443 --- [restartedMain] o.f.core.internal.command.DbValidate : Successfully validated 2 migrations (execution time 00:00.115s)
INFO 78443 --- [restartedMain] o.f.core.internal.command.DbMigrate : Current version of schema `register`: 1.1
INFO 78443 --- [restartedMain] o.f.core.internal.command.DbMigrate : Schema `register` is up to date. No migration necessary.
| instaled_rank | version | description | type | script | checksum | installed_by | installed_on | execution_time | success |
|---------------|---------|---------------|------|-------------------------|-------------|--------------|---------------------|----------------|---------|
| 1 | 1.0 | create tables | SQL | V1_0__create_tables.sql | 1225588812 | root | 2021-01-11 17:57:36 | 460 | 1 |
| 2 | 1.1 | init | SQL | V1_1__init.sql | -1406395169 | root | 2021-01-11 17:57:36 | 27 | 1 |
application.yml
:spring:
flyway:
baseline-on-migrate: true