1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-04 11:44:03 +00:00

Revert "fix: Refactor database management methods to use parameterized queries and add identifier quoting"

This reverts commit 712d7aabec.
This commit is contained in:
Samuel Štancl 2025-11-07 20:41:06 +01:00
parent 9f0328f9ef
commit ffd3678e64
No known key found for this signature in database
GPG key ID: BA146259A1E16C57
3 changed files with 9 additions and 41 deletions

View file

@ -17,28 +17,18 @@ trait ManagesRLSPolicies
{ {
return array_map( return array_map(
fn (stdClass $policy) => $policy->policyname, fn (stdClass $policy) => $policy->policyname,
DB::select( DB::select("SELECT policyname FROM pg_policies WHERE tablename = '{$table}' AND policyname LIKE '%_rls_policy%'")
"SELECT policyname FROM pg_policies WHERE tablename = ? AND policyname LIKE ?",
[$table, '%_rls_policy%']
)
); );
} }
public static function dropRLSPolicies(string $table): int public static function dropRLSPolicies(string $table): int
{ {
$policies = static::getRLSPolicies($table); $policies = static::getRLSPolicies($table);
$quotedTable = static::quoteIdentifier($table);
foreach ($policies as $policy) { foreach ($policies as $policy) {
$quotedPolicy = static::quoteIdentifier($policy); DB::statement('DROP POLICY ? ON ?', [$policy, $table]);
DB::statement("DROP POLICY {$quotedPolicy} ON {$quotedTable}");
} }
return count($policies); return count($policies);
} }
protected static function quoteIdentifier(string $identifier): string
{
return '"' . str_replace('"', '""', $identifier) . '"';
}
} }

View file

@ -10,30 +10,20 @@ class MySQLDatabaseManager extends TenantDatabaseManager
{ {
public function createDatabase(TenantWithDatabase $tenant): bool public function createDatabase(TenantWithDatabase $tenant): bool
{ {
$database = $this->quoteIdentifier($tenant->database()->getName()); $database = $tenant->database()->getName();
$charset = $this->connection()->getConfig('charset'); $charset = $this->connection()->getConfig('charset');
$collation = $this->connection()->getConfig('collation'); $collation = $this->connection()->getConfig('collation');
return $this->connection()->statement("CREATE DATABASE {$database} CHARACTER SET `$charset` COLLATE `$collation`"); return $this->connection()->statement("CREATE DATABASE `{$database}` CHARACTER SET `$charset` COLLATE `$collation`");
} }
public function deleteDatabase(TenantWithDatabase $tenant): bool public function deleteDatabase(TenantWithDatabase $tenant): bool
{ {
$database = $this->quoteIdentifier($tenant->database()->getName()); return $this->connection()->statement("DROP DATABASE `{$tenant->database()->getName()}`");
return $this->connection()->statement("DROP DATABASE {$database}");
} }
public function databaseExists(string $name): bool public function databaseExists(string $name): bool
{ {
return (bool) $this->connection()->selectOne( return (bool) $this->connection()->select("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$name'");
'SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ? LIMIT 1',
[$name]
);
}
protected function quoteIdentifier(string $identifier): string
{
return '`' . str_replace('`', '``', $identifier) . '`';
} }
} }

View file

@ -10,28 +10,16 @@ class PostgreSQLDatabaseManager extends TenantDatabaseManager
{ {
public function createDatabase(TenantWithDatabase $tenant): bool public function createDatabase(TenantWithDatabase $tenant): bool
{ {
$database = $this->quoteIdentifier($tenant->database()->getName()); return $this->connection()->statement("CREATE DATABASE \"{$tenant->database()->getName()}\" WITH TEMPLATE=template0");
return $this->connection()->statement("CREATE DATABASE {$database} WITH TEMPLATE=template0");
} }
public function deleteDatabase(TenantWithDatabase $tenant): bool public function deleteDatabase(TenantWithDatabase $tenant): bool
{ {
$database = $this->quoteIdentifier($tenant->database()->getName()); return $this->connection()->statement("DROP DATABASE \"{$tenant->database()->getName()}\"");
return $this->connection()->statement("DROP DATABASE {$database}");
} }
public function databaseExists(string $name): bool public function databaseExists(string $name): bool
{ {
return (bool) $this->connection()->selectOne( return (bool) $this->connection()->selectOne("SELECT datname FROM pg_database WHERE datname = '$name'");
'SELECT datname FROM pg_database WHERE datname = ? LIMIT 1',
[$name]
);
}
protected function quoteIdentifier(string $identifier): string
{
return '"' . str_replace('"', '""', $identifier) . '"';
} }
} }