Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.8] Default to big increments #26454

Merged
merged 3 commits into from Nov 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Illuminate/Database/Schema/Blueprint.php
Expand Up @@ -526,14 +526,14 @@ public function foreign($columns, $name = null)
}

/**
* Create a new auto-incrementing integer (4-byte) column on the table.
* Create a new auto-incrementing integer column on the table.
*
* @param string $column
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function increments($column)
{
return $this->unsignedInteger($column, true);
return $this->{Builder::$defaultIncrementsType}($column, true);
}

/**
Expand Down Expand Up @@ -569,6 +569,17 @@ public function mediumIncrements($column)
return $this->unsignedMediumInteger($column, true);
}

/**
* Create a new auto-incrementing integer (4-byte) column on the table.
*
* @param string $column
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function integerIncrements($column)
{
return $this->unsignedInteger($column, true);
}

/**
* Create a new auto-incrementing big integer (8-byte) column on the table.
*
Expand Down
17 changes: 17 additions & 0 deletions src/Illuminate/Database/Schema/Builder.php
Expand Up @@ -36,6 +36,13 @@ class Builder
*/
public static $defaultStringLength = 255;

/**
* The default increments type for migrations.
*
* @var string
*/
public static $defaultIncrementsType = 'unsignedBigInteger';

/**
* Create a new database Schema manager.
*
Expand All @@ -59,6 +66,16 @@ public static function defaultStringLength($length)
static::$defaultStringLength = $length;
}

/**
* Set the default increments type to a 4 byte integer.
*
* @return void
*/
public static function useIntegerIncrements()
{
static::$defaultIncrementsType = 'unsignedInteger';
}

