12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100 |
- <?php
- namespace Illuminate\Tests\Database;
- use Illuminate\Database\Connection;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Database\Schema\ForeignIdColumnDefinition;
- use Illuminate\Database\Schema\Grammars\PostgresGrammar;
- use Mockery as m;
- use PHPUnit\Framework\TestCase;
- class DatabasePostgresSchemaGrammarTest extends TestCase
- {
- protected function tearDown(): void
- {
- m::close();
- }
- public function testBasicCreateTable()
- {
- $blueprint = new Blueprint('users');
- $blueprint->create();
- $blueprint->increments('id');
- $blueprint->string('email');
- $blueprint->string('name')->collation('nb_NO.utf8');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('create table "users" ("id" serial primary key not null, "email" varchar(255) not null, "name" varchar(255) collate "nb_NO.utf8" 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->assertSame('alter table "users" add column "id" serial primary key not null, add column "email" varchar(255) not null', $statements[0]);
- }
- public function testCreateTableWithAutoIncrementStartingValue()
- {
- $blueprint = new Blueprint('users');
- $blueprint->create();
- $blueprint->increments('id')->startingValue(1000);
- $blueprint->string('email');
- $blueprint->string('name')->collation('nb_NO.utf8');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(2, $statements);
- $this->assertSame('create table "users" ("id" serial primary key not null, "email" varchar(255) not null, "name" varchar(255) collate "nb_NO.utf8" not null)', $statements[0]);
- $this->assertSame('alter sequence users_id_seq restart with 1000', $statements[1]);
- }
- public function testCreateTableAndCommentColumn()
- {
- $blueprint = new Blueprint('users');
- $blueprint->create();
- $blueprint->increments('id');
- $blueprint->string('email')->comment('my first comment');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(2, $statements);
- $this->assertSame('create table "users" ("id" serial primary key not null, "email" varchar(255) not null)', $statements[0]);
- $this->assertSame('comment on column "users"."email" is \'my first comment\'', $statements[1]);
- }
- public function testCreateTemporaryTable()
- {
- $blueprint = new Blueprint('users');
- $blueprint->create();
- $blueprint->temporary();
- $blueprint->increments('id');
- $blueprint->string('email');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('create temporary table "users" ("id" serial primary key not null, "email" varchar(255) not null)', $statements[0]);
- }
- public function testDropTable()
- {
- $blueprint = new Blueprint('users');
- $blueprint->drop();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('drop table "users"', $statements[0]);
- }
- public function testDropTableIfExists()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dropIfExists();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('drop table if exists "users"', $statements[0]);
- }
- public function testDropColumn()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dropColumn('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" drop column "foo"', $statements[0]);
- $blueprint = new Blueprint('users');
- $blueprint->dropColumn(['foo', 'bar']);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" drop column "foo", drop column "bar"', $statements[0]);
- $blueprint = new Blueprint('users');
- $blueprint->dropColumn('foo', 'bar');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" drop column "foo", drop column "bar"', $statements[0]);
- }
- public function testDropPrimary()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dropPrimary();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" drop constraint "users_pkey"', $statements[0]);
- }
- public function testDropUnique()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dropUnique('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" drop constraint "foo"', $statements[0]);
- }
- public function testDropIndex()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dropIndex('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('drop index "foo"', $statements[0]);
- }
- public function testDropSpatialIndex()
- {
- $blueprint = new Blueprint('geo');
- $blueprint->dropSpatialIndex(['coordinates']);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('drop index "geo_coordinates_spatialindex"', $statements[0]);
- }
- public function testDropForeign()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dropForeign('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" drop constraint "foo"', $statements[0]);
- }
- public function testDropTimestamps()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dropTimestamps();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" drop column "created_at", drop column "updated_at"', $statements[0]);
- }
- public function testDropTimestampsTz()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dropTimestampsTz();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" drop column "created_at", drop column "updated_at"', $statements[0]);
- }
- public function testDropMorphs()
- {
- $blueprint = new Blueprint('photos');
- $blueprint->dropMorphs('imageable');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(2, $statements);
- $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]);
- }
- public function testRenameTable()
- {
- $blueprint = new Blueprint('users');
- $blueprint->rename('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" rename to "foo"', $statements[0]);
- }
- public function testRenameIndex()
- {
- $blueprint = new Blueprint('users');
- $blueprint->renameIndex('foo', 'bar');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter index "foo" rename to "bar"', $statements[0]);
- }
- public function testAddingPrimaryKey()
- {
- $blueprint = new Blueprint('users');
- $blueprint->primary('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add primary key ("foo")', $statements[0]);
- }
- public function testAddingUniqueKey()
- {
- $blueprint = new Blueprint('users');
- $blueprint->unique('foo', 'bar');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add constraint "bar" unique ("foo")', $statements[0]);
- }
- public function testAddingIndex()
- {
- $blueprint = new Blueprint('users');
- $blueprint->index(['foo', 'bar'], 'baz');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('create index "baz" on "users" ("foo", "bar")', $statements[0]);
- }
- public function testAddingIndexWithAlgorithm()
- {
- $blueprint = new Blueprint('users');
- $blueprint->index(['foo', 'bar'], 'baz', 'hash');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('create index "baz" on "users" using hash ("foo", "bar")', $statements[0]);
- }
- public function testAddingFulltextIndex()
- {
- $blueprint = new Blueprint('users');
- $blueprint->fulltext('body');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('create index "users_body_fulltext" on "users" using gin ((to_tsvector(\'english\', "body")))', $statements[0]);
- }
- public function testAddingFulltextIndexMultipleColumns()
- {
- $blueprint = new Blueprint('users');
- $blueprint->fulltext(['body', 'title']);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $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]);
- }
- public function testAddingFulltextIndexWithLanguage()
- {
- $blueprint = new Blueprint('users');
- $blueprint->fulltext('body')->language('spanish');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('create index "users_body_fulltext" on "users" using gin ((to_tsvector(\'spanish\', "body")))', $statements[0]);
- }
- public function testAddingFulltextIndexWithFluency()
- {
- $blueprint = new Blueprint('users');
- $blueprint->string('body')->fulltext();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(2, $statements);
- $this->assertSame('create index "users_body_fulltext" on "users" using gin ((to_tsvector(\'english\', "body")))', $statements[1]);
- }
- public function testAddingSpatialIndex()
- {
- $blueprint = new Blueprint('geo');
- $blueprint->spatialIndex('coordinates');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('create index "geo_coordinates_spatialindex" on "geo" using gist ("coordinates")', $statements[0]);
- }
- public function testAddingFluentSpatialIndex()
- {
- $blueprint = new Blueprint('geo');
- $blueprint->point('coordinates')->spatialIndex();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(2, $statements);
- $this->assertSame('create index "geo_coordinates_spatialindex" on "geo" using gist ("coordinates")', $statements[1]);
- }
- public function testAddingRawIndex()
- {
- $blueprint = new Blueprint('users');
- $blueprint->rawIndex('(function(column))', 'raw_index');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('create index "raw_index" on "users" ((function(column)))', $statements[0]);
- }
- public function testAddingIncrementingID()
- {
- $blueprint = new Blueprint('users');
- $blueprint->increments('id');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "id" serial primary key not null', $statements[0]);
- }
- public function testAddingSmallIncrementingID()
- {
- $blueprint = new Blueprint('users');
- $blueprint->smallIncrements('id');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "id" smallserial primary key not null', $statements[0]);
- }
- public function testAddingMediumIncrementingID()
- {
- $blueprint = new Blueprint('users');
- $blueprint->mediumIncrements('id');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "id" serial primary key not null', $statements[0]);
- }
- public function testAddingID()
- {
- $blueprint = new Blueprint('users');
- $blueprint->id();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "id" bigserial primary key not null', $statements[0]);
- $blueprint = new Blueprint('users');
- $blueprint->id('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" bigserial primary key not null', $statements[0]);
- }
- public function testAddingForeignID()
- {
- $blueprint = new Blueprint('users');
- $foreignId = $blueprint->foreignId('foo');
- $blueprint->foreignId('company_id')->constrained();
- $blueprint->foreignId('laravel_idea_id')->constrained();
- $blueprint->foreignId('team_id')->references('id')->on('teams');
- $blueprint->foreignId('team_column_id')->constrained('teams');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertInstanceOf(ForeignIdColumnDefinition::class, $foreignId);
- $this->assertSame([
- 'alter table "users" add column "foo" bigint not null, add column "company_id" bigint not null, add column "laravel_idea_id" bigint not null, add column "team_id" bigint not null, add column "team_column_id" bigint not null',
- 'alter table "users" add constraint "users_company_id_foreign" foreign key ("company_id") references "companies" ("id")',
- 'alter table "users" add constraint "users_laravel_idea_id_foreign" foreign key ("laravel_idea_id") references "laravel_ideas" ("id")',
- 'alter table "users" add constraint "users_team_id_foreign" foreign key ("team_id") references "teams" ("id")',
- 'alter table "users" add constraint "users_team_column_id_foreign" foreign key ("team_column_id") references "teams" ("id")',
- ], $statements);
- }
- public function testAddingBigIncrementingID()
- {
- $blueprint = new Blueprint('users');
- $blueprint->bigIncrements('id');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "id" bigserial primary key not null', $statements[0]);
- }
- public function testAddingString()
- {
- $blueprint = new Blueprint('users');
- $blueprint->string('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" varchar(255) not null', $statements[0]);
- $blueprint = new Blueprint('users');
- $blueprint->string('foo', 100);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" varchar(100) not null', $statements[0]);
- $blueprint = new Blueprint('users');
- $blueprint->string('foo', 100)->nullable()->default('bar');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" varchar(100) null default \'bar\'', $statements[0]);
- }
- public function testAddingText()
- {
- $blueprint = new Blueprint('users');
- $blueprint->text('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" text not null', $statements[0]);
- }
- public function testAddingBigInteger()
- {
- $blueprint = new Blueprint('users');
- $blueprint->bigInteger('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" bigint not null', $statements[0]);
- $blueprint = new Blueprint('users');
- $blueprint->bigInteger('foo', true);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" bigserial primary key not null', $statements[0]);
- }
- public function testAddingInteger()
- {
- $blueprint = new Blueprint('users');
- $blueprint->integer('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" integer not null', $statements[0]);
- $blueprint = new Blueprint('users');
- $blueprint->integer('foo', true);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" serial primary key not null', $statements[0]);
- }
- public function testAddingMediumInteger()
- {
- $blueprint = new Blueprint('users');
- $blueprint->mediumInteger('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" integer not null', $statements[0]);
- $blueprint = new Blueprint('users');
- $blueprint->mediumInteger('foo', true);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" serial primary key not null', $statements[0]);
- }
- public function testAddingTinyInteger()
- {
- $blueprint = new Blueprint('users');
- $blueprint->tinyInteger('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" smallint not null', $statements[0]);
- $blueprint = new Blueprint('users');
- $blueprint->tinyInteger('foo', true);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" smallserial primary key not null', $statements[0]);
- }
- public function testAddingSmallInteger()
- {
- $blueprint = new Blueprint('users');
- $blueprint->smallInteger('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" smallint not null', $statements[0]);
- $blueprint = new Blueprint('users');
- $blueprint->smallInteger('foo', true);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" smallserial primary key not null', $statements[0]);
- }
- public function testAddingFloat()
- {
- $blueprint = new Blueprint('users');
- $blueprint->float('foo', 5, 2);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" double precision not null', $statements[0]);
- }
- public function testAddingDouble()
- {
- $blueprint = new Blueprint('users');
- $blueprint->double('foo', 15, 8);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" double precision not null', $statements[0]);
- }
- public function testAddingDecimal()
- {
- $blueprint = new Blueprint('users');
- $blueprint->decimal('foo', 5, 2);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" decimal(5, 2) not null', $statements[0]);
- }
- public function testAddingBoolean()
- {
- $blueprint = new Blueprint('users');
- $blueprint->boolean('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" boolean not null', $statements[0]);
- }
- public function testAddingEnum()
- {
- $blueprint = new Blueprint('users');
- $blueprint->enum('role', ['member', 'admin']);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "role" varchar(255) check ("role" in (\'member\', \'admin\')) not null', $statements[0]);
- }
- public function testAddingDate()
- {
- $blueprint = new Blueprint('users');
- $blueprint->date('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" date not null', $statements[0]);
- }
- public function testAddingYear()
- {
- $blueprint = new Blueprint('users');
- $blueprint->year('birth_year');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "birth_year" integer not null', $statements[0]);
- }
- public function testAddingJson()
- {
- $blueprint = new Blueprint('users');
- $blueprint->json('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" json not null', $statements[0]);
- }
- public function testAddingJsonb()
- {
- $blueprint = new Blueprint('users');
- $blueprint->jsonb('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" jsonb not null', $statements[0]);
- }
- public function testAddingDateTime()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dateTime('created_at');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" timestamp(0) without time zone not null', $statements[0]);
- }
- public function testAddingDateTimeWithPrecision()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dateTime('created_at', 1);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" timestamp(1) without time zone not null', $statements[0]);
- }
- public function testAddingDateTimeWithNullPrecision()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dateTime('created_at', null);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" timestamp without time zone not null', $statements[0]);
- }
- public function testAddingDateTimeTz()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dateTimeTz('created_at');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" timestamp(0) with time zone not null', $statements[0]);
- }
- public function testAddingDateTimeTzWithPrecision()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dateTimeTz('created_at', 1);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" timestamp(1) with time zone not null', $statements[0]);
- }
- public function testAddingDateTimeTzWithNullPrecision()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dateTimeTz('created_at', null);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" timestamp with time zone not null', $statements[0]);
- }
- public function testAddingTime()
- {
- $blueprint = new Blueprint('users');
- $blueprint->time('created_at');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" time(0) without time zone not null', $statements[0]);
- }
- public function testAddingTimeWithPrecision()
- {
- $blueprint = new Blueprint('users');
- $blueprint->time('created_at', 1);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" time(1) without time zone not null', $statements[0]);
- }
- public function testAddingTimeWithNullPrecision()
- {
- $blueprint = new Blueprint('users');
- $blueprint->time('created_at', null);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" time without time zone not null', $statements[0]);
- }
- public function testAddingTimeTz()
- {
- $blueprint = new Blueprint('users');
- $blueprint->timeTz('created_at');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" time(0) with time zone not null', $statements[0]);
- }
- public function testAddingTimeTzWithPrecision()
- {
- $blueprint = new Blueprint('users');
- $blueprint->timeTz('created_at', 1);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" time(1) with time zone not null', $statements[0]);
- }
- public function testAddingTimeTzWithNullPrecision()
- {
- $blueprint = new Blueprint('users');
- $blueprint->timeTz('created_at', null);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" time with time zone not null', $statements[0]);
- }
- public function testAddingTimestamp()
- {
- $blueprint = new Blueprint('users');
- $blueprint->timestamp('created_at');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" timestamp(0) without time zone not null', $statements[0]);
- }
- public function testAddingTimestampWithPrecision()
- {
- $blueprint = new Blueprint('users');
- $blueprint->timestamp('created_at', 1);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" timestamp(1) without time zone not null', $statements[0]);
- }
- public function testAddingTimestampWithNullPrecision()
- {
- $blueprint = new Blueprint('users');
- $blueprint->timestamp('created_at', null);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" timestamp without time zone not null', $statements[0]);
- }
- public function testAddingTimestampTz()
- {
- $blueprint = new Blueprint('users');
- $blueprint->timestampTz('created_at');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" timestamp(0) with time zone not null', $statements[0]);
- }
- public function testAddingTimestampTzWithPrecision()
- {
- $blueprint = new Blueprint('users');
- $blueprint->timestampTz('created_at', 1);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" timestamp(1) with time zone not null', $statements[0]);
- }
- public function testAddingTimestampTzWithNullPrecision()
- {
- $blueprint = new Blueprint('users');
- $blueprint->timestampTz('created_at', null);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" timestamp with time zone not null', $statements[0]);
- }
- public function testAddingTimestamps()
- {
- $blueprint = new Blueprint('users');
- $blueprint->timestamps();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" timestamp(0) without time zone null, add column "updated_at" timestamp(0) without time zone null', $statements[0]);
- }
- public function testAddingTimestampsTz()
- {
- $blueprint = new Blueprint('users');
- $blueprint->timestampsTz();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" timestamp(0) with time zone null, add column "updated_at" timestamp(0) with time zone null', $statements[0]);
- }
- public function testAddingBinary()
- {
- $blueprint = new Blueprint('users');
- $blueprint->binary('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" bytea not null', $statements[0]);
- }
- public function testAddingUuid()
- {
- $blueprint = new Blueprint('users');
- $blueprint->uuid('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" uuid not null', $statements[0]);
- }
- public function testAddingForeignUuid()
- {
- $blueprint = new Blueprint('users');
- $foreignUuid = $blueprint->foreignUuid('foo');
- $blueprint->foreignUuid('company_id')->constrained();
- $blueprint->foreignUuid('laravel_idea_id')->constrained();
- $blueprint->foreignUuid('team_id')->references('id')->on('teams');
- $blueprint->foreignUuid('team_column_id')->constrained('teams');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertInstanceOf(ForeignIdColumnDefinition::class, $foreignUuid);
- $this->assertSame([
- 'alter table "users" add column "foo" uuid not null, add column "company_id" uuid not null, add column "laravel_idea_id" uuid not null, add column "team_id" uuid not null, add column "team_column_id" uuid not null',
- 'alter table "users" add constraint "users_company_id_foreign" foreign key ("company_id") references "companies" ("id")',
- 'alter table "users" add constraint "users_laravel_idea_id_foreign" foreign key ("laravel_idea_id") references "laravel_ideas" ("id")',
- 'alter table "users" add constraint "users_team_id_foreign" foreign key ("team_id") references "teams" ("id")',
- 'alter table "users" add constraint "users_team_column_id_foreign" foreign key ("team_column_id") references "teams" ("id")',
- ], $statements);
- }
- public function testAddingGeneratedAs()
- {
- $blueprint = new Blueprint('users');
- $blueprint->increments('foo')->generatedAs();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" integer 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->assertSame('alter table "users" add column "foo" integer 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->assertSame('alter table "users" add column "foo" integer 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();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" integer generated by default as identity not null', $statements[0]);
- }
- public function testAddingVirtualAs()
- {
- $blueprint = new Blueprint('users');
- $blueprint->integer('foo')->nullable();
- $blueprint->boolean('bar')->virtualAs('foo is not null');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" integer null, add column "bar" boolean not null generated always as (foo is not null)', $statements[0]);
- }
- public function testAddingStoredAs()
- {
- $blueprint = new Blueprint('users');
- $blueprint->integer('foo')->nullable();
- $blueprint->boolean('bar')->storedAs('foo is not null');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" integer null, add column "bar" boolean not null generated always as (foo is not null) stored', $statements[0]);
- }
- public function testAddingIpAddress()
- {
- $blueprint = new Blueprint('users');
- $blueprint->ipAddress('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" inet not null', $statements[0]);
- }
- public function testAddingMacAddress()
- {
- $blueprint = new Blueprint('users');
- $blueprint->macAddress('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" macaddr not null', $statements[0]);
- }
- public function testCompileForeign()
- {
- $blueprint = new Blueprint('users');
- $blueprint->foreign('parent_id')->references('id')->on('parents')->onDelete('cascade')->deferrable();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add constraint "users_parent_id_foreign" foreign key ("parent_id") references "parents" ("id") on delete cascade deferrable', $statements[0]);
- $blueprint = new Blueprint('users');
- $blueprint->foreign('parent_id')->references('id')->on('parents')->onDelete('cascade')->deferrable(false)->initiallyImmediate();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $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]);
- $blueprint = new Blueprint('users');
- $blueprint->foreign('parent_id')->references('id')->on('parents')->onDelete('cascade')->deferrable()->initiallyImmediate(false);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $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]);
- $blueprint = new Blueprint('users');
- $blueprint->foreign('parent_id')->references('id')->on('parents')->onDelete('cascade')->deferrable()->notValid();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $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]);
- }
- public function testAddingGeometry()
- {
- $blueprint = new Blueprint('geo');
- $blueprint->geometry('coordinates');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "geo" add column "coordinates" geography(geometry, 4326) not null', $statements[0]);
- }
- public function testAddingPoint()
- {
- $blueprint = new Blueprint('geo');
- $blueprint->point('coordinates');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "geo" add column "coordinates" geography(point, 4326) not null', $statements[0]);
- }
- public function testAddingLineString()
- {
- $blueprint = new Blueprint('geo');
- $blueprint->linestring('coordinates');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "geo" add column "coordinates" geography(linestring, 4326) not null', $statements[0]);
- }
- public function testAddingPolygon()
- {
- $blueprint = new Blueprint('geo');
- $blueprint->polygon('coordinates');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "geo" add column "coordinates" geography(polygon, 4326) not null', $statements[0]);
- }
- public function testAddingGeometryCollection()
- {
- $blueprint = new Blueprint('geo');
- $blueprint->geometrycollection('coordinates');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "geo" add column "coordinates" geography(geometrycollection, 4326) not null', $statements[0]);
- }
- public function testAddingMultiPoint()
- {
- $blueprint = new Blueprint('geo');
- $blueprint->multipoint('coordinates');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "geo" add column "coordinates" geography(multipoint, 4326) not null', $statements[0]);
- }
- public function testAddingMultiLineString()
- {
- $blueprint = new Blueprint('geo');
- $blueprint->multilinestring('coordinates');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "geo" add column "coordinates" geography(multilinestring, 4326) not null', $statements[0]);
- }
- public function testAddingMultiPolygon()
- {
- $blueprint = new Blueprint('geo');
- $blueprint->multipolygon('coordinates');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "geo" add column "coordinates" geography(multipolygon, 4326) not null', $statements[0]);
- }
- public function testCreateDatabase()
- {
- $connection = $this->getConnection();
- $connection->shouldReceive('getConfig')->once()->once()->with('charset')->andReturn('utf8_foo');
- $statement = $this->getGrammar()->compileCreateDatabase('my_database_a', $connection);
- $this->assertSame(
- 'create database "my_database_a" encoding "utf8_foo"',
- $statement
- );
- $connection = $this->getConnection();
- $connection->shouldReceive('getConfig')->once()->once()->with('charset')->andReturn('utf8_bar');
- $statement = $this->getGrammar()->compileCreateDatabase('my_database_b', $connection);
- $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
- );
- }
- public function testDropAllTablesEscapesTableNames()
- {
- $statement = $this->getGrammar()->compileDropAllTables(['alpha', 'beta', 'gamma']);
- $this->assertSame('drop table "alpha","beta","gamma" cascade', $statement);
- }
- public function testDropAllViewsEscapesTableNames()
- {
- $statement = $this->getGrammar()->compileDropAllViews(['alpha', 'beta', 'gamma']);
- $this->assertSame('drop view "alpha","beta","gamma" cascade', $statement);
- }
- public function testDropAllTypesEscapesTableNames()
- {
- $statement = $this->getGrammar()->compileDropAllTypes(['alpha', 'beta', 'gamma']);
- $this->assertSame('drop type "alpha","beta","gamma" cascade', $statement);
- }
- protected function getConnection()
- {
- return m::mock(Connection::class);
- }
- public function getGrammar()
- {
- return new PostgresGrammar;
- }
- public function testGrammarsAreMacroable()
- {
- // compileReplace macro.
- $this->getGrammar()::macro('compileReplace', function () {
- return true;
- });
- $c = $this->getGrammar()::compileReplace();
- $this->assertTrue($c);
- }
- }
|