DatabaseMySqlSchemaGrammarTest.php 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Illuminate\Database\Connection;
  4. use Illuminate\Database\Query\Expression;
  5. use Illuminate\Database\Schema\Blueprint;
  6. use Illuminate\Database\Schema\ForeignIdColumnDefinition;
  7. use Illuminate\Database\Schema\Grammars\MySqlGrammar;
  8. use Mockery as m;
  9. use PHPUnit\Framework\TestCase;
  10. class DatabaseMySqlSchemaGrammarTest extends TestCase
  11. {
  12. protected function tearDown(): void
  13. {
  14. m::close();
  15. }
  16. public function testBasicCreateTable()
  17. {
  18. $blueprint = new Blueprint('users');
  19. $blueprint->create();
  20. $blueprint->increments('id');
  21. $blueprint->string('email');
  22. $conn = $this->getConnection();
  23. $conn->shouldReceive('getConfig')->once()->with('charset')->andReturn('utf8');
  24. $conn->shouldReceive('getConfig')->once()->with('collation')->andReturn('utf8_unicode_ci');
  25. $conn->shouldReceive('getConfig')->once()->with('engine')->andReturn(null);
  26. $statements = $blueprint->toSql($conn, $this->getGrammar());
  27. $this->assertCount(1, $statements);
  28. $this->assertSame("create table `users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255) not null) default character set utf8 collate 'utf8_unicode_ci'", $statements[0]);
  29. $blueprint = new Blueprint('users');
  30. $blueprint->increments('id');
  31. $blueprint->string('email');
  32. $conn = $this->getConnection();
  33. $conn->shouldReceive('getConfig')->andReturn(null);
  34. $statements = $blueprint->toSql($conn, $this->getGrammar());
  35. $this->assertCount(1, $statements);
  36. $this->assertSame('alter table `users` add `id` int unsigned not null auto_increment primary key, add `email` varchar(255) not null', $statements[0]);
  37. }
  38. public function testAutoIncrementStartingValue()
  39. {
  40. $blueprint = new Blueprint('users');
  41. $blueprint->create();
  42. $blueprint->increments('id')->startingValue(1000);
  43. $blueprint->string('email');
  44. $conn = $this->getConnection();
  45. $conn->shouldReceive('getConfig')->once()->with('charset')->andReturn('utf8');
  46. $conn->shouldReceive('getConfig')->once()->with('collation')->andReturn('utf8_unicode_ci');
  47. $conn->shouldReceive('getConfig')->once()->with('engine')->andReturn(null);
  48. $statements = $blueprint->toSql($conn, $this->getGrammar());
  49. $this->assertCount(2, $statements);
  50. $this->assertSame("create table `users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255) not null) default character set utf8 collate 'utf8_unicode_ci'", $statements[0]);
  51. $this->assertSame('alter table `users` auto_increment = 1000', $statements[1]);
  52. }
  53. public function testEngineCreateTable()
  54. {
  55. $blueprint = new Blueprint('users');
  56. $blueprint->create();
  57. $blueprint->increments('id');
  58. $blueprint->string('email');
  59. $blueprint->engine = 'InnoDB';
  60. $conn = $this->getConnection();
  61. $conn->shouldReceive('getConfig')->once()->with('charset')->andReturn('utf8');
  62. $conn->shouldReceive('getConfig')->once()->with('collation')->andReturn('utf8_unicode_ci');
  63. $statements = $blueprint->toSql($conn, $this->getGrammar());
  64. $this->assertCount(1, $statements);
  65. $this->assertSame("create table `users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255) not null) default character set utf8 collate 'utf8_unicode_ci' engine = InnoDB", $statements[0]);
  66. $blueprint = new Blueprint('users');
  67. $blueprint->create();
  68. $blueprint->increments('id');
  69. $blueprint->string('email');
  70. $conn = $this->getConnection();
  71. $conn->shouldReceive('getConfig')->once()->with('charset')->andReturn('utf8');
  72. $conn->shouldReceive('getConfig')->once()->with('collation')->andReturn('utf8_unicode_ci');
  73. $conn->shouldReceive('getConfig')->once()->with('engine')->andReturn('InnoDB');
  74. $statements = $blueprint->toSql($conn, $this->getGrammar());
  75. $this->assertCount(1, $statements);
  76. $this->assertSame("create table `users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255) not null) default character set utf8 collate 'utf8_unicode_ci' engine = InnoDB", $statements[0]);
  77. }
  78. public function testCharsetCollationCreateTable()
  79. {
  80. $blueprint = new Blueprint('users');
  81. $blueprint->create();
  82. $blueprint->increments('id');
  83. $blueprint->string('email');
  84. $blueprint->charset = 'utf8mb4';
  85. $blueprint->collation = 'utf8mb4_unicode_ci';
  86. $conn = $this->getConnection();
  87. $conn->shouldReceive('getConfig')->once()->with('engine')->andReturn(null);
  88. $statements = $blueprint->toSql($conn, $this->getGrammar());
  89. $this->assertCount(1, $statements);
  90. $this->assertSame("create table `users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255) not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'", $statements[0]);
  91. $blueprint = new Blueprint('users');
  92. $blueprint->create();
  93. $blueprint->increments('id');
  94. $blueprint->string('email')->charset('utf8mb4')->collation('utf8mb4_unicode_ci');
  95. $conn = $this->getConnection();
  96. $conn->shouldReceive('getConfig')->once()->with('charset')->andReturn('utf8');
  97. $conn->shouldReceive('getConfig')->once()->with('collation')->andReturn('utf8_unicode_ci');
  98. $conn->shouldReceive('getConfig')->once()->with('engine')->andReturn(null);
  99. $statements = $blueprint->toSql($conn, $this->getGrammar());
  100. $this->assertCount(1, $statements);
  101. $this->assertSame("create table `users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255) character set utf8mb4 collate 'utf8mb4_unicode_ci' not null) default character set utf8 collate 'utf8_unicode_ci'", $statements[0]);
  102. }
  103. public function testBasicCreateTableWithPrefix()
  104. {
  105. $blueprint = new Blueprint('users');
  106. $blueprint->create();
  107. $blueprint->increments('id');
  108. $blueprint->string('email');
  109. $grammar = $this->getGrammar();
  110. $grammar->setTablePrefix('prefix_');
  111. $conn = $this->getConnection();
  112. $conn->shouldReceive('getConfig')->andReturn(null);
  113. $statements = $blueprint->toSql($conn, $grammar);
  114. $this->assertCount(1, $statements);
  115. $this->assertSame('create table `prefix_users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255) not null)', $statements[0]);
  116. }
  117. public function testCreateTemporaryTable()
  118. {
  119. $blueprint = new Blueprint('users');
  120. $blueprint->create();
  121. $blueprint->temporary();
  122. $blueprint->increments('id');
  123. $blueprint->string('email');
  124. $conn = $this->getConnection();
  125. $conn->shouldReceive('getConfig')->andReturn(null);
  126. $statements = $blueprint->toSql($conn, $this->getGrammar());
  127. $this->assertCount(1, $statements);
  128. $this->assertSame('create temporary table `users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255) not null)', $statements[0]);
  129. }
  130. public function testDropTable()
  131. {
  132. $blueprint = new Blueprint('users');
  133. $blueprint->drop();
  134. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  135. $this->assertCount(1, $statements);
  136. $this->assertSame('drop table `users`', $statements[0]);
  137. }
  138. public function testDropTableIfExists()
  139. {
  140. $blueprint = new Blueprint('users');
  141. $blueprint->dropIfExists();
  142. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  143. $this->assertCount(1, $statements);
  144. $this->assertSame('drop table if exists `users`', $statements[0]);
  145. }
  146. public function testDropColumn()
  147. {
  148. $blueprint = new Blueprint('users');
  149. $blueprint->dropColumn('foo');
  150. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  151. $this->assertCount(1, $statements);
  152. $this->assertSame('alter table `users` drop `foo`', $statements[0]);
  153. $blueprint = new Blueprint('users');
  154. $blueprint->dropColumn(['foo', 'bar']);
  155. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  156. $this->assertCount(1, $statements);
  157. $this->assertSame('alter table `users` drop `foo`, drop `bar`', $statements[0]);
  158. $blueprint = new Blueprint('users');
  159. $blueprint->dropColumn('foo', 'bar');
  160. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  161. $this->assertCount(1, $statements);
  162. $this->assertSame('alter table `users` drop `foo`, drop `bar`', $statements[0]);
  163. }
  164. public function testDropPrimary()
  165. {
  166. $blueprint = new Blueprint('users');
  167. $blueprint->dropPrimary();
  168. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  169. $this->assertCount(1, $statements);
  170. $this->assertSame('alter table `users` drop primary key', $statements[0]);
  171. }
  172. public function testDropUnique()
  173. {
  174. $blueprint = new Blueprint('users');
  175. $blueprint->dropUnique('foo');
  176. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  177. $this->assertCount(1, $statements);
  178. $this->assertSame('alter table `users` drop index `foo`', $statements[0]);
  179. }
  180. public function testDropIndex()
  181. {
  182. $blueprint = new Blueprint('users');
  183. $blueprint->dropIndex('foo');
  184. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  185. $this->assertCount(1, $statements);
  186. $this->assertSame('alter table `users` drop index `foo`', $statements[0]);
  187. }
  188. public function testDropSpatialIndex()
  189. {
  190. $blueprint = new Blueprint('geo');
  191. $blueprint->dropSpatialIndex(['coordinates']);
  192. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  193. $this->assertCount(1, $statements);
  194. $this->assertSame('alter table `geo` drop index `geo_coordinates_spatialindex`', $statements[0]);
  195. }
  196. public function testDropForeign()
  197. {
  198. $blueprint = new Blueprint('users');
  199. $blueprint->dropForeign('foo');
  200. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  201. $this->assertCount(1, $statements);
  202. $this->assertSame('alter table `users` drop foreign key `foo`', $statements[0]);
  203. }
  204. public function testDropTimestamps()
  205. {
  206. $blueprint = new Blueprint('users');
  207. $blueprint->dropTimestamps();
  208. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  209. $this->assertCount(1, $statements);
  210. $this->assertSame('alter table `users` drop `created_at`, drop `updated_at`', $statements[0]);
  211. }
  212. public function testDropTimestampsTz()
  213. {
  214. $blueprint = new Blueprint('users');
  215. $blueprint->dropTimestampsTz();
  216. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  217. $this->assertCount(1, $statements);
  218. $this->assertSame('alter table `users` drop `created_at`, drop `updated_at`', $statements[0]);
  219. }
  220. public function testDropMorphs()
  221. {
  222. $blueprint = new Blueprint('photos');
  223. $blueprint->dropMorphs('imageable');
  224. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  225. $this->assertCount(2, $statements);
  226. $this->assertSame('alter table `photos` drop index `photos_imageable_type_imageable_id_index`', $statements[0]);
  227. $this->assertSame('alter table `photos` drop `imageable_type`, drop `imageable_id`', $statements[1]);
  228. }
  229. public function testRenameTable()
  230. {
  231. $blueprint = new Blueprint('users');
  232. $blueprint->rename('foo');
  233. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  234. $this->assertCount(1, $statements);
  235. $this->assertSame('rename table `users` to `foo`', $statements[0]);
  236. }
  237. public function testRenameIndex()
  238. {
  239. $blueprint = new Blueprint('users');
  240. $blueprint->renameIndex('foo', 'bar');
  241. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  242. $this->assertCount(1, $statements);
  243. $this->assertSame('alter table `users` rename index `foo` to `bar`', $statements[0]);
  244. }
  245. public function testAddingPrimaryKey()
  246. {
  247. $blueprint = new Blueprint('users');
  248. $blueprint->primary('foo', 'bar');
  249. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  250. $this->assertCount(1, $statements);
  251. $this->assertSame('alter table `users` add primary key `bar`(`foo`)', $statements[0]);
  252. }
  253. public function testAddingPrimaryKeyWithAlgorithm()
  254. {
  255. $blueprint = new Blueprint('users');
  256. $blueprint->primary('foo', 'bar', 'hash');
  257. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  258. $this->assertCount(1, $statements);
  259. $this->assertSame('alter table `users` add primary key `bar` using hash(`foo`)', $statements[0]);
  260. }
  261. public function testAddingUniqueKey()
  262. {
  263. $blueprint = new Blueprint('users');
  264. $blueprint->unique('foo', 'bar');
  265. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  266. $this->assertCount(1, $statements);
  267. $this->assertSame('alter table `users` add unique `bar`(`foo`)', $statements[0]);
  268. }
  269. public function testAddingIndex()
  270. {
  271. $blueprint = new Blueprint('users');
  272. $blueprint->index(['foo', 'bar'], 'baz');
  273. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  274. $this->assertCount(1, $statements);
  275. $this->assertSame('alter table `users` add index `baz`(`foo`, `bar`)', $statements[0]);
  276. }
  277. public function testAddingIndexWithAlgorithm()
  278. {
  279. $blueprint = new Blueprint('users');
  280. $blueprint->index(['foo', 'bar'], 'baz', 'hash');
  281. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  282. $this->assertCount(1, $statements);
  283. $this->assertSame('alter table `users` add index `baz` using hash(`foo`, `bar`)', $statements[0]);
  284. }
  285. public function testAddingFulltextIndex()
  286. {
  287. $blueprint = new Blueprint('users');
  288. $blueprint->fulltext('body');
  289. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  290. $this->assertCount(1, $statements);
  291. $this->assertSame('alter table `users` add fulltext `users_body_fulltext`(`body`)', $statements[0]);
  292. }
  293. public function testAddingSpatialIndex()
  294. {
  295. $blueprint = new Blueprint('geo');
  296. $blueprint->spatialIndex('coordinates');
  297. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  298. $this->assertCount(1, $statements);
  299. $this->assertSame('alter table `geo` add spatial index `geo_coordinates_spatialindex`(`coordinates`)', $statements[0]);
  300. }
  301. public function testAddingFluentSpatialIndex()
  302. {
  303. $blueprint = new Blueprint('geo');
  304. $blueprint->point('coordinates')->spatialIndex();
  305. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  306. $this->assertCount(2, $statements);
  307. $this->assertSame('alter table `geo` add spatial index `geo_coordinates_spatialindex`(`coordinates`)', $statements[1]);
  308. }
  309. public function testAddingRawIndex()
  310. {
  311. $blueprint = new Blueprint('users');
  312. $blueprint->rawIndex('(function(column))', 'raw_index');
  313. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  314. $this->assertCount(1, $statements);
  315. $this->assertSame('alter table `users` add index `raw_index`((function(column)))', $statements[0]);
  316. }
  317. public function testAddingForeignKey()
  318. {
  319. $blueprint = new Blueprint('users');
  320. $blueprint->foreign('foo_id')->references('id')->on('orders');
  321. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  322. $this->assertCount(1, $statements);
  323. $this->assertSame('alter table `users` add constraint `users_foo_id_foreign` foreign key (`foo_id`) references `orders` (`id`)', $statements[0]);
  324. $blueprint = new Blueprint('users');
  325. $blueprint->foreign('foo_id')->references('id')->on('orders')->cascadeOnDelete();
  326. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  327. $this->assertCount(1, $statements);
  328. $this->assertSame('alter table `users` add constraint `users_foo_id_foreign` foreign key (`foo_id`) references `orders` (`id`) on delete cascade', $statements[0]);
  329. $blueprint = new Blueprint('users');
  330. $blueprint->foreign('foo_id')->references('id')->on('orders')->cascadeOnUpdate();
  331. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  332. $this->assertCount(1, $statements);
  333. $this->assertSame('alter table `users` add constraint `users_foo_id_foreign` foreign key (`foo_id`) references `orders` (`id`) on update cascade', $statements[0]);
  334. }
  335. public function testAddingIncrementingID()
  336. {
  337. $blueprint = new Blueprint('users');
  338. $blueprint->increments('id');
  339. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  340. $this->assertCount(1, $statements);
  341. $this->assertSame('alter table `users` add `id` int unsigned not null auto_increment primary key', $statements[0]);
  342. }
  343. public function testAddingSmallIncrementingID()
  344. {
  345. $blueprint = new Blueprint('users');
  346. $blueprint->smallIncrements('id');
  347. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  348. $this->assertCount(1, $statements);
  349. $this->assertSame('alter table `users` add `id` smallint unsigned not null auto_increment primary key', $statements[0]);
  350. }
  351. public function testAddingID()
  352. {
  353. $blueprint = new Blueprint('users');
  354. $blueprint->id();
  355. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  356. $this->assertCount(1, $statements);
  357. $this->assertSame('alter table `users` add `id` bigint unsigned not null auto_increment primary key', $statements[0]);
  358. $blueprint = new Blueprint('users');
  359. $blueprint->id('foo');
  360. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  361. $this->assertCount(1, $statements);
  362. $this->assertSame('alter table `users` add `foo` bigint unsigned not null auto_increment primary key', $statements[0]);
  363. }
  364. public function testAddingForeignID()
  365. {
  366. $blueprint = new Blueprint('users');
  367. $foreignId = $blueprint->foreignId('foo');
  368. $blueprint->foreignId('company_id')->constrained();
  369. $blueprint->foreignId('laravel_idea_id')->constrained();
  370. $blueprint->foreignId('team_id')->references('id')->on('teams');
  371. $blueprint->foreignId('team_column_id')->constrained('teams');
  372. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  373. $this->assertInstanceOf(ForeignIdColumnDefinition::class, $foreignId);
  374. $this->assertSame([
  375. 'alter table `users` add `foo` bigint unsigned not null, add `company_id` bigint unsigned not null, add `laravel_idea_id` bigint unsigned not null, add `team_id` bigint unsigned not null, add `team_column_id` bigint unsigned not null',
  376. 'alter table `users` add constraint `users_company_id_foreign` foreign key (`company_id`) references `companies` (`id`)',
  377. 'alter table `users` add constraint `users_laravel_idea_id_foreign` foreign key (`laravel_idea_id`) references `laravel_ideas` (`id`)',
  378. 'alter table `users` add constraint `users_team_id_foreign` foreign key (`team_id`) references `teams` (`id`)',
  379. 'alter table `users` add constraint `users_team_column_id_foreign` foreign key (`team_column_id`) references `teams` (`id`)',
  380. ], $statements);
  381. }
  382. public function testAddingBigIncrementingID()
  383. {
  384. $blueprint = new Blueprint('users');
  385. $blueprint->bigIncrements('id');
  386. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  387. $this->assertCount(1, $statements);
  388. $this->assertSame('alter table `users` add `id` bigint unsigned not null auto_increment primary key', $statements[0]);
  389. }
  390. public function testAddingColumnInTableFirst()
  391. {
  392. $blueprint = new Blueprint('users');
  393. $blueprint->string('name')->first();
  394. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  395. $this->assertCount(1, $statements);
  396. $this->assertSame('alter table `users` add `name` varchar(255) not null first', $statements[0]);
  397. }
  398. public function testAddingColumnAfterAnotherColumn()
  399. {
  400. $blueprint = new Blueprint('users');
  401. $blueprint->string('name')->after('foo');
  402. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  403. $this->assertCount(1, $statements);
  404. $this->assertSame('alter table `users` add `name` varchar(255) not null after `foo`', $statements[0]);
  405. }
  406. public function testAddingMultipleColumnsAfterAnotherColumn()
  407. {
  408. $blueprint = new Blueprint('users');
  409. $blueprint->after('foo', function ($blueprint) {
  410. $blueprint->string('one');
  411. $blueprint->string('two');
  412. });
  413. $blueprint->string('three');
  414. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  415. $this->assertCount(1, $statements);
  416. $this->assertSame('alter table `users` add `one` varchar(255) not null after `foo`, add `two` varchar(255) not null after `one`, add `three` varchar(255) not null', $statements[0]);
  417. }
  418. public function testAddingGeneratedColumn()
  419. {
  420. $blueprint = new Blueprint('products');
  421. $blueprint->integer('price');
  422. $blueprint->integer('discounted_virtual')->virtualAs('price - 5');
  423. $blueprint->integer('discounted_stored')->storedAs('price - 5');
  424. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  425. $this->assertCount(1, $statements);
  426. $this->assertSame('alter table `products` add `price` int not null, add `discounted_virtual` int as (price - 5), add `discounted_stored` int as (price - 5) stored', $statements[0]);
  427. $blueprint = new Blueprint('products');
  428. $blueprint->integer('price');
  429. $blueprint->integer('discounted_virtual')->virtualAs('price - 5')->nullable(false);
  430. $blueprint->integer('discounted_stored')->storedAs('price - 5')->nullable(false);
  431. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  432. $this->assertCount(1, $statements);
  433. $this->assertSame('alter table `products` add `price` int not null, add `discounted_virtual` int as (price - 5) not null, add `discounted_stored` int as (price - 5) stored not null', $statements[0]);
  434. }
  435. public function testAddingGeneratedColumnWithCharset()
  436. {
  437. $blueprint = new Blueprint('links');
  438. $blueprint->string('url', 2083)->charset('ascii');
  439. $blueprint->string('url_hash_virtual', 64)->virtualAs('sha2(url, 256)')->charset('ascii');
  440. $blueprint->string('url_hash_stored', 64)->storedAs('sha2(url, 256)')->charset('ascii');
  441. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  442. $this->assertCount(1, $statements);
  443. $this->assertSame('alter table `links` add `url` varchar(2083) character set ascii not null, add `url_hash_virtual` varchar(64) character set ascii as (sha2(url, 256)), add `url_hash_stored` varchar(64) character set ascii as (sha2(url, 256)) stored', $statements[0]);
  444. }
  445. public function testAddingInvisibleColumn()
  446. {
  447. $blueprint = new Blueprint('users');
  448. $blueprint->string('secret', 64)->nullable(false)->invisible();
  449. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  450. $this->assertCount(1, $statements);
  451. $this->assertSame('alter table `users` add `secret` varchar(64) not null invisible', $statements[0]);
  452. }
  453. public function testAddingString()
  454. {
  455. $blueprint = new Blueprint('users');
  456. $blueprint->string('foo');
  457. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  458. $this->assertCount(1, $statements);
  459. $this->assertSame('alter table `users` add `foo` varchar(255) not null', $statements[0]);
  460. $blueprint = new Blueprint('users');
  461. $blueprint->string('foo', 100);
  462. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  463. $this->assertCount(1, $statements);
  464. $this->assertSame('alter table `users` add `foo` varchar(100) not null', $statements[0]);
  465. $blueprint = new Blueprint('users');
  466. $blueprint->string('foo', 100)->nullable()->default('bar');
  467. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  468. $this->assertCount(1, $statements);
  469. $this->assertSame('alter table `users` add `foo` varchar(100) null default \'bar\'', $statements[0]);
  470. $blueprint = new Blueprint('users');
  471. $blueprint->string('foo', 100)->nullable()->default(new Expression('CURRENT TIMESTAMP'));
  472. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  473. $this->assertCount(1, $statements);
  474. $this->assertSame('alter table `users` add `foo` varchar(100) null default CURRENT TIMESTAMP', $statements[0]);
  475. }
  476. public function testAddingText()
  477. {
  478. $blueprint = new Blueprint('users');
  479. $blueprint->text('foo');
  480. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  481. $this->assertCount(1, $statements);
  482. $this->assertSame('alter table `users` add `foo` text not null', $statements[0]);
  483. }
  484. public function testAddingBigInteger()
  485. {
  486. $blueprint = new Blueprint('users');
  487. $blueprint->bigInteger('foo');
  488. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  489. $this->assertCount(1, $statements);
  490. $this->assertSame('alter table `users` add `foo` bigint not null', $statements[0]);
  491. $blueprint = new Blueprint('users');
  492. $blueprint->bigInteger('foo', true);
  493. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  494. $this->assertCount(1, $statements);
  495. $this->assertSame('alter table `users` add `foo` bigint not null auto_increment primary key', $statements[0]);
  496. }
  497. public function testAddingInteger()
  498. {
  499. $blueprint = new Blueprint('users');
  500. $blueprint->integer('foo');
  501. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  502. $this->assertCount(1, $statements);
  503. $this->assertSame('alter table `users` add `foo` int not null', $statements[0]);
  504. $blueprint = new Blueprint('users');
  505. $blueprint->integer('foo', true);
  506. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  507. $this->assertCount(1, $statements);
  508. $this->assertSame('alter table `users` add `foo` int not null auto_increment primary key', $statements[0]);
  509. }
  510. public function testAddingIncrementsWithStartingValues()
  511. {
  512. $blueprint = new Blueprint('users');
  513. $blueprint->id()->startingValue(1000);
  514. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  515. $this->assertCount(2, $statements);
  516. $this->assertSame('alter table `users` add `id` bigint unsigned not null auto_increment primary key', $statements[0]);
  517. $this->assertSame('alter table `users` auto_increment = 1000', $statements[1]);
  518. }
  519. public function testAddingMediumInteger()
  520. {
  521. $blueprint = new Blueprint('users');
  522. $blueprint->mediumInteger('foo');
  523. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  524. $this->assertCount(1, $statements);
  525. $this->assertSame('alter table `users` add `foo` mediumint not null', $statements[0]);
  526. $blueprint = new Blueprint('users');
  527. $blueprint->mediumInteger('foo', true);
  528. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  529. $this->assertCount(1, $statements);
  530. $this->assertSame('alter table `users` add `foo` mediumint not null auto_increment primary key', $statements[0]);
  531. }
  532. public function testAddingSmallInteger()
  533. {
  534. $blueprint = new Blueprint('users');
  535. $blueprint->smallInteger('foo');
  536. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  537. $this->assertCount(1, $statements);
  538. $this->assertSame('alter table `users` add `foo` smallint not null', $statements[0]);
  539. $blueprint = new Blueprint('users');
  540. $blueprint->smallInteger('foo', true);
  541. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  542. $this->assertCount(1, $statements);
  543. $this->assertSame('alter table `users` add `foo` smallint not null auto_increment primary key', $statements[0]);
  544. }
  545. public function testAddingTinyInteger()
  546. {
  547. $blueprint = new Blueprint('users');
  548. $blueprint->tinyInteger('foo');
  549. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  550. $this->assertCount(1, $statements);
  551. $this->assertSame('alter table `users` add `foo` tinyint not null', $statements[0]);
  552. $blueprint = new Blueprint('users');
  553. $blueprint->tinyInteger('foo', true);
  554. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  555. $this->assertCount(1, $statements);
  556. $this->assertSame('alter table `users` add `foo` tinyint not null auto_increment primary key', $statements[0]);
  557. }
  558. public function testAddingFloat()
  559. {
  560. $blueprint = new Blueprint('users');
  561. $blueprint->float('foo', 5, 2);
  562. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  563. $this->assertCount(1, $statements);
  564. $this->assertSame('alter table `users` add `foo` double(5, 2) not null', $statements[0]);
  565. }
  566. public function testAddingDouble()
  567. {
  568. $blueprint = new Blueprint('users');
  569. $blueprint->double('foo');
  570. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  571. $this->assertCount(1, $statements);
  572. $this->assertSame('alter table `users` add `foo` double not null', $statements[0]);
  573. }
  574. public function testAddingDoubleSpecifyingPrecision()
  575. {
  576. $blueprint = new Blueprint('users');
  577. $blueprint->double('foo', 15, 8);
  578. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  579. $this->assertCount(1, $statements);
  580. $this->assertSame('alter table `users` add `foo` double(15, 8) not null', $statements[0]);
  581. }
  582. public function testAddingDecimal()
  583. {
  584. $blueprint = new Blueprint('users');
  585. $blueprint->decimal('foo', 5, 2);
  586. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  587. $this->assertCount(1, $statements);
  588. $this->assertSame('alter table `users` add `foo` decimal(5, 2) not null', $statements[0]);
  589. }
  590. public function testAddingBoolean()
  591. {
  592. $blueprint = new Blueprint('users');
  593. $blueprint->boolean('foo');
  594. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  595. $this->assertCount(1, $statements);
  596. $this->assertSame('alter table `users` add `foo` tinyint(1) not null', $statements[0]);
  597. }
  598. public function testAddingEnum()
  599. {
  600. $blueprint = new Blueprint('users');
  601. $blueprint->enum('role', ['member', 'admin']);
  602. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  603. $this->assertCount(1, $statements);
  604. $this->assertSame('alter table `users` add `role` enum(\'member\', \'admin\') not null', $statements[0]);
  605. }
  606. public function testAddingSet()
  607. {
  608. $blueprint = new Blueprint('users');
  609. $blueprint->set('role', ['member', 'admin']);
  610. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  611. $this->assertCount(1, $statements);
  612. $this->assertSame('alter table `users` add `role` set(\'member\', \'admin\') not null', $statements[0]);
  613. }
  614. public function testAddingJson()
  615. {
  616. $blueprint = new Blueprint('users');
  617. $blueprint->json('foo');
  618. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  619. $this->assertCount(1, $statements);
  620. $this->assertSame('alter table `users` add `foo` json not null', $statements[0]);
  621. }
  622. public function testAddingJsonb()
  623. {
  624. $blueprint = new Blueprint('users');
  625. $blueprint->jsonb('foo');
  626. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  627. $this->assertCount(1, $statements);
  628. $this->assertSame('alter table `users` add `foo` json not null', $statements[0]);
  629. }
  630. public function testAddingDate()
  631. {
  632. $blueprint = new Blueprint('users');
  633. $blueprint->date('foo');
  634. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  635. $this->assertCount(1, $statements);
  636. $this->assertSame('alter table `users` add `foo` date not null', $statements[0]);
  637. }
  638. public function testAddingYear()
  639. {
  640. $blueprint = new Blueprint('users');
  641. $blueprint->year('birth_year');
  642. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  643. $this->assertCount(1, $statements);
  644. $this->assertSame('alter table `users` add `birth_year` year not null', $statements[0]);
  645. }
  646. public function testAddingDateTime()
  647. {
  648. $blueprint = new Blueprint('users');
  649. $blueprint->dateTime('foo');
  650. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  651. $this->assertCount(1, $statements);
  652. $this->assertSame('alter table `users` add `foo` datetime not null', $statements[0]);
  653. $blueprint = new Blueprint('users');
  654. $blueprint->dateTime('foo', 1);
  655. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  656. $this->assertCount(1, $statements);
  657. $this->assertSame('alter table `users` add `foo` datetime(1) not null', $statements[0]);
  658. }
  659. public function testAddingDateTimeWithDefaultCurrent()
  660. {
  661. $blueprint = new Blueprint('users');
  662. $blueprint->dateTime('foo')->useCurrent();
  663. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  664. $this->assertCount(1, $statements);
  665. $this->assertSame('alter table `users` add `foo` datetime default CURRENT_TIMESTAMP not null', $statements[0]);
  666. }
  667. public function testAddingDateTimeWithOnUpdateCurrent()
  668. {
  669. $blueprint = new Blueprint('users');
  670. $blueprint->dateTime('foo')->useCurrentOnUpdate();
  671. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  672. $this->assertCount(1, $statements);
  673. $this->assertSame('alter table `users` add `foo` datetime on update CURRENT_TIMESTAMP not null', $statements[0]);
  674. }
  675. public function testAddingDateTimeWithDefaultCurrentAndOnUpdateCurrent()
  676. {
  677. $blueprint = new Blueprint('users');
  678. $blueprint->dateTime('foo')->useCurrent()->useCurrentOnUpdate();
  679. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  680. $this->assertCount(1, $statements);
  681. $this->assertSame('alter table `users` add `foo` datetime default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP not null', $statements[0]);
  682. }
  683. public function testAddingDateTimeWithDefaultCurrentOnUpdateCurrentAndPrecision()
  684. {
  685. $blueprint = new Blueprint('users');
  686. $blueprint->dateTime('foo', 3)->useCurrent()->useCurrentOnUpdate();
  687. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  688. $this->assertCount(1, $statements);
  689. $this->assertSame('alter table `users` add `foo` datetime(3) default CURRENT_TIMESTAMP(3) on update CURRENT_TIMESTAMP(3) not null', $statements[0]);
  690. }
  691. public function testAddingDateTimeTz()
  692. {
  693. $blueprint = new Blueprint('users');
  694. $blueprint->dateTimeTz('foo', 1);
  695. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  696. $this->assertCount(1, $statements);
  697. $this->assertSame('alter table `users` add `foo` datetime(1) not null', $statements[0]);
  698. $blueprint = new Blueprint('users');
  699. $blueprint->dateTimeTz('foo');
  700. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  701. $this->assertCount(1, $statements);
  702. $this->assertSame('alter table `users` add `foo` datetime not null', $statements[0]);
  703. }
  704. public function testAddingTime()
  705. {
  706. $blueprint = new Blueprint('users');
  707. $blueprint->time('created_at');
  708. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  709. $this->assertCount(1, $statements);
  710. $this->assertSame('alter table `users` add `created_at` time not null', $statements[0]);
  711. }
  712. public function testAddingTimeWithPrecision()
  713. {
  714. $blueprint = new Blueprint('users');
  715. $blueprint->time('created_at', 1);
  716. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  717. $this->assertCount(1, $statements);
  718. $this->assertSame('alter table `users` add `created_at` time(1) not null', $statements[0]);
  719. }
  720. public function testAddingTimeTz()
  721. {
  722. $blueprint = new Blueprint('users');
  723. $blueprint->timeTz('created_at');
  724. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  725. $this->assertCount(1, $statements);
  726. $this->assertSame('alter table `users` add `created_at` time not null', $statements[0]);
  727. }
  728. public function testAddingTimeTzWithPrecision()
  729. {
  730. $blueprint = new Blueprint('users');
  731. $blueprint->timeTz('created_at', 1);
  732. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  733. $this->assertCount(1, $statements);
  734. $this->assertSame('alter table `users` add `created_at` time(1) not null', $statements[0]);
  735. }
  736. public function testAddingTimestamp()
  737. {
  738. $blueprint = new Blueprint('users');
  739. $blueprint->timestamp('created_at');
  740. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  741. $this->assertCount(1, $statements);
  742. $this->assertSame('alter table `users` add `created_at` timestamp not null', $statements[0]);
  743. }
  744. public function testAddingTimestampWithPrecision()
  745. {
  746. $blueprint = new Blueprint('users');
  747. $blueprint->timestamp('created_at', 1);
  748. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  749. $this->assertCount(1, $statements);
  750. $this->assertSame('alter table `users` add `created_at` timestamp(1) not null', $statements[0]);
  751. }
  752. public function testAddingTimestampWithDefault()
  753. {
  754. $blueprint = new Blueprint('users');
  755. $blueprint->timestamp('created_at')->default('2015-07-22 11:43:17');
  756. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  757. $this->assertCount(1, $statements);
  758. $this->assertSame("alter table `users` add `created_at` timestamp not null default '2015-07-22 11:43:17'", $statements[0]);
  759. }
  760. public function testAddingTimestampWithDefaultCurrentSpecifyingPrecision()
  761. {
  762. $blueprint = new Blueprint('users');
  763. $blueprint->timestamp('created_at', 1)->useCurrent();
  764. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  765. $this->assertCount(1, $statements);
  766. $this->assertSame('alter table `users` add `created_at` timestamp(1) default CURRENT_TIMESTAMP(1) not null', $statements[0]);
  767. }
  768. public function testAddingTimestampWithOnUpdateCurrentSpecifyingPrecision()
  769. {
  770. $blueprint = new Blueprint('users');
  771. $blueprint->timestamp('created_at', 1)->useCurrentOnUpdate();
  772. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  773. $this->assertCount(1, $statements);
  774. $this->assertSame('alter table `users` add `created_at` timestamp(1) on update CURRENT_TIMESTAMP(1) not null', $statements[0]);
  775. }
  776. public function testAddingTimestampWithDefaultCurrentAndOnUpdateCurrentSpecifyingPrecision()
  777. {
  778. $blueprint = new Blueprint('users');
  779. $blueprint->timestamp('created_at', 1)->useCurrent()->useCurrentOnUpdate();
  780. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  781. $this->assertCount(1, $statements);
  782. $this->assertSame('alter table `users` add `created_at` timestamp(1) default CURRENT_TIMESTAMP(1) on update CURRENT_TIMESTAMP(1) not null', $statements[0]);
  783. }
  784. public function testAddingTimestampTz()
  785. {
  786. $blueprint = new Blueprint('users');
  787. $blueprint->timestampTz('created_at');
  788. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  789. $this->assertCount(1, $statements);
  790. $this->assertSame('alter table `users` add `created_at` timestamp not null', $statements[0]);
  791. }
  792. public function testAddingTimestampTzWithPrecision()
  793. {
  794. $blueprint = new Blueprint('users');
  795. $blueprint->timestampTz('created_at', 1);
  796. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  797. $this->assertCount(1, $statements);
  798. $this->assertSame('alter table `users` add `created_at` timestamp(1) not null', $statements[0]);
  799. }
  800. public function testAddingTimeStampTzWithDefault()
  801. {
  802. $blueprint = new Blueprint('users');
  803. $blueprint->timestampTz('created_at')->default('2015-07-22 11:43:17');
  804. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  805. $this->assertCount(1, $statements);
  806. $this->assertSame("alter table `users` add `created_at` timestamp not null default '2015-07-22 11:43:17'", $statements[0]);
  807. }
  808. public function testAddingTimestamps()
  809. {
  810. $blueprint = new Blueprint('users');
  811. $blueprint->timestamps();
  812. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  813. $this->assertCount(1, $statements);
  814. $this->assertSame('alter table `users` add `created_at` timestamp null, add `updated_at` timestamp null', $statements[0]);
  815. }
  816. public function testAddingTimestampsTz()
  817. {
  818. $blueprint = new Blueprint('users');
  819. $blueprint->timestampsTz();
  820. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  821. $this->assertCount(1, $statements);
  822. $this->assertSame('alter table `users` add `created_at` timestamp null, add `updated_at` timestamp null', $statements[0]);
  823. }
  824. public function testAddingRememberToken()
  825. {
  826. $blueprint = new Blueprint('users');
  827. $blueprint->rememberToken();
  828. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  829. $this->assertCount(1, $statements);
  830. $this->assertSame('alter table `users` add `remember_token` varchar(100) null', $statements[0]);
  831. }
  832. public function testAddingBinary()
  833. {
  834. $blueprint = new Blueprint('users');
  835. $blueprint->binary('foo');
  836. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  837. $this->assertCount(1, $statements);
  838. $this->assertSame('alter table `users` add `foo` blob not null', $statements[0]);
  839. }
  840. public function testAddingUuid()
  841. {
  842. $blueprint = new Blueprint('users');
  843. $blueprint->uuid('foo');
  844. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  845. $this->assertCount(1, $statements);
  846. $this->assertSame('alter table `users` add `foo` char(36) not null', $statements[0]);
  847. }
  848. public function testAddingForeignUuid()
  849. {
  850. $blueprint = new Blueprint('users');
  851. $foreignUuid = $blueprint->foreignUuid('foo');
  852. $blueprint->foreignUuid('company_id')->constrained();
  853. $blueprint->foreignUuid('laravel_idea_id')->constrained();
  854. $blueprint->foreignUuid('team_id')->references('id')->on('teams');
  855. $blueprint->foreignUuid('team_column_id')->constrained('teams');
  856. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  857. $this->assertInstanceOf(ForeignIdColumnDefinition::class, $foreignUuid);
  858. $this->assertSame([
  859. 'alter table `users` add `foo` char(36) not null, add `company_id` char(36) not null, add `laravel_idea_id` char(36) not null, add `team_id` char(36) not null, add `team_column_id` char(36) not null',
  860. 'alter table `users` add constraint `users_company_id_foreign` foreign key (`company_id`) references `companies` (`id`)',
  861. 'alter table `users` add constraint `users_laravel_idea_id_foreign` foreign key (`laravel_idea_id`) references `laravel_ideas` (`id`)',
  862. 'alter table `users` add constraint `users_team_id_foreign` foreign key (`team_id`) references `teams` (`id`)',
  863. 'alter table `users` add constraint `users_team_column_id_foreign` foreign key (`team_column_id`) references `teams` (`id`)',
  864. ], $statements);
  865. }
  866. public function testAddingIpAddress()
  867. {
  868. $blueprint = new Blueprint('users');
  869. $blueprint->ipAddress('foo');
  870. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  871. $this->assertCount(1, $statements);
  872. $this->assertSame('alter table `users` add `foo` varchar(45) not null', $statements[0]);
  873. }
  874. public function testAddingMacAddress()
  875. {
  876. $blueprint = new Blueprint('users');
  877. $blueprint->macAddress('foo');
  878. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  879. $this->assertCount(1, $statements);
  880. $this->assertSame('alter table `users` add `foo` varchar(17) not null', $statements[0]);
  881. }
  882. public function testAddingGeometry()
  883. {
  884. $blueprint = new Blueprint('geo');
  885. $blueprint->geometry('coordinates');
  886. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  887. $this->assertCount(1, $statements);
  888. $this->assertSame('alter table `geo` add `coordinates` geometry not null', $statements[0]);
  889. }
  890. public function testAddingPoint()
  891. {
  892. $blueprint = new Blueprint('geo');
  893. $blueprint->point('coordinates');
  894. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  895. $this->assertCount(1, $statements);
  896. $this->assertSame('alter table `geo` add `coordinates` point not null', $statements[0]);
  897. }
  898. public function testAddingPointWithSrid()
  899. {
  900. $blueprint = new Blueprint('geo');
  901. $blueprint->point('coordinates', 4326);
  902. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  903. $this->assertCount(1, $statements);
  904. $this->assertSame('alter table `geo` add `coordinates` point not null srid 4326', $statements[0]);
  905. }
  906. public function testAddingPointWithSridColumn()
  907. {
  908. $blueprint = new Blueprint('geo');
  909. $blueprint->point('coordinates', 4326)->after('id');
  910. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  911. $this->assertCount(1, $statements);
  912. $this->assertSame('alter table `geo` add `coordinates` point not null srid 4326 after `id`', $statements[0]);
  913. }
  914. public function testAddingLineString()
  915. {
  916. $blueprint = new Blueprint('geo');
  917. $blueprint->linestring('coordinates');
  918. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  919. $this->assertCount(1, $statements);
  920. $this->assertSame('alter table `geo` add `coordinates` linestring not null', $statements[0]);
  921. }
  922. public function testAddingPolygon()
  923. {
  924. $blueprint = new Blueprint('geo');
  925. $blueprint->polygon('coordinates');
  926. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  927. $this->assertCount(1, $statements);
  928. $this->assertSame('alter table `geo` add `coordinates` polygon not null', $statements[0]);
  929. }
  930. public function testAddingGeometryCollection()
  931. {
  932. $blueprint = new Blueprint('geo');
  933. $blueprint->geometrycollection('coordinates');
  934. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  935. $this->assertCount(1, $statements);
  936. $this->assertSame('alter table `geo` add `coordinates` geometrycollection not null', $statements[0]);
  937. }
  938. public function testAddingMultiPoint()
  939. {
  940. $blueprint = new Blueprint('geo');
  941. $blueprint->multipoint('coordinates');
  942. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  943. $this->assertCount(1, $statements);
  944. $this->assertSame('alter table `geo` add `coordinates` multipoint not null', $statements[0]);
  945. }
  946. public function testAddingMultiLineString()
  947. {
  948. $blueprint = new Blueprint('geo');
  949. $blueprint->multilinestring('coordinates');
  950. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  951. $this->assertCount(1, $statements);
  952. $this->assertSame('alter table `geo` add `coordinates` multilinestring not null', $statements[0]);
  953. }
  954. public function testAddingMultiPolygon()
  955. {
  956. $blueprint = new Blueprint('geo');
  957. $blueprint->multipolygon('coordinates');
  958. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  959. $this->assertCount(1, $statements);
  960. $this->assertSame('alter table `geo` add `coordinates` multipolygon not null', $statements[0]);
  961. }
  962. public function testAddingComment()
  963. {
  964. $blueprint = new Blueprint('users');
  965. $blueprint->string('foo')->comment("Escape ' when using words like it's");
  966. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  967. $this->assertCount(1, $statements);
  968. $this->assertSame("alter table `users` add `foo` varchar(255) not null comment 'Escape \\' when using words like it\\'s'", $statements[0]);
  969. }
  970. public function testDropAllTables()
  971. {
  972. $statement = $this->getGrammar()->compileDropAllTables(['alpha', 'beta', 'gamma']);
  973. $this->assertSame('drop table `alpha`,`beta`,`gamma`', $statement);
  974. }
  975. public function testDropAllViews()
  976. {
  977. $statement = $this->getGrammar()->compileDropAllViews(['alpha', 'beta', 'gamma']);
  978. $this->assertSame('drop view `alpha`,`beta`,`gamma`', $statement);
  979. }
  980. public function testGrammarsAreMacroable()
  981. {
  982. // compileReplace macro.
  983. $this->getGrammar()::macro('compileReplace', function () {
  984. return true;
  985. });
  986. $c = $this->getGrammar()::compileReplace();
  987. $this->assertTrue($c);
  988. }
  989. public function testCreateDatabase()
  990. {
  991. $connection = $this->getConnection();
  992. $connection->shouldReceive('getConfig')->once()->once()->with('charset')->andReturn('utf8mb4_foo');
  993. $connection->shouldReceive('getConfig')->once()->once()->with('collation')->andReturn('utf8mb4_unicode_ci_foo');
  994. $statement = $this->getGrammar()->compileCreateDatabase('my_database_a', $connection);
  995. $this->assertSame(
  996. 'create database `my_database_a` default character set `utf8mb4_foo` default collate `utf8mb4_unicode_ci_foo`',
  997. $statement
  998. );
  999. $connection = $this->getConnection();
  1000. $connection->shouldReceive('getConfig')->once()->once()->with('charset')->andReturn('utf8mb4_bar');
  1001. $connection->shouldReceive('getConfig')->once()->once()->with('collation')->andReturn('utf8mb4_unicode_ci_bar');
  1002. $statement = $this->getGrammar()->compileCreateDatabase('my_database_b', $connection);
  1003. $this->assertSame(
  1004. 'create database `my_database_b` default character set `utf8mb4_bar` default collate `utf8mb4_unicode_ci_bar`',
  1005. $statement
  1006. );
  1007. }
  1008. public function testDropDatabaseIfExists()
  1009. {
  1010. $statement = $this->getGrammar()->compileDropDatabaseIfExists('my_database_a');
  1011. $this->assertSame(
  1012. 'drop database if exists `my_database_a`',
  1013. $statement
  1014. );
  1015. $statement = $this->getGrammar()->compileDropDatabaseIfExists('my_database_b');
  1016. $this->assertSame(
  1017. 'drop database if exists `my_database_b`',
  1018. $statement
  1019. );
  1020. }
  1021. protected function getConnection()
  1022. {
  1023. return m::mock(Connection::class);
  1024. }
  1025. public function getGrammar()
  1026. {
  1027. return new MySqlGrammar;
  1028. }
  1029. }