Skip to content

Commit

Permalink
Create Tests for MySql, Postgres, SqlServer & SQLite to assert the cr…
Browse files Browse the repository at this point in the history
…eating primary key in single query without altering table
  • Loading branch information
khalilst committed Jun 17, 2021
1 parent 55b6c77 commit aeac8cf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
18 changes: 18 additions & 0 deletions tests/Database/DatabaseMySqlSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ public function testBasicCreateTable()
$this->assertSame('alter table `users` add `id` int unsigned not null auto_increment primary key, add `email` varchar(255) not null', $statements[0]);
}

public function testBasicCreateWithPrimaryKey()
{
$blueprint = new Blueprint('users');
$blueprint->create();
$blueprint->string('foo')->primary();

$conn = $this->getConnection();
$conn->shouldReceive('getConfig')->once()->with('charset')->andReturn('utf8');
$conn->shouldReceive('getConfig')->once()->with('collation')->andReturn('utf8_unicode_ci');
$conn->shouldReceive('getConfig')->once()->with('engine')->andReturn(null);


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

$this->assertCount(1, $statements);
$this->assertSame("create table `users` (`foo` varchar(255) primary key not null) default character set utf8 collate 'utf8_unicode_ci'", $statements[0]);
}

public function testAutoIncrementStartingValue()
{
$blueprint = new Blueprint('users');
Expand Down
11 changes: 11 additions & 0 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ public function testBasicCreateTable()
$this->assertSame('alter table "users" add column "id" serial primary key not null, add column "email" varchar(255) not null', $statements[0]);
}

public function testBasicCreateWithPrimaryKey()
{
$blueprint = new Blueprint('users');
$blueprint->create();
$blueprint->string('foo')->primary();
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('create table "users" ("foo" varchar(255) primary key not null)', $statements[0]);
}

public function testCreateTableWithAutoIncrementStartingValue()
{
$blueprint = new Blueprint('users');
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseSQLiteSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function testAddingPrimaryKey()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('create table "users" ("foo" varchar not null, primary key ("foo"))', $statements[0]);
$this->assertSame('create table "users" ("foo" varchar primary key not null)', $statements[0]);
}

public function testAddingForeignKey()
Expand All @@ -209,7 +209,7 @@ public function testAddingForeignKey()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('create table "users" ("foo" varchar not null, "order_id" varchar not null, foreign key("order_id") references "orders"("id"), primary key ("foo"))', $statements[0]);
$this->assertSame('create table "users" ("foo" varchar primary key not null, "order_id" varchar not null, foreign key("order_id") references "orders"("id"))', $statements[0]);
}

public function testAddingUniqueKey()
Expand Down
12 changes: 12 additions & 0 deletions tests/Database/DatabaseSqlServerSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ public function testBasicCreateTable()
$this->assertSame('create table "prefix_users" ("id" int identity primary key not null, "email" nvarchar(255) not null)', $statements[0]);
}

public function testBasicCreateWithPrimaryKey()
{
$blueprint = new Blueprint('users');
$blueprint->create();
$blueprint->string('foo')->primary();
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('create table "users" ("foo" nvarchar(255) primary key not null)', $statements[0]);
}


public function testCreateTemporaryTable()
{
$blueprint = new Blueprint('users');
Expand Down

0 comments on commit aeac8cf

Please sign in to comment.