1
0
mirror of https://github.com/flarum/core.git synced 2025-08-06 08:27:42 +02:00

feat: add support for SQLite (#3984)

* feat: add support for sqlite

* chore: add warning on install

* fix: ignore constraints before transaction begins

* chore: update workflow

* Apply fixes from StyleCI

* chore: generate sqlite dump and manually add foreign keys

* chore: fix actions

* chore: fix actions

* chore: fix actions

* chore: fix actions

* chore: fix actions

* chore: fix actions

* test: fix

* Apply fixes from StyleCI

* fix: sqlite with db prefix

* Apply fixes from StyleCI

* fix: statistics sqlite
This commit is contained in:
Sami Mazouz
2024-06-21 07:25:11 +01:00
committed by GitHub
parent 5ce1aeab47
commit eb6e599df1
61 changed files with 801 additions and 263 deletions

View File

@@ -12,6 +12,7 @@ namespace Flarum\Testing\integration\Extend;
use Flarum\Extend\ExtenderInterface;
use Flarum\Extension\Extension;
use Illuminate\Contracts\Container\Container;
use Illuminate\Database\Connection;
use Illuminate\Database\ConnectionInterface;
class BeginTransactionAndSetDatabase implements ExtenderInterface
@@ -28,8 +29,14 @@ class BeginTransactionAndSetDatabase implements ExtenderInterface
public function extend(Container $container, Extension $extension = null): void
{
/** @var Connection $db */
$db = $container->make(ConnectionInterface::class);
// SQLite requires this be done outside a transaction.
if ($db->getDriverName() === 'sqlite') {
$db->getSchemaBuilder()->disableForeignKeyConstraints();
}
$db->beginTransaction();
($this->setDbOnTestCase)($db);

View File

@@ -21,61 +21,24 @@ class SetupScript
{
use UsesTmpDir;
/**
* Test database host.
*
* @var string
*/
protected $host;
protected string $driver;
protected string $host;
protected int $port;
protected string $name;
protected string $user;
protected string $pass;
protected string $pref;
/**
* Test database port.
*
* @var int
*/
protected $port;
/**
* Test database name.
*
* @var string
*/
protected $name;
/**
* Test database username.
*
* @var string
*/
protected $user;
/**
* Test database password.
*
* @var string
*/
protected $pass;
/**
* Test database prefix.
*
* @var string
*/
protected $pref;
/**
* @var DatabaseConfig
*/
protected $dbConfig;
protected DatabaseConfig $dbConfig;
/**
* Settings to be applied during installation.
* @var array
*/
protected $settings = ['mail_driver' => 'log'];
protected array $settings = ['mail_driver' => 'log'];
public function __construct()
{
$this->driver = getenv('DB_DRIVER') ?: 'mysql';
$this->host = getenv('DB_HOST') ?: 'localhost';
$this->port = intval(getenv('DB_PORT') ?: 3306);
$this->name = getenv('DB_DATABASE') ?: 'flarum_test';
@@ -88,7 +51,12 @@ class SetupScript
{
$tmp = $this->tmpDir();
echo "Connecting to database $this->name at $this->host:$this->port.\n";
if ($this->driver === 'sqlite') {
echo "Connecting to SQLite database at $this->name.\n";
} else {
echo "Connecting to database $this->name at $this->host:$this->port.\n";
}
echo "Warning: all tables will be dropped to ensure clean state. DO NOT use your production database!\n";
echo "Logging in as $this->user with password '$this->pass'.\n";
echo "Table prefix: '$this->pref'\n";
@@ -103,22 +71,31 @@ class SetupScript
echo "\nOff we go...\n";
$this->dbConfig = new DatabaseConfig('mysql', $this->host, $this->port, $this->name, $this->user, $this->pass, $this->pref);
$this->dbConfig = new DatabaseConfig(
$this->driver,
$this->host,
$this->port,
$this->name,
$this->user,
$this->pass,
$this->pref
);
echo "\nWiping DB to ensure clean state\n";
$this->wipeDb();
echo "Success! Proceeding to installation...\n";
$paths = new Paths([
'base' => $tmp,
'public' => "$tmp/public",
'storage' => "$tmp/storage",
'vendor' => getenv('FLARUM_TEST_VENDOR_PATH') ?: getcwd().'/vendor',
]);
$this->setupTmpDir();
$this->dbConfig->prepare($paths);
$installation = new Installation(
new Paths([
'base' => $tmp,
'public' => "$tmp/public",
'storage' => "$tmp/storage",
'vendor' => getenv('FLARUM_TEST_VENDOR_PATH') ?: getcwd().'/vendor',
])
);
echo "\nWiping DB to ensure clean state\n";
$this->wipeDb($paths);
echo "Success! Proceeding to installation...\n";
$installation = new Installation($paths);
$pipeline = $installation
->configPath('config.php')
@@ -140,7 +117,7 @@ class SetupScript
echo "Installation complete\n";
}
protected function wipeDb()
protected function wipeDb(Paths $paths)
{
// Reuse the connection step to include version checks
(new ConnectToDatabase($this->dbConfig, function ($db) {
@@ -149,7 +126,7 @@ class SetupScript
$builder->dropAllTables();
$builder->dropAllViews();
}))->run();
}, $paths->base))->run();
}
/**

View File

@@ -193,7 +193,12 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
protected function populateDatabase(): void
{
// We temporarily disable foreign key checks to simplify this process.
/**
* We temporarily disable foreign key checks to simplify this process.
* SQLite ignores this statement since we are inside a transaction.
* So we do that before starting a transaction.
* @see BeginTransactionAndSetDatabase
*/
$this->database()->getSchemaBuilder()->disableForeignKeyConstraints();
$databaseContent = [];