Migrate DB for Laravel

repository·main·Indexed 41 days ago

https://github.com/thedragoncode/migrate-db

A Laravel package for automating data transfer between different database connections. It provides the `db:migrate` Artisan command to migrate schemas and records from a source to a destination database, with support for MySQL 5.7+, PostgreSQL 9.5+, and MSSQL. Compatible with PHP ^8.0 and Laravel versions ^8.0 through ^13.0.

Tokens
1.4K
Snippets
9
Records
11
Agent score
99%

What's inside Migrate DB

  1. Use the db:migrate command to transfer data

    main

    To transfer data from one database to another, first ensure both the source and target connections are configured in your config/database.php file. Then, run the db:migrate Artisan command specifying the source and target connection names using the --schema-from and --schema-to flags.

    php artisan db:migrate --schema-from=foo --schema-to=bar
  2. Exclude specific tables from migration

    main

    To skip certain tables during the migration process, use the --exclude flag multiple times to specify the tables that should be ignored.

    php artisan db:migrate --schema-from=foo --schema-to=bar --exclude=table1 --exclude=table2
  3. Migrate only specific tables

    main

    If you do not want to migrate the entire schema, use the --tables flag multiple times to specify the exact tables you want to include in the transfer.

    php artisan db:migrate --schema-from=foo --schema-to=bar --tables=table1 --tables=table2
  4. Reference: db:migrate CLI flags

    main

    The db:migrate command uses the following flags to control the data transfer process:

    • --schema-from=<connection_name>: The name of the source database connection.
    • --schema-to=<connection_name>: The name of the target database connection.
    • --tables=<table_name>: Specify a table to include in the migration (can be used multiple times).
    • --exclude=<table_name>: Specify a table to exclude from the migration (can be used multiple times).
  5. Compatibility and Requirements

    main

    Ensure your environment meets the following requirements for Migrate DB:

    PHP & Laravel Versions

    • PHP: ^8.0
    • Laravel: ^8.0, ^9.0, ^10.0, ^11.0, ^12.0, ^13.0

    Supported Databases

    • MySQL 5.7+
    • PostgreSQL 9.5+
    • MSSQL

    PostgreSQL Version Compatibility Note that for Laravel versions 11, 12, and 13, PostgreSQL support is available starting from PostgreSQL version 12.

  6. Initialize a database builder with Manager::of() and get()

    main

    The DragonCode\MigrateDB\Database\Manager class is the entry point for obtaining a database-specific builder. You first specify which Laravel database connection to use via the of(string $connection) method, and then retrieve the appropriate builder implementation using get(). The get() method automatically detects the driver (e.g., MySQL, Postgres, SQL Server) from your Laravel configuration and returns a builder instance that implements DragonCode\Contracts\MigrateDB\Builder.

    use DragonCode\MigrateDB\Database\Manager;
    
    // Get a builder for the 'mysql' connection defined in config/database.php
    $builder = (new Manager())->of('mysql')->get();
  7. Use the BuilderManager facade to manage database migrations

    main

    The BuilderManager facade provides a convenient way to access the DragonCode\MigrateDB\Database\Manager service. It allows you to target specific database connections and resolve the underlying Builder instance used for migration operations.

    Use of(string $connection) to switch the context to a specific database connection, and resolve() to obtain the Builder implementation.

    use DragonCode\MigrateDB\Facades\BuilderManager;
    
    // Access the manager for a specific connection and resolve the builder
    $builder = BuilderManager::of('mysql')->resolve();
  8. Supported database drivers

    main

    The DragonCode\MigrateDB\Constants\Drivers class defines the supported database driver identifiers used throughout the package. When configuring or interacting with the package, use these exact string constants to specify the target database type.

    use DragonCode\MigrateDB\Constants\Drivers;
    
    // Available driver constants:
    // Drivers::MYSQL      // 'mysql'
    // Drivers::POSTGRES   // 'pgsql'
    // Drivers::SQL_SERVER // 'sqlsrv'
  9. Reference: db:migrate CLI options

    main

    The following options are available for the db:migrate command to control the scope and behavior of the data transfer.

    --schema-from= : Source connection name (Required)
    --schema-to=   : Target connection name (Required)
    --exclude=*    : Comma separated table names to exclude from migration
    --tables=*     : Comma separated table names to migrate ONLY (overrides default behavior)
  10. Use the db:migrate CLI command

    main

    The db:migrate command facilitates data transfer between two database connections. It allows you to specify a source connection, a target connection, and control which tables are migrated or excluded. During execution, the command may prompt you to run Laravel migrations on the source or target connections, truncate tables, or drop all tables in the target database.

    php artisan db:migrate --schema-from=source_db --schema-to=target_db