Skip to content

Commit

Permalink
fix(database): ensure dropAllTables and dropAllTriggers methods use d…
Browse files Browse the repository at this point in the history
…b property in LibSQLSchemaBuilder
  • Loading branch information
darkterminal committed Jun 22, 2024
1 parent 66c13c9 commit bee6685
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions src/Database/LibSQLSchemaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

namespace Turso\Driver\Laravel\Database;

use Darkterminal\LibSQL\Exceptions\FeatureNotSupportedException;
use Turso\Driver\Laravel\Exceptions\FeatureNotSupportedException;
use Illuminate\Database\Schema\SQLiteBuilder;

class LibSQLSchemaBuilder extends SQLiteBuilder
{
public function __construct(protected LibSQLDatabase $db, \Illuminate\Database\Connection $connection)
{
parent::__construct($connection);
}

public function createDatabase($name)
{
throw new FeatureNotSupportedException('Creating database is not supported in LibSQL database.');
Expand All @@ -21,10 +26,11 @@ public function dropDatabaseIfExists($name)

protected function dropAllIndexes(): void
{
$statement = $this->connection->getPdo()->query($this->grammar()->compileDropAllIndexes());
$statement = $this->db->prepare($this->grammar()->compileDropAllIndexes());
$statement->execute();

collect($statement['rows'])->each(function (array $query) {
$this->connection->select($query[0]);
collect($statement->fetchAll(\PDO::FETCH_NUM))->each(function (array $query) {
$this->db->query($query[0]);
});
}

Expand All @@ -33,41 +39,41 @@ public function dropAllTables(): void
$this->dropAllTriggers();
$this->dropAllIndexes();

$this->connection->select($this->grammar()->compileDisableForeignKeyConstraints());
$this->db->query($this->grammar()->compileDisableForeignKeyConstraints());

$statement = $this->connection->getPdo()->query($this->grammar()->compileDropAllTables());
$statement = $this->db->prepare($this->grammar()->compileDropAllTables());
$statement->execute();

collect($statement['rows'])->each(function (array $query) {
$this->connection->select($query[0]);
collect($statement->fetchAll(\PDO::FETCH_NUM))->each(function (array $query) {
$this->db->query($query[0]);
});

$this->connection->select($this->grammar()->compileEnableForeignKeyConstraints());
$this->db->query($this->grammar()->compileEnableForeignKeyConstraints());
}

protected function dropAllTriggers(): void
{
$statement = $this->connection->getPdo()->query($this->grammar()->compileDropAllTriggers());
$statement = $this->db->prepare($this->grammar()->compileDropAllTriggers());
$statement->execute();

collect($statement['rows'])->each(function (array $query) {
$this->connection->select($query[0]);
collect($statement->fetchAll(\PDO::FETCH_NUM))->each(function (array $query) {
$this->db->query($query[0]);
});
}

public function dropAllViews(): void
{
$statement = $this->connection->getPdo()->prepare($this->grammar()->compileDropAllViews());
$statement = $this->db->prepare($this->grammar()->compileDropAllViews());
$statement->execute();

collect($statement['rows'])->each(function (array $query) {
$this->connection->select($query[0]);
collect($statement->fetchAll(\PDO::FETCH_NUM))->each(function (array $query) {
$this->db->query($query[0]);
});
}

protected function grammar(): LibSQLSchemaGrammar
{
if (! ($this->grammar instanceof LibSQLSchemaGrammar)) {
$this->grammar = new LibSQLSchemaGrammar();
}

return $this->grammar;
$grammar = new LibSQLSchemaGrammar();
return $grammar;
}
}

0 comments on commit bee6685

Please sign in to comment.