Migrations

You'll write .php (or .sql) files in your migrations dir, which execute SQL to setup and modify your database.

@see_files(docs/RunMigrations.md; Run Migrations, docs/CRUD.md; CRUD & Stored SQL Usage, docs/dev/StoredSql.md; Stored SQL Setup)

Docs

  • File Structure
  • Sample up.php

File Structure

db/
    sql/ ...
    orm/ ...
    sqlv1/ -- optional secondary SQL dir used only during your v1 migrations
    migrate/
        v1/
            up.php    -- executed when going from 0 to 1
            down.php  -- executed when going from 2 to 1
            OtherFile.sql -- you can throw any old files in this dir to use during your migrations.
        v2/
            up.php

Sample up.php

Referencing the sample SQL in @see_file(docs/dev/StoredSql.md, StoredSql).

db/migrate/v1/up.php:

<?php
/**
* @var \Tlf\BigDb $db your BigDb instance is exposed to the Migration scripts
*/

$db->recompile_sql(); // to ensure any changes to your .sql files are loaded.
// $db->addSqlDir($db->root_dir.'/sqlv1/', $force_recompile=true);  // Optional secondary SQL dir just for migrations

$db->exec('create.article');
$db->exec('create.author');

Tip: You can use raw up.sql or down.sql in your migrations dirs instead of .php.