Skip to content

Commit

Permalink
Merge pull request #448 from utopia-php/fix-add-escapes-for-rename
Browse files Browse the repository at this point in the history
Fix non-quoted newKey in MariaDB and add newKey filtering for postgres
  • Loading branch information
abnegate authored Sep 10, 2024
2 parents 1e97fc8 + 20a0467 commit c32d6ea
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
38 changes: 19 additions & 19 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public function updateAttribute(string $collection, string $id, string $type, in
$type = $this->getSQLType($type, $size, $signed, $array);

if (!empty($newKey)) {
$sql = "ALTER TABLE {$this->getSQLTable($name)} CHANGE COLUMN `{$id}` {$newKey} {$type};";
$sql = "ALTER TABLE {$this->getSQLTable($name)} CHANGE COLUMN `{$id}` `{$newKey}` {$type};";
} else {
$sql = "ALTER TABLE {$this->getSQLTable($name)} MODIFY `{$id}` {$type};";
}
Expand Down
1 change: 1 addition & 0 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ public function updateAttribute(string $collection, string $id, string $type, in
{
$name = $this->filter($collection);
$id = $this->filter($id);
$newKey = empty($newKey) ? null : $this->filter($newKey);
$type = $this->getSQLType($type, $size, $signed, $array);

if ($type == 'TIMESTAMP(3)') {
Expand Down
12 changes: 12 additions & 0 deletions tests/e2e/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -5906,6 +5906,18 @@ public function testUpdateAttributeRename(): void
} catch (\Exception $e) {
$this->assertInstanceOf(StructureException::class, $e);
}

// Check new key filtering
static::getDatabase()->updateAttribute(
collection: 'rename_test',
id: 'renamed',
newKey: 'renamed-test',
);

$doc = static::getDatabase()->getDocument('rename_test', $doc->getId());

$this->assertEquals('string', $doc->getAttribute('renamed-test'));
$this->assertArrayNotHasKey('renamed', $doc->getAttributes());
}

public function createRandomString(int $length = 10): string
Expand Down

0 comments on commit c32d6ea

Please sign in to comment.