-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to customize name of dataSource bean and databaseUrl (#523)
* Add ability to customize name of dataSource bean and databaseUrl * Fix tests * Add new demo app
- Loading branch information
Showing
23 changed files
with
499 additions
and
66 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
43 changes: 43 additions & 0 deletions
43
buildSrc/src/main/kotlin/pg-index-health.kotlin-application.gradle.kts
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,43 @@ | ||
import io.gitlab.arturbosch.detekt.Detekt | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
|
||
plugins { | ||
id("org.jetbrains.kotlin.jvm") | ||
id("org.jetbrains.kotlin.plugin.spring") | ||
id("pg-index-health.java-compilation") | ||
id("io.gitlab.arturbosch.detekt") | ||
id("pg-index-health.forbidden-apis") | ||
} | ||
|
||
private val versionCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs") | ||
|
||
dependencies { | ||
implementation("org.jetbrains.kotlin:kotlin-reflect") | ||
|
||
versionCatalog.findLibrary("detekt-formatting").ifPresent { | ||
detektPlugins(it) | ||
} | ||
versionCatalog.findLibrary("detekt-libraries").ifPresent { | ||
detektPlugins(it) | ||
} | ||
} | ||
|
||
tasks.withType<KotlinCompile> { | ||
kotlinOptions { | ||
freeCompilerArgs += "-Xjsr305=strict" | ||
jvmTarget = "11" | ||
} | ||
} | ||
|
||
detekt { | ||
toolVersion = versionCatalog.findVersion("detekt").get().requiredVersion | ||
config.setFrom(file("${rootDir}/config/detekt/detekt.yml")) | ||
buildUponDefaultConfig = true | ||
} | ||
|
||
tasks.withType<Detekt>().configureEach { | ||
reports { | ||
xml.required.set(true) | ||
html.required.set(true) | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
spring-boot-integration/kotlin-custom-ds-demo-app/build.gradle.kts
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,23 @@ | ||
plugins { | ||
id("pg-index-health.kotlin-application") | ||
alias(libs.plugins.spring.boot.gradlePlugin) | ||
alias(libs.plugins.spring.dependency.management) | ||
} | ||
|
||
ext["commons-lang3.version"] = libs.versions.commons.lang3.get() | ||
ext["assertj.version"] = libs.versions.assertj.get() | ||
// ext["mockito.version"] = libs.versions.mockito.get() | ||
ext["junit-jupiter.version"] = libs.versions.junit.get() | ||
|
||
dependencies { | ||
implementation(platform(libs.testcontainers.bom)) | ||
implementation("org.testcontainers:postgresql") | ||
implementation(libs.spring.boot.starter.data.jdbc) | ||
implementation("org.liquibase:liquibase-core:4.30.0") | ||
implementation("com.github.blagerweij:liquibase-sessionlock:1.6.9") | ||
|
||
runtimeOnly(libs.postgresql) | ||
|
||
testImplementation(libs.spring.boot.starter.test) | ||
testImplementation(project(":spring-boot-integration:pg-index-health-test-starter")) | ||
} |
21 changes: 21 additions & 0 deletions
21
...github/mfvanek/pg/spring/postgres/kt/custom/ds/PostgresCustomDataSourceDemoApplication.kt
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,21 @@ | ||
/* | ||
* Copyright (c) 2019-2024. Ivan Vakhrushev and others. | ||
* https://github.com/mfvanek/pg-index-health | ||
* | ||
* This file is a part of "pg-index-health" - a Java library for | ||
* analyzing and maintaining indexes health in PostgreSQL databases. | ||
* | ||
* Licensed under the Apache License 2.0 | ||
*/ | ||
|
||
package io.github.mfvanek.pg.spring.postgres.kt.custom.ds | ||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication | ||
import org.springframework.boot.runApplication | ||
|
||
@SpringBootApplication | ||
class PostgresCustomDataSourceDemoApplication | ||
|
||
fun main(args: Array<String>) { | ||
runApplication<PostgresCustomDataSourceDemoApplication>(*args) | ||
} |
34 changes: 34 additions & 0 deletions
34
...otlin/io/github/mfvanek/pg/spring/postgres/kt/custom/ds/config/DataSourceConfiguration.kt
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,34 @@ | ||
/* | ||
* Copyright (c) 2019-2024. Ivan Vakhrushev and others. | ||
* https://github.com/mfvanek/pg-index-health | ||
* | ||
* This file is a part of "pg-index-health" - a Java library for | ||
* analyzing and maintaining indexes health in PostgreSQL databases. | ||
* | ||
* Licensed under the Apache License 2.0 | ||
*/ | ||
|
||
package io.github.mfvanek.pg.spring.postgres.kt.custom.ds.config | ||
|
||
import com.zaxxer.hikari.HikariConfig | ||
import com.zaxxer.hikari.HikariDataSource | ||
import org.springframework.boot.autoconfigure.liquibase.LiquibaseDataSource | ||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Primary | ||
|
||
@Configuration | ||
class DataSourceConfiguration { | ||
|
||
@Configuration | ||
@ConfigurationProperties("pgih.custom.datasource") | ||
class CustomDataSourceProperties : HikariConfig() | ||
|
||
@Bean | ||
@Primary | ||
@LiquibaseDataSource | ||
fun pgihCustomDataSource(properties: CustomDataSourceProperties): HikariDataSource { | ||
return HikariDataSource(properties) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
spring-boot-integration/kotlin-custom-ds-demo-app/src/main/resources/application.yaml
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,15 @@ | ||
debug: false | ||
|
||
spring: | ||
main: | ||
banner-mode: off | ||
liquibase: | ||
change-log: classpath:/changelog/changelog.yaml | ||
default-schema: custom_ds_schema | ||
|
||
pgih: | ||
custom: | ||
datasource: | ||
jdbc-url: jdbc:tc:postgresql:17.2:///demo_for_pg_index_health_starter?TC_INITSCRIPT=init.sql | ||
driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver | ||
maximum-pool-size: 5 |
3 changes: 3 additions & 0 deletions
3
...ng-boot-integration/kotlin-custom-ds-demo-app/src/main/resources/changelog/changelog.yaml
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,3 @@ | ||
databaseChangeLog: | ||
- include: | ||
file: changelog/sql/warehouse.sql |
12 changes: 12 additions & 0 deletions
12
...boot-integration/kotlin-custom-ds-demo-app/src/main/resources/changelog/sql/warehouse.sql
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,12 @@ | ||
--liquibase formatted sql | ||
|
||
--changeset ivan.vakhrushev:2024.12.04:warehouse.table | ||
create table if not exists warehouse | ||
( | ||
id bigint primary key generated always as identity, | ||
name varchar(255) not null | ||
); | ||
|
||
comment on table warehouse is 'Information about the warehouses'; | ||
comment on column warehouse.id is 'Unique identifier of the warehouse'; | ||
comment on column warehouse.name is 'Human readable name of the warehouse'; |
1 change: 1 addition & 0 deletions
1
spring-boot-integration/kotlin-custom-ds-demo-app/src/main/resources/init.sql
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 @@ | ||
create schema if not exists custom_ds_schema; |
22 changes: 22 additions & 0 deletions
22
...vanek/pg/spring/postgres/kt/custom/ds/PostgresCustomDataSourceDemoApplicationKtRunTest.kt
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,22 @@ | ||
package io.github.mfvanek.pg.spring.postgres.kt.custom.ds | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.assertj.core.api.Assertions.assertThatCode | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.springframework.boot.test.system.CapturedOutput | ||
import org.springframework.boot.test.system.OutputCaptureExtension | ||
|
||
@ExtendWith(OutputCaptureExtension::class) | ||
internal class PostgresCustomDataSourceDemoApplicationKtRunTest { | ||
|
||
@Test | ||
fun applicationShouldRun(output: CapturedOutput) { | ||
assertThatCode { main(arrayOf("--spring.profiles.active=test")) } | ||
.doesNotThrowAnyException() | ||
assertThat(output.all) | ||
.contains("Starting PostgresCustomDataSourceDemoApplicationKt using Java") | ||
.contains("Container is started (JDBC URL: jdbc:postgresql://localhost:") | ||
.contains("Started PostgresCustomDataSourceDemoApplicationKt in") | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
.../mfvanek/pg/spring/postgres/kt/custom/ds/PostgresCustomDataSourceDemoApplicationKtTest.kt
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,60 @@ | ||
package io.github.mfvanek.pg.spring.postgres.kt.custom.ds | ||
|
||
import com.zaxxer.hikari.HikariDataSource | ||
import io.github.mfvanek.pg.connection.PgConnection | ||
import io.github.mfvanek.pg.core.checks.common.DatabaseCheckOnHost | ||
import io.github.mfvanek.pg.core.checks.common.Diagnostic | ||
import io.github.mfvanek.pg.model.context.PgContext | ||
import io.github.mfvanek.pg.model.dbobject.DbObject | ||
import io.github.mfvanek.pg.model.predicates.SkipLiquibaseTablesPredicate | ||
import io.github.mfvanek.pg.model.table.Table | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.context.ApplicationContext | ||
import org.springframework.test.context.ActiveProfiles | ||
|
||
@ActiveProfiles("test") | ||
@SpringBootTest | ||
internal class PostgresCustomDataSourceDemoApplicationKtTest { | ||
|
||
@Autowired | ||
private lateinit var applicationContext: ApplicationContext | ||
|
||
@Autowired | ||
private lateinit var checks: List<DatabaseCheckOnHost<out DbObject?>> | ||
|
||
@Test | ||
fun contextLoadsAndContainsPgIndexHealthBeans() { | ||
assertThat(applicationContext.getBean("pgihCustomDataSource")) | ||
.isInstanceOf(HikariDataSource::class.java) | ||
|
||
assertThat(applicationContext.getBean("pgConnection")) | ||
.isInstanceOf(PgConnection::class.java) | ||
} | ||
|
||
@Test | ||
fun checksShouldWork() { | ||
assertThat(checks) | ||
.hasSameSizeAs(Diagnostic.entries.toTypedArray()) | ||
|
||
checks | ||
.filter { it.isStatic } | ||
.forEach { check -> | ||
val ctx = PgContext.of("custom_ds_schema") | ||
// Due to the use of spring.liquibase.default-schema, all names are resolved without a schema | ||
val listAssert = assertThat(check.check(ctx, SkipLiquibaseTablesPredicate.ofPublic())) | ||
.`as`(check.diagnostic.name) | ||
|
||
when (check.diagnostic) { | ||
Diagnostic.TABLES_NOT_LINKED_TO_OTHERS -> | ||
listAssert | ||
.hasSize(1) | ||
.containsExactly(Table.of("warehouse", 0L)) | ||
|
||
else -> listAssert.isEmpty() | ||
} | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
spring-boot-integration/kotlin-custom-ds-demo-app/src/test/resources/application-test.yaml
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,3 @@ | ||
pg.index.health.test: | ||
datasource-bean-name: pgihCustomDataSource | ||
datasource-url-property-name: pgih.custom.datasource.jdbc-url |
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
Oops, something went wrong.