GithubHelp home page GithubHelp logo

wp-migrations's Introduction

Delicious Brains WordPress Migrations

A WordPress library for managing database table schema upgrades and data seeding.

Ever had to create a custom table for some plugin or custom code to use? To keep the site updated with the latest version of that table you need to keep track of what version the table is at. This can get overly complex for lots of tables.

This package is inspired by Laravel's database migrations. You create a new migration PHP file, add your schema update code, and optionally include a rollback method to reverse the change.

Simply run wp dbi migrate on the command line using WP CLI and any migrations not already run will be executed.

The great thing about making database schema and data updates with migrations, is that the changes are file-based and therefore can be stored in version control, giving you better control when working across different branches.

Requirements

This package is designed to be used on a WordPress site project, not for a plugin or theme.

It needs to be running PHP 5.3 or higher.

You need to have access to run WP CLI on the server. Typically wp dbi migrate will be run as a last stage build step in your deployment process.

Installation

  • composer require deliciousbrains/wp-migrations
  • Bootstrap the package by adding \DeliciousBrains\WPMigrations\Database\Migrator::instance(); to an mu-plugin.

Migrations

By default, the command will look for migration files in /app/migrations directory alongside the vendor folder. This can be altered with the filter dbi_wp_migrations_path. Other paths can be added using the dbi_wp_migrations_paths filter.

Migration file names should follow the yyyy_mm_dd_classname format, eg. 2020_04_09_AddCustomTable.php

An example migration to create a table would look like:

2020_04_09_AddCustomTable.php

<?php

use DeliciousBrains\WPMigrations\Database\AbstractMigration;

class AddCustomTable extends AbstractMigration {

    public function run() {
        global $wpdb;

        $sql = "
            CREATE TABLE " . $wpdb->prefix . "my_table (
            id bigint(20) NOT NULL auto_increment,
            some_column varchar(50) NOT NULL,
            PRIMARY KEY (id)
            ) {$this->get_collation()};
        ";

        dbDelta( $sql );
    }
	
    public function rollback() {
        global $wpdb;
        $wpdb->query( 'DROP TABLE ' . $wpdb->prefix . 'my_table');
    }
}

We are also using the migrations to deploy development data changes at deployment time. Instead of trying to merge the development database into the production one.

For example, to add a new page:

<?php

use DeliciousBrains\WPMigrations\Database\AbstractMigration;

class AddPricingPage extends AbstractMigration {

    public function run() {
        $pricing_page_id = wp_insert_post( array(
            'post_title'  => 'Pricing',
            'post_status' => 'publish',
            'post_type'   => 'page',
        ) );
        update_post_meta( $pricing_page_id, '_wp_page_template', 'page-pricing.php' );
    }
}

Use

You can run specific migrations using the filename as an argument, eg. wp dbi migrate AddCustomTable.

To rollback all migrations you can run wp dbi migrate --rollback, or just a specific migration wp dbi migrate AddCustomTable --rollback.

To quickly scaffold a new migration you can run wp scaffold migration <name>. For example, wp scaffold migration MyMigration will create a new class named MyMigration in the default migration files directory with the correct filename and all required boilerplate code.

wp-migrations's People

Contributors

dennisenderink avatar llanilek avatar polevaultweb avatar szepeviktor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wp-migrations's Issues

PHP 8 compatiblity

Hi,

Can we please merge #11

Every WP-CLI command triggers this warning

xx@yy:~/public_html$ wp search-replace jaimemartinez.local jaimemartinez.nl --network
PHP Warning:  The magic method DeliciousBrains\WPMigrations\Database\Migrator::__wakeup() must have public visibility in /home/xxdomains/jaimemartinez.nl/deployments/releases/20230202131844/vendor/deliciousbrains/wp-migrations/src/Database/Migrator.php on line 223
Warning: The magic method DeliciousBrains\WPMigrations\Database\Migrator::__wakeup() must have public visibility in /home/xxdomains/jaimemartinez.nl/deployments/releases/20230202131844/vendor/deliciousbrains/wp-migrations/src/Database/Migrator.php on line 223

Thank you!

Does this repo need a maintainer?

Hi,
I was wondering if I could help maintaining this repo.
This package looks very promising, as it can allow versioning site configuration (a very recurring problem in Wordpress).
There are 4 very sound pull requests from 2020 which yet haven't been merged and I have seen no activity.
I would be glad to help testing and merging those and keeping this repo active.

Cheers!

Documentation doesn't match behavior for multiple migrations

As per README.md, wp dbi migrate --rollback without the migration argument, should rollback all migrations.

To rollback all migrations you can run wp dbi migrate --rollback, or just a specific migration wp dbi migrate AddCustomTable --rollback.

However, on

if ( $migration && isset( $assoc_args['rollback'] ) ) {

a specific migration is needed for rollback to work, otherwise the command behaves just as without the flag.

Can't manage to make it work

Hi, after some hours trying to make this thing work I think I'm missing something..

I followed the readme steps with no luck, I also tried to filer the path and even checking the filter got applied I always get the same message Warning: There are no migrations to run.

I'm requiring wp-migrations from the root of my project as I'm also pulling wp and other deps via composer.

Not sure what you mean on the readme with "Bootstrap the package by adding \DeliciousBrains\WPMigrations\Database\Migrator::instance(); to an mu-plugin."

I got that line on a mu-plugin but still no luck. I got another error also when trying to run wp dbi migrate, I don't remember it but after running wp dbi migrate --setup it went away leaving me with the mentioned one.

I also tried using the default route app/migrations/AddTestTable.php (removing my filter of course) and same message is printed out.

Any hints?? Thanks!

Infinite Loop because 'vendor' does not exist

In Migrator.php on line 127 you run a loop with the following condition:

basename( $base_path ) != 'vendor'

The name of the vendor-directory is configurable in the composer.json. If that is configured there, this while-loop runs infinitely. Might it not be better to simply use WP_CONTENT_DIR instead?

The function should (in my opinion) look like this:

protected function get_migrations_path() {
    return apply_filters( 'dbi_wp_migrations_path', WP_CONTENT_DIR . '/migrations' );
}

This way it's getting the correct migrations-path correctly, independent of the project-structure.

Thanks in advance,
David

Core tables?

Hi guys,

Just wondering if core table migrations would work with this? Or do the core tables need to be present for the library to work?

Cheers :)

Update AWS SDK

This issue is essentially the same as deliciousbrains/wp-amazon-s3-and-cloudfront#536. The same benefit of using an IAM role should work here too.

I'm attempting to use IAM roles linked to a Kubernetes service account to grant access to S3 instead of giving the entire node an IAM role. Amazon's documentation says that version 3.110.7 or newer of the PHP SDK is required. There have been bugfixes since then, so an update to the latest PHP SDK would be appreciated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.