Code Monkey home page Code Monkey logo

laravel-migrate-fresh's Introduction

The migrate:fresh command is included by default since Laravel 5.5.

An artisan command to build up the database from scratch

Latest Version on Packagist Software License Build Status Quality Score StyleCI Total Downloads

Laravel has a migrate:refresh command to build up the database using migrations. To clear the database it'll first rollback all migrations by using the down method in each migration.

But what if you don't use the down method inside your projects? The migrate:refresh command will fail as the database isn't cleared first.

This package contains a migrate:fresh command that'll nuke all the tables in your database regardless of whether you've set up the down method in each migration.

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

The best postcards are published on our website.

Requirements

This command supports MySQL, sqlite, PostgreSQL and MS SQL databases.

Note: the migrate:fresh command is included by default in Laravel 5.5.

Installation

You can install the package via composer:

composer require spatie/laravel-migrate-fresh

Next add the Spatie\MigrateFresh\Commands\MigrateFresh class to your console kernel.

// app/Console/Kernel.php

protected $commands = [
   ...
    \Spatie\MigrateFresh\Commands\MigrateFresh::class,
]

Usage

This command will drop all tables from your database and run all migrations.

php artisan migrate:fresh

Be aware that the command will knock down all tables in the database. Tables not belonging to your application will be dropped as well.

By tagging on the seed option all seeders will run as well.

php artisan migrate:fresh --seed

If the command is being executed in a production environment, confirmation will be asked first. To suppress the confirmation use the force option.

php artisan migrate:fresh --force

Events

This package fires several events, which you can hook into to perform some extra logic rebuilding the database.

  • Spatie\MigrateFresh\Events\DroppingTables: will be fired right before dropping the first table
  • Spatie\MigrateFresh\Events\DroppedTables: will be fired right after all the tables have been dropped and before running the up steps of the migrations

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

In order for the test to be run you should set up an empty mysql database laravel_migrate_fresh . The username should be root, the required password should be empty.

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

Initial code to drop all tables in a Postgresql db was provided by Jonathan Reinink and reviewed by Peter Matseykanets. Peter also added support for Sqlite.

About Spatie

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

The MIT License (MIT). Please see License File for more information.

laravel-migrate-fresh's People

Contributors

brnpimentel avatar broutard avatar carusogabriel avatar drbyte avatar federicogodan avatar freekmurze avatar introwit avatar jakub-klapka avatar jonphipps avatar matthijsvdorp avatar msonowal avatar pmatseykanets avatar prolonginc avatar redalpha01 avatar rojtjo avatar sebastiandedeyne avatar skyrpex 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

laravel-migrate-fresh's Issues

Parse Error

I'm getting an interesting error in my logs.

