Skip to content

Commit

Permalink
Use the schema of the connection
Browse files Browse the repository at this point in the history
  • Loading branch information
bchapuis committed Jan 13, 2025
1 parent 1ccd27b commit 496cafc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,12 @@ public class RefreshMaterializedViews implements Task {

private Object database;

private String schema = "public";

public RefreshMaterializedViews() {
// Default constructor
}

public RefreshMaterializedViews(Object database, String schema) {
public RefreshMaterializedViews(Object database) {
this.database = database;
this.schema = schema;
}

@Override
Expand All @@ -50,19 +47,22 @@ public void execute(WorkflowContext context) throws Exception {
try (var connection = dataSource.getConnection()) {
LOGGER.info("Connected to PostgreSQL database.");

// 1. Retrieve database objects (tables, views, materialized views).
// Get the schema of the database.
var schema = connection.getSchema();

// Retrieve database objects (tables, views, materialized views).
var objects = DatabaseMetadataRetriever.getObjects(connection, schema);

// 2. Retrieve dependencies between database objects.
// Retrieve dependencies between database objects.
var dependencies = DatabaseMetadataRetriever.getDependencies(connection, schema, objects);

// 3. Build a directed graph of dependencies between the database objects.
var graph = DependencyGraphBuilder.buildGraph(connection, schema, objects, dependencies);
// Build a directed graph of dependencies between the database objects.
var graph = DependencyGraphBuilder.buildGraph(objects, dependencies);

// 4. Perform a topological sort so that dependencies come before dependents.
// Perform a topological sort so that dependencies come before dependents.
var sorted = DependencyGraphBuilder.topologicalSort(graph);

// 5. Refresh materialized views, dropping and recreating indexes if present.
// Refresh materialized views, dropping and recreating indexes if present.
MaterializedViewRefresher.refreshMaterializedViews(connection, sorted);

LOGGER.info("Done refreshing materialized views.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,12 @@ public record DatabaseObject(
String schemaName,
String objectName,
ObjectType objectType) {

}

/**
* Record representing a dependency between two database objects.
*/
public record DatabaseDependency(DatabaseObject source, DatabaseObject dependent) {

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import com.google.common.graph.GraphBuilder;
import com.google.common.graph.MutableGraph;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -49,8 +48,6 @@ private DependencyGraphBuilder() {
* @return a MutableGraph<DatabaseObject>
*/
public static MutableGraph<DatabaseObject> buildGraph(
Connection connection,
String schema,
List<DatabaseObject> objects,
List<DatabaseDependency> dependencies) throws SQLException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,19 @@ private static void dropIndexes(Connection connection, List<DatabaseIndex> index
for (var idx : indexes) {
LOGGER.info("Dropping index: " + idx.indexName());
try (var st = connection.createStatement()) {
var dropSql = "DROP INDEX IF EXISTS " + idx.indexName();
var dropSql = String.format(
"DROP INDEX IF EXISTS %s",
idx.indexName());
st.execute(dropSql);
}
}
}

private static void refreshMaterializedView(Connection connection, DatabaseObject mv)
throws SQLException {
var refreshSql =
"REFRESH MATERIALIZED VIEW " + mv.schemaName() + "." + mv.objectName() + " WITH DATA";
var refreshSql = String.format(
"REFRESH MATERIALIZED VIEW %s WITH DATA",
mv.objectName());
try (var st = connection.createStatement()) {
st.execute(refreshSql);
}
Expand Down

0 comments on commit 496cafc

Please sign in to comment.