Blueprint.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. <?php
  2. namespace Illuminate\Database\Schema;
  3. use Closure;
  4. use Illuminate\Support\Fluent;
  5. use Illuminate\Database\Connection;
  6. use Illuminate\Support\Traits\Macroable;
  7. use Illuminate\Database\Schema\Grammars\Grammar;
  8. class Blueprint
  9. {
  10. use Macroable;
  11. /**
  12. * The table the blueprint describes.
  13. *
  14. * @var string
  15. */
  16. protected $table;
  17. /**
  18. * The columns that should be added to the table.
  19. *
  20. * @var \Illuminate\Support\Fluent[]
  21. */
  22. protected $columns = [];
  23. /**
  24. * The commands that should be run for the table.
  25. *
  26. * @var \Illuminate\Support\Fluent[]
  27. */
  28. protected $commands = [];
  29. /**
  30. * The storage engine that should be used for the table.
  31. *
  32. * @var string
  33. */
  34. public $engine;
  35. /**
  36. * The default character set that should be used for the table.
  37. */
  38. public $charset;
  39. /**
  40. * The collation that should be used for the table.
  41. */
  42. public $collation;
  43. /**
  44. * Whether to make the table temporary.
  45. *
  46. * @var bool
  47. */
  48. public $temporary = false;
  49. /**
  50. * Create a new schema blueprint.
  51. *
  52. * @param string $table
  53. * @param \Closure|null $callback
  54. * @return void
  55. */
  56. public function __construct($table, Closure $callback = null)
  57. {
  58. $this->table = $table;
  59. if (! is_null($callback)) {
  60. $callback($this);
  61. }
  62. }
  63. /**
  64. * Execute the blueprint against the database.
  65. *
  66. * @param \Illuminate\Database\Connection $connection
  67. * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
  68. * @return void
  69. */
  70. public function build(Connection $connection, Grammar $grammar)
  71. {
  72. foreach ($this->toSql($connection, $grammar) as $statement) {
  73. $connection->statement($statement);
  74. }
  75. }
  76. /**
  77. * Get the raw SQL statements for the blueprint.
  78. *
  79. * @param \Illuminate\Database\Connection $connection
  80. * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
  81. * @return array
  82. */
  83. public function toSql(Connection $connection, Grammar $grammar)
  84. {
  85. $this->addImpliedCommands();
  86. $statements = [];
  87. // Each type of command has a corresponding compiler function on the schema
  88. // grammar which is used to build the necessary SQL statements to build
  89. // the blueprint element, so we'll just call that compilers function.
  90. foreach ($this->commands as $command) {
  91. $method = 'compile'.ucfirst($command->name);
  92. if (method_exists($grammar, $method)) {
  93. if (! is_null($sql = $grammar->$method($this, $command, $connection))) {
  94. $statements = array_merge($statements, (array) $sql);
  95. }
  96. }
  97. }
  98. return $statements;
  99. }
  100. /**
  101. * Add the commands that are implied by the blueprint's state.
  102. *
  103. * @return void
  104. */
  105. protected function addImpliedCommands()
  106. {
  107. if (count($this->getAddedColumns()) > 0 && ! $this->creating()) {
  108. array_unshift($this->commands, $this->createCommand('add'));
  109. }
  110. if (count($this->getChangedColumns()) > 0 && ! $this->creating()) {
  111. array_unshift($this->commands, $this->createCommand('change'));
  112. }
  113. $this->addFluentIndexes();
  114. }
  115. /**
  116. * Add the index commands fluently specified on columns.
  117. *
  118. * @return void
  119. */
  120. protected function addFluentIndexes()
  121. {
  122. foreach ($this->columns as $column) {
  123. foreach (['primary', 'unique', 'index', 'spatialIndex'] as $index) {
  124. // If the index has been specified on the given column, but is simply equal
  125. // to "true" (boolean), no name has been specified for this index so the
  126. // index method can be called without a name and it will generate one.
  127. if ($column->{$index} === true) {
  128. $this->{$index}($column->name);
  129. continue 2;
  130. }
  131. // If the index has been specified on the given column, and it has a string
  132. // value, we'll go ahead and call the index method and pass the name for
  133. // the index since the developer specified the explicit name for this.
  134. elseif (isset($column->{$index})) {
  135. $this->{$index}($column->name, $column->{$index});
  136. continue 2;
  137. }
  138. }
  139. }
  140. }
  141. /**
  142. * Determine if the blueprint has a create command.
  143. *
  144. * @return bool
  145. */
  146. protected function creating()
  147. {
  148. return collect($this->commands)->contains(function ($command) {
  149. return $command->name == 'create';
  150. });
  151. }
  152. /**
  153. * Indicate that the table needs to be created.
  154. *
  155. * @return \Illuminate\Support\Fluent
  156. */
  157. public function create()
  158. {
  159. return $this->addCommand('create');
  160. }
  161. /**
  162. * Indicate that the table needs to be temporary.
  163. *
  164. * @return void
  165. */
  166. public function temporary()
  167. {
  168. $this->temporary = true;
  169. }
  170. /**
  171. * Indicate that the table should be dropped.
  172. *
  173. * @return \Illuminate\Support\Fluent
  174. */
  175. public function drop()
  176. {
  177. return $this->addCommand('drop');
  178. }
  179. /**
  180. * Indicate that the table should be dropped if it exists.
  181. *
  182. * @return \Illuminate\Support\Fluent
  183. */
  184. public function dropIfExists()
  185. {
  186. return $this->addCommand('dropIfExists');
  187. }
  188. /**
  189. * Indicate that the given columns should be dropped.
  190. *
  191. * @param array|mixed $columns
  192. * @return \Illuminate\Support\Fluent
  193. */
  194. public function dropColumn($columns)
  195. {
  196. $columns = is_array($columns) ? $columns : func_get_args();
  197. return $this->addCommand('dropColumn', compact('columns'));
  198. }
  199. /**
  200. * Indicate that the given columns should be renamed.
  201. *
  202. * @param string $from
  203. * @param string $to
  204. * @return \Illuminate\Support\Fluent
  205. */
  206. public function renameColumn($from, $to)
  207. {
  208. return $this->addCommand('renameColumn', compact('from', 'to'));
  209. }
  210. /**
  211. * Indicate that the given primary key should be dropped.
  212. *
  213. * @param string|array $index
  214. * @return \Illuminate\Support\Fluent
  215. */
  216. public function dropPrimary($index = null)
  217. {
  218. return $this->dropIndexCommand('dropPrimary', 'primary', $index);
  219. }
  220. /**
  221. * Indicate that the given unique key should be dropped.
  222. *
  223. * @param string|array $index
  224. * @return \Illuminate\Support\Fluent
  225. */
  226. public function dropUnique($index)
  227. {
  228. return $this->dropIndexCommand('dropUnique', 'unique', $index);
  229. }
  230. /**
  231. * Indicate that the given index should be dropped.
  232. *
  233. * @param string|array $index
  234. * @return \Illuminate\Support\Fluent
  235. */
  236. public function dropIndex($index)
  237. {
  238. return $this->dropIndexCommand('dropIndex', 'index', $index);
  239. }
  240. /**
  241. * Indicate that the given spatial index should be dropped.
  242. *
  243. * @param string|array $index
  244. * @return \Illuminate\Support\Fluent
  245. */
  246. public function dropSpatialIndex($index)
  247. {
  248. return $this->dropIndexCommand('dropSpatialIndex', 'spatialIndex', $index);
  249. }
  250. /**
  251. * Indicate that the given foreign key should be dropped.
  252. *
  253. * @param string|array $index
  254. * @return \Illuminate\Support\Fluent
  255. */
  256. public function dropForeign($index)
  257. {
  258. return $this->dropIndexCommand('dropForeign', 'foreign', $index);
  259. }
  260. /**
  261. * Indicate that the timestamp columns should be dropped.
  262. *
  263. * @return void
  264. */
  265. public function dropTimestamps()
  266. {
  267. $this->dropColumn('created_at', 'updated_at');
  268. }
  269. /**
  270. * Indicate that the timestamp columns should be dropped.
  271. *
  272. * @return void
  273. */
  274. public function dropTimestampsTz()
  275. {
  276. $this->dropTimestamps();
  277. }
  278. /**
  279. * Indicate that the soft delete column should be dropped.
  280. *
  281. * @return void
  282. */
  283. public function dropSoftDeletes()
  284. {
  285. $this->dropColumn('deleted_at');
  286. }
  287. /**
  288. * Indicate that the soft delete column should be dropped.
  289. *
  290. * @return void
  291. */
  292. public function dropSoftDeletesTz()
  293. {
  294. $this->dropSoftDeletes();
  295. }
  296. /**
  297. * Indicate that the remember token column should be dropped.
  298. *
  299. * @return void
  300. */
  301. public function dropRememberToken()
  302. {
  303. $this->dropColumn('remember_token');
  304. }
  305. /**
  306. * Rename the table to a given name.
  307. *
  308. * @param string $to
  309. * @return \Illuminate\Support\Fluent
  310. */
  311. public function rename($to)
  312. {
  313. return $this->addCommand('rename', compact('to'));
  314. }
  315. /**
  316. * Specify the primary key(s) for the table.
  317. *
  318. * @param string|array $columns
  319. * @param string $name
  320. * @param string|null $algorithm
  321. * @return \Illuminate\Support\Fluent
  322. */
  323. public function primary($columns, $name = null, $algorithm = null)
  324. {
  325. return $this->indexCommand('primary', $columns, $name, $algorithm);
  326. }
  327. /**
  328. * Specify a unique index for the table.
  329. *
  330. * @param string|array $columns
  331. * @param string $name
  332. * @param string|null $algorithm
  333. * @return \Illuminate\Support\Fluent
  334. */
  335. public function unique($columns, $name = null, $algorithm = null)
  336. {
  337. return $this->indexCommand('unique', $columns, $name, $algorithm);
  338. }
  339. /**
  340. * Specify an index for the table.
  341. *
  342. * @param string|array $columns
  343. * @param string $name
  344. * @param string|null $algorithm
  345. * @return \Illuminate\Support\Fluent
  346. */
  347. public function index($columns, $name = null, $algorithm = null)
  348. {
  349. return $this->indexCommand('index', $columns, $name, $algorithm);
  350. }
  351. /**
  352. * Specify a spatial index for the table.
  353. *
  354. * @param string|array $columns
  355. * @param string $name
  356. * @return \Illuminate\Support\Fluent
  357. */
  358. public function spatialIndex($columns, $name = null)
  359. {
  360. return $this->indexCommand('spatialIndex', $columns, $name);
  361. }
  362. /**
  363. * Specify a foreign key for the table.
  364. *
  365. * @param string|array $columns
  366. * @param string $name
  367. * @return \Illuminate\Support\Fluent
  368. */
  369. public function foreign($columns, $name = null)
  370. {
  371. return $this->indexCommand('foreign', $columns, $name);
  372. }
  373. /**
  374. * Create a new auto-incrementing integer (4-byte) column on the table.
  375. *
  376. * @param string $column
  377. * @return \Illuminate\Support\Fluent
  378. */
  379. public function increments($column)
  380. {
  381. return $this->unsignedInteger($column, true);
  382. }
  383. /**
  384. * Create a new auto-incrementing tiny integer (1-byte) column on the table.
  385. *
  386. * @param string $column
  387. * @return \Illuminate\Support\Fluent
  388. */
  389. public function tinyIncrements($column)
  390. {
  391. return $this->unsignedTinyInteger($column, true);
  392. }
  393. /**
  394. * Create a new auto-incrementing small integer (2-byte) column on the table.
  395. *
  396. * @param string $column
  397. * @return \Illuminate\Support\Fluent
  398. */
  399. public function smallIncrements($column)
  400. {
  401. return $this->unsignedSmallInteger($column, true);
  402. }
  403. /**
  404. * Create a new auto-incrementing medium integer (3-byte) column on the table.
  405. *
  406. * @param string $column
  407. * @return \Illuminate\Support\Fluent
  408. */
  409. public function mediumIncrements($column)
  410. {
  411. return $this->unsignedMediumInteger($column, true);
  412. }
  413. /**
  414. * Create a new auto-incrementing big integer (8-byte) column on the table.
  415. *
  416. * @param string $column
  417. * @return \Illuminate\Support\Fluent
  418. */
  419. public function bigIncrements($column)
  420. {
  421. return $this->unsignedBigInteger($column, true);
  422. }
  423. /**
  424. * Create a new char column on the table.
  425. *
  426. * @param string $column
  427. * @param int $length
  428. * @return \Illuminate\Support\Fluent
  429. */
  430. public function char($column, $length = null)
  431. {
  432. $length = $length ?: Builder::$defaultStringLength;
  433. return $this->addColumn('char', $column, compact('length'));
  434. }
  435. /**
  436. * Create a new string column on the table.
  437. *
  438. * @param string $column
  439. * @param int $length
  440. * @return \Illuminate\Support\Fluent
  441. */
  442. public function string($column, $length = null)
  443. {
  444. $length = $length ?: Builder::$defaultStringLength;
  445. return $this->addColumn('string', $column, compact('length'));
  446. }
  447. /**
  448. * Create a new text column on the table.
  449. *
  450. * @param string $column
  451. * @return \Illuminate\Support\Fluent
  452. */
  453. public function text($column)
  454. {
  455. return $this->addColumn('text', $column);
  456. }
  457. /**
  458. * Create a new medium text column on the table.
  459. *
  460. * @param string $column
  461. * @return \Illuminate\Support\Fluent
  462. */
  463. public function mediumText($column)
  464. {
  465. return $this->addColumn('mediumText', $column);
  466. }
  467. /**
  468. * Create a new long text column on the table.
  469. *
  470. * @param string $column
  471. * @return \Illuminate\Support\Fluent
  472. */
  473. public function longText($column)
  474. {
  475. return $this->addColumn('longText', $column);
  476. }
  477. /**
  478. * Create a new integer (4-byte) column on the table.
  479. *
  480. * @param string $column
  481. * @param bool $autoIncrement
  482. * @param bool $unsigned
  483. * @return \Illuminate\Support\Fluent
  484. */
  485. public function integer($column, $autoIncrement = false, $unsigned = false)
  486. {
  487. return $this->addColumn('integer', $column, compact('autoIncrement', 'unsigned'));
  488. }
  489. /**
  490. * Create a new tiny integer (1-byte) column on the table.
  491. *
  492. * @param string $column
  493. * @param bool $autoIncrement
  494. * @param bool $unsigned
  495. * @return \Illuminate\Support\Fluent
  496. */
  497. public function tinyInteger($column, $autoIncrement = false, $unsigned = false)
  498. {
  499. return $this->addColumn('tinyInteger', $column, compact('autoIncrement', 'unsigned'));
  500. }
  501. /**
  502. * Create a new small integer (2-byte) column on the table.
  503. *
  504. * @param string $column
  505. * @param bool $autoIncrement
  506. * @param bool $unsigned
  507. * @return \Illuminate\Support\Fluent
  508. */
  509. public function smallInteger($column, $autoIncrement = false, $unsigned = false)
  510. {
  511. return $this->addColumn('smallInteger', $column, compact('autoIncrement', 'unsigned'));
  512. }
  513. /**
  514. * Create a new medium integer (3-byte) column on the table.
  515. *
  516. * @param string $column
  517. * @param bool $autoIncrement
  518. * @param bool $unsigned
  519. * @return \Illuminate\Support\Fluent
  520. */
  521. public function mediumInteger($column, $autoIncrement = false, $unsigned = false)
  522. {
  523. return $this->addColumn('mediumInteger', $column, compact('autoIncrement', 'unsigned'));
  524. }
  525. /**
  526. * Create a new big integer (8-byte) column on the table.
  527. *
  528. * @param string $column
  529. * @param bool $autoIncrement
  530. * @param bool $unsigned
  531. * @return \Illuminate\Support\Fluent
  532. */
  533. public function bigInteger($column, $autoIncrement = false, $unsigned = false)
  534. {
  535. return $this->addColumn('bigInteger', $column, compact('autoIncrement', 'unsigned'));
  536. }
  537. /**
  538. * Create a new unsigned integer (4-byte) column on the table.
  539. *
  540. * @param string $column
  541. * @param bool $autoIncrement
  542. * @return \Illuminate\Support\Fluent
  543. */
  544. public function unsignedInteger($column, $autoIncrement = false)
  545. {
  546. return $this->integer($column, $autoIncrement, true);
  547. }
  548. /**
  549. * Create a new unsigned tiny integer (1-byte) column on the table.
  550. *
  551. * @param string $column
  552. * @param bool $autoIncrement
  553. * @return \Illuminate\Support\Fluent
  554. */
  555. public function unsignedTinyInteger($column, $autoIncrement = false)
  556. {
  557. return $this->tinyInteger($column, $autoIncrement, true);
  558. }
  559. /**
  560. * Create a new unsigned small integer (2-byte) column on the table.
  561. *
  562. * @param string $column
  563. * @param bool $autoIncrement
  564. * @return \Illuminate\Support\Fluent
  565. */
  566. public function unsignedSmallInteger($column, $autoIncrement = false)
  567. {
  568. return $this->smallInteger($column, $autoIncrement, true);
  569. }
  570. /**
  571. * Create a new unsigned medium integer (3-byte) column on the table.
  572. *
  573. * @param string $column
  574. * @param bool $autoIncrement
  575. * @return \Illuminate\Support\Fluent
  576. */
  577. public function unsignedMediumInteger($column, $autoIncrement = false)
  578. {
  579. return $this->mediumInteger($column, $autoIncrement, true);
  580. }
  581. /**
  582. * Create a new unsigned big integer (8-byte) column on the table.
  583. *
  584. * @param string $column
  585. * @param bool $autoIncrement
  586. * @return \Illuminate\Support\Fluent
  587. */
  588. public function unsignedBigInteger($column, $autoIncrement = false)
  589. {
  590. return $this->bigInteger($column, $autoIncrement, true);
  591. }
  592. /**
  593. * Create a new float column on the table.
  594. *
  595. * @param string $column
  596. * @param int $total
  597. * @param int $places
  598. * @return \Illuminate\Support\Fluent
  599. */
  600. public function float($column, $total = 8, $places = 2)
  601. {
  602. return $this->addColumn('float', $column, compact('total', 'places'));
  603. }
  604. /**
  605. * Create a new double column on the table.
  606. *
  607. * @param string $column
  608. * @param int|null $total
  609. * @param int|null $places
  610. * @return \Illuminate\Support\Fluent
  611. */
  612. public function double($column, $total = null, $places = null)
  613. {
  614. return $this->addColumn('double', $column, compact('total', 'places'));
  615. }
  616. /**
  617. * Create a new decimal column on the table.
  618. *
  619. * @param string $column
  620. * @param int $total
  621. * @param int $places
  622. * @return \Illuminate\Support\Fluent
  623. */
  624. public function decimal($column, $total = 8, $places = 2)
  625. {
  626. return $this->addColumn('decimal', $column, compact('total', 'places'));
  627. }
  628. /**
  629. * Create a new unsigned decimal column on the table.
  630. *
  631. * @param string $column
  632. * @param int $total
  633. * @param int $places
  634. * @return \Illuminate\Support\Fluent
  635. */
  636. public function unsignedDecimal($column, $total = 8, $places = 2)
  637. {
  638. return $this->addColumn('decimal', $column, [
  639. 'total' => $total, 'places' => $places, 'unsigned' => true,
  640. ]);
  641. }
  642. /**
  643. * Create a new boolean column on the table.
  644. *
  645. * @param string $column
  646. * @return \Illuminate\Support\Fluent
  647. */
  648. public function boolean($column)
  649. {
  650. return $this->addColumn('boolean', $column);
  651. }
  652. /**
  653. * Create a new enum column on the table.
  654. *
  655. * @param string $column
  656. * @param array $allowed
  657. * @return \Illuminate\Support\Fluent
  658. */
  659. public function enum($column, array $allowed)
  660. {
  661. return $this->addColumn('enum', $column, compact('allowed'));
  662. }
  663. /**
  664. * Create a new json column on the table.
  665. *
  666. * @param string $column
  667. * @return \Illuminate\Support\Fluent
  668. */
  669. public function json($column)
  670. {
  671. return $this->addColumn('json', $column);
  672. }
  673. /**
  674. * Create a new jsonb column on the table.
  675. *
  676. * @param string $column
  677. * @return \Illuminate\Support\Fluent
  678. */
  679. public function jsonb($column)
  680. {
  681. return $this->addColumn('jsonb', $column);
  682. }
  683. /**
  684. * Create a new date column on the table.
  685. *
  686. * @param string $column
  687. * @return \Illuminate\Support\Fluent
  688. */
  689. public function date($column)
  690. {
  691. return $this->addColumn('date', $column);
  692. }
  693. /**
  694. * Create a new date-time column on the table.
  695. *
  696. * @param string $column
  697. * @param int $precision
  698. * @return \Illuminate\Support\Fluent
  699. */
  700. public function dateTime($column, $precision = 0)
  701. {
  702. return $this->addColumn('dateTime', $column, compact('precision'));
  703. }
  704. /**
  705. * Create a new date-time column (with time zone) on the table.
  706. *
  707. * @param string $column
  708. * @param int $precision
  709. * @return \Illuminate\Support\Fluent
  710. */
  711. public function dateTimeTz($column, $precision = 0)
  712. {
  713. return $this->addColumn('dateTimeTz', $column, compact('precision'));
  714. }
  715. /**
  716. * Create a new time column on the table.
  717. *
  718. * @param string $column
  719. * @param int $precision
  720. * @return \Illuminate\Support\Fluent
  721. */
  722. public function time($column, $precision = 0)
  723. {
  724. return $this->addColumn('time', $column, compact('precision'));
  725. }
  726. /**
  727. * Create a new time column (with time zone) on the table.
  728. *
  729. * @param string $column
  730. * @param int $precision
  731. * @return \Illuminate\Support\Fluent
  732. */
  733. public function timeTz($column, $precision = 0)
  734. {
  735. return $this->addColumn('timeTz', $column, compact('precision'));
  736. }
  737. /**
  738. * Create a new timestamp column on the table.
  739. *
  740. * @param string $column
  741. * @param int $precision
  742. * @return \Illuminate\Support\Fluent
  743. */
  744. public function timestamp($column, $precision = 0)
  745. {
  746. return $this->addColumn('timestamp', $column, compact('precision'));
  747. }
  748. /**
  749. * Create a new timestamp (with time zone) column on the table.
  750. *
  751. * @param string $column
  752. * @param int $precision
  753. * @return \Illuminate\Support\Fluent
  754. */
  755. public function timestampTz($column, $precision = 0)
  756. {
  757. return $this->addColumn('timestampTz', $column, compact('precision'));
  758. }
  759. /**
  760. * Add nullable creation and update timestamps to the table.
  761. *
  762. * @param int $precision
  763. * @return void
  764. */
  765. public function timestamps($precision = 0)
  766. {
  767. $this->timestamp('created_at', $precision)->nullable();
  768. $this->timestamp('updated_at', $precision)->nullable();
  769. }
  770. /**
  771. * Add nullable creation and update timestamps to the table.
  772. *
  773. * Alias for self::timestamps().
  774. *
  775. * @param int $precision
  776. * @return void
  777. */
  778. public function nullableTimestamps($precision = 0)
  779. {
  780. $this->timestamps($precision);
  781. }
  782. /**
  783. * Add creation and update timestampTz columns to the table.
  784. *
  785. * @param int $precision
  786. * @return void
  787. */
  788. public function timestampsTz($precision = 0)
  789. {
  790. $this->timestampTz('created_at', $precision)->nullable();
  791. $this->timestampTz('updated_at', $precision)->nullable();
  792. }
  793. /**
  794. * Add a "deleted at" timestamp for the table.
  795. *
  796. * @param string $column
  797. * @param int $precision
  798. * @return \Illuminate\Support\Fluent
  799. */
  800. public function softDeletes($column = 'deleted_at', $precision = 0)
  801. {
  802. return $this->timestamp($column, $precision)->nullable();
  803. }
  804. /**
  805. * Add a "deleted at" timestampTz for the table.
  806. *
  807. * @param int $precision
  808. * @return \Illuminate\Support\Fluent
  809. */
  810. public function softDeletesTz($precision = 0)
  811. {
  812. return $this->timestampTz('deleted_at', $precision)->nullable();
  813. }
  814. /**
  815. * Create a new year column on the table.
  816. *
  817. * @param string $column
  818. * @return \Illuminate\Support\Fluent
  819. */
  820. public function year($column)
  821. {
  822. return $this->addColumn('year', $column);
  823. }
  824. /**
  825. * Create a new binary column on the table.
  826. *
  827. * @param string $column
  828. * @return \Illuminate\Support\Fluent
  829. */
  830. public function binary($column)
  831. {
  832. return $this->addColumn('binary', $column);
  833. }
  834. /**
  835. * Create a new uuid column on the table.
  836. *
  837. * @param string $column
  838. * @return \Illuminate\Support\Fluent
  839. */
  840. public function uuid($column)
  841. {
  842. return $this->addColumn('uuid', $column);
  843. }
  844. /**
  845. * Create a new IP address column on the table.
  846. *
  847. * @param string $column
  848. * @return \Illuminate\Support\Fluent
  849. */
  850. public function ipAddress($column)
  851. {
  852. return $this->addColumn('ipAddress', $column);
  853. }
  854. /**
  855. * Create a new MAC address column on the table.
  856. *
  857. * @param string $column
  858. * @return \Illuminate\Support\Fluent
  859. */
  860. public function macAddress($column)
  861. {
  862. return $this->addColumn('macAddress', $column);
  863. }
  864. /**
  865. * Create a new geometry column on the table.
  866. *
  867. * @param string $column
  868. * @return \Illuminate\Support\Fluent
  869. */
  870. public function geometry($column)
  871. {
  872. return $this->addColumn('geometry', $column);
  873. }
  874. /**
  875. * Create a new point column on the table.
  876. *
  877. * @param string $column
  878. * @return \Illuminate\Support\Fluent
  879. */
  880. public function point($column)
  881. {
  882. return $this->addColumn('point', $column);
  883. }
  884. /**
  885. * Create a new linestring column on the table.
  886. *
  887. * @param string $column
  888. * @return \Illuminate\Support\Fluent
  889. */
  890. public function lineString($column)
  891. {
  892. return $this->addColumn('linestring', $column);
  893. }
  894. /**
  895. * Create a new polygon column on the table.
  896. *
  897. * @param string $column
  898. * @return \Illuminate\Support\Fluent
  899. */
  900. public function polygon($column)
  901. {
  902. return $this->addColumn('polygon', $column);
  903. }
  904. /**
  905. * Create a new geometrycollection column on the table.
  906. *
  907. * @param string $column
  908. * @return \Illuminate\Support\Fluent
  909. */
  910. public function geometryCollection($column)
  911. {
  912. return $this->addColumn('geometrycollection', $column);
  913. }
  914. /**
  915. * Create a new multipoint column on the table.
  916. *
  917. * @param string $column
  918. * @return \Illuminate\Support\Fluent
  919. */
  920. public function multiPoint($column)
  921. {
  922. return $this->addColumn('multipoint', $column);
  923. }
  924. /**
  925. * Create a new multilinestring column on the table.
  926. *
  927. * @param string $column
  928. * @return \Illuminate\Support\Fluent
  929. */
  930. public function multiLineString($column)
  931. {
  932. return $this->addColumn('multilinestring', $column);
  933. }
  934. /**
  935. * Create a new multipolygon column on the table.
  936. *
  937. * @param string $column
  938. * @return \Illuminate\Support\Fluent
  939. */
  940. public function multiPolygon($column)
  941. {
  942. return $this->addColumn('multipolygon', $column);
  943. }
  944. /**
  945. * Add the proper columns for a polymorphic table.
  946. *
  947. * @param string $name
  948. * @param string|null $indexName
  949. * @return void
  950. */
  951. public function morphs($name, $indexName = null)
  952. {
  953. $this->unsignedInteger("{$name}_id");
  954. $this->string("{$name}_type");
  955. $this->index(["{$name}_id", "{$name}_type"], $indexName);
  956. }
  957. /**
  958. * Add nullable columns for a polymorphic table.
  959. *
  960. * @param string $name
  961. * @param string|null $indexName
  962. * @return void
  963. */
  964. public function nullableMorphs($name, $indexName = null)
  965. {
  966. $this->unsignedInteger("{$name}_id")->nullable();
  967. $this->string("{$name}_type")->nullable();
  968. $this->index(["{$name}_id", "{$name}_type"], $indexName);
  969. }
  970. /**
  971. * Adds the `remember_token` column to the table.
  972. *
  973. * @return \Illuminate\Support\Fluent
  974. */
  975. public function rememberToken()
  976. {
  977. return $this->string('remember_token', 100)->nullable();
  978. }
  979. /**
  980. * Add a new index command to the blueprint.
  981. *
  982. * @param string $type
  983. * @param string|array $columns
  984. * @param string $index
  985. * @param string|null $algorithm
  986. * @return \Illuminate\Support\Fluent
  987. */
  988. protected function indexCommand($type, $columns, $index, $algorithm = null)
  989. {
  990. $columns = (array) $columns;
  991. // If no name was specified for this index, we will create one using a basic
  992. // convention of the table name, followed by the columns, followed by an
  993. // index type, such as primary or index, which makes the index unique.
  994. $index = $index ?: $this->createIndexName($type, $columns);
  995. return $this->addCommand(
  996. $type, compact('index', 'columns', 'algorithm')
  997. );
  998. }
  999. /**
  1000. * Create a new drop index command on the blueprint.
  1001. *
  1002. * @param string $command
  1003. * @param string $type
  1004. * @param string|array $index
  1005. * @return \Illuminate\Support\Fluent
  1006. */
  1007. protected function dropIndexCommand($command, $type, $index)
  1008. {
  1009. $columns = [];
  1010. // If the given "index" is actually an array of columns, the developer means
  1011. // to drop an index merely by specifying the columns involved without the
  1012. // conventional name, so we will build the index name from the columns.
  1013. if (is_array($index)) {
  1014. $index = $this->createIndexName($type, $columns = $index);
  1015. }
  1016. return $this->indexCommand($command, $columns, $index);
  1017. }
  1018. /**
  1019. * Create a default index name for the table.
  1020. *
  1021. * @param string $type
  1022. * @param array $columns
  1023. * @return string
  1024. */
  1025. protected function createIndexName($type, array $columns)
  1026. {
  1027. $index = strtolower($this->table.'_'.implode('_', $columns).'_'.$type);
  1028. return str_replace(['-', '.'], '_', $index);
  1029. }
  1030. /**
  1031. * Add a new column to the blueprint.
  1032. *
  1033. * @param string $type
  1034. * @param string $name
  1035. * @param array $parameters
  1036. * @return \Illuminate\Support\Fluent
  1037. */
  1038. public function addColumn($type, $name, array $parameters = [])
  1039. {
  1040. $this->columns[] = $column = new Fluent(
  1041. array_merge(compact('type', 'name'), $parameters)
  1042. );
  1043. return $column;
  1044. }
  1045. /**
  1046. * Remove a column from the schema blueprint.
  1047. *
  1048. * @param string $name
  1049. * @return $this
  1050. */
  1051. public function removeColumn($name)
  1052. {
  1053. $this->columns = array_values(array_filter($this->columns, function ($c) use ($name) {
  1054. return $c['attributes']['name'] != $name;
  1055. }));
  1056. return $this;
  1057. }
  1058. /**
  1059. * Add a new command to the blueprint.
  1060. *
  1061. * @param string $name
  1062. * @param array $parameters
  1063. * @return \Illuminate\Support\Fluent
  1064. */
  1065. protected function addCommand($name, array $parameters = [])
  1066. {
  1067. $this->commands[] = $command = $this->createCommand($name, $parameters);
  1068. return $command;
  1069. }
  1070. /**
  1071. * Create a new Fluent command.
  1072. *
  1073. * @param string $name
  1074. * @param array $parameters
  1075. * @return \Illuminate\Support\Fluent
  1076. */
  1077. protected function createCommand($name, array $parameters = [])
  1078. {
  1079. return new Fluent(array_merge(compact('name'), $parameters));
  1080. }
  1081. /**
  1082. * Get the table the blueprint describes.
  1083. *
  1084. * @return string
  1085. */
  1086. public function getTable()
  1087. {
  1088. return $this->table;
  1089. }
  1090. /**
  1091. * Get the columns on the blueprint.
  1092. *
  1093. * @return \Illuminate\Support\Fluent[]
  1094. */
  1095. public function getColumns()
  1096. {
  1097. return $this->columns;
  1098. }
  1099. /**
  1100. * Get the commands on the blueprint.
  1101. *
  1102. * @return \Illuminate\Support\Fluent[]
  1103. */
  1104. public function getCommands()
  1105. {
  1106. return $this->commands;
  1107. }
  1108. /**
  1109. * Get the columns on the blueprint that should be added.
  1110. *
  1111. * @return \Illuminate\Support\Fluent[]
  1112. */
  1113. public function getAddedColumns()
  1114. {
  1115. return array_filter($this->columns, function ($column) {
  1116. return ! $column->change;
  1117. });
  1118. }
  1119. /**
  1120. * Get the columns on the blueprint that should be changed.
  1121. *
  1122. * @return \Illuminate\Support\Fluent[]
  1123. */
  1124. public function getChangedColumns()
  1125. {
  1126. return array_filter($this->columns, function ($column) {
  1127. return (bool) $column->change;
  1128. });
  1129. }
  1130. }