Skip to content

Commit

Permalink
fix adding constraints on sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
hafezdivandari committed Jan 17, 2024
1 parent 53e3246 commit a7ea909
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
12 changes: 12 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ public function compileChange(Blueprint $blueprint, Fluent $command, Connection
throw new LogicException('This database driver does not support modifying columns.');
}

/**
* Compile a primary key command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*/
public function compilePrimary(Blueprint $blueprint, Fluent $command)
{
throw new RuntimeException('This database driver does not support adding primary key.');
}

/**
* Compile a fulltext index key command.
*
Expand Down
12 changes: 9 additions & 3 deletions src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,18 @@ public function compileForeignKeys($table)
*/
public function compileCreate(Blueprint $blueprint, Fluent $command)
{
$primaryKey = tap($this->getCommandByName($blueprint, 'primary'), fn($pk) => $pk?->shouldBeSkipped());

$foreignKeys = tap($this->getCommandsByName($blueprint, 'foreign'),
fn ($commands) => collect($commands)->each->shouldBeSkipped()
);

return sprintf('%s table %s (%s%s%s)',
$blueprint->temporary ? 'create temporary' : 'create',
$this->wrapTable($blueprint),
implode(', ', $this->getColumns($blueprint)),
$this->addForeignKeys($this->getCommandsByName($blueprint, 'foreign')),
$this->addPrimaryKeys($this->getCommandByName($blueprint, 'primary'))
$this->addForeignKeys($foreignKeys),
$this->addPrimaryKeys($primaryKey)
);
}

Expand Down Expand Up @@ -419,7 +425,7 @@ public function compileSpatialIndex(Blueprint $blueprint, Fluent $command)
*/
public function compileForeign(Blueprint $blueprint, Fluent $command)
{
// Handled on table creation...
throw new RuntimeException('This database driver does not support adding foreign key constraints.');
}

/**
Expand Down
37 changes: 14 additions & 23 deletions tests/Database/DatabaseSQLiteSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,31 +306,31 @@ public function testAddingForeignID()
{
$blueprint = new Blueprint('users');
$foreignId = $blueprint->foreignId('foo');
$blueprint->foreignId('company_id')->constrained();
$blueprint->foreignId('laravel_idea_id')->constrained();
$blueprint->foreignId('team_id')->references('id')->on('teams');
$blueprint->foreignId('team_column_id')->constrained('teams');

$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertInstanceOf(ForeignIdColumnDefinition::class, $foreignId);
$this->assertSame([
'alter table "users" add column "foo" integer not null',
'alter table "users" add column "company_id" integer not null',
'alter table "users" add column "laravel_idea_id" integer not null',
'alter table "users" add column "team_id" integer not null',
'alter table "users" add column "team_column_id" integer not null',
], $statements);
}

public function testAddingForeignIdSpecifyingIndexNameInConstraint()
public function testAddingForeignKeyConstraint()
{
$this->expectException(\RuntimeException::class);

$blueprint = new Blueprint('users');
$blueprint->foreignId('company_id')->constrained(indexName: 'my_index');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
$this->assertSame([
'alter table "users" add column "company_id" integer not null',
], $statements);
$blueprint->foreignId('company_id')->constrained();
$blueprint->toSql($this->getConnection(), $this->getGrammar());
}

public function testAlterTableAddingPrimaryKey()
{
$this->expectException(\RuntimeException::class);

$blueprint = new Blueprint('users');
$blueprint->primary('id');
$blueprint->toSql($this->getConnection(), $this->getGrammar());
}

public function testAddingBigIncrementingID()
Expand Down Expand Up @@ -727,20 +727,11 @@ public function testAddingForeignUuid()
{
$blueprint = new Blueprint('users');
$foreignUuid = $blueprint->foreignUuid('foo');
$blueprint->foreignUuid('company_id')->constrained();
$blueprint->foreignUuid('laravel_idea_id')->constrained();
$blueprint->foreignUuid('team_id')->references('id')->on('teams');
$blueprint->foreignUuid('team_column_id')->constrained('teams');

$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertInstanceOf(ForeignIdColumnDefinition::class, $foreignUuid);
$this->assertSame([
'alter table "users" add column "foo" varchar not null',
'alter table "users" add column "company_id" varchar not null',
'alter table "users" add column "laravel_idea_id" varchar not null',
'alter table "users" add column "team_id" varchar not null',
'alter table "users" add column "team_column_id" varchar not null',
], $statements);
}

Expand Down

0 comments on commit a7ea909

Please sign in to comment.