Skip to content

Commit

Permalink
feat(pgsql): handle complex ON CONFLICT target expressions (#7)
Browse files Browse the repository at this point in the history
* feat(pgsql): handle complex ON CONFLICT target expressions

* Update ImportConfiguration.php

Co-authored-by: Thibaut Selingue <[email protected]>

---------

Co-authored-by: Thibaut Selingue <[email protected]>
  • Loading branch information
lucasmirloup and thislg authored May 21, 2024
1 parent 2de3da8 commit 4cebf33
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
15 changes: 10 additions & 5 deletions ImportConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,15 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode('copy_condition')->defaultValue('')->end()
->booleanNode('distinct')->defaultFalse()->end()
->scalarNode('joins')->defaultValue('')->end()
->scalarNode('conflict_target')->defaultValue('')->end()
->arrayNode('conflict_target')
->beforeNormalization()
->ifString()
->then(function ($v) { return ['sql' => '('.$v.')']; })
->end()
->children()
->scalarNode('sql')->defaultValue('')->end()
->end()
->end()
->variableNode('non_updateable_fields')
->defaultValue([])
->end()
Expand All @@ -237,9 +245,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->prototype('array')
->beforeNormalization()
->ifString()
->then(function ($v) {
return ['property' => $v];
})
->then(function ($v) { return ['property' => $v]; })
->end()
->children()
->scalarNode('sql')->defaultNull()->end()
Expand All @@ -255,7 +261,6 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->end()

->end()
->end()
->end()
Expand Down
2 changes: 1 addition & 1 deletion ImportResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function getJoins(): ?string

public function getConflictTarget(): ?string
{
return $this->config['copy']['strategy_options']['conflict_target'] ?: null;
return $this->config['copy']['strategy_options']['conflict_target']['sql'] ?: null;
}

public function isDistinct(): bool
Expand Down
2 changes: 1 addition & 1 deletion Strategy/InsertIgnoreStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private function postgresqlCopy(ImportResource $resource): int

$sql = "INSERT INTO $tablename ($columns)
SELECT $tempColumns FROM $tempTablename temp $joins $whereClause
ON CONFLICT ($conflictTargetClause) DO NOTHING"
ON CONFLICT $conflictTargetClause DO NOTHING"
;

$this->connection->beginTransaction();
Expand Down
2 changes: 1 addition & 1 deletion Strategy/InsertOrUpdateStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private function postgresqlCopy(ImportResource $resource): int

$sql = "INSERT INTO $tablename ($columns)
SELECT $distinct $tempColumns FROM $tempTablename temp $joins $whereClause
ON CONFLICT ($conflictTargetClause) DO UPDATE SET $updateClause"
ON CONFLICT $conflictTargetClause DO UPDATE SET $updateClause"
;

$this->connection->beginTransaction();
Expand Down

0 comments on commit 4cebf33

Please sign in to comment.