2013-01-10 15:28:59 -06:00
< ? php
2017-01-17 14:30:19 +00:00
namespace Illuminate\Tests\Database ;
2018-10-03 22:05:43 +02:00
use Illuminate\Database\Connection ;
2023-11-13 17:39:12 +01:00
use Illuminate\Database\Query\Expression ;
2013-01-10 15:28:59 -06:00
use Illuminate\Database\Schema\Blueprint ;
2022-04-03 17:10:58 +02:00
use Illuminate\Database\Schema\Builder ;
2019-11-02 09:17:13 -04:00
use Illuminate\Database\Schema\ForeignIdColumnDefinition ;
2018-10-03 22:05:43 +02:00
use Illuminate\Database\Schema\Grammars\PostgresGrammar ;
2024-06-12 23:07:20 -05:00
use Illuminate\Database\Schema\PostgresBuilder ;
2025-08-06 17:04:44 +02:00
use Illuminate\Tests\Database\Fixtures\Enums\Foo ;
2019-09-10 17:16:05 +02:00
use Mockery as m ;
2024-06-12 23:07:20 -05:00
use PHPUnit\Framework\Attributes\DataProvider ;
use PHPUnit\Framework\Attributes\TestWith ;
2019-09-10 17:16:05 +02:00
use PHPUnit\Framework\TestCase ;
2013-01-10 15:28:59 -06:00
2016-12-30 21:31:11 +01:00
class DatabasePostgresSchemaGrammarTest extends TestCase
2015-06-01 16:26:53 +01:00
{
2015-06-01 15:56:31 +01:00
public function testBasicCreateTable ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> create ();
$blueprint -> increments ( 'id' );
$blueprint -> string ( 'email' );
2019-07-18 14:14:03 +02:00
$blueprint -> string ( 'name' ) -> collation ( 'nb_NO.utf8' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2023-01-05 01:54:52 +03:30
$this -> assertSame ( 'create table "users" ("id" serial not null primary key, "email" varchar(255) not null, "name" varchar(255) collate "nb_NO.utf8" not null)' , $statements [ 0 ]);
2013-01-10 15:28:59 -06:00
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> increments ( 'id' );
$blueprint -> string ( 'email' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2024-07-04 18:01:28 +03:30
$this -> assertCount ( 2 , $statements );
$this -> assertSame ([
'alter table "users" add column "id" serial not null primary key' ,
'alter table "users" add column "email" varchar(255) not null' ,
], $statements );
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2024-09-26 03:46:33 +12:00
public function testAddingVector ()
{
2024-10-14 20:56:14 +08:00
$blueprint = new Blueprint ( $this -> getConnection (), 'embeddings' );
2024-09-26 03:46:33 +12:00
$blueprint -> vector ( 'embedding' , 384 );
$statements = $blueprint -> toSql ( $this -> getConnection (), $this -> getGrammar ());
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'alter table "embeddings" add column "embedding" vector(384) not null' , $statements [ 0 ]);
}
2026-02-27 17:38:20 +01:00
public function testAddingTsvectorColumn ()
{
$blueprint = new Blueprint ( $this -> getConnection (), 'test' );
$blueprint -> tsvector ( 'search_vector' );
$statements = $blueprint -> toSql ();
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'alter table "test" add column "search_vector" tsvector not null' , $statements [ 0 ]);
}
public function testAddingNullableTsvectorColumn ()
{
$blueprint = new Blueprint ( $this -> getConnection (), 'test' );
$blueprint -> tsvector ( 'search_vector' ) -> nullable ();
$statements = $blueprint -> toSql ();
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'alter table "test" add column "search_vector" tsvector null' , $statements [ 0 ]);
}
public function testAddingTsvectorColumnWithStoredAs ()
{
$blueprint = new Blueprint ( $this -> getConnection (), 'test' );
$blueprint -> tsvector ( 'search_vector' ) -> nullable () -> storedAs ( " to_tsvector('english', coalesce(name, '')) " );
$statements = $blueprint -> toSql ();
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'alter table "test" add column "search_vector" tsvector null generated always as (to_tsvector(\'english\', coalesce(name, \'\'))) stored' , $statements [ 0 ]);
}
2020-09-06 15:37:42 -05:00
public function testCreateTableWithAutoIncrementStartingValue ()
{
2025-02-12 20:00:57 +03:30
$connection = $this -> getConnection ();
$connection -> getSchemaBuilder () -> shouldReceive ( 'parseSchemaAndTable' ) -> andReturn ([ null , 'users' ]);
$blueprint = new Blueprint ( $connection , 'users' );
2020-09-06 15:37:42 -05:00
$blueprint -> create ();
$blueprint -> increments ( 'id' ) -> startingValue ( 1000 );
$blueprint -> string ( 'email' );
$blueprint -> string ( 'name' ) -> collation ( 'nb_NO.utf8' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2020-09-06 15:37:42 -05:00
$this -> assertCount ( 2 , $statements );
2023-01-05 01:54:52 +03:30
$this -> assertSame ( 'create table "users" ("id" serial not null primary key, "email" varchar(255) not null, "name" varchar(255) collate "nb_NO.utf8" not null)' , $statements [ 0 ]);
2026-02-06 21:35:36 +09:00
$this -> assertSame ( " select setval(pg_get_serial_sequence(' \" users \" ', 'id'), 1000, false) " , $statements [ 1 ]);
2020-09-06 15:37:42 -05:00
}
2023-01-05 01:54:52 +03:30
public function testAddColumnsWithMultipleAutoIncrementStartingValue ()
{
2025-02-12 20:00:57 +03:30
$builder = $this -> getBuilder ();
$builder -> shouldReceive ( 'parseSchemaAndTable' ) -> andReturn ([ null , 'users' ]);
$blueprint = new Blueprint ( $this -> getConnection ( builder : $builder ), 'users' );
2023-01-05 01:54:52 +03:30
$blueprint -> id () -> from ( 100 );
$blueprint -> increments ( 'code' ) -> from ( 200 );
$blueprint -> string ( 'name' ) -> from ( 300 );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2023-01-05 01:54:52 +03:30
$this -> assertEquals ([
2024-07-04 18:01:28 +03:30
'alter table "users" add column "id" bigserial not null primary key' ,
'alter table "users" add column "code" serial not null primary key' ,
'alter table "users" add column "name" varchar(255) not null' ,
2026-02-06 21:35:36 +09:00
" select setval(pg_get_serial_sequence(' \" users \" ', 'id'), 100, false) " ,
" select setval(pg_get_serial_sequence(' \" users \" ', 'code'), 200, false) " ,
2023-01-05 01:54:52 +03:30
], $statements );
}
2017-10-28 19:00:27 -02:00
public function testCreateTableAndCommentColumn ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2017-10-28 19:00:27 -02:00
$blueprint -> create ();
$blueprint -> increments ( 'id' );
2017-10-28 19:06:45 -02:00
$blueprint -> string ( 'email' ) -> comment ( 'my first comment' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2017-10-28 19:00:27 -02:00
$this -> assertCount ( 2 , $statements );
2023-01-05 01:54:52 +03:30
$this -> assertSame ( 'create table "users" ("id" serial not null primary key, "email" varchar(255) not null)' , $statements [ 0 ]);
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'comment on column "users"."email" is \'my first comment\'' , $statements [ 1 ]);
2017-10-28 19:00:27 -02:00
}
2017-11-17 05:42:54 -02:00
public function testCreateTemporaryTable ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2017-11-17 05:42:54 -02:00
$blueprint -> create ();
$blueprint -> temporary ();
$blueprint -> increments ( 'id' );
$blueprint -> string ( 'email' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2017-11-17 05:42:54 -02:00
$this -> assertCount ( 1 , $statements );
2023-01-05 01:54:52 +03:30
$this -> assertSame ( 'create temporary table "users" ("id" serial not null primary key, "email" varchar(255) not null)' , $statements [ 0 ]);
2017-11-17 05:42:54 -02:00
}
2015-06-01 15:56:31 +01:00
public function testDropTable ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> drop ();
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'drop table "users"' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2015-06-01 15:56:31 +01:00
public function testDropTableIfExists ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> dropIfExists ();
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'drop table if exists "users"' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2015-06-01 15:56:31 +01:00
public function testDropColumn ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> dropColumn ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" drop column "foo"' , $statements [ 0 ]);
2013-01-10 15:28:59 -06:00
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 16:35:44 +01:00
$blueprint -> dropColumn ([ 'foo' , 'bar' ]);
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" drop column "foo", drop column "bar"' , $statements [ 0 ]);
2013-04-17 15:52:32 +02:00
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> dropColumn ( 'foo' , 'bar' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-04-17 15:52:32 +02:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" drop column "foo", drop column "bar"' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2015-06-01 15:56:31 +01:00
public function testDropPrimary ()
{
2025-02-12 20:00:57 +03:30
$connection = $this -> getConnection ();
$connection -> getSchemaBuilder () -> shouldReceive ( 'parseSchemaAndTable' ) -> andReturn ([ null , 'users' ]);
$blueprint = new Blueprint ( $connection , 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> dropPrimary ();
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" drop constraint "users_pkey"' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2015-06-01 15:56:31 +01:00
public function testDropUnique ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> dropUnique ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" drop constraint "foo"' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2015-06-01 15:56:31 +01:00
public function testDropIndex ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> dropIndex ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'drop index "foo"' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2017-11-02 03:45:27 -02:00
public function testDropSpatialIndex ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'geo' );
2017-11-02 03:45:27 -02:00
$blueprint -> dropSpatialIndex ([ 'coordinates' ]);
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2017-11-02 03:45:27 -02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'drop index "geo_coordinates_spatialindex"' , $statements [ 0 ]);
2017-11-02 03:45:27 -02:00
}
2015-06-01 15:56:31 +01:00
public function testDropForeign ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> dropForeign ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" drop constraint "foo"' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2015-06-01 15:56:31 +01:00
public function testDropTimestamps ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> dropTimestamps ();
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-03-24 20:10:24 +01:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" drop column "created_at", drop column "updated_at"' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-03-24 20:10:24 +01:00
2015-06-28 00:13:27 +10:00
public function testDropTimestampsTz ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-28 00:13:27 +10:00
$blueprint -> dropTimestampsTz ();
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2015-06-28 00:13:27 +10:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" drop column "created_at", drop column "updated_at"' , $statements [ 0 ]);
2015-06-28 00:13:27 +10:00
}
2018-03-05 22:52:29 -05:00
public function testDropMorphs ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'photos' );
2018-03-05 22:52:29 -05:00
$blueprint -> dropMorphs ( 'imageable' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2018-03-05 22:52:29 -05:00
2018-03-06 23:33:17 -05:00
$this -> assertCount ( 2 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'drop index "photos_imageable_type_imageable_id_index"' , $statements [ 0 ]);
$this -> assertSame ( 'alter table "photos" drop column "imageable_type", drop column "imageable_id"' , $statements [ 1 ]);
2018-03-05 22:52:29 -05:00
}
2015-06-01 15:56:31 +01:00
public function testRenameTable ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> rename ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" rename to "foo"' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2018-05-08 16:56:30 +03:00
public function testRenameIndex ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2018-05-08 16:56:30 +03:00
$blueprint -> renameIndex ( 'foo' , 'bar' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2018-05-08 16:56:30 +03:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter index "foo" rename to "bar"' , $statements [ 0 ]);
2018-05-08 16:56:30 +03:00
}
2015-06-01 15:56:31 +01:00
public function testAddingPrimaryKey ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> primary ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add primary key ("foo")' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2015-06-01 15:56:31 +01:00
public function testAddingUniqueKey ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> unique ( 'foo' , 'bar' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add constraint "bar" unique ("foo")' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2025-03-16 19:58:57 -04:00
public function testAddingUniqueKeyWithNullsNotDistinct ()
{
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
$blueprint -> unique ( 'foo' , 'bar' ) -> nullsNotDistinct ();
$statements = $blueprint -> toSql ();
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'alter table "users" add constraint "bar" unique nulls not distinct ("foo")' , $statements [ 0 ]);
}
public function testAddingUniqueKeyWithNullsDistinct ()
{
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
$blueprint -> unique ( 'foo' , 'bar' ) -> nullsNotDistinct ( false );
$statements = $blueprint -> toSql ();
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'alter table "users" add constraint "bar" unique nulls distinct ("foo")' , $statements [ 0 ]);
}
2025-08-12 21:22:24 +07:00
public function testAddingUniqueKeyOnline ()
{
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
$blueprint -> unique ( 'foo' ) -> online ();
$statements = $blueprint -> toSql ();
$this -> assertCount ( 2 , $statements );
$this -> assertSame ( 'create unique index concurrently "users_foo_unique" on "users" ("foo")' , $statements [ 0 ]);
$this -> assertSame ( 'alter table "users" add constraint "users_foo_unique" unique using index "users_foo_unique"' , $statements [ 1 ]);
}
2015-06-01 15:56:31 +01:00
public function testAddingIndex ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 16:35:44 +01:00
$blueprint -> index ([ 'foo' , 'bar' ], 'baz' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'create index "baz" on "users" ("foo", "bar")' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2016-03-18 06:52:16 -07:00
public function testAddingIndexWithAlgorithm ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2016-03-18 06:52:16 -07:00
$blueprint -> index ([ 'foo' , 'bar' ], 'baz' , 'hash' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2016-03-18 06:52:16 -07:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'create index "baz" on "users" using hash ("foo", "bar")' , $statements [ 0 ]);
2016-03-18 06:52:16 -07:00
}
2025-08-12 21:22:24 +07:00
public function testAddingIndexOnline ()
{
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
$blueprint -> index ( 'foo' , 'baz' ) -> online ();
$statements = $blueprint -> toSql ();
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'create index concurrently "baz" on "users" ("foo")' , $statements [ 0 ]);
}
2021-12-07 15:25:54 +01:00
public function testAddingFulltextIndex ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2021-12-07 15:25:54 +01:00
$blueprint -> fulltext ( 'body' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2021-12-07 15:25:54 +01:00
$this -> assertCount ( 1 , $statements );
2022-01-06 16:50:35 +01:00
$this -> assertSame ( 'create index "users_body_fulltext" on "users" using gin ((to_tsvector(\'english\', "body")))' , $statements [ 0 ]);
}
public function testAddingFulltextIndexMultipleColumns ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2022-01-06 16:50:35 +01:00
$blueprint -> fulltext ([ 'body' , 'title' ]);
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2022-01-06 16:50:35 +01:00
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'create index "users_body_title_fulltext" on "users" using gin ((to_tsvector(\'english\', "body") || to_tsvector(\'english\', "title")))' , $statements [ 0 ]);
2021-12-07 15:25:54 +01:00
}
public function testAddingFulltextIndexWithLanguage ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2021-12-07 15:25:54 +01:00
$blueprint -> fulltext ( 'body' ) -> language ( 'spanish' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2021-12-07 15:25:54 +01:00
$this -> assertCount ( 1 , $statements );
2022-01-06 16:50:35 +01:00
$this -> assertSame ( 'create index "users_body_fulltext" on "users" using gin ((to_tsvector(\'spanish\', "body")))' , $statements [ 0 ]);
2021-12-07 15:25:54 +01:00
}
2025-08-12 21:22:24 +07:00
public function testAddingFulltextIndexOnline ()
{
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
$blueprint -> fulltext ( 'body' ) -> online ();
$statements = $blueprint -> toSql ();
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'create index concurrently "users_body_fulltext" on "users" using gin ((to_tsvector(\'english\', "body")))' , $statements [ 0 ]);
}
2021-12-07 15:25:54 +01:00
public function testAddingFulltextIndexWithFluency ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2021-12-07 15:25:54 +01:00
$blueprint -> string ( 'body' ) -> fulltext ();
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2021-12-07 15:25:54 +01:00
$this -> assertCount ( 2 , $statements );
2022-01-06 16:50:35 +01:00
$this -> assertSame ( 'create index "users_body_fulltext" on "users" using gin ((to_tsvector(\'english\', "body")))' , $statements [ 1 ]);
2021-12-07 15:25:54 +01:00
}
2017-11-02 03:25:39 -02:00
public function testAddingSpatialIndex ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'geo' );
2017-11-02 03:25:39 -02:00
$blueprint -> spatialIndex ( 'coordinates' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2017-11-02 03:25:39 -02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'create index "geo_coordinates_spatialindex" on "geo" using gist ("coordinates")' , $statements [ 0 ]);
2017-11-02 03:25:39 -02:00
}
2025-08-12 21:22:24 +07:00
public function testAddingSpatialIndexOnline ()
{
$blueprint = new Blueprint ( $this -> getConnection (), 'geo' );
$blueprint -> spatialIndex ( 'coordinates' ) -> online ();
$statements = $blueprint -> toSql ();
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'create index concurrently "geo_coordinates_spatialindex" on "geo" using gist ("coordinates")' , $statements [ 0 ]);
}
2017-11-02 03:25:39 -02:00
public function testAddingFluentSpatialIndex ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'geo' );
2024-01-18 20:57:15 +03:30
$blueprint -> geometry ( 'coordinates' , 'point' ) -> spatialIndex ();
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2017-11-02 03:25:39 -02:00
$this -> assertCount ( 2 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'create index "geo_coordinates_spatialindex" on "geo" using gist ("coordinates")' , $statements [ 1 ]);
2017-11-02 03:25:39 -02:00
}
2025-07-19 01:24:25 +09:00
public function testAddingSpatialIndexWithOperatorClass ()
{
$blueprint = new Blueprint ( $this -> getConnection (), 'geo' );
$blueprint -> spatialIndex ( 'coordinates' , 'my_index' , 'point_ops' );
$statements = $blueprint -> toSql ();
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'create index "my_index" on "geo" using gist ("coordinates" point_ops)' , $statements [ 0 ]);
}
public function testAddingSpatialIndexWithOperatorClassMultipleColumns ()
{
$blueprint = new Blueprint ( $this -> getConnection (), 'geo' );
$blueprint -> spatialIndex ([ 'coordinates' , 'location' ], 'my_index' , 'point_ops' );
$statements = $blueprint -> toSql ();
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'create index "my_index" on "geo" using gist ("coordinates" point_ops, "location" point_ops)' , $statements [ 0 ]);
}
2025-08-12 21:22:24 +07:00
public function testAddingSpatialIndexWithOperatorClassOnline ()
{
$blueprint = new Blueprint ( $this -> getConnection (), 'geo' );
$blueprint -> spatialIndex ( 'coordinates' , 'my_index' , 'point_ops' ) -> online ();
$statements = $blueprint -> toSql ();
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'create index concurrently "my_index" on "geo" using gist ("coordinates" point_ops)' , $statements [ 0 ]);
}
2026-01-10 13:13:49 -06:00
public function testAddingVectorIndex ()
{
$blueprint = new Blueprint ( $this -> getConnection (), 'posts' );
$blueprint -> vectorIndex ( 'embeddings' );
$statements = $blueprint -> toSql ();
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'create index "posts_embeddings_vectorindex" on "posts" using hnsw ("embeddings" vector_cosine_ops)' , $statements [ 0 ]);
}
public function testAddingVectorIndexOnline ()
{
$blueprint = new Blueprint ( $this -> getConnection (), 'posts' );
$blueprint -> vectorIndex ( 'embeddings' ) -> online ();
$statements = $blueprint -> toSql ();
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'create index concurrently "posts_embeddings_vectorindex" on "posts" using hnsw ("embeddings" vector_cosine_ops)' , $statements [ 0 ]);
}
public function testAddingVectorIndexWithName ()
{
$blueprint = new Blueprint ( $this -> getConnection (), 'posts' );
$blueprint -> vectorIndex ( 'embeddings' , 'my_vector_index' );
$statements = $blueprint -> toSql ();
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'create index "my_vector_index" on "posts" using hnsw ("embeddings" vector_cosine_ops)' , $statements [ 0 ]);
}
public function testAddingFluentVectorIndex ()
{
$blueprint = new Blueprint ( $this -> getConnection (), 'posts' );
$blueprint -> vector ( 'embeddings' , 1536 ) -> vectorIndex ();
$statements = $blueprint -> toSql ();
$this -> assertCount ( 2 , $statements );
$this -> assertSame ( 'create index "posts_embeddings_vectorindex" on "posts" using hnsw ("embeddings" vector_cosine_ops)' , $statements [ 1 ]);
}
public function testAddingFluentIndexOnVectorColumn ()
{
$blueprint = new Blueprint ( $this -> getConnection (), 'posts' );
$blueprint -> vector ( 'embeddings' , 1536 ) -> index ();
$statements = $blueprint -> toSql ();
$this -> assertCount ( 2 , $statements );
$this -> assertSame ( 'create index "posts_embeddings_vectorindex" on "posts" using hnsw ("embeddings" vector_cosine_ops)' , $statements [ 1 ]);
}
2020-04-16 21:06:48 -04:00
public function testAddingRawIndex ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2020-04-16 21:06:48 -04:00
$blueprint -> rawIndex ( '(function(column))' , 'raw_index' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2020-04-16 21:06:48 -04:00
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'create index "raw_index" on "users" ((function(column)))' , $statements [ 0 ]);
}
2025-08-12 21:22:24 +07:00
public function testAddingRawIndexOnline ()
{
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
$blueprint -> rawIndex ( '(function(column))' , 'raw_index' ) -> online ();
$statements = $blueprint -> toSql ();
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'create index concurrently "raw_index" on "users" ((function(column)))' , $statements [ 0 ]);
}
2015-06-01 15:56:31 +01:00
public function testAddingIncrementingID ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> increments ( 'id' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2023-01-05 01:54:52 +03:30
$this -> assertSame ( 'alter table "users" add column "id" serial not null primary key' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2015-07-03 13:30:58 +03:00
public function testAddingSmallIncrementingID ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-07-03 13:30:58 +03:00
$blueprint -> smallIncrements ( 'id' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2015-07-03 13:30:58 +03:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2023-01-05 01:54:52 +03:30
$this -> assertSame ( 'alter table "users" add column "id" smallserial not null primary key' , $statements [ 0 ]);
2015-07-03 13:30:58 +03:00
}
public function testAddingMediumIncrementingID ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-07-03 13:30:58 +03:00
$blueprint -> mediumIncrements ( 'id' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2015-07-03 13:30:58 +03:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2023-01-05 01:54:52 +03:30
$this -> assertSame ( 'alter table "users" add column "id" serial not null primary key' , $statements [ 0 ]);
2015-07-03 13:30:58 +03:00
}
2019-11-01 09:02:47 -04:00
public function testAddingID ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2019-11-01 09:02:47 -04:00
$blueprint -> id ();
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2019-11-01 09:02:47 -04:00
$this -> assertCount ( 1 , $statements );
2023-01-05 01:54:52 +03:30
$this -> assertSame ( 'alter table "users" add column "id" bigserial not null primary key' , $statements [ 0 ]);
2019-11-01 09:02:47 -04:00
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2019-11-01 09:02:47 -04:00
$blueprint -> id ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2019-11-01 09:02:47 -04:00
$this -> assertCount ( 1 , $statements );
2023-01-05 01:54:52 +03:30
$this -> assertSame ( 'alter table "users" add column "foo" bigserial not null primary key' , $statements [ 0 ]);
2019-11-01 09:02:47 -04:00
}
public function testAddingForeignID ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2019-11-02 09:17:13 -04:00
$foreignId = $blueprint -> foreignId ( 'foo' );
$blueprint -> foreignId ( 'company_id' ) -> constrained ();
2020-05-16 19:24:58 -04:00
$blueprint -> foreignId ( 'laravel_idea_id' ) -> constrained ();
2019-11-02 09:17:13 -04:00
$blueprint -> foreignId ( 'team_id' ) -> references ( 'id' ) -> on ( 'teams' );
2020-03-09 10:26:02 -03:00
$blueprint -> foreignId ( 'team_column_id' ) -> constrained ( 'teams' );
2019-11-02 09:17:13 -04:00
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2019-11-01 09:02:47 -04:00
2019-11-02 09:17:13 -04:00
$this -> assertInstanceOf ( ForeignIdColumnDefinition :: class , $foreignId );
$this -> assertSame ([
2024-07-04 18:01:28 +03:30
'alter table "users" add column "foo" bigint not null' ,
'alter table "users" add column "company_id" bigint not null' ,
2019-11-02 09:17:13 -04:00
'alter table "users" add constraint "users_company_id_foreign" foreign key ("company_id") references "companies" ("id")' ,
2024-07-04 18:01:28 +03:30
'alter table "users" add column "laravel_idea_id" bigint not null' ,
2020-05-16 19:24:58 -04:00
'alter table "users" add constraint "users_laravel_idea_id_foreign" foreign key ("laravel_idea_id") references "laravel_ideas" ("id")' ,
2024-07-04 18:01:28 +03:30
'alter table "users" add column "team_id" bigint not null' ,
2019-11-02 09:17:13 -04:00
'alter table "users" add constraint "users_team_id_foreign" foreign key ("team_id") references "teams" ("id")' ,
2024-07-04 18:01:28 +03:30
'alter table "users" add column "team_column_id" bigint not null' ,
2020-03-09 10:26:02 -03:00
'alter table "users" add constraint "users_team_column_id_foreign" foreign key ("team_column_id") references "teams" ("id")' ,
2019-11-02 09:17:13 -04:00
], $statements );
2019-11-01 09:02:47 -04:00
}
2023-04-11 15:18:22 -04:00
public function testAddingForeignIdSpecifyingIndexNameInConstraint ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2023-04-11 15:18:22 -04:00
$blueprint -> foreignId ( 'company_id' ) -> constrained ( indexName : 'my_index' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2023-04-11 15:18:22 -04:00
$this -> assertSame ([
'alter table "users" add column "company_id" bigint not null' ,
'alter table "users" add constraint "my_index" foreign key ("company_id") references "companies" ("id")' ,
], $statements );
}
2015-06-01 15:56:31 +01:00
public function testAddingBigIncrementingID ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> bigIncrements ( 'id' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-05-13 11:29:22 +07:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2023-01-05 01:54:52 +03:30
$this -> assertSame ( 'alter table "users" add column "id" bigserial not null primary key' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-05-13 11:29:22 +07:00
2015-06-01 15:56:31 +01:00
public function testAddingString ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> string ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "foo" varchar(255) not null' , $statements [ 0 ]);
2013-01-10 15:28:59 -06:00
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> string ( 'foo' , 100 );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "foo" varchar(100) not null' , $statements [ 0 ]);
2013-01-10 15:28:59 -06:00
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> string ( 'foo' , 100 ) -> nullable () -> default ( 'bar' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "foo" varchar(100) null default \'bar\'' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2022-04-03 17:10:58 +02:00
public function testAddingStringWithoutLengthLimit ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2022-04-03 17:10:58 +02:00
$blueprint -> string ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2022-04-03 17:10:58 +02:00
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'alter table "users" add column "foo" varchar(255) not null' , $statements [ 0 ]);
Builder :: $defaultStringLength = null ;
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2022-04-03 17:10:58 +02:00
$blueprint -> string ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2022-04-03 17:10:58 +02:00
try {
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'alter table "users" add column "foo" varchar not null' , $statements [ 0 ]);
} finally {
Builder :: $defaultStringLength = 255 ;
}
}
public function testAddingCharWithoutLengthLimit ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2022-04-03 17:10:58 +02:00
$blueprint -> char ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2022-04-03 17:10:58 +02:00
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'alter table "users" add column "foo" char(255) not null' , $statements [ 0 ]);
Builder :: $defaultStringLength = null ;
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2022-04-03 17:10:58 +02:00
$blueprint -> char ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2022-04-03 17:10:58 +02:00
try {
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'alter table "users" add column "foo" char not null' , $statements [ 0 ]);
} finally {
Builder :: $defaultStringLength = 255 ;
}
}
2015-06-01 15:56:31 +01:00
public function testAddingText ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> text ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "foo" text not null' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2015-06-01 15:56:31 +01:00
public function testAddingBigInteger ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> bigInteger ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-05-13 11:29:22 +07:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "foo" bigint not null' , $statements [ 0 ]);
2013-05-13 11:29:22 +07:00
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> bigInteger ( 'foo' , true );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-05-13 11:29:22 +07:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2023-01-05 01:54:52 +03:30
$this -> assertSame ( 'alter table "users" add column "foo" bigserial not null primary key' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-05-13 11:29:22 +07:00
2015-06-01 15:56:31 +01:00
public function testAddingInteger ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> integer ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "foo" integer not null' , $statements [ 0 ]);
2013-01-10 15:28:59 -06:00
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> integer ( 'foo' , true );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2023-01-05 01:54:52 +03:30
$this -> assertSame ( 'alter table "users" add column "foo" serial not null primary key' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2015-06-01 15:56:31 +01:00
public function testAddingMediumInteger ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> mediumInteger ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-05-13 11:29:22 +07:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "foo" integer not null' , $statements [ 0 ]);
2015-02-18 17:08:35 +02:00
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> mediumInteger ( 'foo' , true );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2015-02-18 17:08:35 +02:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2023-01-05 01:54:52 +03:30
$this -> assertSame ( 'alter table "users" add column "foo" serial not null primary key' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-05-13 11:29:22 +07:00
2015-06-01 15:56:31 +01:00
public function testAddingTinyInteger ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> tinyInteger ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-03-07 13:28:54 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "foo" smallint not null' , $statements [ 0 ]);
2015-02-18 17:31:18 +02:00
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> tinyInteger ( 'foo' , true );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2015-02-18 17:31:18 +02:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2023-01-05 01:54:52 +03:30
$this -> assertSame ( 'alter table "users" add column "foo" smallserial not null primary key' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-03-07 13:28:54 -06:00
2015-06-01 15:56:31 +01:00
public function testAddingSmallInteger ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> smallInteger ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-05-08 21:30:41 +01:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "foo" smallint not null' , $statements [ 0 ]);
2015-02-18 17:31:18 +02:00
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> smallInteger ( 'foo' , true );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2015-02-18 17:31:18 +02:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2023-01-05 01:54:52 +03:30
$this -> assertSame ( 'alter table "users" add column "foo" smallserial not null primary key' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-05-08 21:30:41 +01:00
2015-06-01 15:56:31 +01:00
public function testAddingFloat ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2023-12-12 23:18:30 +03:30
$blueprint -> float ( 'foo' , 5 );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2023-12-12 23:18:30 +03:30
$this -> assertSame ( 'alter table "users" add column "foo" float(5) not null' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2015-06-01 15:56:31 +01:00
public function testAddingDouble ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2023-12-12 23:18:30 +03:30
$blueprint -> double ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-10-02 11:51:10 -05:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "foo" double precision not null' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-10-02 11:51:10 -05:00
2015-06-01 15:56:31 +01:00
public function testAddingDecimal ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> decimal ( 'foo' , 5 , 2 );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "foo" decimal(5, 2) not null' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2015-06-01 15:56:31 +01:00
public function testAddingBoolean ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> boolean ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "foo" boolean not null' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2015-06-01 15:56:31 +01:00
public function testAddingEnum ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2017-11-21 14:05:17 -02:00
$blueprint -> enum ( 'role' , [ 'member' , 'admin' ]);
2025-08-06 17:04:44 +02:00
$blueprint -> enum ( 'status' , Foo :: cases ());
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2025-08-06 17:04:44 +02:00
$this -> assertCount ( 2 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "role" varchar(255) check ("role" in (\'member\', \'admin\')) not null' , $statements [ 0 ]);
2025-08-06 17:04:44 +02:00
$this -> assertSame ( 'alter table "users" add column "status" varchar(255) check ("status" in (\'bar\')) not null' , $statements [ 1 ]);
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2015-06-01 15:56:31 +01:00
public function testAddingDate ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> date ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2013-01-10 15:28:59 -06:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "foo" date not null' , $statements [ 0 ]);
2017-12-10 00:02:05 +01:00
}
2025-05-07 13:08:59 -05:00
public function testAddingDateWithDefaultCurrent ()
{
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
$blueprint -> date ( 'foo' ) -> useCurrent ();
$statements = $blueprint -> toSql ();
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'alter table "users" add column "foo" date not null default CURRENT_DATE' , $statements [ 0 ]);
}
2017-12-10 00:02:05 +01:00
public function testAddingYear ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2017-12-10 00:02:05 +01:00
$blueprint -> year ( 'birth_year' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2017-12-10 00:02:05 +01:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "birth_year" integer not null' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2015-04-05 15:45:40 +00:00
2025-05-07 13:08:59 -05:00
public function testAddingYearWithDefaultCurrent ()
{
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
$blueprint -> year ( 'birth_year' ) -> useCurrent ();
$statements = $blueprint -> toSql ();
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'alter table "users" add column "birth_year" integer not null default EXTRACT(YEAR FROM CURRENT_DATE)' , $statements [ 0 ]);
}
2015-06-01 15:56:31 +01:00
public function testAddingJson ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> json ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2015-04-05 15:45:40 +00:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "foo" json not null' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2015-04-05 15:45:40 +00:00
2015-06-01 15:56:31 +01:00
public function testAddingJsonb ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> jsonb ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2015-04-05 15:45:40 +00:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "foo" jsonb not null' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2013-01-10 15:28:59 -06:00
2024-06-12 23:07:20 -05:00
#[DataProvider('datetimeAndPrecisionProvider')]
public function testAddingDatetimeMethods ( string $method , string $type , ? int $userPrecision , false | int | null $grammarPrecision , ? int $expected )
2015-06-01 15:56:31 +01:00
{
2024-06-12 23:07:20 -05:00
PostgresBuilder :: defaultTimePrecision ( $grammarPrecision );
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
$blueprint -> { $method }( 'created_at' , $userPrecision );
$statements = $blueprint -> toSql ();
$type = is_null ( $expected ) ? $type : " { $type } ( { $expected } ) " ;
$with = str_contains ( $method , 'Tz' ) ? 'with' : 'without' ;
2017-11-20 13:27:29 -02:00
$this -> assertCount ( 1 , $statements );
2024-06-12 23:07:20 -05:00
$this -> assertSame ( " alter table \" users \" add column \" created_at \" { $type } { $with } time zone not null " , $statements [ 0 ]);
2017-11-20 13:27:29 -02:00
}
2013-01-10 15:28:59 -06:00
2024-06-12 23:07:20 -05:00
#[TestWith(['timestamps'])]
#[TestWith(['timestampsTz'])]
public function testAddingTimestamps ( string $method )
2017-11-20 13:27:29 -02:00
{
2024-06-12 23:07:20 -05:00
PostgresBuilder :: defaultTimePrecision ( 0 );
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
$blueprint -> { $method }();
$statements = $blueprint -> toSql ();
$with = str_contains ( $method , 'Tz' ) ? 'with' : 'without' ;
2024-07-04 18:01:28 +03:30
$this -> assertCount ( 2 , $statements );
$this -> assertSame ([
2024-06-12 23:07:20 -05:00
" alter table \" users \" add column \" created_at \" timestamp(0) { $with } time zone null " ,
" alter table \" users \" add column \" updated_at \" timestamp(0) { $with } time zone null " ,
2024-07-04 18:01:28 +03:30
], $statements );
2015-06-28 00:13:27 +10:00
}
2015-06-01 15:56:31 +01:00
public function testAddingBinary ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-06-01 15:56:31 +01:00
$blueprint -> binary ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2015-06-01 15:56:31 +01:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "foo" bytea not null' , $statements [ 0 ]);
2015-06-01 15:56:31 +01:00
}
2015-10-20 00:31:28 -04:00
public function testAddingUuid ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2015-10-20 00:31:28 -04:00
$blueprint -> uuid ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2015-10-20 00:31:28 -04:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "foo" uuid not null' , $statements [ 0 ]);
2015-10-20 00:31:28 -04:00
}
2021-01-24 19:56:21 -05:00
public function testAddingUuidDefaultsColumnName ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2021-01-24 19:56:21 -05:00
$blueprint -> uuid ();
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2021-01-24 19:56:21 -05:00
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'alter table "users" add column "uuid" uuid not null' , $statements [ 0 ]);
}
2020-06-07 14:41:05 +01:00
public function testAddingForeignUuid ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2020-06-07 14:41:05 +01:00
$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' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2020-06-07 14:41:05 +01:00
$this -> assertInstanceOf ( ForeignIdColumnDefinition :: class , $foreignUuid );
$this -> assertSame ([
2024-07-04 18:01:28 +03:30
'alter table "users" add column "foo" uuid not null' ,
'alter table "users" add column "company_id" uuid not null' ,
2020-06-07 14:41:05 +01:00
'alter table "users" add constraint "users_company_id_foreign" foreign key ("company_id") references "companies" ("id")' ,
2024-07-04 18:01:28 +03:30
'alter table "users" add column "laravel_idea_id" uuid not null' ,
2020-06-07 14:41:05 +01:00
'alter table "users" add constraint "users_laravel_idea_id_foreign" foreign key ("laravel_idea_id") references "laravel_ideas" ("id")' ,
2024-07-04 18:01:28 +03:30
'alter table "users" add column "team_id" uuid not null' ,
2020-06-07 14:41:05 +01:00
'alter table "users" add constraint "users_team_id_foreign" foreign key ("team_id") references "teams" ("id")' ,
2024-07-04 18:01:28 +03:30
'alter table "users" add column "team_column_id" uuid not null' ,
2020-06-07 14:41:05 +01:00
'alter table "users" add constraint "users_team_column_id_foreign" foreign key ("team_column_id") references "teams" ("id")' ,
], $statements );
}
2018-10-12 00:58:53 -04:00
public function testAddingGeneratedAs ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2018-10-12 00:58:53 -04:00
$blueprint -> increments ( 'foo' ) -> generatedAs ();
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2018-10-12 00:58:53 -04:00
$this -> assertCount ( 1 , $statements );
2023-01-05 01:54:52 +03:30
$this -> assertSame ( 'alter table "users" add column "foo" integer not null generated by default as identity primary key' , $statements [ 0 ]);
2018-10-12 00:58:53 -04:00
// With always modifier
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2018-10-12 00:58:53 -04:00
$blueprint -> increments ( 'foo' ) -> generatedAs () -> always ();
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2018-10-12 00:58:53 -04:00
$this -> assertCount ( 1 , $statements );
2023-01-05 01:54:52 +03:30
$this -> assertSame ( 'alter table "users" add column "foo" integer not null generated always as identity primary key' , $statements [ 0 ]);
2018-10-12 00:58:53 -04:00
// With sequence options
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2018-10-12 00:58:53 -04:00
$blueprint -> increments ( 'foo' ) -> generatedAs ( 'increment by 10 start with 100' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2018-10-12 00:58:53 -04:00
$this -> assertCount ( 1 , $statements );
2023-01-05 01:54:52 +03:30
$this -> assertSame ( 'alter table "users" add column "foo" integer not null generated by default as identity (increment by 10 start with 100) primary key' , $statements [ 0 ]);
2018-10-12 00:58:53 -04:00
// Not a primary key
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2018-10-12 00:58:53 -04:00
$blueprint -> integer ( 'foo' ) -> generatedAs ();
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2018-10-12 00:58:53 -04:00
$this -> assertCount ( 1 , $statements );
2023-01-05 01:54:52 +03:30
$this -> assertSame ( 'alter table "users" add column "foo" integer not null generated by default as identity' , $statements [ 0 ]);
2018-10-12 00:58:53 -04:00
}
2019-12-28 13:55:06 +01:00
public function testAddingVirtualAs ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2019-12-28 13:55:06 +01:00
$blueprint -> integer ( 'foo' ) -> nullable ();
$blueprint -> boolean ( 'bar' ) -> virtualAs ( 'foo is not null' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2024-07-04 18:01:28 +03:30
$this -> assertCount ( 2 , $statements );
$this -> assertSame ([
'alter table "users" add column "foo" integer null' ,
2025-10-07 17:02:30 +02:00
'alter table "users" add column "bar" boolean not null generated always as (foo is not null) virtual' ,
2024-07-04 18:01:28 +03:30
], $statements );
2023-11-13 17:39:12 +01:00
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2023-11-13 17:39:12 +01:00
$blueprint -> integer ( 'foo' ) -> nullable ();
$blueprint -> boolean ( 'bar' ) -> virtualAs ( new Expression ( 'foo is not null' ));
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2024-07-04 18:01:28 +03:30
$this -> assertCount ( 2 , $statements );
$this -> assertSame ([
'alter table "users" add column "foo" integer null' ,
2025-10-07 17:02:30 +02:00
'alter table "users" add column "bar" boolean not null generated always as (foo is not null) virtual' ,
2024-07-04 18:01:28 +03:30
], $statements );
2019-12-28 13:55:06 +01:00
}
public function testAddingStoredAs ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2019-12-28 13:55:06 +01:00
$blueprint -> integer ( 'foo' ) -> nullable ();
$blueprint -> boolean ( 'bar' ) -> storedAs ( 'foo is not null' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2024-07-04 18:01:28 +03:30
$this -> assertCount ( 2 , $statements );
$this -> assertSame ([
'alter table "users" add column "foo" integer null' ,
'alter table "users" add column "bar" boolean not null generated always as (foo is not null) stored' ,
], $statements );
2023-11-13 17:39:12 +01:00
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2023-11-13 17:39:12 +01:00
$blueprint -> integer ( 'foo' ) -> nullable ();
$blueprint -> boolean ( 'bar' ) -> storedAs ( new Expression ( 'foo is not null' ));
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2024-07-04 18:01:28 +03:30
$this -> assertCount ( 2 , $statements );
$this -> assertSame ([
'alter table "users" add column "foo" integer null' ,
'alter table "users" add column "bar" boolean not null generated always as (foo is not null) stored' ,
], $statements );
2019-12-28 13:55:06 +01:00
}
2016-03-27 17:38:14 -04:00
public function testAddingIpAddress ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2016-03-27 17:38:14 -04:00
$blueprint -> ipAddress ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2016-03-27 17:38:14 -04:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "foo" inet not null' , $statements [ 0 ]);
2016-03-27 17:38:14 -04:00
}
2020-12-08 14:09:30 -05:00
public function testAddingIpAddressDefaultsColumnName ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2020-12-08 14:09:30 -05:00
$blueprint -> ipAddress ();
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2020-12-08 14:09:30 -05:00
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'alter table "users" add column "ip_address" inet not null' , $statements [ 0 ]);
}
2016-03-27 17:38:14 -04:00
public function testAddingMacAddress ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2016-03-27 17:38:14 -04:00
$blueprint -> macAddress ( 'foo' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2016-03-27 17:38:14 -04:00
2017-05-19 22:41:52 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add column "foo" macaddr not null' , $statements [ 0 ]);
2016-03-27 17:38:14 -04:00
}
2020-12-08 14:09:30 -05:00
public function testAddingMacAddressDefaultsColumnName ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2020-12-08 14:09:30 -05:00
$blueprint -> macAddress ();
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2020-12-08 14:09:30 -05:00
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'alter table "users" add column "mac_address" macaddr not null' , $statements [ 0 ]);
}
2017-09-07 17:46:58 +02:00
public function testCompileForeign ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2017-09-07 17:46:58 +02:00
$blueprint -> foreign ( 'parent_id' ) -> references ( 'id' ) -> on ( 'parents' ) -> onDelete ( 'cascade' ) -> deferrable ();
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2017-09-07 17:46:58 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add constraint "users_parent_id_foreign" foreign key ("parent_id") references "parents" ("id") on delete cascade deferrable' , $statements [ 0 ]);
2017-09-07 17:46:58 +02:00
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2017-09-07 17:46:58 +02:00
$blueprint -> foreign ( 'parent_id' ) -> references ( 'id' ) -> on ( 'parents' ) -> onDelete ( 'cascade' ) -> deferrable ( false ) -> initiallyImmediate ();
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2017-09-07 17:46:58 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add constraint "users_parent_id_foreign" foreign key ("parent_id") references "parents" ("id") on delete cascade not deferrable' , $statements [ 0 ]);
2017-09-07 17:46:58 +02:00
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2017-09-07 17:46:58 +02:00
$blueprint -> foreign ( 'parent_id' ) -> references ( 'id' ) -> on ( 'parents' ) -> onDelete ( 'cascade' ) -> deferrable () -> initiallyImmediate ( false );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2017-09-07 17:46:58 +02:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add constraint "users_parent_id_foreign" foreign key ("parent_id") references "parents" ("id") on delete cascade deferrable initially deferred' , $statements [ 0 ]);
2018-12-08 02:52:52 +13:00
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'users' );
2018-12-08 02:52:52 +13:00
$blueprint -> foreign ( 'parent_id' ) -> references ( 'id' ) -> on ( 'parents' ) -> onDelete ( 'cascade' ) -> deferrable () -> notValid ();
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2018-12-08 02:52:52 +13:00
$this -> assertCount ( 1 , $statements );
2019-08-27 14:48:17 +02:00
$this -> assertSame ( 'alter table "users" add constraint "users_parent_id_foreign" foreign key ("parent_id") references "parents" ("id") on delete cascade deferrable not valid' , $statements [ 0 ]);
2017-09-07 17:46:58 +02:00
}
2017-09-07 05:55:45 +03:00
public function testAddingGeometry ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'geo' );
2017-11-02 04:38:45 -02:00
$blueprint -> geometry ( 'coordinates' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2018-08-26 09:51:44 -04:00
$this -> assertCount ( 1 , $statements );
2024-01-18 20:57:15 +03:30
$this -> assertSame ( 'alter table "geo" add column "coordinates" geometry not null' , $statements [ 0 ]);
}
public function testAddingGeography ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'geo' );
2024-01-18 20:57:15 +03:30
$blueprint -> geography ( 'coordinates' , 'pointzm' , 4269 );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2024-01-18 20:57:15 +03:30
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'alter table "geo" add column "coordinates" geography(pointzm,4269) not null' , $statements [ 0 ]);
2017-09-07 05:55:45 +03:00
}
public function testAddingPoint ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'geo' );
2024-01-18 20:57:15 +03:30
$blueprint -> geometry ( 'coordinates' , 'point' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2024-01-18 20:57:15 +03:30
$this -> assertCount ( 1 , $statements );
$this -> assertSame ( 'alter table "geo" add column "coordinates" geometry(point) not null' , $statements [ 0 ]);
}
public function testAddingPointWithSrid ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'geo' );
2024-01-18 20:57:15 +03:30
$blueprint -> geometry ( 'coordinates' , 'point' , 4269 );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2017-09-07 05:55:45 +03:00
$this -> assertCount ( 1 , $statements );
2024-01-18 20:57:15 +03:30
$this -> assertSame ( 'alter table "geo" add column "coordinates" geometry(point,4269) not null' , $statements [ 0 ]);
2017-09-07 05:55:45 +03:00
}
2017-11-02 04:38:45 -02:00
public function testAddingLineString ()
2017-09-07 05:55:45 +03:00
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'geo' );
2024-01-18 20:57:15 +03:30
$blueprint -> geometry ( 'coordinates' , 'linestring' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2017-09-07 05:55:45 +03:00
$this -> assertCount ( 1 , $statements );
2024-01-18 20:57:15 +03:30
$this -> assertSame ( 'alter table "geo" add column "coordinates" geometry(linestring) not null' , $statements [ 0 ]);
2017-09-07 05:55:45 +03:00
}
public function testAddingPolygon ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'geo' );
2024-01-18 20:57:15 +03:30
$blueprint -> geometry ( 'coordinates' , 'polygon' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2017-09-07 05:55:45 +03:00
$this -> assertCount ( 1 , $statements );
2024-01-18 20:57:15 +03:30
$this -> assertSame ( 'alter table "geo" add column "coordinates" geometry(polygon) not null' , $statements [ 0 ]);
2017-09-07 05:55:45 +03:00
}
public function testAddingGeometryCollection ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'geo' );
2024-01-18 20:57:15 +03:30
$blueprint -> geometry ( 'coordinates' , 'geometrycollection' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2017-09-07 05:55:45 +03:00
$this -> assertCount ( 1 , $statements );
2024-01-18 20:57:15 +03:30
$this -> assertSame ( 'alter table "geo" add column "coordinates" geometry(geometrycollection) not null' , $statements [ 0 ]);
2017-09-07 05:55:45 +03:00
}
2017-11-02 04:38:45 -02:00
public function testAddingMultiPoint ()
2017-09-07 05:55:45 +03:00
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'geo' );
2024-01-18 20:57:15 +03:30
$blueprint -> geometry ( 'coordinates' , 'multipoint' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2017-09-07 05:55:45 +03:00
$this -> assertCount ( 1 , $statements );
2024-01-18 20:57:15 +03:30
$this -> assertSame ( 'alter table "geo" add column "coordinates" geometry(multipoint) not null' , $statements [ 0 ]);
2017-09-07 05:55:45 +03:00
}
2017-11-02 04:38:45 -02:00
public function testAddingMultiLineString ()
2017-09-07 05:55:45 +03:00
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'geo' );
2024-01-18 20:57:15 +03:30
$blueprint -> geometry ( 'coordinates' , 'multilinestring' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2017-09-07 05:55:45 +03:00
$this -> assertCount ( 1 , $statements );
2024-01-18 20:57:15 +03:30
$this -> assertSame ( 'alter table "geo" add column "coordinates" geometry(multilinestring) not null' , $statements [ 0 ]);
2017-09-07 05:55:45 +03:00
}
public function testAddingMultiPolygon ()
{
2024-06-12 23:07:20 -05:00
$blueprint = new Blueprint ( $this -> getConnection (), 'geo' );
2024-01-18 20:57:15 +03:30
$blueprint -> geometry ( 'coordinates' , 'multipolygon' );
2024-06-12 23:07:20 -05:00
$statements = $blueprint -> toSql ();
2017-09-07 05:55:45 +03:00
$this -> assertCount ( 1 , $statements );
2024-01-18 20:57:15 +03:30
$this -> assertSame ( 'alter table "geo" add column "coordinates" geometry(multipolygon) not null' , $statements [ 0 ]);
2017-09-07 05:55:45 +03:00
}
2021-01-13 15:20:19 +01:00
public function testCreateDatabase ()
{
$connection = $this -> getConnection ();
$connection -> shouldReceive ( 'getConfig' ) -> once () -> once () -> with ( 'charset' ) -> andReturn ( 'utf8_foo' );
2025-02-12 20:00:57 +03:30
$statement = $this -> getGrammar ( $connection ) -> compileCreateDatabase ( 'my_database_a' );
2021-01-13 15:20:19 +01:00
$this -> assertSame (
'create database "my_database_a" encoding "utf8_foo"' ,
$statement
);
$connection = $this -> getConnection ();
$connection -> shouldReceive ( 'getConfig' ) -> once () -> once () -> with ( 'charset' ) -> andReturn ( 'utf8_bar' );
2025-02-12 20:00:57 +03:30
$statement = $this -> getGrammar ( $connection ) -> compileCreateDatabase ( 'my_database_b' );
2021-01-13 15:20:19 +01:00
$this -> assertSame (
'create database "my_database_b" encoding "utf8_bar"' ,
$statement
);
}
public function testDropDatabaseIfExists ()
{
$statement = $this -> getGrammar () -> compileDropDatabaseIfExists ( 'my_database_a' );
$this -> assertSame (
'drop database if exists "my_database_a"' ,
$statement
);
$statement = $this -> getGrammar () -> compileDropDatabaseIfExists ( 'my_database_b' );
$this -> assertSame (
'drop database if exists "my_database_b"' ,
$statement
);
}
2017-03-31 20:57:30 +00:00
public function testDropAllTablesEscapesTableNames ()
{
$statement = $this -> getGrammar () -> compileDropAllTables ([ 'alpha' , 'beta' , 'gamma' ]);
2025-02-25 17:38:31 +03:30
$this -> assertSame ( 'drop table "alpha", "beta", "gamma" cascade' , $statement );
2017-03-31 20:57:30 +00:00
}
2018-06-08 11:40:49 +02:00
public function testDropAllViewsEscapesTableNames ()
{
$statement = $this -> getGrammar () -> compileDropAllViews ([ 'alpha' , 'beta' , 'gamma' ]);
2025-02-25 17:38:31 +03:30
$this -> assertSame ( 'drop view "alpha", "beta", "gamma" cascade' , $statement );
2018-06-08 11:40:49 +02:00
}
2019-04-30 11:46:29 -04:00
public function testDropAllTypesEscapesTableNames ()
{
$statement = $this -> getGrammar () -> compileDropAllTypes ([ 'alpha' , 'beta' , 'gamma' ]);
2025-02-25 17:38:31 +03:30
$this -> assertSame ( 'drop type "alpha", "beta", "gamma" cascade' , $statement );
2019-04-30 11:46:29 -04:00
}
2025-02-28 01:50:28 +03:30
public function testDropAllTablesWithPrefixAndSchema ()
{
$connection = $this -> getConnection ( prefix : 'prefix_' );
$statement = $this -> getGrammar ( $connection ) -> compileDropAllTables ([ 'schema.alpha' , 'schema.beta' , 'schema.gamma' ]);
$this -> assertSame ( 'drop table "schema"."alpha", "schema"."beta", "schema"."gamma" cascade' , $statement );
}
public function testDropAllViewsWithPrefixAndSchema ()
{
$connection = $this -> getConnection ( prefix : 'prefix_' );
$statement = $this -> getGrammar ( $connection ) -> compileDropAllViews ([ 'schema.alpha' , 'schema.beta' , 'schema.gamma' ]);
$this -> assertSame ( 'drop view "schema"."alpha", "schema"."beta", "schema"."gamma" cascade' , $statement );
}
public function testDropAllTypesWithPrefixAndSchema ()
{
$connection = $this -> getConnection ( prefix : 'prefix_' );
$statement = $this -> getGrammar ( $connection ) -> compileDropAllTypes ([ 'schema.alpha' , 'schema.beta' , 'schema.gamma' ]);
$this -> assertSame ( 'drop type "schema"."alpha", "schema"."beta", "schema"."gamma" cascade' , $statement );
}
public function testDropAllDomainsWithPrefixAndSchema ()
{
$connection = $this -> getConnection ( prefix : 'prefix_' );
$statement = $this -> getGrammar ( $connection ) -> compileDropAllDomains ([ 'schema.alpha' , 'schema.beta' , 'schema.gamma' ]);
$this -> assertSame ( 'drop domain "schema"."alpha", "schema"."beta", "schema"."gamma" cascade' , $statement );
}
2023-10-26 21:50:57 +03:30
public function testCompileColumns ()
2020-12-08 17:07:49 -05:00
{
2024-05-11 03:24:21 +08:00
$connection = $this -> getConnection ();
$connection -> shouldReceive ( 'getServerVersion' ) -> once () -> andReturn ( '12.0.0' );
2025-02-12 20:00:57 +03:30
$statement = $connection -> getSchemaGrammar () -> compileColumns ( 'public' , 'table' );
2020-12-08 17:07:49 -05:00
2023-10-26 21:50:57 +03:30
$this -> assertStringContainsString ( " where c.relname = 'table' and n.nspname = 'public' " , $statement );
2020-12-08 17:07:49 -05:00
}
2024-06-12 23:07:20 -05:00
protected function getConnection (
? PostgresGrammar $grammar = null ,
2025-02-28 01:50:28 +03:30
? PostgresBuilder $builder = null ,
string $prefix = ''
2024-06-12 23:07:20 -05:00
) {
2025-02-12 20:00:57 +03:30
$connection = m :: mock ( Connection :: class )
2025-02-28 01:50:28 +03:30
-> shouldReceive ( 'getTablePrefix' ) -> andReturn ( $prefix )
2025-02-12 20:00:57 +03:30
-> shouldReceive ( 'getConfig' ) -> with ( 'prefix_indexes' ) -> andReturn ( null )
-> getMock ();
$grammar ? ? = $this -> getGrammar ( $connection );
2024-06-12 23:07:20 -05:00
$builder ? ? = $this -> getBuilder ();
2025-02-12 20:00:57 +03:30
return $connection
2024-06-12 23:07:20 -05:00
-> shouldReceive ( 'getSchemaGrammar' ) -> andReturn ( $grammar )
-> shouldReceive ( 'getSchemaBuilder' ) -> andReturn ( $builder )
-> getMock ();
2015-06-01 15:56:31 +01:00
}
2025-02-12 20:00:57 +03:30
public function getGrammar ( ? Connection $connection = null )
2015-06-01 15:56:31 +01:00
{
2025-02-12 20:00:57 +03:30
return new PostgresGrammar ( $connection ? ? $this -> getConnection ());
2015-06-01 15:56:31 +01:00
}
2018-06-08 11:42:58 +02:00
2024-06-12 23:07:20 -05:00
public function getBuilder ()
{
return mock ( PostgresBuilder :: class );
}
/** @return list<array{method: string, type: string, user: int|null, grammar: false|int|null, expected: int|null}> */
public static function datetimeAndPrecisionProvider () : array
{
$methods = [
[ 'method' => 'datetime' , 'type' => 'timestamp' ],
[ 'method' => 'datetimeTz' , 'type' => 'timestamp' ],
[ 'method' => 'timestamp' , 'type' => 'timestamp' ],
[ 'method' => 'timestampTz' , 'type' => 'timestamp' ],
[ 'method' => 'time' , 'type' => 'time' ],
[ 'method' => 'timeTz' , 'type' => 'time' ],
];
$precisions = [
'user can override grammar default' => [ 'userPrecision' => 1 , 'grammarPrecision' => null , 'expected' => 1 ],
'fallback to grammar default' => [ 'userPrecision' => null , 'grammarPrecision' => 5 , 'expected' => 5 ],
'fallback to database default' => [ 'userPrecision' => null , 'grammarPrecision' => null , 'expected' => null ],
];
$result = [];
foreach ( $methods as $datetime ) {
foreach ( $precisions as $precision ) {
$result [] = array_merge ( $datetime , $precision );
}
}
return $result ;
}
2018-06-08 11:42:58 +02:00
public function testGrammarsAreMacroable ()
{
// compileReplace macro.
$this -> getGrammar () :: macro ( 'compileReplace' , function () {
return true ;
});
$c = $this -> getGrammar () :: compileReplace ();
$this -> assertTrue ( $c );
}
2013-05-08 21:30:41 +01:00
}