Skip to content
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
13 changes: 11 additions & 2 deletions framework/core/src/Database/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Flarum\Database\Exception\MigrationKeyMissing;
use Flarum\Extension\Extension;
use Flarum\Install\DatabaseConfig;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Filesystem\Filesystem;
Expand Down Expand Up @@ -224,8 +225,11 @@ public function resolve(string $path, string $file): array
*
* @param string $path to the directory containing the dump.
*/
public function installFromSchema(string $path, string $driver): bool
public function installFromSchema(string $path, DatabaseConfig $dbConfig): bool
{
$driver = $dbConfig->toArray()['driver'];
$pgSearchPath = $dbConfig->toArray()['search_path'] ?? 'public';

$schemaPath = "$path/$driver-install.dump";

if (! file_exists($schemaPath)) {
Expand All @@ -252,11 +256,16 @@ public function installFromSchema(string $path, string $driver): bool
$this->connection->getTablePrefix() ?? '',
$statement
);

if ($driver === 'pgsql' && $pgSearchPath != 'public') {
$statement = preg_replace('/(public)([\s.;])/', "$pgSearchPath\$2", $statement);
}

$this->connection->statement($statement);
}

if ($driver === 'pgsql') {
$this->connection->statement('SELECT pg_catalog.set_config(\'search_path\', \'public\', false)');
$this->connection->statement("SELECT pg_catalog.set_config('search_path', '$pgSearchPath', false)");
}

$this->connection->getSchemaBuilder()->enableForeignKeyConstraints();
Expand Down
1 change: 1 addition & 0 deletions framework/core/src/Install/Console/FileDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ private function getDatabaseConfiguration(): DatabaseConfig
$this->databaseConfiguration['host'] ?? 'localhost',
$this->databaseConfiguration['port'] ?? 3306,
$this->databaseConfiguration['database'] ?? 'flarum',
$this->databaseConfiguration['search_path'] ?? 'public',
$this->databaseConfiguration['username'] ?? 'root',
$this->databaseConfiguration['password'] ?? '',
$this->databaseConfiguration['prefix'] ?? ''
Expand Down
15 changes: 13 additions & 2 deletions framework/core/src/Install/Console/UserDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,30 @@ private function getDatabaseConfiguration(): DatabaseConfig
if (Str::contains($host, ':')) {
[$host, $port] = explode(':', $host, 2);
}
}

$database = $this->ask('Database name (required):', required: true);

if ($driver === 'pgsql') {
$schema = $this->ask('Schema (Default: public):', 'public');
}

if (in_array($driver, ['mysql', 'mariadb', 'pgsql'])) {
$user = $this->ask('Database user (required):', required: true);
$password = $this->secret('Database password:');
}

$prefix = $this->ask('Prefix:');

return new DatabaseConfig(
$driver,
$host ?? null,
intval($port),
$this->ask('Database name (required):', required: true),
$database,
$schema ?? null,
$user ?? null,
$password ?? null,
$this->ask('Prefix:')
$prefix ?? null
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ private function makeDatabaseConfig(array $input): DatabaseConfig
$host,
intval($port),
Arr::get($input, 'dbName'),
Arr::get($input, 'dbSchema'),
Arr::get($input, 'dbUsername'),
Arr::get($input, 'dbPassword'),
Arr::get($input, 'tablePrefix')
Expand Down
7 changes: 6 additions & 1 deletion framework/core/src/Install/DatabaseConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __construct(
private readonly ?string $host,
private readonly int $port,
private string $database,
private readonly ?string $schema,
private readonly ?string $username,
#[\SensitiveParameter] private readonly ?string $password,
private readonly ?string $prefix
Expand Down Expand Up @@ -58,6 +59,10 @@ private function validate(): void
throw new ValidationFailed('Please specify the database name.');
}

if (empty($this->schema) && $this->driver == 'pgsql') {
throw new ValidationFailed('Please specify the schema name.');
}

if (empty($this->username) && in_array($this->driver, ['mysql', 'mariadb', 'pgsql'])) {
throw new ValidationFailed('Please specify the username for accessing the database.');
}
Expand Down Expand Up @@ -100,7 +105,7 @@ private function driverOptions(): array
'username' => $this->username,
'password' => $this->password,
'charset' => 'utf8',
'search_path' => 'public',
'search_path' => $this->schema,
'sslmode' => 'prefer',
],
'sqlite' => [
Expand Down
2 changes: 1 addition & 1 deletion framework/core/src/Install/Installation.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function ($connection) {
});

