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

Filter Bug #382

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"ext-pdo": "*",
"ext-mbstring": "*",
"php": ">=8.0",
"utopia-php/framework": "0.*.*",
"utopia-php/framework": "0.33.*",
"utopia-php/cache": "0.9.*",
"utopia-php/mongo": "0.3.*"
},
Expand Down
55 changes: 26 additions & 29 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 phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
stopOnFailure="true">
<testsuites>
<testsuite name="unit">
<directory>./tests/unit</directory>
Expand Down
62 changes: 25 additions & 37 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function createCollection(string $name, array $attributes = [], array $in
}

$sql = "
CREATE TABLE IF NOT EXISTS {$this->getSQLTable($id)} (
CREATE TABLE {$this->getSQLTable($id)} (
_id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
_uid VARCHAR(255) NOT NULL,
_createdAt DATETIME(3) DEFAULT NULL,
Expand Down Expand Up @@ -147,46 +147,34 @@ public function createCollection(string $name, array $attributes = [], array $in

$sql = $this->trigger(Database::EVENT_COLLECTION_CREATE, $sql);

try {
$this->getPDO()
->prepare($sql)
->execute();
$this->getPDO()->prepare($sql)->execute();

$sql = "
CREATE TABLE IF NOT EXISTS {$this->getSQLTable($id . '_perms')} (
_id int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
_type VARCHAR(12) NOT NULL,
_permission VARCHAR(255) NOT NULL,
_document VARCHAR(255) NOT NULL,
PRIMARY KEY (_id),
";

if ($this->shareTables) {
$sql .= "
_tenant INT(11) UNSIGNED DEFAULT NULL,
UNIQUE INDEX _index1 (_document, _tenant, _type, _permission),
INDEX _permission (_tenant, _permission, _type)
";
} else {
$sql .= "
UNIQUE INDEX _index1 (_document, _type, _permission),
INDEX _permission (_permission, _type)
";
}
$sql = "
CREATE TABLE {$this->getSQLTable($id . '_perms')} (
_id int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
_type VARCHAR(12) NOT NULL,
_permission VARCHAR(255) NOT NULL,
_document VARCHAR(255) NOT NULL,
PRIMARY KEY (_id),
";

$sql .= ")";
if ($this->shareTables) {
$sql .= "
_tenant INT(11) UNSIGNED DEFAULT NULL,
UNIQUE INDEX _index1 (_document, _tenant, _type, _permission),
INDEX _permission (_tenant, _permission, _type)
";
} else {
$sql .= "
UNIQUE INDEX _index1 (_document, _type, _permission),
INDEX _permission (_permission, _type)
";
}

$sql = $this->trigger(Database::EVENT_COLLECTION_CREATE, $sql);
$sql .= ")";

$this->getPDO()
->prepare($sql)
->execute();
} catch (\Exception $th) {
$this->getPDO()
->prepare("DROP TABLE IF EXISTS {$this->getSQLTable($id)}, {$this->getSQLTable($id . '_perms')};")
->execute();
throw $th;
}
$sql = $this->trigger(Database::EVENT_COLLECTION_CREATE, $sql);
$this->getPDO()->prepare($sql)->execute();

return true;
}
Expand Down
20 changes: 18 additions & 2 deletions src/Database/Adapter/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ public function create(string $name): bool
*/
public function exists(string $database, string $collection = null): bool
{
$database = $this->filter($database);

if (!\is_null($collection)) {
$collection = $this->filter($collection);
$collection = $this->getNamespace() . "_" . $collection;
$list = $this->flattenArray($this->listCollections())[0]->firstBatch;
foreach ($list as $obj) {
Expand All @@ -109,7 +112,19 @@ public function exists(string $database, string $collection = null): bool
return false;
}

return $this->getClient()->selectDatabase() != null;
$list = $this->getClient()->listDatabaseNames();

if($list->ok !== 1.0) {
return false;
}

foreach ($list->databases as $db) {
if($db->name === $database) {
return true;
}
}

return false;
}

/**
Expand Down Expand Up @@ -1546,7 +1561,8 @@ public function getLimitForIndexes(): int
*/
public function getSupportForSchemas(): bool
{
return true;
// todo: Mongo does not support get schema
return false;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function createCollection(string $name, array $attributes = [], array $in
}

$sql = "
CREATE TABLE IF NOT EXISTS {$this->getSQLTable($id)} (
CREATE TABLE {$this->getSQLTable($id)} (
_id SERIAL NOT NULL,
_uid VARCHAR(255) NOT NULL,
_tenant INTEGER DEFAULT NULL,
Expand Down Expand Up @@ -135,7 +135,7 @@ public function createCollection(string $name, array $attributes = [], array $in
$stmt->execute();

$sql = "
CREATE TABLE IF NOT EXISTS {$this->getSQLTable($id . '_perms')} (
CREATE TABLE {$this->getSQLTable($id . '_perms')} (
_id SERIAL NOT NULL,
_tenant INTEGER DEFAULT NULL,
_type VARCHAR(12) NOT NULL,
Expand Down
4 changes: 2 additions & 2 deletions src/Database/Adapter/SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function createCollection(string $name, array $attributes = [], array $in
}

$sql = "
CREATE TABLE IF NOT EXISTS `{$this->getSQLTable($id)}` (
CREATE TABLE `{$this->getSQLTable($id)}` (
`_id` INTEGER PRIMARY KEY AUTOINCREMENT,
`_uid` VARCHAR(36) NOT NULL,
`_tenant` INTEGER DEFAULT NULL,
Expand Down Expand Up @@ -166,7 +166,7 @@ public function createCollection(string $name, array $attributes = [], array $in
}

$sql = "
CREATE TABLE IF NOT EXISTS `{$this->getSQLTable($id)}_perms` (
CREATE TABLE `{$this->getSQLTable($id)}_perms` (
`_id` INTEGER PRIMARY KEY AUTOINCREMENT,
`_tenant` INTEGER DEFAULT NULL,
`_type` VARCHAR(12) NOT NULL,
Expand Down
Loading
Loading