All Projects → stidges → laravel-fk-migration

stidges / laravel-fk-migration

Licence: MIT license
Helpful base migration for creating all your foreign key without worrying about the order of your migrations.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-fk-migration

migrations
Migrations is a database migration tool that uses go's database/sql from the standard library
Stars: ✭ 17 (-54.05%)
Mutual labels:  migrations
upscheme
Database migrations and schema updates made easy
Stars: ✭ 737 (+1891.89%)
Mutual labels:  migrations
shyft
⬡ Shyft is a server-side framework for building powerful GraphQL APIs 🚀
Stars: ✭ 56 (+51.35%)
Mutual labels:  migrations
strong migrations
Catch unsafe migrations in your Elixir application
Stars: ✭ 58 (+56.76%)
Mutual labels:  migrations
powerorm
A very simple but effective php orm
Stars: ✭ 21 (-43.24%)
Mutual labels:  migrations
krab
Krab is a migration and automation tool for PostgreSQL based on HCL syntax
Stars: ✭ 15 (-59.46%)
Mutual labels:  migrations
gloat
Next-gen database migrations framework for Go.
Stars: ✭ 15 (-59.46%)
Mutual labels:  migrations
mongodb-migrations
Managed migration support for MongoDB
Stars: ✭ 22 (-40.54%)
Mutual labels:  migrations
metana
Abstract task migration tool written in Go for Golang services. Database and non database migration management brought to the CLI.
Stars: ✭ 61 (+64.86%)
Mutual labels:  migrations
pg-migrate
PostgreSQL migration tool
Stars: ✭ 39 (+5.41%)
Mutual labels:  migrations
serverless-typeorm-migrations
Database migrations for AWS Lambda and RDS using TypeORM Migrations
Stars: ✭ 38 (+2.7%)
Mutual labels:  migrations
migrant
Migration management for PostgreSQL/SQLite/MySQL
Stars: ✭ 85 (+129.73%)
Mutual labels:  migrations
db-migrator.go
DB migrations. CLI and Golang
Stars: ✭ 13 (-64.86%)
Mutual labels:  migrations
apistar alembic migrations
Alembic migrations for apistar
Stars: ✭ 18 (-51.35%)
Mutual labels:  migrations
db-safedelete
Attempts to invoke force delete, if it fails - falls back to soft delete
Stars: ✭ 16 (-56.76%)
Mutual labels:  foreign-keys
underbase
MongoDB schema and data migration library based on semver
Stars: ✭ 19 (-48.65%)
Mutual labels:  migrations
linkifier
Database reverse engineering
Stars: ✭ 32 (-13.51%)
Mutual labels:  foreign-keys
butterfly
Application transformation tool
Stars: ✭ 35 (-5.41%)
Mutual labels:  migrations
migrant lib
Embeddable migration management
Stars: ✭ 22 (-40.54%)
Mutual labels:  migrations
slim migrations
Let's you write slightly slimmer Rails migrations.
Stars: ✭ 14 (-62.16%)
Mutual labels:  migrations

Laravel FK Migration

Latest Stable Version Total Downloads Build Status License

This Laravel package provides a base migration for you to extend to easily create all your foreign keys. If you ever ran into the problem where you had to reorganize all your migrations due to failing foreign key constraints, this is the package for you!

Getting Started

This package can be installed through Composer, just add it to your composer.json file:

{
    "require": {
        "stidges/laravel-fk-migration": "1.*"
    }
}

After you have added it to your composer.json file, make sure you update your dependencies:

composer update

Basic Usage

To get started, create a new class in your app/database/migrations/ directory. If you want to make sure this migration gets executed last, you can name it something like 9999_99_99_999999_create_foreign_keys.php (this might be slightly overdone, but you get the idea).

Next, add the following content to the empty class you just created:

<?php

use Stidges\LaravelFkMigration\Migration;

class CreateForeignKeys extends Migration {

    /**
     * The foreign keys to create or drop.
     *
     * @var array
     */
    protected $keys = [];

}

The $keys array is where you can define your foreign keys. It should be an associative array, where the key is the table name, and the value is a (list of) foreign key(s). Below you can find a list of options that can be specified for the foreign keys.

Key Default Description
column none The column on which to create the foreign key.
references 'id' The referenced column in the foreign table.
on none The referenced table. If this option is not passed then it will create the name from the passed column (e.g. 'user_id' becomes 'users')
onUpdate 'cascade' The referential action to execute when the referenced column is updated.
onDelete 'restrict' The referential action to execute when the referenced column is deleted.

Note: As a minimum you should specify the column property for each foreign key. If you forget to specify this, an exception will be thrown.

Basic Example

Below you can find a basic example for reference.

<?php

use Stidges\LaravelFkMigration\Migration;

class CreateForeignKeys extends Migration {

    protected $keys = [
        'posts'    => [ 'column' => 'category_id' ],
        'post_tag' => [
            [ 'column' => 'post_id', 'onDelete' => 'cascade' ],
            [ 'column' => 'tag_id' ],
        ],
    ];
    
}

Extended Example

Internally, the migration will call a getKeys() method, which by default returns the specified $keys array. You are free to override this method if you wish to have more flexibility when defining keys. For example, if you have a lot of tables referencing the users table, you can do the following:

<?php

use Stidges\LaravelFkMigration\Migration;

class CreateForeignKeys extends Migration {

    protected $keys = [];
    
    protected $presets = [
        'user' => [ 'column' => 'user_id' ],
    ];
    
    public function getKeys()
    {
        $keys = [
            'posts'      => [ $this->presets['user'] ],
            'tags'       => [ $this->presets['user'] ],
            'categories' => [ $this->presets['user'] ],
        ];
        
        return $keys;
    }
}

This way you don't have to copy the same foreign key reference over and over!

Contributing

All suggestions and pull requests are welcome! If you make any substantial changes, please provide tests along with your pull requests!

License

Copyright (c) 2014 Stidges - Released under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].