Search code examples
javaschemacrawler

How to use SchemaCrawler's Offline Snapshots in Java code


I think the header explains it all: Is there a nice way to create and load offline snapshots of database schema using SchemaCrawler without using command line? If yes, can you provide some example code / link please? If not, some example java code to use command line options would be helpful too (I don't have much experience with that)!

Thanks for any kind of help!

PS: I managed to create offline snapshot with this code:

    final SchemaCrawlerOptions options = new SchemaCrawlerOptions();
    // Set what details are required in the schema - this affects the
    // time taken to crawl the schema
    options.setSchemaInfoLevel(SchemaInfoLevelBuilder.standard());
    options.setRoutineInclusionRule(new ExcludeAll());
    options.setSchemaInclusionRule(new RegularExpressionInclusionRule(/* some regex here*/));
    options.setTableInclusionRule(new RegularExpressionExclusionRule(/*some regex here*/));

    outputOptions.setCompressedOutputFile(Paths.get("./test_db_snapshot.xml"));
    final String command = "serialize";

    final Executable executable = new SchemaCrawlerExecutable(command);
    executable.setSchemaCrawlerOptions(options);
    executable.setOutputOptions(outputOptions);
    executable.execute(getConnection());

Not sure how to connect to it though.


Solution

  • You need to use the schemacrawler.tools.offline.OfflineSnapshotExecutable along with an schemacrawler.tools.offline.jdbc.OfflineConnection to "connect" to your database snapshoot.

    Please take a look at the following code: OfflineSnapshotTest.offlineSnapshotExecutable()

    And @ZidaneT, to load an offline snapshot, use code like that in LoadSnapshotTest.java

    Sualeh Fatehi, SchemaCrawler