Using Phinx Programmatically

This is an old post!

This post is over 2 years old. Solutions referenced in this article may no longer be valid. Please consider this when utilizing any information referenced here.

Phinx is a really cool database migration package that allows you to write changes to your database as code. It keeps track of which changes have been applied and allows you the option of rolling back if you hit an issue.

All the documentation on Phinx describes a typical setup where you would run the phinx command to do your migrations. And that is all fine and good in most projects. But what happens if you are integrating Phinx into an existing project that already has a lot of the usual scaffolding in place?

Why would you want to do this? Maybe this file will be copiled into a Phar so there is no other commands to call. Maybe it uses an internal SQLite database that is persistent. In my case, I am building an application contained in a Docker container that already has symfony\console and a bunch of console commands. It seems weird to just shell out and run some commands.

But, Phinx is written in PHP. And that means that there is a good chance that there is a way to do it programmatically from within the code we have already written (such as from our existing bootstrap command).

It turns out that there is, and it is VERY straightforward. It looks something like this:

use Phinx\Config\Config;
use Phinx\Migration\Manager;

$base = "/full/path/to/your/root";

$phinx = new Manager(
    new Config([
        'paths' => [
            'migrations' => "$base/Database/Migrations",
            'seeds'      => "$base/Database/Seeds",
        ],
        'environments' => [
            'default_migration_table' => 'phinxlog',
            'default_database'        => 'production',
            'production'              => [
                'adapter'   => $config->get('database.driver'),
                'host'      => $config->get('database.host'),
                'user'      => $config->get('database.username'),
                'pass'      => $config->get('database.password'),
                'port'      => $config->get('database.port'),
                'name'      => $config->get('database.database'),
                'suffix'    => $config->get('database.suffix'),
            ]
        ]
    ]),
    $input,
    $output
);

// Run any migrations.
$phinx->migrate('production');

Want to rollback?

$phinx->rollback('production');

Some notes about this:

  • $input and $output are symfony/console’s InputInterface and OutputInterface. I was already running this from an existing console command so it made sense to just reuse them,

  • $config->get() is an config fetcher, but I am sure you can see what is going on here.

Basically, the config you write should match the Phinx Yaml file.

About the Author

Hi, I'm Rob! I'm a blogger and software developer. I wrote petfeedd, dystill, and various other projects and libraries. I'm into electronics, general hackery, and model trains and airplanes. I am based in Huntsville, Alabama, USA.

About Me · Contact Me · Don't Hire Isaiah Armstrong

Did this article help you out?

I don't earn any money from this site.

I run no ads, sell no products and participate in no affiliate programs. I do not accept gifts in exchange for articles, guest articles or link exchanges. I don't track you or sell your data. The only third-party Javascript on this website is Google Analytics.

In general I run this site very much like a 1990s homepage or early 2000s personal blog, meaning that I do this solely because it's fun! I enjoy writing and sharing what I learn.

If you found this article helpful and want to show your appreciation, a tip or donation would be very welcome. Feel free to choose from the options below.

Comments (0)

Interested in why you can't leave comments on my blog? Read the article about why comments are uniquely terrible and need to die. If you are still interested in commenting on this article, feel free to reach out to me directly and/or share it on social media.

Contact Me
Share It

Interested in reading more?

PHP

Monitoring for Filesystem Changes using PHP and Laravel

Let’s say you have a Laravel application that does some data processing, and you want to monitor a directory for incoming changes, that you can then process using queued jobs. There are a couple of ways you could do something like this. You could scan those directories on a schedule using a cronjob. It’s doable. But what happens if you want to monitor a few thousand directories for changes? You can use tools like incron. Also doable, but another dependency. But what if I told you you could do it all with PHP. And within Laravel, no less?
Read More
Release Announcements

New Open Source Code

Launched two new pieces of open source code in the last couple of months. PlayerControls PlayerControls is a macOS Cocoa framework that creates a View containing playback controls for media like videos or sounds. It is written in pure Swift 4 and has no dependencies. SearchParser SearchParser is a parser that converts a freeform query into an intermediate object, that can then be converted to query many backends (SQL, ElasticSearch, etc). It includes translators for SQL (using PDO) and Laravel Eloquent ORM. It supports a faceted language search as commonly found on many sites across the web. It is written in modern PHP. Both are licensed under the MIT license. Go check them out on Github.
Read More
PHP

Building Meaningful Video Thumbnails Using FFMPEG and PHP

Working on doing some upgrades for one of my clients and I hit on an idea. He has a lot of videos available, but each one only has a static image as a thumbnail, taken at a set point in the video (by default; the owner or and admin can go in and recreate the thumbnail at a different time point if they want.) But what if, instead, we could create an animated GIF composed of several frames from the video? From a user’s perspective, a single frame might not tell you a lot about a video. But ten frames taken over the course of the whole video can tell you a lot more about the video than the single frame would. How would we implement something like that?
Read More