DatabasePostgresSchemaGrammarTest.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Illuminate\Database\Connection;
  4. use Illuminate\Database\Schema\Blueprint;
  5. use Illuminate\Database\Schema\ForeignIdColumnDefinition;
  6. use Illuminate\Database\Schema\Grammars\PostgresGrammar;
  7. use Mockery as m;
  8. use PHPUnit\Framework\TestCase;
  9. class DatabasePostgresSchemaGrammarTest extends TestCase
  10. {
  11. protected function tearDown(): void
  12. {
  13. m::close();
  14. }
  15. public function testBasicCreateTable()
  16. {
  17. $blueprint = new Blueprint('users');
  18. $blueprint->create();
  19. $blueprint->increments('id');
  20. $blueprint->string('email');
  21. $blueprint->string('name')->collation('nb_NO.utf8');
  22. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  23. $this->assertCount(1, $statements);
  24. $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]);
  25. $blueprint = new Blueprint('users');
  26. $blueprint->increments('id');
  27. $blueprint->string('email');
  28. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  29. $this->assertCount(1, $statements);
  30. $this->assertSame('alter table "users" add column "id" serial primary key not null, add column "email" varchar(255) not null', $statements[0]);
  31. }
  32. public function testCreateTableWithAutoIncrementStartingValue()
  33. {
  34. $blueprint = new Blueprint('users');
  35. $blueprint->create();
  36. $blueprint->increments('id')->startingValue(1000);
  37. $blueprint->string('email');
  38. $blueprint->string('name')->collation('nb_NO.utf8');
  39. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  40. $this->assertCount(2, $statements);
  41. $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]);
  42. $this->assertSame('alter sequence users_id_seq restart with 1000', $statements[1]);
  43. }
  44. public function testCreateTableAndCommentColumn()
  45. {
  46. $blueprint = new Blueprint('users');
  47. $blueprint->create();
  48. $blueprint->increments('id');
  49. $blueprint->string('email')->comment('my first comment');
  50. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  51. $this->assertCount(2, $statements);
  52. $this->assertSame('create table "users" ("id" serial primary key not null, "email" varchar(255) not null)', $statements[0]);
  53. $this->assertSame('comment on column "users"."email" is \'my first comment\'', $statements[1]);
  54. }
  55. public function testCreateTemporaryTable()
  56. {
  57. $blueprint = new Blueprint('users');
  58. $blueprint->create();
  59. $blueprint->temporary();
  60. $blueprint->increments('id');
  61. $blueprint->string('email');
  62. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  63. $this->assertCount(1, $statements);
  64. $this->assertSame('create temporary table "users" ("id" serial primary key not null, "email" varchar(255) not null)', $statements[0]);
  65. }
  66. public function testDropTable()
  67. {
  68. $blueprint = new Blueprint('users');
  69. $blueprint->drop();
  70. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  71. $this->assertCount(1, $statements);
  72. $this->assertSame('drop table "users"', $statements[0]);
  73. }
  74. public function testDropTableIfExists()
  75. {
  76. $blueprint = new Blueprint('users');
  77. $blueprint->dropIfExists();
  78. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  79. $this->assertCount(1, $statements);
  80. $this->assertSame('drop table if exists "users"', $statements[0]);
  81. }
  82. public function testDropColumn()
  83. {
  84. $blueprint = new Blueprint('users');
  85. $blueprint->dropColumn('foo');
  86. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  87. $this->assertCount(1, $statements);
  88. $this->assertSame('alter table "users" drop column "foo"', $statements[0]);
  89. $blueprint = new Blueprint('users');
  90. $blueprint->dropColumn(['foo', 'bar']);
  91. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  92. $this->assertCount(1, $statements);
  93. $this->assertSame('alter table "users" drop column "foo", drop column "bar"', $statements[0]);
  94. $blueprint = new Blueprint('users');
  95. $blueprint->dropColumn('foo', 'bar');
  96. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  97. $this->assertCount(1, $statements);
  98. $this->assertSame('alter table "users" drop column "foo", drop column "bar"', $statements[0]);
  99. }
  100. public function testDropPrimary()
  101. {
  102. $blueprint = new Blueprint('users');
  103. $blueprint->dropPrimary();
  104. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  105. $this->assertCount(1, $statements);
  106. $this->assertSame('alter table "users" drop constraint "users_pkey"', $statements[0]);
  107. }
  108. public function testDropUnique()
  109. {
  110. $blueprint = new Blueprint('users');
  111. $blueprint->dropUnique('foo');
  112. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  113. $this->assertCount(1, $statements);
  114. $this->assertSame('alter table "users" drop constraint "foo"', $statements[0]);
  115. }
  116. public function testDropIndex()
  117. {
  118. $blueprint = new Blueprint('users');
  119. $blueprint->dropIndex('foo');
  120. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  121. $this->assertCount(1, $statements);
  122. $this->assertSame('drop index "foo"', $statements[0]);
  123. }
  124. public function testDropSpatialIndex()
  125. {
  126. $blueprint = new Blueprint('geo');
  127. $blueprint->dropSpatialIndex(['coordinates']);
  128. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  129. $this->assertCount(1, $statements);
  130. $this->assertSame('drop index "geo_coordinates_spatialindex"', $statements[0]);
  131. }
  132. public function testDropForeign()
  133. {
  134. $blueprint = new Blueprint('users');
  135. $blueprint->dropForeign('foo');
  136. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  137. $this->assertCount(1, $statements);
  138. $this->assertSame('alter table "users" drop constraint "foo"', $statements[0]);
  139. }
  140. public function testDropTimestamps()
  141. {
  142. $blueprint = new Blueprint('users');
  143. $blueprint->dropTimestamps();
  144. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  145. $this->assertCount(1, $statements);
  146. $this->assertSame('alter table "users" drop column "created_at", drop column "updated_at"', $statements[0]);
  147. }
  148. public function testDropTimestampsTz()
  149. {
  150. $blueprint = new Blueprint('users');
  151. $blueprint->dropTimestampsTz();
  152. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  153. $this->assertCount(1, $statements);
  154. $this->assertSame('alter table "users" drop column "created_at", drop column "updated_at"', $statements[0]);
  155. }
  156. public function testDropMorphs()
  157. {
  158. $blueprint = new Blueprint('photos');
  159. $blueprint->dropMorphs('imageable');
  160. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  161. $this->assertCount(2, $statements);
  162. $this->assertSame('drop index "photos_imageable_type_imageable_id_index"', $statements[0]);
  163. $this->assertSame('alter table "photos" drop column "imageable_type", drop column "imageable_id"', $statements[1]);
  164. }
  165. public function testRenameTable()
  166. {
  167. $blueprint = new Blueprint('users');
  168. $blueprint->rename('foo');
  169. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  170. $this->assertCount(1, $statements);
  171. $this->assertSame('alter table "users" rename to "foo"', $statements[0]);
  172. }
  173. public function testRenameIndex()
  174. {
  175. $blueprint = new Blueprint('users');
  176. $blueprint->renameIndex('foo', 'bar');
  177. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  178. $this->assertCount(1, $statements);
  179. $this->assertSame('alter index "foo" rename to "bar"', $statements[0]);
  180. }
  181. public function testAddingPrimaryKey()
  182. {
  183. $blueprint = new Blueprint('users');
  184. $blueprint->primary('foo');
  185. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  186. $this->assertCount(1, $statements);
  187. $this->assertSame('alter table "users" add primary key ("foo")', $statements[0]);
  188. }
  189. public function testAddingUniqueKey()
  190. {
  191. $blueprint = new Blueprint('users');
  192. $blueprint->unique('foo', 'bar');
  193. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  194. $this->assertCount(1, $statements);
  195. $this->assertSame('alter table "users" add constraint "bar" unique ("foo")', $statements[0]);
  196. }
  197. public function testAddingIndex()
  198. {
  199. $blueprint = new Blueprint('users');
  200. $blueprint->index(['foo', 'bar'], 'baz');
  201. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  202. $this->assertCount(1, $statements);
  203. $this->assertSame('create index "baz" on "users" ("foo", "bar")', $statements[0]);
  204. }
  205. public function testAddingIndexWithAlgorithm()
  206. {
  207. $blueprint = new Blueprint('users');
  208. $blueprint->index(['foo', 'bar'], 'baz', 'hash');
  209. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  210. $this->assertCount(1, $statements);
  211. $this->assertSame('create index "baz" on "users" using hash ("foo", "bar")', $statements[0]);
  212. }
  213. public function testAddingFulltextIndex()
  214. {
  215. $blueprint = new Blueprint('users');
  216. $blueprint->fulltext('body');
  217. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  218. $this->assertCount(1, $statements);
  219. $this->assertSame('create index "users_body_fulltext" on "users" using gin ((to_tsvector(\'english\', "body")))', $statements[0]);
  220. }
  221. public function testAddingFulltextIndexMultipleColumns()
  222. {
  223. $blueprint = new Blueprint('users');
  224. $blueprint->fulltext(['body', 'title']);
  225. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  226. $this->assertCount(1, $statements);
  227. $this->assertSame('create index "users_body_title_fulltext" on "users" using gin ((to_tsvector(\'english\', "body") || to_tsvector(\'english\', "title")))', $statements[0]);
  228. }
  229. public function testAddingFulltextIndexWithLanguage()
  230. {
  231. $blueprint = new Blueprint('users');
  232. $blueprint->fulltext('body')->language('spanish');
  233. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  234. $this->assertCount(1, $statements);
  235. $this->assertSame('create index "users_body_fulltext" on "users" using gin ((to_tsvector(\'spanish\', "body")))', $statements[0]);
  236. }
  237. public function testAddingFulltextIndexWithFluency()
  238. {
  239. $blueprint = new Blueprint('users');
  240. $blueprint->string('body')->fulltext();
  241. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  242. $this->assertCount(2, $statements);
  243. $this->assertSame('create index "users_body_fulltext" on "users" using gin ((to_tsvector(\'english\', "body")))', $statements[1]);
  244. }
  245. public function testAddingSpatialIndex()
  246. {
  247. $blueprint = new Blueprint('geo');
  248. $blueprint->spatialIndex('coordinates');
  249. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  250. $this->assertCount(1, $statements);
  251. $this->assertSame('create index "geo_coordinates_spatialindex" on "geo" using gist ("coordinates")', $statements[0]);
  252. }
  253. public function testAddingFluentSpatialIndex()
  254. {
  255. $blueprint = new Blueprint('geo');
  256. $blueprint->point('coordinates')->spatialIndex();
  257. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  258. $this->assertCount(2, $statements);
  259. $this->assertSame('create index "geo_coordinates_spatialindex" on "geo" using gist ("coordinates")', $statements[1]);
  260. }
  261. public function testAddingRawIndex()
  262. {
  263. $blueprint = new Blueprint('users');
  264. $blueprint->rawIndex('(function(column))', 'raw_index');
  265. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  266. $this->assertCount(1, $statements);
  267. $this->assertSame('create index "raw_index" on "users" ((function(column)))', $statements[0]);
  268. }
  269. public function testAddingIncrementingID()
  270. {
  271. $blueprint = new Blueprint('users');
  272. $blueprint->increments('id');
  273. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  274. $this->assertCount(1, $statements);
  275. $this->assertSame('alter table "users" add column "id" serial primary key not null', $statements[0]);
  276. }
  277. public function testAddingSmallIncrementingID()
  278. {
  279. $blueprint = new Blueprint('users');
  280. $blueprint->smallIncrements('id');
  281. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  282. $this->assertCount(1, $statements);
  283. $this->assertSame('alter table "users" add column "id" smallserial primary key not null', $statements[0]);
  284. }
  285. public function testAddingMediumIncrementingID()
  286. {
  287. $blueprint = new Blueprint('users');
  288. $blueprint->mediumIncrements('id');
  289. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  290. $this->assertCount(1, $statements);
  291. $this->assertSame('alter table "users" add column "id" serial primary key not null', $statements[0]);
  292. }
  293. public function testAddingID()
  294. {
  295. $blueprint = new Blueprint('users');
  296. $blueprint->id();
  297. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  298. $this->assertCount(1, $statements);
  299. $this->assertSame('alter table "users" add column "id" bigserial primary key not null', $statements[0]);
  300. $blueprint = new Blueprint('users');
  301. $blueprint->id('foo');
  302. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  303. $this->assertCount(1, $statements);
  304. $this->assertSame('alter table "users" add column "foo" bigserial primary key not null', $statements[0]);
  305. }
  306. public function testAddingForeignID()
  307. {
  308. $blueprint = new Blueprint('users');
  309. $foreignId = $blueprint->foreignId('foo');
  310. $blueprint->foreignId('company_id')->constrained();
  311. $blueprint->foreignId('laravel_idea_id')->constrained();
  312. $blueprint->foreignId('team_id')->references('id')->on('teams');
  313. $blueprint->foreignId('team_column_id')->constrained('teams');
  314. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  315. $this->assertInstanceOf(ForeignIdColumnDefinition::class, $foreignId);
  316. $this->assertSame([
  317. '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',
  318. 'alter table "users" add constraint "users_company_id_foreign" foreign key ("company_id") references "companies" ("id")',
  319. 'alter table "users" add constraint "users_laravel_idea_id_foreign" foreign key ("laravel_idea_id") references "laravel_ideas" ("id")',
  320. 'alter table "users" add constraint "users_team_id_foreign" foreign key ("team_id") references "teams" ("id")',
  321. 'alter table "users" add constraint "users_team_column_id_foreign" foreign key ("team_column_id") references "teams" ("id")',
  322. ], $statements);
  323. }
  324. public function testAddingBigIncrementingID()
  325. {
  326. $blueprint = new Blueprint('users');
  327. $blueprint->bigIncrements('id');
  328. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  329. $this->assertCount(1, $statements);
  330. $this->assertSame('alter table "users" add column "id" bigserial primary key not null', $statements[0]);
  331. }
  332. public function testAddingString()
  333. {
  334. $blueprint = new Blueprint('users');
  335. $blueprint->string('foo');
  336. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  337. $this->assertCount(1, $statements);
  338. $this->assertSame('alter table "users" add column "foo" varchar(255) not null', $statements[0]);
  339. $blueprint = new Blueprint('users');
  340. $blueprint->string('foo', 100);
  341. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  342. $this->assertCount(1, $statements);
  343. $this->assertSame('alter table "users" add column "foo" varchar(100) not null', $statements[0]);
  344. $blueprint = new Blueprint('users');
  345. $blueprint->string('foo', 100)->nullable()->default('bar');
  346. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  347. $this->assertCount(1, $statements);
  348. $this->assertSame('alter table "users" add column "foo" varchar(100) null default \'bar\'', $statements[0]);
  349. }
  350. public function testAddingText()
  351. {
  352. $blueprint = new Blueprint('users');
  353. $blueprint->text('foo');
  354. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  355. $this->assertCount(1, $statements);
  356. $this->assertSame('alter table "users" add column "foo" text not null', $statements[0]);
  357. }
  358. public function testAddingBigInteger()
  359. {
  360. $blueprint = new Blueprint('users');
  361. $blueprint->bigInteger('foo');
  362. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  363. $this->assertCount(1, $statements);
  364. $this->assertSame('alter table "users" add column "foo" bigint not null', $statements[0]);
  365. $blueprint = new Blueprint('users');
  366. $blueprint->bigInteger('foo', true);
  367. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  368. $this->assertCount(1, $statements);
  369. $this->assertSame('alter table "users" add column "foo" bigserial primary key not null', $statements[0]);
  370. }
  371. public function testAddingInteger()
  372. {
  373. $blueprint = new Blueprint('users');
  374. $blueprint->integer('foo');
  375. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  376. $this->assertCount(1, $statements);
  377. $this->assertSame('alter table "users" add column "foo" integer not null', $statements[0]);
  378. $blueprint = new Blueprint('users');
  379. $blueprint->integer('foo', true);
  380. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  381. $this->assertCount(1, $statements);
  382. $this->assertSame('alter table "users" add column "foo" serial primary key not null', $statements[0]);
  383. }
  384. public function testAddingMediumInteger()
  385. {
  386. $blueprint = new Blueprint('users');
  387. $blueprint->mediumInteger('foo');
  388. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  389. $this->assertCount(1, $statements);
  390. $this->assertSame('alter table "users" add column "foo" integer not null', $statements[0]);
  391. $blueprint = new Blueprint('users');
  392. $blueprint->mediumInteger('foo', true);
  393. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  394. $this->assertCount(1, $statements);
  395. $this->assertSame('alter table "users" add column "foo" serial primary key not null', $statements[0]);
  396. }
  397. public function testAddingTinyInteger()
  398. {
  399. $blueprint = new Blueprint('users');
  400. $blueprint->tinyInteger('foo');
  401. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  402. $this->assertCount(1, $statements);
  403. $this->assertSame('alter table "users" add column "foo" smallint not null', $statements[0]);
  404. $blueprint = new Blueprint('users');
  405. $blueprint->tinyInteger('foo', true);
  406. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  407. $this->assertCount(1, $statements);
  408. $this->assertSame('alter table "users" add column "foo" smallserial primary key not null', $statements[0]);
  409. }
  410. public function testAddingSmallInteger()
  411. {
  412. $blueprint = new Blueprint('users');
  413. $blueprint->smallInteger('foo');
  414. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  415. $this->assertCount(1, $statements);
  416. $this->assertSame('alter table "users" add column "foo" smallint not null', $statements[0]);
  417. $blueprint = new Blueprint('users');
  418. $blueprint->smallInteger('foo', true);
  419. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  420. $this->assertCount(1, $statements);
  421. $this->assertSame('alter table "users" add column "foo" smallserial primary key not null', $statements[0]);
  422. }
  423. public function testAddingFloat()
  424. {
  425. $blueprint = new Blueprint('users');
  426. $blueprint->float('foo', 5, 2);
  427. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  428. $this->assertCount(1, $statements);
  429. $this->assertSame('alter table "users" add column "foo" double precision not null', $statements[0]);
  430. }
  431. public function testAddingDouble()
  432. {
  433. $blueprint = new Blueprint('users');
  434. $blueprint->double('foo', 15, 8);
  435. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  436. $this->assertCount(1, $statements);
  437. $this->assertSame('alter table "users" add column "foo" double precision not null', $statements[0]);
  438. }
  439. public function testAddingDecimal()
  440. {
  441. $blueprint = new Blueprint('users');
  442. $blueprint->decimal('foo', 5, 2);
  443. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  444. $this->assertCount(1, $statements);
  445. $this->assertSame('alter table "users" add column "foo" decimal(5, 2) not null', $statements[0]);
  446. }
  447. public function testAddingBoolean()
  448. {
  449. $blueprint = new Blueprint('users');
  450. $blueprint->boolean('foo');
  451. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  452. $this->assertCount(1, $statements);
  453. $this->assertSame('alter table "users" add column "foo" boolean not null', $statements[0]);
  454. }
  455. public function testAddingEnum()
  456. {
  457. $blueprint = new Blueprint('users');
  458. $blueprint->enum('role', ['member', 'admin']);
  459. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  460. $this->assertCount(1, $statements);
  461. $this->assertSame('alter table "users" add column "role" varchar(255) check ("role" in (\'member\', \'admin\')) not null', $statements[0]);
  462. }
  463. public function testAddingDate()
  464. {
  465. $blueprint = new Blueprint('users');
  466. $blueprint->date('foo');
  467. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  468. $this->assertCount(1, $statements);
  469. $this->assertSame('alter table "users" add column "foo" date not null', $statements[0]);
  470. }
  471. public function testAddingYear()
  472. {
  473. $blueprint = new Blueprint('users');
  474. $blueprint->year('birth_year');
  475. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  476. $this->assertCount(1, $statements);
  477. $this->assertSame('alter table "users" add column "birth_year" integer not null', $statements[0]);
  478. }
  479. public function testAddingJson()
  480. {
  481. $blueprint = new Blueprint('users');
  482. $blueprint->json('foo');
  483. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  484. $this->assertCount(1, $statements);
  485. $this->assertSame('alter table "users" add column "foo" json not null', $statements[0]);
  486. }
  487. public function testAddingJsonb()
  488. {
  489. $blueprint = new Blueprint('users');
  490. $blueprint->jsonb('foo');
  491. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  492. $this->assertCount(1, $statements);
  493. $this->assertSame('alter table "users" add column "foo" jsonb not null', $statements[0]);
  494. }
  495. public function testAddingDateTime()
  496. {
  497. $blueprint = new Blueprint('users');
  498. $blueprint->dateTime('created_at');
  499. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  500. $this->assertCount(1, $statements);
  501. $this->assertSame('alter table "users" add column "created_at" timestamp(0) without time zone not null', $statements[0]);
  502. }
  503. public function testAddingDateTimeWithPrecision()
  504. {
  505. $blueprint = new Blueprint('users');
  506. $blueprint->dateTime('created_at', 1);
  507. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  508. $this->assertCount(1, $statements);
  509. $this->assertSame('alter table "users" add column "created_at" timestamp(1) without time zone not null', $statements[0]);
  510. }
  511. public function testAddingDateTimeWithNullPrecision()
  512. {
  513. $blueprint = new Blueprint('users');
  514. $blueprint->dateTime('created_at', null);
  515. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  516. $this->assertCount(1, $statements);
  517. $this->assertSame('alter table "users" add column "created_at" timestamp without time zone not null', $statements[0]);
  518. }
  519. public function testAddingDateTimeTz()
  520. {
  521. $blueprint = new Blueprint('users');
  522. $blueprint->dateTimeTz('created_at');
  523. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  524. $this->assertCount(1, $statements);
  525. $this->assertSame('alter table "users" add column "created_at" timestamp(0) with time zone not null', $statements[0]);
  526. }
  527. public function testAddingDateTimeTzWithPrecision()
  528. {
  529. $blueprint = new Blueprint('users');
  530. $blueprint->dateTimeTz('created_at', 1);
  531. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  532. $this->assertCount(1, $statements);
  533. $this->assertSame('alter table "users" add column "created_at" timestamp(1) with time zone not null', $statements[0]);
  534. }
  535. public function testAddingDateTimeTzWithNullPrecision()
  536. {
  537. $blueprint = new Blueprint('users');
  538. $blueprint->dateTimeTz('created_at', null);
  539. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  540. $this->assertCount(1, $statements);
  541. $this->assertSame('alter table "users" add column "created_at" timestamp with time zone not null', $statements[0]);
  542. }
  543. public function testAddingTime()
  544. {
  545. $blueprint = new Blueprint('users');
  546. $blueprint->time('created_at');
  547. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  548. $this->assertCount(1, $statements);
  549. $this->assertSame('alter table "users" add column "created_at" time(0) without time zone not null', $statements[0]);
  550. }
  551. public function testAddingTimeWithPrecision()
  552. {
  553. $blueprint = new Blueprint('users');
  554. $blueprint->time('created_at', 1);
  555. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  556. $this->assertCount(1, $statements);
  557. $this->assertSame('alter table "users" add column "created_at" time(1) without time zone not null', $statements[0]);
  558. }
  559. public function testAddingTimeWithNullPrecision()
  560. {
  561. $blueprint = new Blueprint('users');
  562. $blueprint->time('created_at', null);
  563. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  564. $this->assertCount(1, $statements);
  565. $this->assertSame('alter table "users" add column "created_at" time without time zone not null', $statements[0]);
  566. }
  567. public function testAddingTimeTz()
  568. {
  569. $blueprint = new Blueprint('users');
  570. $blueprint->timeTz('created_at');
  571. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  572. $this->assertCount(1, $statements);
  573. $this->assertSame('alter table "users" add column "created_at" time(0) with time zone not null', $statements[0]);
  574. }
  575. public function testAddingTimeTzWithPrecision()
  576. {
  577. $blueprint = new Blueprint('users');
  578. $blueprint->timeTz('created_at', 1);
  579. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  580. $this->assertCount(1, $statements);
  581. $this->assertSame('alter table "users" add column "created_at" time(1) with time zone not null', $statements[0]);
  582. }
  583. public function testAddingTimeTzWithNullPrecision()
  584. {
  585. $blueprint = new Blueprint('users');
  586. $blueprint->timeTz('created_at', null);
  587. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  588. $this->assertCount(1, $statements);
  589. $this->assertSame('alter table "users" add column "created_at" time with time zone not null', $statements[0]);
  590. }
  591. public function testAddingTimestamp()
  592. {
  593. $blueprint = new Blueprint('users');
  594. $blueprint->timestamp('created_at');
  595. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  596. $this->assertCount(1, $statements);
  597. $this->assertSame('alter table "users" add column "created_at" timestamp(0) without time zone not null', $statements[0]);
  598. }
  599. public function testAddingTimestampWithPrecision()
  600. {
  601. $blueprint = new Blueprint('users');
  602. $blueprint->timestamp('created_at', 1);
  603. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  604. $this->assertCount(1, $statements);
  605. $this->assertSame('alter table "users" add column "created_at" timestamp(1) without time zone not null', $statements[0]);
  606. }
  607. public function testAddingTimestampWithNullPrecision()
  608. {
  609. $blueprint = new Blueprint('users');
  610. $blueprint->timestamp('created_at', null);
  611. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  612. $this->assertCount(1, $statements);
  613. $this->assertSame('alter table "users" add column "created_at" timestamp without time zone not null', $statements[0]);
  614. }
  615. public function testAddingTimestampTz()
  616. {
  617. $blueprint = new Blueprint('users');
  618. $blueprint->timestampTz('created_at');
  619. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  620. $this->assertCount(1, $statements);
  621. $this->assertSame('alter table "users" add column "created_at" timestamp(0) with time zone not null', $statements[0]);
  622. }
  623. public function testAddingTimestampTzWithPrecision()
  624. {
  625. $blueprint = new Blueprint('users');
  626. $blueprint->timestampTz('created_at', 1);
  627. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  628. $this->assertCount(1, $statements);
  629. $this->assertSame('alter table "users" add column "created_at" timestamp(1) with time zone not null', $statements[0]);
  630. }
  631. public function testAddingTimestampTzWithNullPrecision()
  632. {
  633. $blueprint = new Blueprint('users');
  634. $blueprint->timestampTz('created_at', null);
  635. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  636. $this->assertCount(1, $statements);
  637. $this->assertSame('alter table "users" add column "created_at" timestamp with time zone not null', $statements[0]);
  638. }
  639. public function testAddingTimestamps()
  640. {
  641. $blueprint = new Blueprint('users');
  642. $blueprint->timestamps();
  643. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  644. $this->assertCount(1, $statements);
  645. $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]);
  646. }
  647. public function testAddingTimestampsTz()
  648. {
  649. $blueprint = new Blueprint('users');
  650. $blueprint->timestampsTz();
  651. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  652. $this->assertCount(1, $statements);
  653. $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]);
  654. }
  655. public function testAddingBinary()
  656. {
  657. $blueprint = new Blueprint('users');
  658. $blueprint->binary('foo');
  659. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  660. $this->assertCount(1, $statements);
  661. $this->assertSame('alter table "users" add column "foo" bytea not null', $statements[0]);
  662. }
  663. public function testAddingUuid()
  664. {
  665. $blueprint = new Blueprint('users');
  666. $blueprint->uuid('foo');
  667. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  668. $this->assertCount(1, $statements);
  669. $this->assertSame('alter table "users" add column "foo" uuid not null', $statements[0]);
  670. }
  671. public function testAddingForeignUuid()
  672. {
  673. $blueprint = new Blueprint('users');
  674. $foreignUuid = $blueprint->foreignUuid('foo');
  675. $blueprint->foreignUuid('company_id')->constrained();
  676. $blueprint->foreignUuid('laravel_idea_id')->constrained();
  677. $blueprint->foreignUuid('team_id')->references('id')->on('teams');
  678. $blueprint->foreignUuid('team_column_id')->constrained('teams');
  679. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  680. $this->assertInstanceOf(ForeignIdColumnDefinition::class, $foreignUuid);
  681. $this->assertSame([
  682. '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',
  683. 'alter table "users" add constraint "users_company_id_foreign" foreign key ("company_id") references "companies" ("id")',
  684. 'alter table "users" add constraint "users_laravel_idea_id_foreign" foreign key ("laravel_idea_id") references "laravel_ideas" ("id")',
  685. 'alter table "users" add constraint "users_team_id_foreign" foreign key ("team_id") references "teams" ("id")',
  686. 'alter table "users" add constraint "users_team_column_id_foreign" foreign key ("team_column_id") references "teams" ("id")',
  687. ], $statements);
  688. }
  689. public function testAddingGeneratedAs()
  690. {
  691. $blueprint = new Blueprint('users');
  692. $blueprint->increments('foo')->generatedAs();
  693. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  694. $this->assertCount(1, $statements);
  695. $this->assertSame('alter table "users" add column "foo" integer generated by default as identity primary key not null', $statements[0]);
  696. // With always modifier
  697. $blueprint = new Blueprint('users');
  698. $blueprint->increments('foo')->generatedAs()->always();
  699. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  700. $this->assertCount(1, $statements);
  701. $this->assertSame('alter table "users" add column "foo" integer generated always as identity primary key not null', $statements[0]);
  702. // With sequence options
  703. $blueprint = new Blueprint('users');
  704. $blueprint->increments('foo')->generatedAs('increment by 10 start with 100');
  705. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  706. $this->assertCount(1, $statements);
  707. $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]);
  708. // Not a primary key
  709. $blueprint = new Blueprint('users');
  710. $blueprint->integer('foo')->generatedAs();
  711. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  712. $this->assertCount(1, $statements);
  713. $this->assertSame('alter table "users" add column "foo" integer generated by default as identity not null', $statements[0]);
  714. }
  715. public function testAddingVirtualAs()
  716. {
  717. $blueprint = new Blueprint('users');
  718. $blueprint->integer('foo')->nullable();
  719. $blueprint->boolean('bar')->virtualAs('foo is not null');
  720. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  721. $this->assertCount(1, $statements);
  722. $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]);
  723. }
  724. public function testAddingStoredAs()
  725. {
  726. $blueprint = new Blueprint('users');
  727. $blueprint->integer('foo')->nullable();
  728. $blueprint->boolean('bar')->storedAs('foo is not null');
  729. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  730. $this->assertCount(1, $statements);
  731. $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]);
  732. }
  733. public function testAddingIpAddress()
  734. {
  735. $blueprint = new Blueprint('users');
  736. $blueprint->ipAddress('foo');
  737. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  738. $this->assertCount(1, $statements);
  739. $this->assertSame('alter table "users" add column "foo" inet not null', $statements[0]);
  740. }
  741. public function testAddingMacAddress()
  742. {
  743. $blueprint = new Blueprint('users');
  744. $blueprint->macAddress('foo');
  745. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  746. $this->assertCount(1, $statements);
  747. $this->assertSame('alter table "users" add column "foo" macaddr not null', $statements[0]);
  748. }
  749. public function testCompileForeign()
  750. {
  751. $blueprint = new Blueprint('users');
  752. $blueprint->foreign('parent_id')->references('id')->on('parents')->onDelete('cascade')->deferrable();
  753. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  754. $this->assertCount(1, $statements);
  755. $this->assertSame('alter table "users" add constraint "users_parent_id_foreign" foreign key ("parent_id") references "parents" ("id") on delete cascade deferrable', $statements[0]);
  756. $blueprint = new Blueprint('users');
  757. $blueprint->foreign('parent_id')->references('id')->on('parents')->onDelete('cascade')->deferrable(false)->initiallyImmediate();
  758. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  759. $this->assertCount(1, $statements);
  760. $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]);
  761. $blueprint = new Blueprint('users');
  762. $blueprint->foreign('parent_id')->references('id')->on('parents')->onDelete('cascade')->deferrable()->initiallyImmediate(false);
  763. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  764. $this->assertCount(1, $statements);
  765. $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]);
  766. $blueprint = new Blueprint('users');
  767. $blueprint->foreign('parent_id')->references('id')->on('parents')->onDelete('cascade')->deferrable()->notValid();
  768. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  769. $this->assertCount(1, $statements);
  770. $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]);
  771. }
  772. public function testAddingGeometry()
  773. {
  774. $blueprint = new Blueprint('geo');
  775. $blueprint->geometry('coordinates');
  776. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  777. $this->assertCount(1, $statements);
  778. $this->assertSame('alter table "geo" add column "coordinates" geography(geometry, 4326) not null', $statements[0]);
  779. }
  780. public function testAddingPoint()
  781. {
  782. $blueprint = new Blueprint('geo');
  783. $blueprint->point('coordinates');
  784. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  785. $this->assertCount(1, $statements);
  786. $this->assertSame('alter table "geo" add column "coordinates" geography(point, 4326) not null', $statements[0]);
  787. }
  788. public function testAddingLineString()
  789. {
  790. $blueprint = new Blueprint('geo');
  791. $blueprint->linestring('coordinates');
  792. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  793. $this->assertCount(1, $statements);
  794. $this->assertSame('alter table "geo" add column "coordinates" geography(linestring, 4326) not null', $statements[0]);
  795. }
  796. public function testAddingPolygon()
  797. {
  798. $blueprint = new Blueprint('geo');
  799. $blueprint->polygon('coordinates');
  800. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  801. $this->assertCount(1, $statements);
  802. $this->assertSame('alter table "geo" add column "coordinates" geography(polygon, 4326) not null', $statements[0]);
  803. }
  804. public function testAddingGeometryCollection()
  805. {
  806. $blueprint = new Blueprint('geo');
  807. $blueprint->geometrycollection('coordinates');
  808. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  809. $this->assertCount(1, $statements);
  810. $this->assertSame('alter table "geo" add column "coordinates" geography(geometrycollection, 4326) not null', $statements[0]);
  811. }
  812. public function testAddingMultiPoint()
  813. {
  814. $blueprint = new Blueprint('geo');
  815. $blueprint->multipoint('coordinates');
  816. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  817. $this->assertCount(1, $statements);
  818. $this->assertSame('alter table "geo" add column "coordinates" geography(multipoint, 4326) not null', $statements[0]);
  819. }
  820. public function testAddingMultiLineString()
  821. {
  822. $blueprint = new Blueprint('geo');
  823. $blueprint->multilinestring('coordinates');
  824. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  825. $this->assertCount(1, $statements);
  826. $this->assertSame('alter table "geo" add column "coordinates" geography(multilinestring, 4326) not null', $statements[0]);
  827. }
  828. public function testAddingMultiPolygon()
  829. {
  830. $blueprint = new Blueprint('geo');
  831. $blueprint->multipolygon('coordinates');
  832. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  833. $this->assertCount(1, $statements);
  834. $this->assertSame('alter table "geo" add column "coordinates" geography(multipolygon, 4326) not null', $statements[0]);
  835. }
  836. public function testCreateDatabase()
  837. {
  838. $connection = $this->getConnection();
  839. $connection->shouldReceive('getConfig')->once()->once()->with('charset')->andReturn('utf8_foo');
  840. $statement = $this->getGrammar()->compileCreateDatabase('my_database_a', $connection);
  841. $this->assertSame(
  842. 'create database "my_database_a" encoding "utf8_foo"',
  843. $statement
  844. );
  845. $connection = $this->getConnection();
  846. $connection->shouldReceive('getConfig')->once()->once()->with('charset')->andReturn('utf8_bar');
  847. $statement = $this->getGrammar()->compileCreateDatabase('my_database_b', $connection);
  848. $this->assertSame(
  849. 'create database "my_database_b" encoding "utf8_bar"',
  850. $statement
  851. );
  852. }
  853. public function testDropDatabaseIfExists()
  854. {
  855. $statement = $this->getGrammar()->compileDropDatabaseIfExists('my_database_a');
  856. $this->assertSame(
  857. 'drop database if exists "my_database_a"',
  858. $statement
  859. );
  860. $statement = $this->getGrammar()->compileDropDatabaseIfExists('my_database_b');
  861. $this->assertSame(
  862. 'drop database if exists "my_database_b"',
  863. $statement
  864. );
  865. }
  866. public function testDropAllTablesEscapesTableNames()
  867. {
  868. $statement = $this->getGrammar()->compileDropAllTables(['alpha', 'beta', 'gamma']);
  869. $this->assertSame('drop table "alpha","beta","gamma" cascade', $statement);
  870. }
  871. public function testDropAllViewsEscapesTableNames()
  872. {
  873. $statement = $this->getGrammar()->compileDropAllViews(['alpha', 'beta', 'gamma']);
  874. $this->assertSame('drop view "alpha","beta","gamma" cascade', $statement);
  875. }
  876. public function testDropAllTypesEscapesTableNames()
  877. {
  878. $statement = $this->getGrammar()->compileDropAllTypes(['alpha', 'beta', 'gamma']);
  879. $this->assertSame('drop type "alpha","beta","gamma" cascade', $statement);
  880. }
  881. protected function getConnection()
  882. {
  883. return m::mock(Connection::class);
  884. }
  885. public function getGrammar()
  886. {
  887. return new PostgresGrammar;
  888. }
  889. public function testGrammarsAreMacroable()
  890. {
  891. // compileReplace macro.
  892. $this->getGrammar()::macro('compileReplace', function () {
  893. return true;
  894. });
  895. $c = $this->getGrammar()::compileReplace();
  896. $this->assertTrue($c);
  897. }
  898. }