Merge pull request 'dynamicDbValues' (#7) from dynamicDbValues into main
Reviewed-on: binarygolem/stktrk#7
This commit is contained in:
commit
c1940875e3
21
pom.xml
21
pom.xml
@ -174,6 +174,11 @@
|
||||
<artifactId>javafaker</artifactId>
|
||||
<version>1.0.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>42.7.4</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -190,16 +195,23 @@
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<!--
|
||||
<plugin>
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-maven-plugin</artifactId>
|
||||
<version>10.17.2</version>
|
||||
<configuration>
|
||||
<sqlMigrationSeparator>__</sqlMigrationSeparator>
|
||||
|
||||
|
||||
|
||||
<configFiles>${project.basedir}/src/main/resources/application.properties</configFiles>
|
||||
<locations>
|
||||
<location>classpath:com/stktrk/app/db/migrations</location>
|
||||
</locations>
|
||||
|
||||
<sqlMigrationSeparator>__</sqlMigrationSeparator>
|
||||
|
||||
|
||||
<url>jdbc:postgresql://192.168.178.50:7654/flyway_db</url>
|
||||
<user>flyway_user</user>
|
||||
<password>7e7v55UcYGrY0e3UPYI0qtyMA4YJ1ZkTEaoyZ252GluFkiEMHVT9U5ULS7Rg2rGi</password>
|
||||
@ -207,12 +219,13 @@
|
||||
<schema>flyway_db</schema>
|
||||
</schemas>
|
||||
|
||||
|
||||
</configuration>
|
||||
|
||||
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
|
||||
<!- https://mvnrepository.com/artifact/org.postgresql/postgresql
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
@ -223,6 +236,8 @@
|
||||
</dependencies>
|
||||
|
||||
</plugin>
|
||||
|
||||
-->
|
||||
</plugins>
|
||||
</build>
|
||||
<repositories>
|
||||
|
||||
@ -7,17 +7,19 @@ import com.stktrk.app.db.ConnectionPool;
|
||||
import org.apache.tinkerpop.gremlin.structure.Vertex;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
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.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
// 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
|
||||
public class AppApplication extends SpringBootServletInitializer {
|
||||
|
||||
@ -30,28 +32,26 @@ public class AppApplication extends SpringBootServletInitializer {
|
||||
|
||||
|
||||
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();
|
||||
|
||||
Faker faker = new Faker();
|
||||
g.traversal().addV("Profile").property("name", faker.name().firstName()).property("lastName", faker.name().lastName())
|
||||
.iterate();
|
||||
Vertex res = g.traversal().addV("Profile").property("name", faker.name().firstName()).property("lastName", faker.name().lastName())
|
||||
.next();
|
||||
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();
|
||||
|
||||
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");
|
||||
|
||||
|
||||
|
||||
x.forEach(v -> sj.add(v.id().toString()+ " - " +(String) v.property("name").value() + " " + (String) v.property("lastName").value() ));
|
||||
|
||||
return "Hello, " + name + "! There are " + x.size() + " vertices.<br/>\n" + sj;
|
||||
return "Hello, " + name + "! Added "+ res.property("name").value() + ". There are " + x.size() + " vertices.<br/>\n" + sj;
|
||||
}
|
||||
|
||||
@GetMapping("/delete")
|
||||
|
||||
48
src/main/java/com/stktrk/app/db/FlywayConfig.java
Normal file
48
src/main/java/com/stktrk/app/db/FlywayConfig.java
Normal 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
|
||||
}
|
||||
}
|
||||
@ -1,10 +1,8 @@
|
||||
package com.stktrk.app.db;
|
||||
|
||||
import com.arcadedb.gremlin.ArcadeGraphFactory;
|
||||
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.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
@ -13,8 +11,7 @@ import java.util.Properties;
|
||||
|
||||
public interface MigrationWrapper {
|
||||
|
||||
@PostConstruct
|
||||
default void executeQuery(GraphDbConfig graphDbConfig, String query) throws ClassNotFoundException, SQLException {
|
||||
default void executeQuery(@Nonnull GraphDbConfig graphDbConfig, @Nonnull String query) throws ClassNotFoundException, SQLException {
|
||||
|
||||
Class.forName("org.postgresql.Driver");
|
||||
Properties props = new Properties();
|
||||
|
||||
@ -2,17 +2,20 @@ 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;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Properties;
|
||||
|
||||
@Component
|
||||
@Data
|
||||
public class V1__CreateTest extends BaseJavaMigration implements MigrationWrapper {
|
||||
@Autowired
|
||||
GraphDbConfig graphDbConfig;
|
||||
|
||||
public void migrate(Context context) throws Exception {
|
||||
|
||||
this.executeQuery(graphDbConfig,"CREATE VERTEX TYPE test");
|
||||
}
|
||||
}
|
||||
|
||||
@ -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");
|
||||
}
|
||||
}
|
||||
25
src/main/resources/application-rasmus.properties
Normal file
25
src/main/resources/application-rasmus.properties
Normal 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
|
||||
@ -2,11 +2,20 @@ spring.application.name=app
|
||||
server.port=9090
|
||||
springdoc.swagger-ui.path=/swagger-ui.html
|
||||
|
||||
flyway.user=flyway_user
|
||||
flyway.password=7e7v55UcYGrY0e3UPYI0qtyMA4YJ1ZkTEaoyZ252GluFkiEMHVT9U5ULS7Rg2rGi
|
||||
flyway.schemas=flyway_db
|
||||
#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_user2
|
||||
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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user