/**
* Determine if the given table exists.
*
Expand Down
37 changes: 29 additions & 8 deletions tests/Database/DatabaseMySqlSchemaGrammarTest.php
Expand Up @@ -5,6 +5,7 @@
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Illuminate\Database\Connection;
use Illuminate\Database\Schema\Builder;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Grammars\MySqlGrammar;
Expand All @@ -31,7 +32,7 @@ public function testBasicCreateTable()
$statements = $blueprint->toSql($conn, $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals("create table `users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255) not null) default character set utf8 collate 'utf8_unicode_ci'", $statements[0]);
$this->assertEquals("create table `users` (`id` bigint unsigned not null auto_increment primary key, `email` varchar(255) not null) default character set utf8 collate 'utf8_unicode_ci'", $statements[0]);

$blueprint = new Blueprint('users');
$blueprint->increments('id');
Expand All @@ -43,7 +44,7 @@ public function testBasicCreateTable()
$statements = $blueprint->toSql($conn, $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table `users` add `id` int unsigned not null auto_increment primary key, add `email` varchar(255) not null', $statements[0]);
$this->assertEquals('alter table `users` add `id` bigint unsigned not null auto_increment primary key, add `email` varchar(255) not null', $statements[0]);
}

public function testEngineCreateTable()
Expand All @@ -61,7 +62,7 @@ public function testEngineCreateTable()
$statements = $blueprint->toSql($conn, $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals("create table `users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255) not null) default character set utf8 collate 'utf8_unicode_ci' engine = InnoDB", $statements[0]);
$this->assertEquals("create table `users` (`id` bigint unsigned not null auto_increment primary key, `email` varchar(255) not null) default character set utf8 collate 'utf8_unicode_ci' engine = InnoDB", $statements[0]);

$blueprint = new Blueprint('users');
$blueprint->create();
Expand All @@ -76,7 +77,7 @@ public function testEngineCreateTable()
$statements = $blueprint->toSql($conn, $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals("create table `users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255) not null) default character set utf8 collate 'utf8_unicode_ci' engine = InnoDB", $statements[0]);
$this->assertEquals("create table `users` (`id` bigint unsigned not null auto_increment primary key, `email` varchar(255) not null) default character set utf8 collate 'utf8_unicode_ci' engine = InnoDB", $statements[0]);
}

public function testCharsetCollationCreateTable()
Expand All @@ -94,7 +95,7 @@ public function testCharsetCollationCreateTable()
$statements = $blueprint->toSql($conn, $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals("create table `users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255) not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'", $statements[0]);
$this->assertEquals("create table `users` (`id` bigint unsigned not null auto_increment primary key, `email` varchar(255) not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'", $statements[0]);

$blueprint = new Blueprint('users');
$blueprint->create();
Expand All @@ -109,7 +110,7 @@ public function testCharsetCollationCreateTable()
$statements = $blueprint->toSql($conn, $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals("create table `users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255) character set utf8mb4 collate 'utf8mb4_unicode_ci' not null) default character set utf8 collate 'utf8_unicode_ci'", $statements[0]);
$this->assertEquals("create table `users` (`id` bigint unsigned not null auto_increment primary key, `email` varchar(255) character set utf8mb4 collate 'utf8mb4_unicode_ci' not null) default character set utf8 collate 'utf8_unicode_ci'", $statements[0]);
}

public function testBasicCreateTableWithPrefix()
Expand All @@ -127,7 +128,7 @@ public function testBasicCreateTableWithPrefix()
$statements = $blueprint->toSql($conn, $grammar);

$this->assertCount(1, $statements);
$this->assertEquals('create table `prefix_users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255) not null)', $statements[0]);
$this->assertEquals('create table `prefix_users` (`id` bigint unsigned not null auto_increment primary key, `email` varchar(255) not null)', $statements[0]);
}

public function testCreateTemporaryTable()
Expand All @@ -144,7 +145,7 @@ public function testCreateTemporaryTable()
$statements = $blueprint->toSql($conn, $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('create temporary table `users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255) not null)', $statements[0]);
$this->assertEquals('create temporary table `users` (`id` bigint unsigned not null auto_increment primary key, `email` varchar(255) not null)', $statements[0]);
}

public function testDropTable()
Expand Down Expand Up @@ -378,8 +379,18 @@ public function testAddingIncrementingID()
$blueprint->increments('id');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table `users` add `id` bigint unsigned not null auto_increment primary key', $statements[0]);

Builder::useIntegerIncrements();
$blueprint = new Blueprint('users');
$blueprint->increments('id');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table `users` add `id` int unsigned not null auto_increment primary key', $statements[0]);

Builder::$defaultIncrementsType = 'unsignedBigInteger';
}

public function testAddingSmallIncrementingID()
Expand All @@ -392,6 +403,16 @@ public function testAddingSmallIncrementingID()
$this->assertEquals('alter table `users` add `id` smallint unsigned not null auto_increment primary key', $statements[0]);
}

public function testAddingIntegerIncrementingID()
{
$blueprint = new Blueprint('users');
$blueprint->integerIncrements('id');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table `users` add `id` int unsigned not null auto_increment primary key', $statements[0]);
}

public function testAddingBigIncrementingID()
{
$blueprint = new Blueprint('users');
Expand Down
35 changes: 28 additions & 7 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Expand Up @@ -5,6 +5,7 @@
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Illuminate\Database\Connection;
use Illuminate\Database\Schema\Builder;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Grammars\PostgresGrammar;

Expand All @@ -24,15 +25,15 @@ public function testBasicCreateTable()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('create table "users" ("id" serial primary key not null, "email" varchar(255) not null)', $statements[0]);
$this->assertEquals('create table "users" ("id" bigserial primary key not null, "email" varchar(255) not null)', $statements[0]);

$blueprint = new Blueprint('users');
$blueprint->increments('id');
$blueprint->string('email');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table "users" add column "id" serial primary key not null, add column "email" varchar(255) not null', $statements[0]);
$this->assertEquals('alter table "users" add column "id" bigserial primary key not null, add column "email" varchar(255) not null', $statements[0]);
}

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

$this->assertCount(2, $statements);
$this->assertEquals('create table "users" ("id" serial primary key not null, "email" varchar(255) not null)', $statements[0]);
$this->assertEquals('create table "users" ("id" bigserial primary key not null, "email" varchar(255) not null)', $statements[0]);
$this->assertEquals('comment on column "users"."email" is \'my first comment\'', $statements[1]);
}

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

$this->assertCount(1, $statements);
$this->assertEquals('create temporary table "users" ("id" serial primary key not null, "email" varchar(255) not null)', $statements[0]);
$this->assertEquals('create temporary table "users" ("id" bigserial primary key not null, "email" varchar(255) not null)', $statements[0]);
}

public function testDropTable()
Expand Down Expand Up @@ -272,8 +273,18 @@ public function testAddingIncrementingID()
$blueprint->increments('id');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table "users" add column "id" bigserial primary key not null', $statements[0]);

Builder::useIntegerIncrements();
$blueprint = new Blueprint('users');
$blueprint->increments('id');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table "users" add column "id" serial primary key not null', $statements[0]);

Builder::$defaultIncrementsType = 'unsignedBigInteger';
}

public function testAddingSmallIncrementingID()
Expand All @@ -296,6 +307,16 @@ public function testAddingMediumIncrementingID()
$this->assertEquals('alter table "users" add column "id" serial primary key not null', $statements[0]);
}

public function testAddingIntegerIncrementingID()
{
$blueprint = new Blueprint('users');
$blueprint->integerIncrements('id');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table "users" add column "id" serial primary key not null', $statements[0]);
}

public function testAddingBigIncrementingID()
{
$blueprint = new Blueprint('users');
Expand Down Expand Up @@ -666,19 +687,19 @@ public function testAddingGeneratedAs()
$blueprint->increments('foo')->generatedAs();
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
$this->assertCount(1, $statements);
$this->assertEquals('alter table "users" add column "foo" integer generated by default as identity primary key not null', $statements[0]);
$this->assertEquals('alter table "users" add column "foo" bigint generated by default as identity primary key not null', $statements[0]);
// With always modifier
$blueprint = new Blueprint('users');
$blueprint->increments('foo')->generatedAs()->always();
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
$this->assertCount(1, $statements);
$this->assertEquals('alter table "users" add column "foo" integer generated always as identity primary key not null', $statements[0]);
$this->assertEquals('alter table "users" add column "foo" bigint generated always as identity primary key not null', $statements[0]);
// With sequence options
$blueprint = new Blueprint('users');
$blueprint->increments('foo')->generatedAs('increment by 10 start with 100');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
$this->assertCount(1, $statements);
$this->assertEquals('alter table "users" add column "foo" integer generated by default as identity (increment by 10 start with 100) primary key not null', $statements[0]);
$this->assertEquals('alter table "users" add column "foo" bigint generated by default as identity (increment by 10 start with 100) primary key not null', $statements[0]);
// Not a primary key
$blueprint = new Blueprint('users');
$blueprint->integer('foo')->generatedAs();
Expand Down
29 changes: 25 additions & 4 deletions tests/Database/DatabaseSqlServerSchemaGrammarTest.php
Expand Up @@ -5,6 +5,7 @@
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Illuminate\Database\Connection;
use Illuminate\Database\Schema\Builder;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Grammars\SqlServerGrammar;

Expand All @@ -24,15 +25,15 @@ public function testBasicCreateTable()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('create table "users" ("id" int identity primary key not null, "email" nvarchar(255) not null)', $statements[0]);
$this->assertEquals('create table "users" ("id" bigint identity primary key not null, "email" nvarchar(255) not null)', $statements[0]);

$blueprint = new Blueprint('users');
$blueprint->increments('id');
$blueprint->string('email');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table "users" add "id" int identity primary key not null, "email" nvarchar(255) not null', $statements[0]);
$this->assertEquals('alter table "users" add "id" bigint identity primary key not null, "email" nvarchar(255) not null', $statements[0]);

$blueprint = new Blueprint('users');
$blueprint->create();
Expand All @@ -41,7 +42,7 @@ public function testBasicCreateTable()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()->setTablePrefix('prefix_'));

$this->assertCount(1, $statements);
$this->assertEquals('create table "prefix_users" ("id" int identity primary key not null, "email" nvarchar(255) not null)', $statements[0]);
$this->assertEquals('create table "prefix_users" ("id" bigint identity primary key not null, "email" nvarchar(255) not null)', $statements[0]);
}

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

$this->assertCount(1, $statements);
$this->assertEquals('create table "#users" ("id" int identity primary key not null, "email" nvarchar(255) not null)', $statements[0]);
$this->assertEquals('create table "#users" ("id" bigint identity primary key not null, "email" nvarchar(255) not null)', $statements[0]);
}

public function testDropTable()
Expand Down Expand Up @@ -272,8 +273,18 @@ public function testAddingIncrementingID()
$blueprint->increments('id');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table "users" add "id" bigint identity primary key not null', $statements[0]);

Builder::useIntegerIncrements();
$blueprint = new Blueprint('users');
$blueprint->increments('id');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table "users" add "id" int identity primary key not null', $statements[0]);

Builder::$defaultIncrementsType = 'unsignedBigInteger';
}

public function testAddingSmallIncrementingID()
Expand All @@ -296,6 +307,16 @@ public function testAddingMediumIncrementingID()
$this->assertEquals('alter table "users" add "id" int identity primary key not null', $statements[0]);
}

public function testAddingIntegerIncrementingID()
{
$blueprint = new Blueprint('users');
$blueprint->integerIncrements('id');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table "users" add "id" int identity primary key not null', $statements[0]);
}

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