1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-05-06 15:24:03 +00:00

Test how the Postgres DB manager handles charsets

If DB charset is configured to null, new databases will use the Postgres server's default charset (which is utf8 by default)

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
lukinovec 2026-04-24 12:02:13 +02:00
parent 7001e2d161
commit d41ac46340

View file

@ -539,6 +539,43 @@ test('partial tenant connection templates get merged into the central connection
expect($manager->connection()->getConfig('url'))->toBeNull();
});
test('newly created postgres databases use the correct charset', function (string|null $charset) {
config([
'tenancy.database.managers.pgsql' => PostgreSQLDatabaseManager::class,
'database.connections.pgsql.charset' => $charset, // If null, Postgres creates the DB with the server's default charset
]);
// Purge connection to make sure the updated charset config is used
DB::purge('pgsql');
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener());
$tenant = Tenant::create([
'tenancy_db_connection' => 'pgsql',
]);
$databaseName = $tenant->database()->getName();
// Postgres server's default charset (utf8)
$serverCharset = DB::connection('pgsql')
->selectOne("SELECT pg_encoding_to_char(encoding) AS encoding FROM pg_database WHERE datname = 'template1'")
->encoding;
// Charset of the newly created tenant database
$dbCharset = DB::connection('pgsql')
->selectOne('SELECT pg_encoding_to_char(encoding) AS encoding FROM pg_database WHERE datname = ?', [$databaseName])
->encoding;
$expectedCharset = $charset !== null ? strtoupper($charset) : $serverCharset;
expect($dbCharset)->toBe($expectedCharset);
})->with([
null,
'utf8',
]);
// Datasets
dataset('database_managers', [
['mysql', MySQLDatabaseManager::class],