如何在java中删除模式的所有数据库对象?

时间:2018-06-15 16:16:14

标签: java oracle testing jdbc

对于java中的oracle数据库模式,清除(删除所有对象,表,视图等)的最佳方法是什么?

我可以使用Oracle数据库客户端运行https://stackoverflow.com/a/1690419/812093之类的东西但是有没有可以使用的java库或工具?我知道我可以读取连接元数据,遍历所有对象并删除它们,但我想知道是否有人之前做过,如果有任何可以使用的开箱即可。

我需要此功能才能设置集成测试 - 清理数据库架构 - 针对该模式执行一系列脚本以初始化数据库

2 个答案:

答案 0 :(得分:1)

迁飞

看起来Flyway数据库迁移工具提供了我正在寻找的功能 - 至少如果你觉得自己喜欢黑客攻击:)因为我需要这个功能仅用于集成测试,这对我来说已经足够了。让我分享一下我的代码。

添加Flyway和Oracle驱动程序依赖

<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
    <version>5.1.1</version>
    <scope>test</scope>
</dependency>       
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc7</artifactId>
    <version>12.1.0.2</version>
    <scope>provided</scope>
</dependency>   

执行脚本和清理db的主类

public class ExecuteScriptsAgainstOracle {

    public static void main(String args[]) throws Exception, SQLException {

        Flyway flyway = new Flyway();
        flyway.setDataSource("jdbc:oracle:thin:@xxx:1521:yyy", "zzz", "zzz");
        flyway.setTable("auto_schema_setup_flyway");

        Database<?> database = DatabaseFactory.createDatabase(flyway.getConfiguration(), false);

// clean the database (remove all objects of the schema)
        flyway.clean();
        flyway.setSkipDefaultResolvers(true);
        flyway.setResolvers(new MyMigrationResolver(database, flyway.getConfiguration()));
        flyway.migrate();
    }

}

列出要执行的文件的MigrationResolver

package org.flywaydb.core.internal.resolver.sql;

公共类MyMigrationResolver实现MigrationResolver {

private Database<?> database;
private Configuration configuration;
private int order = 0;

public MyMigrationResolver(Database<?> database, Configuration configuration) {
    this.database = database;
    this.configuration = configuration;
}

@Override
public Collection<ResolvedMigration> resolveMigrations() {

    Collection<ResolvedMigration> scripts = new LinkedList<ResolvedMigration>();
    scripts.add(script(folder1\\somescript.dml"));
    scripts.add(script("folder2\\someOtherScript.sql"));

    return scripts;
}

private ResolvedMigrationImpl script(String scriptName) {


    String baseFolder = "target\\scripts\\";
    order++;

    ResolvedMigrationImpl migration = new ResolvedMigrationImpl();
    migration.setScript(baseFolder+scriptName);
    migration.setType(MigrationType.SQL);
    migration.setDescription(""+String.format("%03d",order)+" "+scriptName);
    migration.setExecutor(new SqlMigrationExecutor(database,
            new FileSystemResource(migration.getScript(), configuration.getEncoding()),
            new PlaceholderReplacer() {

                @Override
                public String replacePlaceholders(String input) {

                    // just remove parts of the sql that flyway can't deal with
                    input = StringUtils.replace(input, "WHENEVER SQLERROR EXIT SQL.SQLCODE ROLLBACK;", "");
                    input = StringUtils.replace(input, "SET DEFINE OFF;", "");

                    return input;
                }

                @Override
                public Map<String, String> getPlaceholderReplacements() {
                    return null;
                }
            }
            , configuration));
    return migration;
}

}

答案 1 :(得分:0)

因为你的问题不明确,所以我会画出两个方向:

方向1 - 同时删除所有对象和用户。

最好的方法是:jq -n '{key1: "value1", key2: $gpg}' --arg gpg hi | curl -d @- 在具有drop user <user_name_here> cascade;系统特权的用户下运行SQL语句。

方向2 - 删除所有对象但仍保留用户(所有角色和权限)

首先列出所有权限并复制到记事本(例如hr用户):

DROP USER

然后删除用户级联;

重新创建用户并运行记事本文件的内容。

感谢Alex Poole的建议!