[2017-06-23 17:19:00] local.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'parse error, expecting `';'' or `'{''' in /PATHTOAPPLICATION/vendor/spatie/laravel-migrate-fresh/src/Commands/MigrateFresh.php:59
Stack trace:
#0 {main}  

The line in question: https://github.com/spatie/laravel-migrate-fresh/blob/master/src/Commands/MigrateFresh.php#L59

The things that make this error interesting:

  1. It shows up only in the laravel.log file
  2. The command works fine when ran
  3. It seems to show up in the log every time the scheduler is ran from crontab (every minute)
  4. I setup a new laravel installation and pulled in only this package and it does show the error

It seems to have something to do with the return type. If I remove the return type it works fine.

Tables are not the only objects in the database

Although for the majority of simple, in terms of DB schema, projects just a table dropper will suffice, but technically speaking there are other very important objects in the database that can be migrated and should be taken care of:

  • sequences - the ones that weren't created as part of SERIAL data type and therefore won't be affected by CASCADE
  • functions
  • custom data types and domains

And there is also a situation when one may use multiple schemas in the database for very good and practical reasons i.e. separating staging/processing and factual schemas.

Class conflict with Schema

Thanks for this project, it's very helpful.

I unfortunately have a project that contains an un-namespaced class called "Schema" that I'm having to integrate into a Laravel app. I can't alter that class, so in my own migrations I've had to fully qualify the Schema facade. I know this isn't your problem at all, but it would be helpful to me (and perhaps future others in my situation) if you could fully qualify the use Schema in Spatie\MigrateFresh\TableDroppers\Mysql, like so:

<?php

namespace Spatie\MigrateFresh\TableDroppers;

use DB;
use stdClass;
use Illuminate\Support\Facades\Schema;

class Mysql implements TableDropper

If not, no worries... Just close this issue and I'll work with a personal fork.

[FEATURE] Support for LaravelDoctrine Migrations

Hello,
do you think, that adding support for LaravelDoctrine Migrations (http://www.laraveldoctrine.org/docs/1.3/migrations) makes sense for this base package? I see lot of people lately, who are starting to use Doctrine on Laravel (I also understand why) and this might be useful for them.

Technically, it simply means not calling migrate, but doctrine:migrations:migrate. But there will be need for configuration. I'm thinking standard migrate-fresh.php published into config_path(), where you would specify usage of Laravel migrations or Doctrine migrations.
Feature detection is not possible, since people can use Doctrine with Laravel migrations and vice versa.

I can make PR for that, if you are interested to have it in your package.

Thanks, Jakub

Dangerous scope, does not support table prefixes

First of all, thank you for your awesome contributions.

My concern is that running this inside shared Homestead drops tables that belong to other projects.

There should be implementation of some safeguards that let's the developer know what tables are about to be dropped and a possibility to bail out before any destructive actions take place.

You are welcome to check out how I implemented it in our similar command. It's still in development and takes a bit different approach (and hasn't been tested yet).

Rename `TableDropper` to `DbObjectDropper`

I'd propose to change the interface to something like

namespace Spatie\MigrateFresh\DbObjectDroppers;

interface DbObjectDropper
{
    public function dropAll();
}

to better reflect the reality - tables are not the only database objects that need to be taken care of.

@freekmurze What is your take on it? I can do the PR and probably help with MySQL and SQLite droppers.

Double prefix for mysql tables

When creating & loading snapshots via the command snapshot:create and snapshot:load, an error occurs, because the table names get prefixed twice.

testuser@e61ca43e6aa7:/var/www/laravel$ ./artisan snapshot:create                                                   
Creating new snapshot...
Snapshot `2018-12-20_15-26-06` created (size: 9.59 MB)
testuser@e61ca43e6aa7:/var/www/laravel$ ./artisan snapshot:load 2018-12-20_15-26-06

In Connection.php line 664:
                                                                                                                                                               
  SQLSTATE[42S02]: Base table or view not found: 1051 Unknown table 'homestead.lv_lv_localfinder_categories' (SQL: drop table `lv_lv_localfinder_categories`)  
                                                                                                                                                               

In PDOStatement.php line 143:
                                                                                                              
  SQLSTATE[42S02]: Base table or view not found: 1051 Unknown table 'homestead.lv_lv_localfinder_categories'  
                                                                                                              

In PDOStatement.php line 141:
                                                                                                              
  SQLSTATE[42S02]: Base table or view not found: 1051 Unknown table 'homestead.lv_lv_localfinder_categories'  
                                                                                                              

I have tracked it down to Spatie\MigrateFresh\TableDroppers\Mysql::dropAllTables(), apparently Schema::drop() expects the table name without the prefix, but the query SHOW FULL TABLES WHERE Table_Type = 'BASE TABLE' returns the full table name. Probably this has changed in laravel at some point? I am using laravel v5.5.40

My current work around is to use a custom command, which sets the database prefix to an empty string before calling the snapshot:load command:

// in a custom console command handle()
\DB::connection(\DB::getDefaultConnection())->setTablePrefix('');
$this->call('snapshot:load', ['name' => $name]);

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.