$pipeline->pipe(function () {
return new Steps\RunMigrations($this->db, $this->dbConfig->toArray()['driver'], $this->getMigrationPath());
return new Steps\RunMigrations($this->db, $this->dbConfig, $this->getMigrationPath());
});

$pipeline->pipe(function () {
Expand Down
5 changes: 3 additions & 2 deletions framework/core/src/Install/Steps/RunMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Flarum\Database\DatabaseMigrationRepository;
use Flarum\Database\Migrator;
use Flarum\Install\DatabaseConfig;
use Flarum\Install\Step;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Filesystem\Filesystem;
Expand All @@ -19,7 +20,7 @@
{
public function __construct(
private ConnectionInterface $database,
private string $driver,
private DatabaseConfig $dbConfig,
private string $path
) {
}
Expand All @@ -33,7 +34,7 @@ public function run(): void
{
$migrator = $this->getMigrator();

if (! $migrator->repositoryExists() && ! $migrator->installFromSchema($this->path, $this->driver)) {
if (! $migrator->repositoryExists() && ! $migrator->installFromSchema($this->path, $this->dbConfig)) {
$migrator->getRepository()->createRepository();
}

Expand Down
13 changes: 12 additions & 1 deletion framework/core/views/install/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
<input class="FormControl" name="dbName" value="flarum">
</div>

<div data-group="pgsql">
<div class="FormField">
<label>Schema</label>
<input class="FormControl" name="dbSchema" value="public">
</div>
</div>

<div data-group="mysql,mariadb,pgsql">
<div class="FormField">
<label>Host</label>
Expand Down Expand Up @@ -90,7 +97,9 @@
document.addEventListener('DOMContentLoaded', function() {
document.querySelector('form input').select();

document.querySelector('select[name="dbDriver"]').addEventListener('change', function() {
const dbDriver = document.querySelector('select[name="dbDriver"]');

dbDriver.addEventListener('change', function() {
document.querySelectorAll('[data-group]').forEach(function(group) {
group.style.display = 'none';
});
Expand All @@ -102,6 +111,8 @@
});
});

dbDriver.dispatchEvent(new Event('change', { bubbles: true }));

document.querySelector('form').addEventListener('submit', function(e) {
e.preventDefault();

Expand Down
5 changes: 5 additions & 0 deletions php-packages/testing/src/integration/Setup/SetupScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class SetupScript
protected string $host;
protected int $port;
protected string $name;
protected string $schema;
protected string $user;
protected string $pass;
protected string $pref;
Expand All @@ -46,6 +47,7 @@ public function __construct()
default => 0,
});
$this->name = getenv('DB_DATABASE') ?: 'flarum_test';
$this->schema = getenv('DB_SCHEMA') ?: 'public';
$this->user = getenv('DB_USERNAME') ?: 'root';
$this->pass = getenv('DB_PASSWORD') ?? 'root';
$this->pref = getenv('DB_PREFIX') ?: '';
Expand All @@ -57,6 +59,8 @@ public function run()

if ($this->driver === 'sqlite') {
echo "Connecting to SQLite database at $this->name.\n";
} elseif ($this->driver === 'pgsql') {
echo "Connecting to database $this->name, schema $this->schema at $this->host:$this->port.\n";
} else {
echo "Connecting to database $this->name at $this->host:$this->port.\n";
}
Expand All @@ -80,6 +84,7 @@ public function run()
$this->host,
$this->port,
$this->name,
$this->schema,
$this->user,
$this->pass,
$this->pref
Expand Down
Loading