marshmallow/laravel-database-sync 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

marshmallow/laravel-database-sync

Composer 安装命令:

composer require marshmallow/laravel-database-sync

包简介

Sync your production database in to your local database to start developing with production data

README 文档

README

alt text

Laravel Database Sync

Latest Version on Packagist Tests Total Downloads

Sync your production database into your local database to start developing with production data. This package enables seamless synchronization of data from a remote database to your local development environment.

Table of Contents

Requirements

  • PHP ^8.2
  • Laravel ^10.0|^11.0|^12.0|^13.0

Installation

You can install the package via composer:

composer require marshmallow/laravel-database-sync --dev

Configuration

Publish the configuration file:

php artisan vendor:publish --tag="database-sync-config"

Environment Variables

Add these variables to your .env file:

DATABASE_SYNC_REMOTE_USER_AND_HOST="forge@1.1.1.1"
DATABASE_SYNC_REMOTE_DATABASE_USERNAME=forge
DATABASE_SYNC_REMOTE_DATABASE_PASSWORD=

DATABASE_SYNC_TEMPORARY_FILE_LOCATION_REMOTE=~/new_data.sql
DATABASE_SYNC_TEMPORARY_FILE_LOCATION_LOCAL=~/Downloads/new_data.sql

Important: When connecting to a Forge-provisioned database server, you must use the main database user that was created during the initial server provisioning. Other database users created afterward may not have the necessary privileges to execute the required database commands for synchronization.

Usage

Basic Synchronization

To sync your remote database to local:

php artisan db-sync

By default, the package uses batch file transfers for optimal performance, transferring all table data in a single file to minimize network overhead.

Advanced Options

The sync command supports several options:

php artisan db-sync [options]

Available options:

  • --date: Sync records from a specific date
  • --suite: Use a predefined suite of tables
  • --table: Sync a specific table
  • --tenant: Specify tenant for multi-tenant applications
  • --skip-landlord: Skip landlord database in multi-tenant setup
  • --full-sync: Sync the full table without a date constraint
  • --status: View the sync history and status for all tables
  • --individual-transfers: Use individual file transfers for each table (legacy behavior)

Per-Table Sync Tracking

The package now tracks the last sync date for each individual table, preventing data loss when syncing single tables. This means:

  • Each table maintains its own sync history
  • When syncing a specific table with --table, only that table's sync date is considered
  • The package automatically falls back to the global sync date for backward compatibility
  • You can view the sync status of all tables using the --status option
  • Sync timestamps are captured at the start of the process to prevent missing data created during sync

Viewing Sync Status

To see the last sync date for all tables:

php artisan db-sync --status

This will display a table showing each table name and its last sync date, helping you track which tables have been synced recently and which might need attention.

How It Works

  1. Sync Start Timestamp: The sync timestamp is captured when the sync process begins
  2. Individual Table Tracking: Each table's sync date is stored separately in the cache file
  3. Atomic Updates: Cache is only updated when the entire sync process completes successfully
  4. Automatic Fallback: If no table-specific date exists, the global sync date is used
  5. Backward Compatibility: Existing installations continue to work without any changes
  6. Debug Information: When running with -vvv (debug mode), you'll see which sync date is being used for each table
  7. Error Recovery: If sync fails, the cache remains unchanged with the previous sync dates

File Transfer Optimization

The package uses batch file transfers by default to minimize network overhead:

  • Batch Mode (Default): All tables are dumped to a single file and transferred once
  • Individual Mode: Each table is transferred separately (legacy behavior)

Configuration

Control the transfer mode in config/database-sync.php:

'file_transfer_mode' => 'batch', // or 'individual'

Or via environment variable:

DATABASE_SYNC_FILE_TRANSFER_MODE=batch

Benefits of Batch Transfer

  • Reduced network overhead: Single file transfer instead of multiple
  • Faster sync times: Especially noticeable with many tables
  • Better compression: SSH compression works more efficiently on larger files
  • Lower resource usage: Fewer process spawns and file operations

When Individual Transfers Are Used

  • Single table sync (--table=tablename)
  • Explicit override (--individual-transfers)
  • Config set to individual mode

Table Configuration

You can exclude specific tables from synchronization in the config/database-sync.php file:

'tables' => [
    'ignore' => [
        'action_events',
        'jobs',
        'telescope_entries',
        'password_resets',
    ],
],

Timeout Configuration

For large databases, you may need to adjust the process timeout to prevent operations from timing out:

// Set timeout in seconds (default: 300 seconds / 5 minutes)
'process_timeout' => 600, // 10 minutes

// Or set to null to disable timeout entirely for very large databases
'process_timeout' => null,

You can also set this via environment variable:

DATABASE_SYNC_PROCESS_TIMEOUT=600

This timeout applies to:

  • MySQL dump operations (mysqldump)
  • MySQL import operations (mysql)
  • File transfer operations (scp)

Synchronization Suites

Define custom synchronization suites in the configuration file to group tables for specific sync tasks:

'suites' => [
    'orders' => [
        'orders',
        'order_items',
    ],
],

Then use the suite option:

php artisan db-sync --suite=orders

Multi-Tenant Support

The package supports multi-tenant architectures. Enable it in the configuration:

'multi_tenant' => [
    'landlord' => [
        'database_name' => 'marshmallow_landord',
        'tables' => [
            'ignore' => [
                'action_events',
            ],
        ],
    ],
    'tenants' => [
        'database_names' => [
            'marshmallow_nl' => [
                'tables' => [
                    'ignore' => [
                        'users',
                    ],
                ],
            ],
            'marshmallow_dev',
            'marshmallow_io',
        ],
        'tables' => [
            'ignore' => [
                'logs',
            ],
        ],
    ],
],

Configure tenant-specific settings in your configuration file and use the --tenant option to sync specific tenant databases:

php artisan db-sync --tenant="marshmallow_nl" --skip-landlord
php artisan db-sync --tenant="marshmallow_nl" --skip-landlord --suite=orders

Testing

This package uses Pest PHP for testing. To run the tests:

composer test

To run tests with coverage report:

composer test-coverage

Test Structure

The test suite includes:

  • Unit Tests: Testing individual components

    • Config class tests
    • DatabaseSync class tests
    • Other utility classes
  • Feature Tests: Testing the package functionality

    • Command execution tests
    • Multi-tenant functionality
    • Suite configurations
    • Table filtering

Writing Tests

To add new tests, create a new test file in either the tests/Unit or tests/Feature directory. The package uses Pest's expressive syntax:

test('your test description', function () {
    // Your test code
    expect($result)->toBe($expected);
});

Security

  • Never commit sensitive database credentials to version control
  • Always use environment variables for sensitive information
  • Ensure proper access controls on both remote and local databases

Changelog

Please see CHANGELOG for more information on what has changed recently.

Support

For support, please email stef@marshmallow.dev

Credits

License

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

marshmallow/laravel-database-sync 适用场景与选型建议

marshmallow/laravel-database-sync 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.54k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 03 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「marshmallow」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 marshmallow/laravel-database-sync 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 marshmallow/laravel-database-sync 我们能提供哪些服务?
定制开发 / 二次开发

基于 marshmallow/laravel-database-sync 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.54k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-21