Flyway 未在 Spring MVC 中获取迁移文件
Flyway not picking up migration file in Spring MVC
我遵循了一些教程并为数据库初始化配置了 Flyway。
我从 MYSQL(无数据)中获取了模式转储并将文件命名为 V1__initialSchema.sql
。所以这充满了特定的创建 table、外键等,由 mysql.
转储
然后我配置了 bean:
Flyway 初始服务器
@Bean(initMethod = "migrate")
protected Flyway flyway() {
Flyway flyway = new Flyway();
flyway.setBaselineOnMigrate(true);
//flyway.setLocations("classpath:db/migration");
flyway.setDataSource(dataSource());
return flyway;
}
JPA 初始化程序
@Bean
@DependsOn(value = "flyway")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
factory.setDataSource(dataSource());
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.ideafactory.mvc", "com.ideafactory.plugins");
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
//jpaProperties.put("hibernate.hbm2ddl.auto","create-drop");
jpaProperties.put("hibernate.show_sql", false);
jpaProperties.put("hibernate.format_sql", false);
jpaProperties.put("hibernate.use_sql_comments", false);
jpaProperties.put("hibnerate.connection.CharSet", "utf8");
jpaProperties.put("hibernate.connect.characterEncoding", "utf8");
jpaProperties.put("hibernate.connection.useUnicode", true);
jpaProperties.put("jadira.usertype.autoRegisterUserTypes", true);
factory.setJpaProperties(jpaProperties);
factory.afterPropertiesSet();
factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
return factory;
}
我打开了日志记录,我可以看到它是 "skipping" 我的初始化文件,我不确定为什么。架构尚未创建。
l.util.logging.slf4j.Slf4jLog 40 debug - Scanning for classpath resources at 'db/migration' (Prefix: 'V', Suffix: '.sql')
16:35:08.272 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning URL: file:/Users//Documents/Projects/MerchantX/target/java_ecommerce/WEB-INF/classes/db/migration/
16:35:08.273 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - JBoss VFS v2 available: false
16:35:08.273 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning starting at classpath root in filesystem: /Users//Documents/Projects/MerchantX/target/java_ecommerce/WEB-INF/classes/
16:35:08.273 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning for resources in path: /Users//Documents/Projects/MerchantX/target/java_ecommerce/WEB-INF/classes/db/migration (db/migration)
16:35:08.273 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Found resource: db/migration/V1__initialSchema.sql
16:35:08.279 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning for classes at 'db/migration' (Implementing: 'org.flywaydb.core.api.migration.jdbc.JdbcMigration')
16:35:08.279 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning URL: file:/Users//Documents/Projects/MerchantX/target/java_ecommerce/WEB-INF/classes/db/migration/
16:35:08.279 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - JBoss VFS v2 available: false
16:35:08.280 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning starting at classpath root in filesystem: /Users//Documents/Projects/MerchantX/target/java_ecommerce/WEB-INF/classes/
16:35:08.280 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning for resources in path: /Users//Documents/Projects/MerchantX/target/java_ecommerce/WEB-INF/classes/db/migration (db/migration)
16:35:08.280 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Filtering out resource: db/migration/V1__initialSchema.sql (filename: V1__initialSchema.sql)
16:35:08.281 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning for classes at 'db/migration' (Implementing: 'org.flywaydb.core.api.migration.spring.SpringJdbcMigration')
16:35:08.281 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning URL: file:/Users//Documents/Projects/MerchantX/target/java_ecommerce/WEB-INF/classes/db/migration/
16:35:08.282 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - JBoss VFS v2 available: false
16:35:08.282 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning starting at classpath root in filesystem: /Users//Documents/Projects/MerchantX/target/java_ecommerce/WEB-INF/classes/
16:35:08.282 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning for resources in path: /Users//Documents/Projects/MerchantX/target/java_ecommerce/WEB-INF/classes/db/migration (db/migration)
16:35:08.282 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Filtering out resource: db/migration/V1__initialSchema.sql (filename: V1__initialSchema.sql)
我以前没有用过Flyway,谁能解释一下为什么它过滤掉了我的初始化文件?
AFAIK baselineOnMigrate
从数据库中的实际模式创建第一个 (V1) 版本。并且只会应用以下版本(V1.1、V2、...)。
所以要么不要使用 baselineOnMigrate
,但您需要从空数据库模式开始,或者从(例如)V2.
开始为您的版本编制索引
我遵循了一些教程并为数据库初始化配置了 Flyway。
我从 MYSQL(无数据)中获取了模式转储并将文件命名为 V1__initialSchema.sql
。所以这充满了特定的创建 table、外键等,由 mysql.
然后我配置了 bean:
Flyway 初始服务器
@Bean(initMethod = "migrate")
protected Flyway flyway() {
Flyway flyway = new Flyway();
flyway.setBaselineOnMigrate(true);
//flyway.setLocations("classpath:db/migration");
flyway.setDataSource(dataSource());
return flyway;
}
JPA 初始化程序
@Bean
@DependsOn(value = "flyway")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
factory.setDataSource(dataSource());
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.ideafactory.mvc", "com.ideafactory.plugins");
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
//jpaProperties.put("hibernate.hbm2ddl.auto","create-drop");
jpaProperties.put("hibernate.show_sql", false);
jpaProperties.put("hibernate.format_sql", false);
jpaProperties.put("hibernate.use_sql_comments", false);
jpaProperties.put("hibnerate.connection.CharSet", "utf8");
jpaProperties.put("hibernate.connect.characterEncoding", "utf8");
jpaProperties.put("hibernate.connection.useUnicode", true);
jpaProperties.put("jadira.usertype.autoRegisterUserTypes", true);
factory.setJpaProperties(jpaProperties);
factory.afterPropertiesSet();
factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
return factory;
}
我打开了日志记录,我可以看到它是 "skipping" 我的初始化文件,我不确定为什么。架构尚未创建。
l.util.logging.slf4j.Slf4jLog 40 debug - Scanning for classpath resources at 'db/migration' (Prefix: 'V', Suffix: '.sql')
16:35:08.272 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning URL: file:/Users//Documents/Projects/MerchantX/target/java_ecommerce/WEB-INF/classes/db/migration/
16:35:08.273 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - JBoss VFS v2 available: false
16:35:08.273 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning starting at classpath root in filesystem: /Users//Documents/Projects/MerchantX/target/java_ecommerce/WEB-INF/classes/
16:35:08.273 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning for resources in path: /Users//Documents/Projects/MerchantX/target/java_ecommerce/WEB-INF/classes/db/migration (db/migration)
16:35:08.273 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Found resource: db/migration/V1__initialSchema.sql
16:35:08.279 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning for classes at 'db/migration' (Implementing: 'org.flywaydb.core.api.migration.jdbc.JdbcMigration')
16:35:08.279 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning URL: file:/Users//Documents/Projects/MerchantX/target/java_ecommerce/WEB-INF/classes/db/migration/
16:35:08.279 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - JBoss VFS v2 available: false
16:35:08.280 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning starting at classpath root in filesystem: /Users//Documents/Projects/MerchantX/target/java_ecommerce/WEB-INF/classes/
16:35:08.280 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning for resources in path: /Users//Documents/Projects/MerchantX/target/java_ecommerce/WEB-INF/classes/db/migration (db/migration)
16:35:08.280 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Filtering out resource: db/migration/V1__initialSchema.sql (filename: V1__initialSchema.sql)
16:35:08.281 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning for classes at 'db/migration' (Implementing: 'org.flywaydb.core.api.migration.spring.SpringJdbcMigration')
16:35:08.281 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning URL: file:/Users//Documents/Projects/MerchantX/target/java_ecommerce/WEB-INF/classes/db/migration/
16:35:08.282 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - JBoss VFS v2 available: false
16:35:08.282 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning starting at classpath root in filesystem: /Users//Documents/Projects/MerchantX/target/java_ecommerce/WEB-INF/classes/
16:35:08.282 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Scanning for resources in path: /Users//Documents/Projects/MerchantX/target/java_ecommerce/WEB-INF/classes/db/migration (db/migration)
16:35:08.282 DEBUG org.flywaydb.core.internal.util.logging.slf4j.Slf4jLog 40 debug - Filtering out resource: db/migration/V1__initialSchema.sql (filename: V1__initialSchema.sql)
我以前没有用过Flyway,谁能解释一下为什么它过滤掉了我的初始化文件?
AFAIK baselineOnMigrate
从数据库中的实际模式创建第一个 (V1) 版本。并且只会应用以下版本(V1.1、V2、...)。
所以要么不要使用 baselineOnMigrate
,但您需要从空数据库模式开始,或者从(例如)V2.