You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Blueprint.php 37KB

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