Giter Club home page Giter Club logo

andrewgatenbyvs / snap-migrations Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 3.0 32 KB

๐Ÿš€ Super-fast alternative to running database migrations (and optionally, seeds) during Tests, for Lumen & Laravel. Can save 75% of the migration (and seed) setup time, versus using the regular DatabaseMigrations trait.

PHP 100.00%
lumen lumen-php-framework lumen-package unit-testing database-migrations integration-testing php-library phpunit phpunit-util laravel

snap-migrations's Introduction

Code Climate maintainability Code Climate technical debt GitHub last commit PHP from Packagist Packagist

Snap Migrations (for Lumen & Laravel)

This package is designed to be used within your TestCase class and allows for Lumen & Laravel applications to create a static SQL dump of a migrated (and seeded) database. It will automatically do this on the first Test run from a TestCase that uses this Trait. Each subsequent Test run will then use the SQL dumped copy of the database.

Install

Install the package via Composer:

composer require --dev andrewgatenby/snapmigrations

Usage

After a successful installation, you can add SnapMigration to your Test classes (potentially a parent class). This package is designed to replace the regular DatabaseMigrations class that you might use:

<?php

namespace Tests;

use AndrewGatenby\SnapMigrations\SnapMigrations;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class MyAmazingTestCase extends BaseTestCase
{
    use SnapMigrations;
}

The Snap Migration SQL dump itself will be stored in storage/snap_migration.sql by default, but this can be modified by use of an environment variable called SNAP_MIGRATION_SQL_FILE. Note that it would still be within storage/.

If there is a new database migration created in database/migrations then Snap Migrations will burst its own cached copy and generate a new snapshot.

If the Snap Migration gets stuck or out of sync, you can manually delete the file and it will be built afresh.

The SnapMigrations Trait has its own setUp method as the main entry point. If your Test classes have their own setUp method, then you can alias the SnapMigrations::setUp method, similar to this:

<?php

namespace Tests;

use AndrewGatenby\SnapMigrations\SnapMigrations;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class MyAmazingTestCase extends BaseTestCase
{
    use SnapMigrations {
        setUp as setUpSnapMigrations;
    }
    
    public function setUp()
    {
        // run setUp for Snap Migrations, which also calls parent::setUp()
        $this->setUpSnapMigrations();
        // my regular setUp tasks for MyAmazingTestCase
    }
}

Credit

This package was inspired by the awesome looking Snipe Migrations that I'd tried to make use of first, but needed a Lumen-friendly implementation. It also uses the excellent MySQL Dump package.

snap-migrations's People

Contributors

andrewgatenbyvs avatar martinbean avatar martinparrish avatar westie avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.