Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: EXPOSED-258 Enhance upsert to allow exclusion of columns set on conflict #2006

Merged
merged 3 commits into from
Feb 26, 2024

Conversation

bog-walk
Copy link
Member

@bog-walk bog-walk commented Feb 22, 2024

Currently, if the onUpdate parameter of upsert() is specified, this argument will be used in the update clause of the upsert statement. If the argument is left null, a duplicate of the column-value assignments from the insert body block will be used instead.
So if a user wants to only update a subset of columns, or not update columns with default values for examples, the only option is to provide an argument to onUpdate that duplicates the insert block:

object TestTable : Table("tester") {
    val id = integer("id").uniqueIndex()
    val created = datetime("created").defaultExpression(CurrentDateTime)
    val updated = datetime("updated").defaultExpression(CurrentDateTime)
    val name = varchar("name", 32)
    val age = integer("age")
}

// for example, column "created" should never be updated
val updateValues = listOf(
    TestTable.updated to CurrentDateTime,
    TestTable.name to stringParam("B"),
    TestTable.age to intParam(100)
)
TestTable.upsert(onUpdate = updateValues) {
    it[id] = 1
    it[name] = "B"
    it[age] = 100
}

This can become verbose, so an additional parameter, onUpdateExclude, has been introduced, which duplicates all insert column-value pairs except those for the specified columns:

// the above example is reduced to this
TestTable.upsert(onUpdateExclude = listOf(TestTable.created)) {
    it[id] = 1
    it[name] = "B"
    it[age] = 100
}

This parameter could also be used in the reverse case when the number of columns to update is small but the user wants to avoid duplicating the insert values:

// for example, only column "updated" should be updated
// could provide a set of all other columns or
TestTable.upsert(onUpdateExclude = TestTable.columns - TestTable.updated) {
    it[id] = 1
    it[name] = "B"
    it[age] = 100
}

Next steps:

  • Update documentation
  • Update Wiki once released

… conflict

Currently, if the onUpdate parameter of upsert() is provided an argument, these
column-value pairs will be used in the update clause of the upsert statement.
If the argument is left null, a duplicate of the column-value assignments from
the insert block will be used instead.
If a user wants to only update a subset of columns, or not update columns with
default values, the only option is to provide an argument to onUpdate that
duplicates most of the insert block.

This can become verbose, so an additional parameter, onUpdateExclude, has been
introduced, which duplicates all insert column-value pairs except those for the
specifically excluded columns.
This parameter could also be used in the case when the number of columns to update
is small but the user wants to avoid duplicating the insert values.
… conflict

Edit documentation section to include new sample
@bog-walk bog-walk requested review from e5l and joc-a February 22, 2024 20:35
… conflict

- Change order of private batchInsert() vararg parameter
- Change onExcludeUpdate parameter to be a List instead of Set
@bog-walk bog-walk requested a review from e5l February 23, 2024 14:55
@bog-walk bog-walk merged commit b22a3bc into main Feb 26, 2024
5 checks passed
@bog-walk bog-walk deleted the bog-walk/add-upsert-exclude-update branch February 26, 2024 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants