DatabaseSqlServerSchemaGrammarTest.php 37 KB

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