Search in sources :

Example 1 with RestoredDatabaseSnapshot

use of liquibase.snapshot.RestoredDatabaseSnapshot in project liquibase by liquibase.

the class YamlSnapshotParser method parse.

@Override
public DatabaseSnapshot parse(String path, ResourceAccessor resourceAccessor) throws LiquibaseParseException {
    Yaml yaml = new Yaml();
    try {
        InputStream stream = StreamUtil.singleInputStream(path, resourceAccessor);
        if (stream == null) {
            throw new LiquibaseParseException(path + " does not exist");
        }
        Map parsedYaml;
        try {
            parsedYaml = yaml.loadAs(new InputStreamReader(stream, LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding()), Map.class);
        } catch (Exception e) {
            throw new LiquibaseParseException("Syntax error in " + getSupportedFileExtensions()[0] + ": " + e.getMessage(), e);
        } finally {
            try {
                stream.close();
            } catch (IOException ioe) {
            }
        }
        Map rootList = (Map) parsedYaml.get("snapshot");
        if (rootList == null) {
            throw new LiquibaseParseException("Could not find root snapshot node");
        }
        String shortName = (String) ((Map) rootList.get("database")).get("shortName");
        Database database = DatabaseFactory.getInstance().getDatabase(shortName).getClass().newInstance();
        DatabaseSnapshot snapshot = new RestoredDatabaseSnapshot(database);
        ParsedNode snapshotNode = new ParsedNode(null, "snapshot");
        snapshotNode.setValue(rootList);
        Map metadata = (Map) rootList.get("metadata");
        if (metadata != null) {
            snapshot.getMetadata().putAll(metadata);
        }
        snapshot.load(snapshotNode, resourceAccessor);
        return snapshot;
    } catch (Throwable e) {
        if (e instanceof LiquibaseParseException) {
            throw (LiquibaseParseException) e;
        }
        throw new LiquibaseParseException(e);
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) Yaml(org.yaml.snakeyaml.Yaml) IOException(java.io.IOException) LiquibaseParseException(liquibase.exception.LiquibaseParseException) LiquibaseParseException(liquibase.exception.LiquibaseParseException) Database(liquibase.database.Database) Map(java.util.Map) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) RestoredDatabaseSnapshot(liquibase.snapshot.RestoredDatabaseSnapshot) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) RestoredDatabaseSnapshot(liquibase.snapshot.RestoredDatabaseSnapshot)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Map (java.util.Map)1 Database (liquibase.database.Database)1 LiquibaseParseException (liquibase.exception.LiquibaseParseException)1 ParsedNode (liquibase.parser.core.ParsedNode)1 DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)1 EmptyDatabaseSnapshot (liquibase.snapshot.EmptyDatabaseSnapshot)1 RestoredDatabaseSnapshot (liquibase.snapshot.RestoredDatabaseSnapshot)1 Yaml (org.yaml.snakeyaml.Yaml)1