CodeIgniter4/dbmgmt/migration.html
2019-05-06 15:00:34 -07:00

776 lines
40 KiB
HTML

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Database Migrations &mdash; CodeIgniter4 4.0.0-beta.3 documentation</title>
<link rel="shortcut icon" href="../_static/ci-icon.ico"/>
<link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
<link rel="top" title="CodeIgniter4 4.0.0-beta.3 documentation" href="../index.html"/>
<link rel="up" title="Managing Databases" href="index.html"/>
<link rel="next" title="Database Seeding" href="seeds.html"/>
<link rel="prev" title="Database Forge Class" href="forge.html"/>
<script src="../_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="../index.html" class="icon icon-home"> CodeIgniter4
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../concepts/index.html">CodeIgniter4 Overview</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../incoming/index.html">Controllers and Routing</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../outgoing/index.html">Building Responses</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../database/index.html">Working With Databases</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../models/index.html">Modeling Data</a></li>
</ul>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Managing Databases</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="forge.html">Database Manipulation with Database Forge</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Database Migrations</a></li>
<li class="toctree-l2"><a class="reference internal" href="seeds.html">Database Seeding</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Library Reference</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../testing/index.html">Testing</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../cli/index.html">Command Line Usage</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../extending/index.html">Extending CodeIgniter</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../license.html">The MIT License (MIT)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../changelogs/index.html">Change Logs</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">CodeIgniter4</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html">Docs</a> &raquo;</li>
<li><a href="index.html">Managing Databases</a> &raquo;</li>
<li>Database Migrations</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="database-migrations">
<h1>Database Migrations<a class="headerlink" href="#database-migrations" title="Permalink to this headline"></a></h1>
<p>Migrations are a convenient way for you to alter your database in a
structured and organized manner. You could edit fragments of SQL by hand
but you would then be responsible for telling other developers that they
need to go and run them. You would also have to keep track of which changes
need to be run against the production machines next time you deploy.</p>
<p>The database table <strong>migration</strong> tracks which migrations have already been
run so all you have to do is update your application files and
call <code class="docutils literal"><span class="pre">$migration-&gt;current()</span></code> to work out which migrations should be run.
The current version is found in <strong>app/Config/Migrations.php</strong>.</p>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#migration-file-names" id="id1">Migration file names</a></li>
<li><a class="reference internal" href="#create-a-migration" id="id2">Create a Migration</a><ul>
<li><a class="reference internal" href="#using-currentversion" id="id3">Using $currentVersion</a></li>
<li><a class="reference internal" href="#database-groups" id="id4">Database Groups</a></li>
<li><a class="reference internal" href="#namespaces" id="id5">Namespaces</a></li>
</ul>
</li>
<li><a class="reference internal" href="#usage-example" id="id6">Usage Example</a></li>
<li><a class="reference internal" href="#command-line-tools" id="id7">Command-Line Tools</a></li>
<li><a class="reference internal" href="#migration-preferences" id="id8">Migration Preferences</a></li>
<li><a class="reference internal" href="#class-reference" id="id9">Class Reference</a></li>
</ul>
</div>
<div class="custom-index container"></div><div class="section" id="migration-file-names">
<h2><a class="toc-backref" href="#id1">Migration file names</a><a class="headerlink" href="#migration-file-names" title="Permalink to this headline"></a></h2>
<p>Each Migration is run in numeric order forward or backwards depending on the
method taken. Two numbering styles are available:</p>
<ul class="simple">
<li><strong>Sequential:</strong> each migration is numbered in sequence, starting with <strong>001</strong>.
Each number must be three digits, and there must not be any gaps in the
sequence. (This was the numbering scheme prior to CodeIgniter 3.0.)</li>
<li><strong>Timestamp:</strong> each migration is numbered using the timestamp when the migration
was created, in <strong>YYYYMMDDHHIISS</strong> format (e.g. <strong>20121031100537</strong>). This
helps prevent numbering conflicts when working in a team environment, and is
the preferred scheme in CodeIgniter 3.0 and later.</li>
</ul>
<p>The desired style may be selected using the <code class="docutils literal"><span class="pre">$type</span></code> setting in your
<em>app/Config/Migrations.php</em> file. The default setting is timestamp.</p>
<p>Regardless of which numbering style you choose to use, prefix your migration
files with the migration number followed by an underscore and a descriptive
name for the migration. For example:</p>
<ul class="simple">
<li>001_add_blog.php (sequential numbering)</li>
<li>20121031100537_add_blog.php (timestamp numbering)</li>
</ul>
</div>
<div class="section" id="create-a-migration">
<h2><a class="toc-backref" href="#id2">Create a Migration</a><a class="headerlink" href="#create-a-migration" title="Permalink to this headline"></a></h2>
<p>This will be the first migration for a new site which has a blog. All
migrations go in the <strong>app/Database/Migrations/</strong> directory and have names such
as <em>20121031100537_Add_blog.php</em>.</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span> <span class="k">namespace</span> <span class="nx">App\Database\Migrations</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">Migration_Add_blog</span> <span class="k">extends</span> <span class="nx">\CodeIgniter\Database\Migration</span> <span class="p">{</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">up</span><span class="p">()</span>
<span class="p">{</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">forge</span><span class="o">-&gt;</span><span class="na">addField</span><span class="p">([</span>
<span class="s1">&#39;blog_id&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
<span class="s1">&#39;type&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;INT&#39;</span><span class="p">,</span>
<span class="s1">&#39;constraint&#39;</span> <span class="o">=&gt;</span> <span class="mi">5</span><span class="p">,</span>
<span class="s1">&#39;unsigned&#39;</span> <span class="o">=&gt;</span> <span class="k">TRUE</span><span class="p">,</span>
<span class="s1">&#39;auto_increment&#39;</span> <span class="o">=&gt;</span> <span class="k">TRUE</span>
<span class="p">],</span>
<span class="s1">&#39;blog_title&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
<span class="s1">&#39;type&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;VARCHAR&#39;</span><span class="p">,</span>
<span class="s1">&#39;constraint&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;100&#39;</span><span class="p">,</span>
<span class="p">],</span>
<span class="s1">&#39;blog_description&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
<span class="s1">&#39;type&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;TEXT&#39;</span><span class="p">,</span>
<span class="s1">&#39;null&#39;</span> <span class="o">=&gt;</span> <span class="k">TRUE</span><span class="p">,</span>
<span class="p">],</span>
<span class="p">]);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">forge</span><span class="o">-&gt;</span><span class="na">addKey</span><span class="p">(</span><span class="s1">&#39;blog_id&#39;</span><span class="p">,</span> <span class="k">TRUE</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">forge</span><span class="o">-&gt;</span><span class="na">createTable</span><span class="p">(</span><span class="s1">&#39;blog&#39;</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">down</span><span class="p">()</span>
<span class="p">{</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">forge</span><span class="o">-&gt;</span><span class="na">dropTable</span><span class="p">(</span><span class="s1">&#39;blog&#39;</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Then in <strong>app/Config/Migrations.php</strong> set <code class="docutils literal"><span class="pre">$currentVersion</span> <span class="pre">=</span> <span class="pre">20121031100537;</span></code>.</p>
<p>The database connection and the database Forge class are both available to you through
<code class="docutils literal"><span class="pre">$this-&gt;db</span></code> and <code class="docutils literal"><span class="pre">$this-&gt;forge</span></code>, respectively.</p>
<p>Alternatively, you can use a command-line call to generate a skeleton migration file. See
below for more details.</p>
<div class="section" id="using-currentversion">
<h3><a class="toc-backref" href="#id3">Using $currentVersion</a><a class="headerlink" href="#using-currentversion" title="Permalink to this headline"></a></h3>
<p>The $currentVersion setting allows you to mark a location that your main application namespace should be set at.
This is especially helpful for use in a production setting. In your application, you can always
update the migration to the current version, and not latest to ensure your production and staging
servers are running the correct schema. On your development servers, you can add additional migrations
for code that is not ready for production, yet. By using the <code class="docutils literal"><span class="pre">latest()</span></code> method, you can be assured
that your development machines are always running the bleeding edge schema.</p>
</div>
<div class="section" id="database-groups">
<h3><a class="toc-backref" href="#id4">Database Groups</a><a class="headerlink" href="#database-groups" title="Permalink to this headline"></a></h3>
<p>A migration will only be run against a single database group. If you have multiple groups defined in
<strong>app/Config/Database.php</strong>, then it will run against the <code class="docutils literal"><span class="pre">$defaultGroup</span></code> as specified
in that same configuration file. There may be times when you need different schemas for different
database groups. Perhaps you have one database that is used for all general site information, while
another database is used for mission critical data. You can ensure that migrations are run only
against the proper group by setting the <code class="docutils literal"><span class="pre">$DBGroup</span></code> property on your migration. This name must
match the name of the database group exactly:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span> <span class="k">namespace</span> <span class="nx">App\Database\Migrations</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">Migration_Add_blog</span> <span class="k">extends</span> <span class="nx">\CodeIgniter\Database\Migration</span>
<span class="p">{</span>
<span class="k">protected</span> <span class="nv">$DBGroup</span> <span class="o">=</span> <span class="s1">&#39;alternate_db_group&#39;</span><span class="p">;</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">up</span><span class="p">()</span> <span class="p">{</span> <span class="o">.</span> <span class="o">.</span> <span class="o">.</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">down</span><span class="p">()</span> <span class="p">{</span> <span class="o">.</span> <span class="o">.</span> <span class="o">.</span> <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="namespaces">
<h3><a class="toc-backref" href="#id5">Namespaces</a><a class="headerlink" href="#namespaces" title="Permalink to this headline"></a></h3>
<p>The migration library can automatically scan all namespaces you have defined within
<strong>app/Config/Autoload.php</strong> or loaded from an external source like Composer, using
the <code class="docutils literal"><span class="pre">$psr4</span></code> property for matching directory names. It will include all migrations
it finds in Database/Migrations.</p>
<p>Each namespace has it&#8217;s own version sequence, this will help you upgrade and downgrade each module (namespace) without affecting other namespaces.</p>
<p>For example, assume that we have the following namespaces defined in our Autoload
configuration file:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$psr4</span> <span class="o">=</span> <span class="p">[</span>
<span class="s1">&#39;App&#39;</span> <span class="o">=&gt;</span> <span class="nx">APPPATH</span><span class="p">,</span>
<span class="s1">&#39;MyCompany&#39;</span> <span class="o">=&gt;</span> <span class="nx">ROOTPATH</span><span class="o">.</span><span class="s1">&#39;MyCompany&#39;</span>
<span class="p">];</span>
</pre></div>
</div>
<p>This will look for any migrations located at both <strong>APPPATH/Database/Migrations</strong> and
<strong>ROOTPATH/Database/Migrations</strong>. This makes it simple to include migrations in your
re-usable, modular code suites.</p>
</div>
</div>
<div class="section" id="usage-example">
<h2><a class="toc-backref" href="#id6">Usage Example</a><a class="headerlink" href="#usage-example" title="Permalink to this headline"></a></h2>
<p>In this example some simple code is placed in <strong>app/Controllers/Migrate.php</strong>
to update the schema:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span> <span class="k">namespace</span> <span class="nx">App\Controllers</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">Migrate</span> <span class="k">extends</span> <span class="nx">\CodeIgniter\Controller</span>
<span class="p">{</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">index</span><span class="p">()</span>
<span class="p">{</span>
<span class="nv">$migrate</span> <span class="o">=</span> <span class="nx">\Config\Services</span><span class="o">::</span><span class="na">migrations</span><span class="p">();</span>
<span class="k">try</span>
<span class="p">{</span>
<span class="nv">$migrate</span><span class="o">-&gt;</span><span class="na">current</span><span class="p">();</span>
<span class="p">}</span>
<span class="k">catch</span> <span class="p">(</span><span class="nx">\Exception</span> <span class="nv">$e</span><span class="p">)</span>
<span class="p">{</span>
<span class="c1">// Do something with the error here...</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="command-line-tools">
<h2><a class="toc-backref" href="#id7">Command-Line Tools</a><a class="headerlink" href="#command-line-tools" title="Permalink to this headline"></a></h2>
<p>CodeIgniter ships with several <a class="reference internal" href="../cli/cli_commands.html"><span class="doc">commands</span></a> that are available from the command line to help
you work with migrations. These tools are not required to use migrations but might make things easier for those of you
that wish to use them. The tools primarily provide access to the same methods that are available within the MigrationRunner class.</p>
<p><strong>latest</strong></p>
<p>Migrates all database groups to the latest available migrations:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o">&gt;</span> <span class="nx">php</span> <span class="nx">spark</span> <span class="nx">migrate</span><span class="o">:</span><span class="nx">latest</span>
</pre></div>
</div>
<p>You can use (latest) with the following options:</p>
<ul class="simple">
<li>(-g) to chose database group, otherwise default database group will be used.</li>
<li>(-n) to choose namespace, otherwise (App) namespace will be used.</li>
<li>(-all) to migrate all namespaces to the latest migration</li>
</ul>
<p>This example will migrate Blog namespace to latest:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o">&gt;</span> <span class="nx">php</span> <span class="nx">spark</span> <span class="nx">migrate</span><span class="o">:</span><span class="nx">latest</span> <span class="o">-</span><span class="nx">g</span> <span class="nx">test</span> <span class="o">-</span><span class="nx">n</span> <span class="nx">Blog</span>
</pre></div>
</div>
<p><strong>current</strong></p>
<p>Migrates the (App) namespace to match the version set in <code class="docutils literal"><span class="pre">$currentVersion</span></code>. This will migrate both
up and down as needed to match the specified version:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o">&gt;</span> <span class="nx">php</span> <span class="nx">spark</span> <span class="nx">migrate</span><span class="o">:</span><span class="nb">current</span>
</pre></div>
</div>
<p>You can use (current) with the following options:</p>
<ul class="simple">
<li>(-g) to chose database group, otherwise default database group will be used.</li>
</ul>
<p><strong>version</strong></p>
<p>Migrates to the specified version. If no version is provided, you will be prompted
for the version.</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="c1">// Asks you for the version...</span>
<span class="o">&gt;</span> <span class="nx">php</span> <span class="nx">spark</span> <span class="nx">migrate</span><span class="o">:</span><span class="nx">version</span>
<span class="nx">Version</span><span class="o">:</span>
<span class="c1">// Sequential</span>
<span class="o">&gt;</span> <span class="nx">php</span> <span class="nx">spark</span> <span class="nx">migrate</span><span class="o">:</span><span class="nx">version</span> <span class="mo">007</span>
<span class="c1">// Timestamp</span>
<span class="o">&gt;</span> <span class="nx">php</span> <span class="nx">spark</span> <span class="nx">migrate</span><span class="o">:</span><span class="nx">version</span> <span class="mi">20161426211300</span>
</pre></div>
</div>
<p>You can use (version) with the following options:</p>
<ul class="simple">
<li>(-g) to chose database group, otherwise default database group will be used.</li>
<li>(-n) to choose namespace, otherwise (App) namespace will be used.</li>
</ul>
<p><strong>rollback</strong></p>
<p>Rolls back all migrations, taking all database groups to a blank slate, effectively migration 0:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o">&gt;</span> <span class="nx">php</span> <span class="nx">spark</span> <span class="nx">migrate</span><span class="o">:</span><span class="nx">rollback</span>
</pre></div>
</div>
<p>You can use (rollback) with the following options:</p>
<ul class="simple">
<li>(-g) to chose database group, otherwise default database group will be used.</li>
<li>(-n) to choose namespace, otherwise (App) namespace will be used.</li>
<li>(-all) to migrate all namespaces to the latest migration</li>
</ul>
<p><strong>refresh</strong></p>
<p>Refreshes the database state by first rolling back all migrations, and then migrating to the latest version:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o">&gt;</span> <span class="nx">php</span> <span class="nx">spark</span> <span class="nx">migrate</span><span class="o">:</span><span class="nx">refresh</span>
</pre></div>
</div>
<p>You can use (refresh) with the following options:</p>
<ul class="simple">
<li>(-g) to chose database group, otherwise default database group will be used.</li>
<li>(-n) to choose namespace, otherwise (App) namespace will be used.</li>
<li>(-all) to migrate all namespaces to the latest migration</li>
</ul>
<p><strong>status</strong></p>
<p>Displays a list of all migrations and the date and time they ran, or &#8216;&#8211;&#8217; if they have not been run:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o">&gt;</span> <span class="nx">php</span> <span class="nx">spark</span> <span class="nx">migrate</span><span class="o">:</span><span class="nx">status</span>
<span class="nx">Filename</span> <span class="nx">Migrated</span> <span class="nx">On</span>
<span class="nx">First_migration</span><span class="o">.</span><span class="nx">php</span> <span class="mi">2016</span><span class="o">-</span><span class="mo">04</span><span class="o">-</span><span class="mi">25</span> <span class="mo">04</span><span class="o">:</span><span class="mi">44</span><span class="o">:</span><span class="mi">22</span>
</pre></div>
</div>
<p>You can use (refresh) with the following options:</p>
<ul class="simple">
<li>(-g) to chose database group, otherwise default database group will be used.</li>
</ul>
<p><strong>create</strong></p>
<p>Creates a skeleton migration file in <strong>app/Database/Migrations</strong>.</p>
<ul>
<li><p class="first">When migration type is timestamp, using the YYYYMMDDHHIISS format:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o">&gt;</span> <span class="nx">php</span> <span class="nx">spark</span> <span class="nx">migrate</span><span class="o">:</span><span class="nx">create</span> <span class="p">[</span><span class="nx">filename</span><span class="p">]</span>
</pre></div>
</div>
</li>
<li><p class="first">When migration type is sequential, using the numbered in sequence, default with 001:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o">&gt;</span> <span class="nx">php</span> <span class="nx">spark</span> <span class="nx">migrate</span><span class="o">:</span><span class="nx">create</span> <span class="p">[</span><span class="nx">filename</span><span class="p">]</span> <span class="mo">001</span>
</pre></div>
</div>
</li>
</ul>
<p>You can use (create) with the following options:</p>
<ul class="simple">
<li>(-n) to choose namespace, otherwise (App) namespace will be used.</li>
</ul>
</div>
<div class="section" id="migration-preferences">
<h2><a class="toc-backref" href="#id8">Migration Preferences</a><a class="headerlink" href="#migration-preferences" title="Permalink to this headline"></a></h2>
<p>The following is a table of all the config options for migrations, available in <strong>app/Config/Migrations.php</strong>.</p>
<table border="1" class="docutils">
<colgroup>
<col width="19%" />
<col width="16%" />
<col width="19%" />
<col width="45%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Preference</th>
<th class="head">Default</th>
<th class="head">Options</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><strong>enabled</strong></td>
<td>TRUE</td>
<td>TRUE / FALSE</td>
<td>Enable or disable migrations.</td>
</tr>
<tr class="row-odd"><td><strong>path</strong></td>
<td>&#8216;Database/Migrations/&#8217;</td>
<td>None</td>
<td>The path to your migrations folder.</td>
</tr>
<tr class="row-even"><td><strong>currentVersion</strong></td>
<td>0</td>
<td>None</td>
<td>The current version your database should use.</td>
</tr>
<tr class="row-odd"><td><strong>table</strong></td>
<td>migrations</td>
<td>None</td>
<td>The table name for storing the schema version number.</td>
</tr>
<tr class="row-even"><td><strong>type</strong></td>
<td>&#8216;timestamp&#8217;</td>
<td>&#8216;timestamp&#8217; / &#8216;sequential&#8217;</td>
<td>The type of numeric identifier used to name migration files.</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="class-reference">
<h2><a class="toc-backref" href="#id9">Class Reference</a><a class="headerlink" href="#class-reference" title="Permalink to this headline"></a></h2>
<dl class="class">
<dt id="CodeIgniterDatabaseMigrationRunner">
<em class="property">class </em><code class="descname">CodeIgniterDatabaseMigrationRunner</code><a class="headerlink" href="#CodeIgniterDatabaseMigrationRunner" title="Permalink to this definition"></a></dt>
<dd><dl class="method">
<dt id="CodeIgniterDatabaseMigrationRunner::current">
<code class="descname">current</code><span class="sig-paren">(</span><em>$group</em><span class="sig-paren">)</span><a class="headerlink" href="#CodeIgniterDatabaseMigrationRunner::current" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$group</strong> (<em>mixed</em>) &#8211; database group name, if null (App) namespace will be used.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">TRUE if no migrations are found, current version string on success, FALSE on failure</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">mixed</p>
</td>
</tr>
</tbody>
</table>
<p>Migrates up to the current version (whatever is set for
<code class="docutils literal"><span class="pre">$currentVersion</span></code> in <em>app/Config/Migrations.php</em>).</p>
</dd></dl>
<dl class="method">
<dt id="CodeIgniterDatabaseMigrationRunner::findMigrations">
<code class="descname">findMigrations</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#CodeIgniterDatabaseMigrationRunner::findMigrations" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">An array of migration files</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">array</td>
</tr>
</tbody>
</table>
<p>An array of migration filenames are returned that are found in the <strong>path</strong> property.</p>
</dd></dl>
<dl class="method">
<dt id="CodeIgniterDatabaseMigrationRunner::latest">
<code class="descname">latest</code><span class="sig-paren">(</span><em>$namespace</em>, <em>$group</em><span class="sig-paren">)</span><a class="headerlink" href="#CodeIgniterDatabaseMigrationRunner::latest" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$namespace</strong> (<em>mixed</em>) &#8211; application namespace, if null (App) namespace will be used.</li>
<li><strong>$group</strong> (<em>mixed</em>) &#8211; database group name, if null default database group will be used.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">Current version string on success, FALSE on failure</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">mixed</p>
</td>
</tr>
</tbody>
</table>
<p>This works much the same way as <code class="docutils literal"><span class="pre">current()</span></code> but instead of looking for
the <code class="docutils literal"><span class="pre">$currentVersion</span></code> the Migration class will use the very
newest migration found in the filesystem.</p>
</dd></dl>
<dl class="method">
<dt id="CodeIgniterDatabaseMigrationRunner::latestAll">
<code class="descname">latestAll</code><span class="sig-paren">(</span><em>$group</em><span class="sig-paren">)</span><a class="headerlink" href="#CodeIgniterDatabaseMigrationRunner::latestAll" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$group</strong> (<em>mixed</em>) &#8211; database group name, if null default database group will be used.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">TRUE on success, FALSE on failure</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">mixed</p>
</td>
</tr>
</tbody>
</table>
<p>This works much the same way as <code class="docutils literal"><span class="pre">latest()</span></code> but instead of looking for
one namespace, the Migration class will use the very
newest migration found for all namespaces.</p>
</dd></dl>
<dl class="method">
<dt id="CodeIgniterDatabaseMigrationRunner::version">
<code class="descname">version</code><span class="sig-paren">(</span><em>$target_version</em>, <em>$namespace</em>, <em>$group</em><span class="sig-paren">)</span><a class="headerlink" href="#CodeIgniterDatabaseMigrationRunner::version" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$namespace</strong> (<em>mixed</em>) &#8211; application namespace, if null (App) namespace will be used.</li>
<li><strong>$group</strong> (<em>mixed</em>) &#8211; database group name, if null default database group will be used.</li>
<li><strong>$target_version</strong> (<em>mixed</em>) &#8211; Migration version to process</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">Current version string on success, FALSE on failure or no migrations are found</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">mixed</p>
</td>
</tr>
</tbody>
</table>
<p>Version can be used to roll back changes or step forwards programmatically to
specific versions. It works just like <code class="docutils literal"><span class="pre">current()</span></code> but ignores <code class="docutils literal"><span class="pre">$currentVersion</span></code>.</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$migration</span><span class="o">-&gt;</span><span class="na">version</span><span class="p">(</span><span class="mi">5</span><span class="p">);</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="CodeIgniterDatabaseMigrationRunner::setNamespace">
<code class="descname">setNamespace</code><span class="sig-paren">(</span><em>$namespace</em><span class="sig-paren">)</span><a class="headerlink" href="#CodeIgniterDatabaseMigrationRunner::setNamespace" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$namespace</strong> (<em>string</em>) &#8211; application namespace.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">The current MigrationRunner instance</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">CodeIgniterDatabaseMigrationRunner</p>
</td>
</tr>
</tbody>
</table>
<p>Sets the path the library should look for migration files:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$migration</span><span class="o">-&gt;</span><span class="na">setNamespace</span><span class="p">(</span><span class="nv">$path</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">latest</span><span class="p">();</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="CodeIgniterDatabaseMigrationRunner::setGroup">
<code class="descname">setGroup</code><span class="sig-paren">(</span><em>$group</em><span class="sig-paren">)</span><a class="headerlink" href="#CodeIgniterDatabaseMigrationRunner::setGroup" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$group</strong> (<em>string</em>) &#8211; database group name.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">The current MigrationRunner instance</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">CodeIgniterDatabaseMigrationRunner</p>
</td>
</tr>
</tbody>
</table>
<p>Sets the path the library should look for migration files:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$migration</span><span class="o">-&gt;</span><span class="na">setNamespace</span><span class="p">(</span><span class="nv">$path</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">latest</span><span class="p">();</span>
</pre></div>
</div>
</dd></dl>
</dd></dl>
</div>
</div>
</div>
<div class="articleComments">
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="seeds.html" class="btn btn-neutral float-right" title="Database Seeding" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="forge.html" class="btn btn-neutral" title="Database Forge Class" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
&copy; Copyright 2014-2019 British Columbia Institute of Technology.
Last updated on May 06, 2019.
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'../',
VERSION:'4.0.0-beta.3',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: false,
SOURCELINK_SUFFIX: ''
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>