DatabaseSQLiteSchemaGrammarTest.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Illuminate\Database\Capsule\Manager;
  4. use Illuminate\Database\Connection;
  5. use Illuminate\Database\Schema\Blueprint;
  6. use Illuminate\Database\Schema\ForeignIdColumnDefinition;
  7. use Illuminate\Database\Schema\Grammars\SQLiteGrammar;
  8. use Mockery as m;
  9. use PHPUnit\Framework\TestCase;
  10. use RuntimeException;
  11. class DatabaseSQLiteSchemaGrammarTest extends TestCase
  12. {
  13. protected function tearDown(): void
  14. {
  15. m::close();
  16. }
  17. public function testBasicCreateTable()
  18. {
  19. $blueprint = new Blueprint('users');
  20. $blueprint->create();
  21. $blueprint->increments('id');
  22. $blueprint->string('email');
  23. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  24. $this->assertCount(1, $statements);
  25. $this->assertSame('create table "users" ("id" integer not null primary key autoincrement, "email" varchar not null)', $statements[0]);
  26. $blueprint = new Blueprint('users');
  27. $blueprint->increments('id');
  28. $blueprint->string('email');
  29. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  30. $this->assertCount(2, $statements);
  31. $expected = [
  32. 'alter table "users" add column "id" integer not null primary key autoincrement',
  33. 'alter table "users" add column "email" varchar not null',
  34. ];
  35. $this->assertEquals($expected, $statements);
  36. }
  37. public function testCreateTemporaryTable()
  38. {
  39. $blueprint = new Blueprint('users');
  40. $blueprint->create();
  41. $blueprint->temporary();
  42. $blueprint->increments('id');
  43. $blueprint->string('email');
  44. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  45. $this->assertCount(1, $statements);
  46. $this->assertSame('create temporary table "users" ("id" integer not null primary key autoincrement, "email" varchar not null)', $statements[0]);
  47. }
  48. public function testDropTable()
  49. {
  50. $blueprint = new Blueprint('users');
  51. $blueprint->drop();
  52. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  53. $this->assertCount(1, $statements);
  54. $this->assertSame('drop table "users"', $statements[0]);
  55. }
  56. public function testDropTableIfExists()
  57. {
  58. $blueprint = new Blueprint('users');
  59. $blueprint->dropIfExists();
  60. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  61. $this->assertCount(1, $statements);
  62. $this->assertSame('drop table if exists "users"', $statements[0]);
  63. }
  64. public function testDropUnique()
  65. {
  66. $blueprint = new Blueprint('users');
  67. $blueprint->dropUnique('foo');
  68. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  69. $this->assertCount(1, $statements);
  70. $this->assertSame('drop index "foo"', $statements[0]);
  71. }
  72. public function testDropIndex()
  73. {
  74. $blueprint = new Blueprint('users');
  75. $blueprint->dropIndex('foo');
  76. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  77. $this->assertCount(1, $statements);
  78. $this->assertSame('drop index "foo"', $statements[0]);
  79. }
  80. public function testDropColumn()
  81. {
  82. $db = new Manager;
  83. $db->addConnection([
  84. 'driver' => 'sqlite',
  85. 'database' => ':memory:',
  86. 'prefix' => 'prefix_',
  87. ]);
  88. $schema = $db->getConnection()->getSchemaBuilder();
  89. $schema->create('users', function (Blueprint $table) {
  90. $table->string('email');
  91. $table->string('name');
  92. });
  93. $this->assertTrue($schema->hasTable('users'));
  94. $this->assertTrue($schema->hasColumn('users', 'name'));
  95. $schema->table('users', function (Blueprint $table) {
  96. $table->dropColumn('name');
  97. });
  98. $this->assertFalse($schema->hasColumn('users', 'name'));
  99. }
  100. public function testDropSpatialIndex()
  101. {
  102. $this->expectException(RuntimeException::class);
  103. $this->expectExceptionMessage('The database driver in use does not support spatial indexes.');
  104. $blueprint = new Blueprint('geo');
  105. $blueprint->dropSpatialIndex(['coordinates']);
  106. $blueprint->toSql($this->getConnection(), $this->getGrammar());
  107. }
  108. public function testRenameTable()
  109. {
  110. $blueprint = new Blueprint('users');
  111. $blueprint->rename('foo');
  112. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  113. $this->assertCount(1, $statements);
  114. $this->assertSame('alter table "users" rename to "foo"', $statements[0]);
  115. }
  116. public function testRenameIndex()
  117. {
  118. $db = new Manager;
  119. $db->addConnection([
  120. 'driver' => 'sqlite',
  121. 'database' => ':memory:',
  122. 'prefix' => 'prefix_',
  123. ]);
  124. $schema = $db->getConnection()->getSchemaBuilder();
  125. $schema->create('users', function (Blueprint $table) {
  126. $table->string('name');
  127. $table->string('email');
  128. });
  129. $schema->table('users', function (Blueprint $table) {
  130. $table->index(['name', 'email'], 'index1');
  131. });
  132. $manager = $db->getConnection()->getDoctrineSchemaManager();
  133. $details = $manager->listTableDetails('prefix_users');
  134. $this->assertTrue($details->hasIndex('index1'));
  135. $this->assertFalse($details->hasIndex('index2'));
  136. $schema->table('users', function (Blueprint $table) {
  137. $table->renameIndex('index1', 'index2');
  138. });
  139. $details = $manager->listTableDetails('prefix_users');
  140. $this->assertFalse($details->hasIndex('index1'));
  141. $this->assertTrue($details->hasIndex('index2'));
  142. $this->assertEquals(['name', 'email'], $details->getIndex('index2')->getUnquotedColumns());
  143. }
  144. public function testAddingPrimaryKey()
  145. {
  146. $blueprint = new Blueprint('users');
  147. $blueprint->create();
  148. $blueprint->string('foo')->primary();
  149. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  150. $this->assertCount(1, $statements);
  151. $this->assertSame('create table "users" ("foo" varchar not null, primary key ("foo"))', $statements[0]);
  152. }
  153. public function testAddingForeignKey()
  154. {
  155. $blueprint = new Blueprint('users');
  156. $blueprint->create();
  157. $blueprint->string('foo')->primary();
  158. $blueprint->string('order_id');
  159. $blueprint->foreign('order_id')->references('id')->on('orders');
  160. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  161. $this->assertCount(1, $statements);
  162. $this->assertSame('create table "users" ("foo" varchar not null, "order_id" varchar not null, foreign key("order_id") references "orders"("id"), primary key ("foo"))', $statements[0]);
  163. }
  164. public function testAddingUniqueKey()
  165. {
  166. $blueprint = new Blueprint('users');
  167. $blueprint->unique('foo', 'bar');
  168. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  169. $this->assertCount(1, $statements);
  170. $this->assertSame('create unique index "bar" on "users" ("foo")', $statements[0]);
  171. }
  172. public function testAddingIndex()
  173. {
  174. $blueprint = new Blueprint('users');
  175. $blueprint->index(['foo', 'bar'], 'baz');
  176. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  177. $this->assertCount(1, $statements);
  178. $this->assertSame('create index "baz" on "users" ("foo", "bar")', $statements[0]);
  179. }
  180. public function testAddingSpatialIndex()
  181. {
  182. $this->expectException(RuntimeException::class);
  183. $this->expectExceptionMessage('The database driver in use does not support spatial indexes.');
  184. $blueprint = new Blueprint('geo');
  185. $blueprint->spatialIndex('coordinates');
  186. $blueprint->toSql($this->getConnection(), $this->getGrammar());
  187. }
  188. public function testAddingFluentSpatialIndex()
  189. {
  190. $this->expectException(RuntimeException::class);
  191. $this->expectExceptionMessage('The database driver in use does not support spatial indexes.');
  192. $blueprint = new Blueprint('geo');
  193. $blueprint->point('coordinates')->spatialIndex();
  194. $blueprint->toSql($this->getConnection(), $this->getGrammar());
  195. }
  196. public function testAddingRawIndex()
  197. {
  198. $blueprint = new Blueprint('users');
  199. $blueprint->rawIndex('(function(column))', 'raw_index');
  200. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  201. $this->assertCount(1, $statements);
  202. $this->assertSame('create index "raw_index" on "users" ((function(column)))', $statements[0]);
  203. }
  204. public function testAddingIncrementingID()
  205. {
  206. $blueprint = new Blueprint('users');
  207. $blueprint->increments('id');
  208. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  209. $this->assertCount(1, $statements);
  210. $this->assertSame('alter table "users" add column "id" integer not null primary key autoincrement', $statements[0]);
  211. }
  212. public function testAddingSmallIncrementingID()
  213. {
  214. $blueprint = new Blueprint('users');
  215. $blueprint->smallIncrements('id');
  216. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  217. $this->assertCount(1, $statements);
  218. $this->assertSame('alter table "users" add column "id" integer not null primary key autoincrement', $statements[0]);
  219. }
  220. public function testAddingMediumIncrementingID()
  221. {
  222. $blueprint = new Blueprint('users');
  223. $blueprint->mediumIncrements('id');
  224. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  225. $this->assertCount(1, $statements);
  226. $this->assertSame('alter table "users" add column "id" integer not null primary key autoincrement', $statements[0]);
  227. }
  228. public function testAddingID()
  229. {
  230. $blueprint = new Blueprint('users');
  231. $blueprint->id();
  232. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  233. $this->assertCount(1, $statements);
  234. $this->assertSame('alter table "users" add column "id" integer not null primary key autoincrement', $statements[0]);
  235. $blueprint = new Blueprint('users');
  236. $blueprint->id('foo');
  237. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  238. $this->assertCount(1, $statements);
  239. $this->assertSame('alter table "users" add column "foo" integer not null primary key autoincrement', $statements[0]);
  240. }
  241. public function testAddingForeignID()
  242. {
  243. $blueprint = new Blueprint('users');
  244. $foreignId = $blueprint->foreignId('foo');
  245. $blueprint->foreignId('company_id')->constrained();
  246. $blueprint->foreignId('laravel_idea_id')->constrained();
  247. $blueprint->foreignId('team_id')->references('id')->on('teams');
  248. $blueprint->foreignId('team_column_id')->constrained('teams');
  249. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  250. $this->assertInstanceOf(ForeignIdColumnDefinition::class, $foreignId);
  251. $this->assertSame([
  252. 'alter table "users" add column "foo" integer not null',
  253. 'alter table "users" add column "company_id" integer not null',
  254. 'alter table "users" add column "laravel_idea_id" integer not null',
  255. 'alter table "users" add column "team_id" integer not null',
  256. 'alter table "users" add column "team_column_id" integer not null',
  257. ], $statements);
  258. }
  259. public function testAddingBigIncrementingID()
  260. {
  261. $blueprint = new Blueprint('users');
  262. $blueprint->bigIncrements('id');
  263. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  264. $this->assertCount(1, $statements);
  265. $this->assertSame('alter table "users" add column "id" integer not null primary key autoincrement', $statements[0]);
  266. }
  267. public function testAddingString()
  268. {
  269. $blueprint = new Blueprint('users');
  270. $blueprint->string('foo');
  271. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  272. $this->assertCount(1, $statements);
  273. $this->assertSame('alter table "users" add column "foo" varchar not null', $statements[0]);
  274. $blueprint = new Blueprint('users');
  275. $blueprint->string('foo', 100);
  276. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  277. $this->assertCount(1, $statements);
  278. $this->assertSame('alter table "users" add column "foo" varchar not null', $statements[0]);
  279. $blueprint = new Blueprint('users');
  280. $blueprint->string('foo', 100)->nullable()->default('bar');
  281. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  282. $this->assertCount(1, $statements);
  283. $this->assertSame('alter table "users" add column "foo" varchar default \'bar\'', $statements[0]);
  284. }
  285. public function testAddingText()
  286. {
  287. $blueprint = new Blueprint('users');
  288. $blueprint->text('foo');
  289. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  290. $this->assertCount(1, $statements);
  291. $this->assertSame('alter table "users" add column "foo" text not null', $statements[0]);
  292. }
  293. public function testAddingBigInteger()
  294. {
  295. $blueprint = new Blueprint('users');
  296. $blueprint->bigInteger('foo');
  297. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  298. $this->assertCount(1, $statements);
  299. $this->assertSame('alter table "users" add column "foo" integer not null', $statements[0]);
  300. $blueprint = new Blueprint('users');
  301. $blueprint->bigInteger('foo', true);
  302. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  303. $this->assertCount(1, $statements);
  304. $this->assertSame('alter table "users" add column "foo" integer not null primary key autoincrement', $statements[0]);
  305. }
  306. public function testAddingInteger()
  307. {
  308. $blueprint = new Blueprint('users');
  309. $blueprint->integer('foo');
  310. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  311. $this->assertCount(1, $statements);
  312. $this->assertSame('alter table "users" add column "foo" integer not null', $statements[0]);
  313. $blueprint = new Blueprint('users');
  314. $blueprint->integer('foo', true);
  315. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  316. $this->assertCount(1, $statements);
  317. $this->assertSame('alter table "users" add column "foo" integer not null primary key autoincrement', $statements[0]);
  318. }
  319. public function testAddingMediumInteger()
  320. {
  321. $blueprint = new Blueprint('users');
  322. $blueprint->mediumInteger('foo');
  323. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  324. $this->assertCount(1, $statements);
  325. $this->assertSame('alter table "users" add column "foo" integer not null', $statements[0]);
  326. $blueprint = new Blueprint('users');
  327. $blueprint->mediumInteger('foo', true);
  328. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  329. $this->assertCount(1, $statements);
  330. $this->assertSame('alter table "users" add column "foo" integer not null primary key autoincrement', $statements[0]);
  331. }
  332. public function testAddingTinyInteger()
  333. {
  334. $blueprint = new Blueprint('users');
  335. $blueprint->tinyInteger('foo');
  336. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  337. $this->assertCount(1, $statements);
  338. $this->assertSame('alter table "users" add column "foo" integer not null', $statements[0]);
  339. $blueprint = new Blueprint('users');
  340. $blueprint->tinyInteger('foo', true);
  341. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  342. $this->assertCount(1, $statements);
  343. $this->assertSame('alter table "users" add column "foo" integer not null primary key autoincrement', $statements[0]);
  344. }
  345. public function testAddingSmallInteger()
  346. {
  347. $blueprint = new Blueprint('users');
  348. $blueprint->smallInteger('foo');
  349. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  350. $this->assertCount(1, $statements);
  351. $this->assertSame('alter table "users" add column "foo" integer not null', $statements[0]);
  352. $blueprint = new Blueprint('users');
  353. $blueprint->smallInteger('foo', true);
  354. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  355. $this->assertCount(1, $statements);
  356. $this->assertSame('alter table "users" add column "foo" integer not null primary key autoincrement', $statements[0]);
  357. }
  358. public function testAddingFloat()
  359. {
  360. $blueprint = new Blueprint('users');
  361. $blueprint->float('foo', 5, 2);
  362. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  363. $this->assertCount(1, $statements);
  364. $this->assertSame('alter table "users" add column "foo" float not null', $statements[0]);
  365. }
  366. public function testAddingDouble()
  367. {
  368. $blueprint = new Blueprint('users');
  369. $blueprint->double('foo', 15, 8);
  370. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  371. $this->assertCount(1, $statements);
  372. $this->assertSame('alter table "users" add column "foo" float not null', $statements[0]);
  373. }
  374. public function testAddingDecimal()
  375. {
  376. $blueprint = new Blueprint('users');
  377. $blueprint->decimal('foo', 5, 2);
  378. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  379. $this->assertCount(1, $statements);
  380. $this->assertSame('alter table "users" add column "foo" numeric not null', $statements[0]);
  381. }
  382. public function testAddingBoolean()
  383. {
  384. $blueprint = new Blueprint('users');
  385. $blueprint->boolean('foo');
  386. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  387. $this->assertCount(1, $statements);
  388. $this->assertSame('alter table "users" add column "foo" tinyint(1) not null', $statements[0]);
  389. }
  390. public function testAddingEnum()
  391. {
  392. $blueprint = new Blueprint('users');
  393. $blueprint->enum('role', ['member', 'admin']);
  394. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  395. $this->assertCount(1, $statements);
  396. $this->assertSame('alter table "users" add column "role" varchar check ("role" in (\'member\', \'admin\')) not null', $statements[0]);
  397. }
  398. public function testAddingJson()
  399. {
  400. $blueprint = new Blueprint('users');
  401. $blueprint->json('foo');
  402. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  403. $this->assertCount(1, $statements);
  404. $this->assertSame('alter table "users" add column "foo" text not null', $statements[0]);
  405. }
  406. public function testAddingJsonb()
  407. {
  408. $blueprint = new Blueprint('users');
  409. $blueprint->jsonb('foo');
  410. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  411. $this->assertCount(1, $statements);
  412. $this->assertSame('alter table "users" add column "foo" text not null', $statements[0]);
  413. }
  414. public function testAddingDate()
  415. {
  416. $blueprint = new Blueprint('users');
  417. $blueprint->date('foo');
  418. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  419. $this->assertCount(1, $statements);
  420. $this->assertSame('alter table "users" add column "foo" date not null', $statements[0]);
  421. }
  422. public function testAddingYear()
  423. {
  424. $blueprint = new Blueprint('users');
  425. $blueprint->year('birth_year');
  426. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  427. $this->assertCount(1, $statements);
  428. $this->assertSame('alter table "users" add column "birth_year" integer not null', $statements[0]);
  429. }
  430. public function testAddingDateTime()
  431. {
  432. $blueprint = new Blueprint('users');
  433. $blueprint->dateTime('created_at');
  434. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  435. $this->assertCount(1, $statements);
  436. $this->assertSame('alter table "users" add column "created_at" datetime not null', $statements[0]);
  437. }
  438. public function testAddingDateTimeWithPrecision()
  439. {
  440. $blueprint = new Blueprint('users');
  441. $blueprint->dateTime('created_at', 1);
  442. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  443. $this->assertCount(1, $statements);
  444. $this->assertSame('alter table "users" add column "created_at" datetime not null', $statements[0]);
  445. }
  446. public function testAddingDateTimeTz()
  447. {
  448. $blueprint = new Blueprint('users');
  449. $blueprint->dateTimeTz('created_at');
  450. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  451. $this->assertCount(1, $statements);
  452. $this->assertSame('alter table "users" add column "created_at" datetime not null', $statements[0]);
  453. }
  454. public function testAddingDateTimeTzWithPrecision()
  455. {
  456. $blueprint = new Blueprint('users');
  457. $blueprint->dateTimeTz('created_at', 1);
  458. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  459. $this->assertCount(1, $statements);
  460. $this->assertSame('alter table "users" add column "created_at" datetime not null', $statements[0]);
  461. }
  462. public function testAddingTime()
  463. {
  464. $blueprint = new Blueprint('users');
  465. $blueprint->time('created_at');
  466. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  467. $this->assertCount(1, $statements);
  468. $this->assertSame('alter table "users" add column "created_at" time not null', $statements[0]);
  469. }
  470. public function testAddingTimeWithPrecision()
  471. {
  472. $blueprint = new Blueprint('users');
  473. $blueprint->time('created_at', 1);
  474. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  475. $this->assertCount(1, $statements);
  476. $this->assertSame('alter table "users" add column "created_at" time not null', $statements[0]);
  477. }
  478. public function testAddingTimeTz()
  479. {
  480. $blueprint = new Blueprint('users');
  481. $blueprint->timeTz('created_at');
  482. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  483. $this->assertCount(1, $statements);
  484. $this->assertSame('alter table "users" add column "created_at" time not null', $statements[0]);
  485. }
  486. public function testAddingTimeTzWithPrecision()
  487. {
  488. $blueprint = new Blueprint('users');
  489. $blueprint->timeTz('created_at', 1);
  490. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  491. $this->assertCount(1, $statements);
  492. $this->assertSame('alter table "users" add column "created_at" time not null', $statements[0]);
  493. }
  494. public function testAddingTimestamp()
  495. {
  496. $blueprint = new Blueprint('users');
  497. $blueprint->timestamp('created_at');
  498. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  499. $this->assertCount(1, $statements);
  500. $this->assertSame('alter table "users" add column "created_at" datetime not null', $statements[0]);
  501. }
  502. public function testAddingTimestampWithPrecision()
  503. {
  504. $blueprint = new Blueprint('users');
  505. $blueprint->timestamp('created_at', 1);
  506. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  507. $this->assertCount(1, $statements);
  508. $this->assertSame('alter table "users" add column "created_at" datetime not null', $statements[0]);
  509. }
  510. public function testAddingTimestampTz()
  511. {
  512. $blueprint = new Blueprint('users');
  513. $blueprint->timestampTz('created_at');
  514. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  515. $this->assertCount(1, $statements);
  516. $this->assertSame('alter table "users" add column "created_at" datetime not null', $statements[0]);
  517. }
  518. public function testAddingTimestampTzWithPrecision()
  519. {
  520. $blueprint = new Blueprint('users');
  521. $blueprint->timestampTz('created_at', 1);
  522. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  523. $this->assertCount(1, $statements);
  524. $this->assertSame('alter table "users" add column "created_at" datetime not null', $statements[0]);
  525. }
  526. public function testAddingTimestamps()
  527. {
  528. $blueprint = new Blueprint('users');
  529. $blueprint->timestamps();
  530. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  531. $this->assertCount(2, $statements);
  532. $this->assertEquals([
  533. 'alter table "users" add column "created_at" datetime',
  534. 'alter table "users" add column "updated_at" datetime',
  535. ], $statements);
  536. }
  537. public function testAddingTimestampsTz()
  538. {
  539. $blueprint = new Blueprint('users');
  540. $blueprint->timestampsTz();
  541. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  542. $this->assertCount(2, $statements);
  543. $this->assertEquals([
  544. 'alter table "users" add column "created_at" datetime',
  545. 'alter table "users" add column "updated_at" datetime',
  546. ], $statements);
  547. }
  548. public function testAddingRememberToken()
  549. {
  550. $blueprint = new Blueprint('users');
  551. $blueprint->rememberToken();
  552. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  553. $this->assertCount(1, $statements);
  554. $this->assertSame('alter table "users" add column "remember_token" varchar', $statements[0]);
  555. }
  556. public function testAddingBinary()
  557. {
  558. $blueprint = new Blueprint('users');
  559. $blueprint->binary('foo');
  560. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  561. $this->assertCount(1, $statements);
  562. $this->assertSame('alter table "users" add column "foo" blob not null', $statements[0]);
  563. }
  564. public function testAddingUuid()
  565. {
  566. $blueprint = new Blueprint('users');
  567. $blueprint->uuid('foo');
  568. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  569. $this->assertCount(1, $statements);
  570. $this->assertSame('alter table "users" add column "foo" varchar not null', $statements[0]);
  571. }
  572. public function testAddingForeignUuid()
  573. {
  574. $blueprint = new Blueprint('users');
  575. $foreignUuid = $blueprint->foreignUuid('foo');
  576. $blueprint->foreignUuid('company_id')->constrained();
  577. $blueprint->foreignUuid('laravel_idea_id')->constrained();
  578. $blueprint->foreignUuid('team_id')->references('id')->on('teams');
  579. $blueprint->foreignUuid('team_column_id')->constrained('teams');
  580. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  581. $this->assertInstanceOf(ForeignIdColumnDefinition::class, $foreignUuid);
  582. $this->assertSame([
  583. 'alter table "users" add column "foo" varchar not null',
  584. 'alter table "users" add column "company_id" varchar not null',
  585. 'alter table "users" add column "laravel_idea_id" varchar not null',
  586. 'alter table "users" add column "team_id" varchar not null',
  587. 'alter table "users" add column "team_column_id" varchar not null',
  588. ], $statements);
  589. }
  590. public function testAddingIpAddress()
  591. {
  592. $blueprint = new Blueprint('users');
  593. $blueprint->ipAddress('foo');
  594. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  595. $this->assertCount(1, $statements);
  596. $this->assertSame('alter table "users" add column "foo" varchar not null', $statements[0]);
  597. }
  598. public function testAddingMacAddress()
  599. {
  600. $blueprint = new Blueprint('users');
  601. $blueprint->macAddress('foo');
  602. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  603. $this->assertCount(1, $statements);
  604. $this->assertSame('alter table "users" add column "foo" varchar not null', $statements[0]);
  605. }
  606. public function testAddingGeometry()
  607. {
  608. $blueprint = new Blueprint('geo');
  609. $blueprint->geometry('coordinates');
  610. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  611. $this->assertCount(1, $statements);
  612. $this->assertSame('alter table "geo" add column "coordinates" geometry not null', $statements[0]);
  613. }
  614. public function testAddingPoint()
  615. {
  616. $blueprint = new Blueprint('geo');
  617. $blueprint->point('coordinates');
  618. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  619. $this->assertCount(1, $statements);
  620. $this->assertSame('alter table "geo" add column "coordinates" point not null', $statements[0]);
  621. }
  622. public function testAddingLineString()
  623. {
  624. $blueprint = new Blueprint('geo');
  625. $blueprint->linestring('coordinates');
  626. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  627. $this->assertCount(1, $statements);
  628. $this->assertSame('alter table "geo" add column "coordinates" linestring not null', $statements[0]);
  629. }
  630. public function testAddingPolygon()
  631. {
  632. $blueprint = new Blueprint('geo');
  633. $blueprint->polygon('coordinates');
  634. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  635. $this->assertCount(1, $statements);
  636. $this->assertSame('alter table "geo" add column "coordinates" polygon not null', $statements[0]);
  637. }
  638. public function testAddingGeometryCollection()
  639. {
  640. $blueprint = new Blueprint('geo');
  641. $blueprint->geometrycollection('coordinates');
  642. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  643. $this->assertCount(1, $statements);
  644. $this->assertSame('alter table "geo" add column "coordinates" geometrycollection not null', $statements[0]);
  645. }
  646. public function testAddingMultiPoint()
  647. {
  648. $blueprint = new Blueprint('geo');
  649. $blueprint->multipoint('coordinates');
  650. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  651. $this->assertCount(1, $statements);
  652. $this->assertSame('alter table "geo" add column "coordinates" multipoint not null', $statements[0]);
  653. }
  654. public function testAddingMultiLineString()
  655. {
  656. $blueprint = new Blueprint('geo');
  657. $blueprint->multilinestring('coordinates');
  658. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  659. $this->assertCount(1, $statements);
  660. $this->assertSame('alter table "geo" add column "coordinates" multilinestring not null', $statements[0]);
  661. }
  662. public function testAddingMultiPolygon()
  663. {
  664. $blueprint = new Blueprint('geo');
  665. $blueprint->multipolygon('coordinates');
  666. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  667. $this->assertCount(1, $statements);
  668. $this->assertSame('alter table "geo" add column "coordinates" multipolygon not null', $statements[0]);
  669. }
  670. public function testAddingGeneratedColumn()
  671. {
  672. $blueprint = new Blueprint('products');
  673. $blueprint->create();
  674. $blueprint->integer('price');
  675. $blueprint->integer('discounted_virtual')->virtualAs('"price" - 5');
  676. $blueprint->integer('discounted_stored')->storedAs('"price" - 5');
  677. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  678. $this->assertCount(1, $statements);
  679. $this->assertSame('create table "products" ("price" integer not null, "discounted_virtual" integer as ("price" - 5), "discounted_stored" integer as ("price" - 5) stored)', $statements[0]);
  680. $blueprint = new Blueprint('products');
  681. $blueprint->integer('price');
  682. $blueprint->integer('discounted_virtual')->virtualAs('"price" - 5')->nullable(false);
  683. $blueprint->integer('discounted_stored')->storedAs('"price" - 5')->nullable(false);
  684. $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
  685. $this->assertCount(2, $statements);
  686. $expected = [
  687. 'alter table "products" add column "price" integer not null',
  688. 'alter table "products" add column "discounted_virtual" integer as ("price" - 5) not null',
  689. ];
  690. $this->assertSame($expected, $statements);
  691. }
  692. public function testGrammarsAreMacroable()
  693. {
  694. // compileReplace macro.
  695. $this->getGrammar()::macro('compileReplace', function () {
  696. return true;
  697. });
  698. $c = $this->getGrammar()::compileReplace();
  699. $this->assertTrue($c);
  700. }
  701. protected function getConnection()
  702. {
  703. return m::mock(Connection::class);
  704. }
  705. public function getGrammar()
  706. {
  707. return new SQLiteGrammar;
  708. }
  709. }