Skip to content

Commit

Permalink
fix(sql): Ensure that the columns are kept in sync with body
Browse files Browse the repository at this point in the history
This covers the `application`, `name`, etc. normalized columns that are
also represented within the json `body` blob.
  • Loading branch information
ajordens committed Jul 8, 2019
1 parent c86ff70 commit 0acbd56
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
5 changes: 1 addition & 4 deletions front50-sql/front50-sql.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ apply from: "$rootDir/gradle/kotlin.gradle"
dependencies {
implementation project(":front50-sql-mariadb")

implementation "com.netflix.spinnaker.front50:front50-core:${front50Version}"
implementation "com.netflix.spinnaker.front50:front50-core"

implementation "com.netflix.spinnaker.kork:kork-sql"
implementation "com.netflix.spinnaker.kork:kork-exceptions"

// implementation "org.jooq:jooq:3.11.11"
implementation "io.strikt:strikt-core"
implementation "com.netflix.hystrix:hystrix-core"
implementation "io.github.resilience4j:resilience4j-retry"

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core"

// runtimeOnly "mysql:mysql-connector-java"

testImplementation "io.mockk:mockk"
testImplementation "mysql:mysql-connector-java"
testImplementation "dev.minutest:minutest"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class SqlStorageService(
.set(field("last_modified_at"), MySQLDSL.values(field("last_modified_at")) as Any)
.set(field("last_modified_by"), MySQLDSL.values(field("last_modified_by")) as Any)
.set(field("is_deleted"), MySQLDSL.values(field("is_deleted")) as Any)
.apply(definitionsByType[objectType]!!.onDuplicateKeyUpdate())
}

insert.execute()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import com.netflix.spinnaker.front50.model.delivery.Delivery
import com.netflix.spinnaker.front50.model.pipeline.Pipeline
import com.netflix.spinnaker.front50.model.project.Project
import org.jooq.Field
import org.jooq.InsertOnDuplicateSetMoreStep
import org.jooq.Record
import org.jooq.impl.DSL
import org.jooq.util.mysql.MySQLDSL
import java.nio.charset.StandardCharsets.UTF_8
import java.time.Clock

Expand Down Expand Up @@ -76,6 +79,10 @@ open class DefaultTableDefinition(
objectKey, objectAsString, signature, item.lastModified, clock.millis()
)
}

open fun onDuplicateKeyUpdate(): InsertOnDuplicateSetMoreStep<Record>.() -> Unit {
return {}
}
}

class DeliveryTableDefinition : DefaultTableDefinition(ObjectType.DELIVERY, "deliveries", true) {
Expand Down Expand Up @@ -103,6 +110,12 @@ class DeliveryTableDefinition : DefaultTableDefinition(ObjectType.DELIVERY, "del
false
)
}

override fun onDuplicateKeyUpdate(): InsertOnDuplicateSetMoreStep<Record>.() -> Unit {
return {
set(DSL.field("application"), MySQLDSL.values(DSL.field("application")) as Any)
}
}
}

class PipelineTableDefinition : DefaultTableDefinition(ObjectType.PIPELINE, "pipelines", true) {
Expand Down Expand Up @@ -132,6 +145,13 @@ class PipelineTableDefinition : DefaultTableDefinition(ObjectType.PIPELINE, "pip
false
)
}

override fun onDuplicateKeyUpdate(): InsertOnDuplicateSetMoreStep<Record>.() -> Unit {
return {
set(DSL.field("name"), MySQLDSL.values(DSL.field("name")) as Any)
set(DSL.field("application"), MySQLDSL.values(DSL.field("application")) as Any)
}
}
}

class PipelineStrategyTableDefinition : DefaultTableDefinition(ObjectType.STRATEGY, "pipeline_strategies", true) {
Expand Down Expand Up @@ -161,6 +181,13 @@ class PipelineStrategyTableDefinition : DefaultTableDefinition(ObjectType.STRATE
false
)
}

override fun onDuplicateKeyUpdate(): InsertOnDuplicateSetMoreStep<Record>.() -> Unit {
return {
set(DSL.field("name"), MySQLDSL.values(DSL.field("name")) as Any)
set(DSL.field("application"), MySQLDSL.values(DSL.field("application")) as Any)
}
}
}

class ProjectTableDefinition : DefaultTableDefinition(ObjectType.PROJECT, "projects", true) {
Expand Down Expand Up @@ -188,4 +215,10 @@ class ProjectTableDefinition : DefaultTableDefinition(ObjectType.PROJECT, "proje
false
)
}

override fun onDuplicateKeyUpdate(): InsertOnDuplicateSetMoreStep<Record>.() -> Unit {
return {
set(DSL.field("name"), MySQLDSL.values(DSL.field("name")) as Any)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.netflix.spinnaker.kork.sql.config.SqlRetryProperties
import dev.minutest.junit.JUnit5Minutests
import dev.minutest.rootContext
import org.jooq.SQLDialect
import org.jooq.impl.DSL
import org.junit.jupiter.api.AfterAll
import strikt.api.expectThat
import strikt.api.expectThrows
Expand Down Expand Up @@ -137,6 +138,16 @@ internal object SqlStorageServiceTests : JUnit5Minutests {
pipeline = sqlStorageService.loadObject(ObjectType.PIPELINE, "id-pipeline001")
expectThat(pipeline.name).isEqualTo("pipeline001_updated")

expectThat(
jooq
.select(DSL.field("id", String::class.java))
.from("pipelines")
.where(
DSL.field("name", String::class.java).eq("pipeline001_updated")
)
.fetchOne(DSL.field("id", String::class.java))
).isEqualTo("id-pipeline001")

// delete the pipeline
sqlStorageService.deleteObject(ObjectType.PIPELINE, "id-pipeline001")

Expand Down

0 comments on commit 0acbd56

Please sign in to comment.