-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
620 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/liquibase/ext/neo4j/snapshot/LabelSnapshotGeneratorNeo4j.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package liquibase.ext.neo4j.snapshot; | ||
|
||
import liquibase.database.Database; | ||
import liquibase.exception.DatabaseException; | ||
import liquibase.exception.LiquibaseException; | ||
import liquibase.ext.neo4j.database.Neo4jDatabase; | ||
import liquibase.ext.neo4j.structure.Label; | ||
import liquibase.snapshot.DatabaseSnapshot; | ||
import liquibase.snapshot.InvalidExampleException; | ||
import liquibase.snapshot.SnapshotGenerator; | ||
import liquibase.snapshot.SnapshotGeneratorChain; | ||
import liquibase.snapshot.jvm.CatalogSnapshotGenerator; | ||
import liquibase.statement.core.RawSqlStatement; | ||
import liquibase.structure.DatabaseObject; | ||
import liquibase.structure.core.Catalog; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class LabelSnapshotGeneratorNeo4j implements SnapshotGenerator { | ||
|
||
@Override | ||
public int getPriority(Class<? extends DatabaseObject> objectType, Database database) { | ||
if (!(database instanceof Neo4jDatabase)) { | ||
return PRIORITY_NONE; | ||
} | ||
if (Label.class.isAssignableFrom(objectType)) { | ||
return PRIORITY_DEFAULT; | ||
} | ||
if (Catalog.class.isAssignableFrom(objectType)) { | ||
return PRIORITY_ADDITIONAL; | ||
} | ||
return PRIORITY_NONE; | ||
} | ||
|
||
@Override | ||
public <T extends DatabaseObject> T snapshot(T example, DatabaseSnapshot snapshot, SnapshotGeneratorChain chain) throws DatabaseException, InvalidExampleException { | ||
Database database = snapshot.getDatabase(); | ||
if (!(database instanceof Neo4jDatabase)) { | ||
return chain.snapshot(example, snapshot); | ||
} | ||
if (!snapshot.getSnapshotControl().shouldInclude(Label.class)) { | ||
return chain.snapshot(example, snapshot); | ||
} | ||
if (example instanceof Label) { | ||
return example; | ||
} | ||
if (!(example instanceof Catalog)) { | ||
return chain.snapshot(example, snapshot); | ||
} | ||
Catalog catalog = (Catalog) example; | ||
Neo4jDatabase neo4j = (Neo4jDatabase) snapshot.getDatabase(); | ||
retrieveLabels(neo4j, catalog) | ||
.forEach(catalog::addDatabaseObject); | ||
return example; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public Class<? extends DatabaseObject>[] addsTo() { | ||
return new Class[]{Catalog.class}; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public Class<? extends SnapshotGenerator>[] replaces() { | ||
return new Class[] {CatalogSnapshotGenerator.class}; | ||
} | ||
|
||
private static List<Label> retrieveLabels(Neo4jDatabase database, Catalog catalog) throws DatabaseException { | ||
try { | ||
return database.run(new RawSqlStatement("CALL db.labels() YIELD label RETURN label")) | ||
.stream() | ||
.map(row -> new Label(catalog, (String) row.get("label"))) | ||
.collect(Collectors.toList()); | ||
} catch (LiquibaseException e) { | ||
throw new DatabaseException("Could not retrieve node labels during label snapshot", e); | ||
} | ||
} | ||
} |
Oops, something went wrong.