简体   繁体   English

在 spring boot 中从测试执行中跳过 schema.sql 执行

[英]Skip schema.sql execution from test execution in spring boot

I have below piece of SQL DDL in schema.sql under src/main/resources/ .我在src/main/resources/下的schema.sql有下面一段SQL DDL

ALTER TABLE `user` ADD INDEX IF NOT EXISTS `user_id_index`(user_name(30));

Now test cases are failing because it is trying to create the index in test execution where given table not existing in test DB config.现在测试用例失败了,因为它试图在测试执行中创建索引,而给定的表在测试数据库配置中不存在。

Below is my test DB config.下面是我的测试数据库配置。

public DataSource dataSource() {
    DBConfigurationBuilder config = DBConfigurationBuilder.newBuilder();
    config.setPort(0);
    DB db = newEmbeddedDB(config.build());
    db.start();
    HikariConfig hikariConfig = new HikariConfig();
    hikariConfig.setJdbcUrl(config.getURL("unit-test"));
    return new HikariDataSource(hikariConfig) {
        @PreDestroy
        @SneakyThrows
        public void shutdown() {
            db.stop();
        }
    };
}

Test failure:测试失败:

Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file: xyz/target/classes/schema.sql]: ALTER TABLE `user` ADD INDEX IF NOT EXISTS `user_id_index`(user_name(30)); nested exception is java.sql.SQLSyntaxErrorException: Table 'unit-test.user' doesn't exist
at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:622)
at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:254)
at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:49)
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runScripts(DataSourceInitializer.java:202)
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.createSchema(DataSourceInitializer.java:101)
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.afterPropertiesSet(DataSourceInitializerInvoker.java:63)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1847)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1784)
... 101 common frames omitted
Caused by: java.sql.SQLSyntaxErrorException: Table 'unit-test.user' doesn't exist
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)

How to skip the schema.sql is being executed in test execution?如何跳过测试执行中正在执行的schema.sql or any alterative way to try?或任何其他尝试方法?

For now, added a table create statement just before the index creation to fix the test failures.现在,在索引创建之前添加了一个 table create 语句来修复测试失败。

CREATE TABLE IF NOT EXISTS `user` (
  `id` varchar(255) NOT NULL,
  `user_name` text DEFAULT NULL,
   PRIMARY KEY (`id`)
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM