postgres driver updates use dynamic values

This commit is contained in:
Rasmus Neikes 2024-09-01 17:52:14 +02:00
parent db986c8fb5
commit a51115e8e7
8 changed files with 148 additions and 29 deletions

21
pom.xml
View File

@ -174,6 +174,11 @@
<artifactId>javafaker</artifactId> <artifactId>javafaker</artifactId>
<version>1.0.2</version> <version>1.0.2</version>
</dependency> </dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.4</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -190,16 +195,23 @@
</excludes> </excludes>
</configuration> </configuration>
</plugin> </plugin>
<!--
<plugin> <plugin>
<groupId>org.flywaydb</groupId> <groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId> <artifactId>flyway-maven-plugin</artifactId>
<version>10.17.2</version> <version>10.17.2</version>
<configuration> <configuration>
<sqlMigrationSeparator>__</sqlMigrationSeparator>
<configFiles>${project.basedir}/src/main/resources/application.properties</configFiles>
<locations> <locations>
<location>classpath:com/stktrk/app/db/migrations</location> <location>classpath:com/stktrk/app/db/migrations</location>
</locations> </locations>
<sqlMigrationSeparator>__</sqlMigrationSeparator>
<url>jdbc:postgresql://192.168.178.50:7654/flyway_db</url> <url>jdbc:postgresql://192.168.178.50:7654/flyway_db</url>
<user>flyway_user</user> <user>flyway_user</user>
<password>7e7v55UcYGrY0e3UPYI0qtyMA4YJ1ZkTEaoyZ252GluFkiEMHVT9U5ULS7Rg2rGi</password> <password>7e7v55UcYGrY0e3UPYI0qtyMA4YJ1ZkTEaoyZ252GluFkiEMHVT9U5ULS7Rg2rGi</password>
@ -207,12 +219,13 @@
<schema>flyway_db</schema> <schema>flyway_db</schema>
</schemas> </schemas>
</configuration> </configuration>
<dependencies> <dependencies>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql --> <!- https://mvnrepository.com/artifact/org.postgresql/postgresql
<dependency> <dependency>
<groupId>org.postgresql</groupId> <groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId> <artifactId>postgresql</artifactId>
@ -223,6 +236,8 @@
</dependencies> </dependencies>
</plugin> </plugin>
-->
</plugins> </plugins>
</build> </build>
<repositories> <repositories>

View File

@ -4,20 +4,23 @@ import com.arcadedb.gremlin.ArcadeGraph;
import com.github.javafaker.Faker; import com.github.javafaker.Faker;
import com.stktrk.app.db.ConnectionPool; import com.stktrk.app.db.ConnectionPool;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.structure.Vertex; import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Random;
import java.util.StringJoiner; import java.util.StringJoiner;
// https://stackoverflow.com/questions/51221777/failed-to-configure-a-datasource-url-attribute-is-not-specified-and-no-embedd // https://stackoverflow.com/questions/51221777/failed-to-configure-a-datasource-url-attribute-is-not-specified-and-no-embedd
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) @SpringBootApplication(exclude = { FlywayAutoConfiguration.class})
@RestController @RestController
public class AppApplication extends SpringBootServletInitializer { public class AppApplication extends SpringBootServletInitializer {
@ -30,28 +33,26 @@ public class AppApplication extends SpringBootServletInitializer {
List<Vertex> x = List.of(); List<Vertex> x = List.of();
/* try (ArcadeGraphFactory pool = ArcadeGraphFactory.withRemote("192.168.178.50", 2480, "graph", "root", "playwithdata")) {
try (ArcadeGraph graph = pool.get()) {
x = graph.traversal().V().toList();
}
}*/
ArcadeGraph g = ConnectionPool.getGraph(); ArcadeGraph g = ConnectionPool.getGraph();
Faker faker = new Faker(); Faker faker = new Faker();
g.traversal().addV("Profile").property("name", faker.name().firstName()).property("lastName", faker.name().lastName()) Vertex res = g.traversal().addV("Profile").property("name", faker.name().firstName()).property("lastName", faker.name().lastName())
.iterate(); .next();
x = g.traversal().V().toList(); x = g.traversal().V().toList();
Vertex target = x.get(new Random().nextInt(x.size()));
g.traversal().V(res.id()).addE("friend").to(target).iterate();
g.close(); g.close();
x.sort((a, b) -> a.id().toString().compareTo(b.id().toString()));
x.sort(Comparator.comparing(a -> a.id().toString()));
StringJoiner sj = new StringJoiner("<br />\n"); StringJoiner sj = new StringJoiner("<br />\n");
x.forEach(v -> sj.add(v.id().toString()+ " - " +(String) v.property("name").value() + " " + (String) v.property("lastName").value() )); x.forEach(v -> sj.add(v.id().toString()+ " - " +(String) v.property("name").value() + " " + (String) v.property("lastName").value() ));
return "Hello, " + name + "! Added "+ res.property("name").value() + ". There are " + x.size() + " vertices.<br/>\n" + sj;
return "Hello, " + name + "! There are " + x.size() + " vertices.<br/>\n" + sj;
} }
@GetMapping("/delete") @GetMapping("/delete")

View File

@ -0,0 +1,48 @@
package com.stktrk.app.db;
import jakarta.annotation.PostConstruct;
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.migration.JavaMigration;
import org.flywaydb.core.api.output.MigrateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.sql.SQLException;
@Configuration
public class FlywayConfig {
// Custom migration history table name
private static final String SCHEMA_HISTORY_TABLE = "flyway_schema_history";
@Autowired
private DataSource dataSource;
@Autowired
private ApplicationContext applicationContext;
@PostConstruct
public void migrate() throws SQLException {
// Retrieving all migration implementation classes through the Spring container
// All implementation classes no longer need to be defined in the `db.migration` package,
// they can be placed in any location that supports Bean scanning
JavaMigration[] migrationBeans = applicationContext
.getBeansOfType(JavaMigration.class)
.values()
.toArray(new JavaMigration[0]);
Flyway flyway = Flyway.configure()
.dataSource(dataSource) // No need to configure flyway JDBC URL by using the original DataSource
.defaultSchema(dataSource.getConnection().getSchema())
.locations("db/migration") // Default migration script path
.table(SCHEMA_HISTORY_TABLE) // Default migration history table is `flyway_schema_history`
.baselineOnMigrate(true) // Default false, must be set to true for initial migration on existing databases
.baselineVersion("0") // Default "1"
.executeInTransaction(true) // Performing migration as a transaction
.installedBy(applicationContext.getId()) // Using microservice name as migration executor
.javaMigrations(migrationBeans) // Registering migration classes
.load();
MigrateResult migrate = flyway.migrate(); // Executing migration by calling subclass implementations sequentially
}
}

View File

@ -1,10 +1,8 @@
package com.stktrk.app.db; package com.stktrk.app.db;
import com.arcadedb.gremlin.ArcadeGraphFactory;
import com.stktrk.app.configuration.GraphDbConfig; import com.stktrk.app.configuration.GraphDbConfig;
import org.springframework.beans.factory.annotation.Autowired; import jakarta.annotation.Nonnull;
import javax.annotation.PostConstruct;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
@ -13,8 +11,7 @@ import java.util.Properties;
public interface MigrationWrapper { public interface MigrationWrapper {
@PostConstruct default void executeQuery(@Nonnull GraphDbConfig graphDbConfig, @Nonnull String query) throws ClassNotFoundException, SQLException {
default void executeQuery(GraphDbConfig graphDbConfig, String query) throws ClassNotFoundException, SQLException {
Class.forName("org.postgresql.Driver"); Class.forName("org.postgresql.Driver");
Properties props = new Properties(); Properties props = new Properties();

View File

@ -2,17 +2,20 @@ package com.stktrk.app.db.migrations;
import com.stktrk.app.configuration.GraphDbConfig; import com.stktrk.app.configuration.GraphDbConfig;
import com.stktrk.app.db.MigrationWrapper; import com.stktrk.app.db.MigrationWrapper;
import lombok.Data;
import org.flywaydb.core.api.migration.BaseJavaMigration; import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context; import org.flywaydb.core.api.migration.Context;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.sql.*; @Component
import java.util.Properties; @Data
public class V1__CreateTest extends BaseJavaMigration implements MigrationWrapper { public class V1__CreateTest extends BaseJavaMigration implements MigrationWrapper {
@Autowired @Autowired
GraphDbConfig graphDbConfig; GraphDbConfig graphDbConfig;
public void migrate(Context context) throws Exception { public void migrate(Context context) throws Exception {
this.executeQuery(graphDbConfig,"CREATE VERTEX TYPE test"); this.executeQuery(graphDbConfig,"CREATE VERTEX TYPE test");
} }
} }

View File

@ -0,0 +1,21 @@
package com.stktrk.app.db.migrations;
import com.stktrk.app.configuration.GraphDbConfig;
import com.stktrk.app.db.MigrationWrapper;
import lombok.Data;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Data
public class V2__CreateUser extends BaseJavaMigration implements MigrationWrapper {
@Autowired
GraphDbConfig graphDbConfig;
public void migrate(Context context) throws Exception {
this.executeQuery(graphDbConfig,"CREATE VERTEX TYPE user");
}
}

View File

@ -0,0 +1,25 @@
spring.application.name=app
server.port=9090
springdoc.swagger-ui.path=/swagger-ui.html
#spring.flyway.user=flyway_user
#spring.flyway.password=7e7v55UcYGrY0e3UPYI0qtyMA4YJ1ZkTEaoyZ252GluFkiEMHVT9U5ULS7Rg2rGi
#spring.flyway.schemas=flyway_db
#spring.flyway.url=jdbc:postgresql://192.168.178.50:7654/flyway_db
#spring.flyway.locations=classpath:com/stktrk/app/db/migrations
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url= jdbc:postgresql://192.168.178.50:7654/flyway_db
spring.datasource.username=flyway_user
spring.datasource.password=7e7v55UcYGrY0e3UPYI0qtyMA4YJ1ZkTEaoyZ252GluFkiEMHVT9U5ULS7Rg2rGi
spring.datasource.defaultSchema=flyway_db
spring.datasource.hikari.schema=flyway_db
graph.user=root
graph.password=playwithdata
graph.connection=jdbc:postgresql://192.168.178.50:5432/graph
graph.http-port:2480
graph.postgres-port:5432
graph.host=192.168.178.50
graph.graph-name=graph

View File

@ -2,11 +2,20 @@ spring.application.name=app
server.port=9090 server.port=9090
springdoc.swagger-ui.path=/swagger-ui.html springdoc.swagger-ui.path=/swagger-ui.html
flyway.user=flyway_user #spring.flyway.user=flyway_user
flyway.password=7e7v55UcYGrY0e3UPYI0qtyMA4YJ1ZkTEaoyZ252GluFkiEMHVT9U5ULS7Rg2rGi #spring.flyway.password=7e7v55UcYGrY0e3UPYI0qtyMA4YJ1ZkTEaoyZ252GluFkiEMHVT9U5ULS7Rg2rGi
flyway.schemas=flyway_db #spring.flyway.schemas=flyway_db
#spring.flyway.url=jdbc:postgresql://192.168.178.50:7654/flyway_db
#spring.flyway.locations=classpath:com/stktrk/app/db/migrations
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url= jdbc:postgresql://192.168.178.50:7654/flyway_db
spring.datasource.username=flyway_user2
spring.datasource.password=7e7v55UcYGrY0e3UPYI0qtyMA4YJ1ZkTEaoyZ252GluFkiEMHVT9U5ULS7Rg2rGi
spring.datasource.defaultSchema=flyway_db
spring.datasource.hikari.schema=flyway_db
graph.user=root graph.user=root
graph.password=playwithdata graph.password=playwithdata
graph.connection=jdbc:postgresql://192.168.178.50:5432/graph graph.connection=jdbc:postgresql://192.168.178.50:5432/graph