-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* #73 Update Yaci Store: PostgreSQL support and schema management * Fix artifact save path and clarify comment formatting * Update external DB management message in YaciStoreService
- Loading branch information
Showing
9 changed files
with
182 additions
and
16 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
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
99 changes: 99 additions & 0 deletions
99
...ain/java/com/bloxbean/cardano/yacicli/localcluster/yacistore/YaciStoreCustomDbHelper.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,99 @@ | ||
package com.bloxbean.cardano.yacicli.localcluster.yacistore; | ||
|
||
import com.bloxbean.cardano.yacicli.localcluster.ClusterService; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.commons.io.FileUtils; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.sql.*; | ||
|
||
import static com.bloxbean.cardano.yacicli.util.ConsoleWriter.*; | ||
import static com.bloxbean.cardano.yacicli.util.ConsoleWriter.success; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Getter | ||
public class YaciStoreCustomDbHelper { | ||
private final ClusterService clusterService; | ||
|
||
//External postgres db configuration | ||
@Value("${yaci.store.db.url:#{null}}") | ||
private String storeDbUrl; | ||
@Value("${yaci.store.db.username:#{null}}") | ||
private String storeDbUsername; | ||
@Value("${yaci.store.db.password:#{null}}") | ||
private String storeDbPassword; | ||
|
||
public void dropDatabase(String clusterName) { | ||
if (storeDbUrl == null || storeDbUrl.isEmpty() || !storeDbUrl.contains("postgresql")) { | ||
//Looks like default db. | ||
Path clusterFolder = clusterService.getClusterFolder(clusterName); | ||
Path nodeFolder = clusterFolder.resolve("node"); | ||
String dbDir = "yaci_store"; | ||
Path dbPath = nodeFolder.resolve(dbDir); | ||
|
||
writeLn(info("Deleting Yaci Store db folder : " + dbPath.toFile().getAbsolutePath())); | ||
if (dbPath.toFile().exists()) { | ||
try { | ||
FileUtils.deleteDirectory(dbPath.toFile()); | ||
writeLn(success("Yaci Store db folder deleted successfully")); | ||
} catch (IOException e) { | ||
writeLn(error("Yaci store db could not be deleted : " + dbPath.toAbsolutePath())); | ||
} | ||
} | ||
|
||
return; | ||
} | ||
|
||
if(storeDbUrl != null && storeDbUrl.contains("postgres")) { | ||
dropPostgresSqlSchema(storeDbUrl, storeDbUsername, storeDbPassword); | ||
} | ||
} | ||
|
||
private void dropPostgresSqlSchema(String url, String user, String password) { | ||
Connection conn = null; | ||
try { | ||
Class.forName("org.postgresql.Driver"); | ||
|
||
// Establish the connection | ||
conn = DriverManager.getConnection(url, user, password); | ||
|
||
String schema = conn.getSchema(); | ||
|
||
writeLn("Got schema: " + schema); | ||
// Try to drop the schema | ||
String dropSchemaSQL = "DROP SCHEMA " + schema + " CASCADE"; | ||
try (Statement stmt = conn.createStatement()) { | ||
stmt.executeUpdate(dropSchemaSQL); | ||
writeLn("Schema '" + schema + "' dropped successfully."); | ||
} catch (SQLException e) { | ||
// Handle specific SQLState codes | ||
String sqlState = e.getSQLState(); | ||
if ("42501".equals(sqlState)) { | ||
writeLn(error("Insufficient privileges to drop the schema '" + schema + "'.")); | ||
} else if ("3F000".equals(sqlState)) { | ||
writeLn(error("Schema '" + schema + "' does not exist.")); | ||
} else { | ||
writeLn(error("An error occurred while trying to drop the schema:")); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} catch (ClassNotFoundException e) { | ||
writeLn(error("PostgreSQL JDBC Driver not found.")); | ||
e.printStackTrace(); | ||
} catch (SQLException e) { | ||
writeLn(error("Database connection error occurred.")); | ||
e.printStackTrace(); | ||
} finally { | ||
// Close the connection | ||
if (conn != null) { | ||
try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } | ||
} | ||
} | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
tag=0.10.0-preview1 | ||
tag=0.10.0-preview2 | ||
revision= |