Flyway is famous as Java DB migration, but I haven't touched it, so I introduced it during the New Year holidays. It was estimated that it would take 3 hours to work, but in reality it took about 15 minutes.
The only steps required to install it are as follows: Easy!
build.gradle
ext {
// Flyway
flywayVersion = "4.1.2" // 2017,0315 latest
}
dependencies {
// Flyway
compile ("org.flywaydb:flyway-core:$flywayVersion")
}
plugins {
id "org.flywaydb.flyway" version "4.1.2" // 2017,0315 latest
}
flyway {
user = '' //DB user name
url = ''//DB URL
}
Next, deploy SQL files such as CretateTable statement below
・ Src / main / resources / db / migration /
There is a naming convention for the file names of the files to be deployed. Also, it seems that Create Database etc. can not be written
sql:V1.0__Initial_Setup.sql
CREATE TABLE IF NOT EXISTS ANSWER_INFO_TBL (
NO BIGINT(10) UNSIGNED PRIMARY KEY AUTO_INCREMENT,
TYPE TINYINT(1) NOT NULL,
ANSWERKEY VARCHAR(81) UNIQUE NOT NULL,
KEYHASH VARCHAR(64) UNIQUE NOT NULL ,
CREATE_DATE DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=INNODB DEFAULT CHARSET=UTF8;
CREATE TABLE IF NOT EXISTS SCORE_INFO_TBL (
NO BIGINT(10) UNSIGNED PRIMARY KEY AUTO_INCREMENT,
SCORE MEDIUMINT(7) UNSIGNED DEFAULT 0 NOT NULL,
NAME VARCHAR(64) DEFAULT '' NOT NULL,
MEMO VARCHAR(64) DEFAULT '' NOT NULL,
UPDATE_DATE DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (NO)
REFERENCES ANSWER_INFO_TBL(NO)
ON DELETE CASCADE
) ENGINE=INNODB DEFAULT CHARSET=UTF8;
ALTER TABLE ANSWER_INFO_TBL ADD INDEX INDEX_ANSWERKEY(ANSWERKEY ASC);
ALTER TABLE ANSWER_INFO_TBL ADD INDEX INDEX_KEYHASH(KEYHASH ASC);
DB migration is performed automatically when launching the application from IntelliJ IDEA.
While making an application, I may create garbage data or want to reset the DB. However, I want to keep clean data ... → Extract existing records that have no problem and insert them automatically after DB reset →→ Let Flyway do it!
If you start Springboot under the above conditions after dropping the table, it will automate the table creation and record creation. If the version control of the sql file is also good, the hurdle for building the environment will be lowered!
I was able to introduce it smoothly, so I feel like pushing my back, which I had been hesitant to introduce. DB resets are frequent, so be careful not to neglect the management of sql files. In addition, I think that there are some useful functions that I haven't seen yet just by introducing them, so I will add them as soon as the functions are discovered. Also, it seems that there is an article tag of Flyway, so I think you can follow it.
When changing from Date type to LocalDate type in JPA Entity It seems that you have to use the following techniques
WebApp.java
@SpringBootApplication
@Import(Domain.class)
@EntityScan(basePackageClasses = {WebApp.class, Jsr310JpaConverters.class})
@EnableScheduling
public class WebApp extends SpringBootServletInitializer {
public static void main(String[] args) {
new SpringApplicationBuilder(WebApp.class)
.profiles("dev")
.run(args);
}
}
The miso is that Jsr310JpaConverters.class is the target of Entity Scan.
Recommended Posts