arminsam/db-data-importer

一个方便的工具,用于将生产数据库中的选择数据导入到您的本地环境数据库

v0.0.5 2017-10-30 12:21 UTC

This package is not auto-updated.

Last update: 2024-09-19 05:48:42 UTC


README

DDI 是一个工具,它使您的应用程序中特定 主题 的数据(例如产品、订单、发货等)能够从源数据库导入到目标数据库,给定 id(例如产品 id = 100,101,102)。

一个 主题 可以是一个单独的表,或者是一系列相关表。

一个 源数据库 通常是从中导出数据的生产数据库。

一个 目标数据库 通常是将数据导入到的本地数据库。

该工具提供三个命令,每个命令针对特定目的

  1. 清理 命令可以在导入之前可选地使用,以截断提供的 上下文 中所有表。
  2. 导出 命令用于将数据或表结构导出到 .sql 备份文件中。
  3. 导入 命令用于将生成的 .sql 文件导入到目标数据库中。

该工具允许您定义三种类型的 上下文 来分组您的数据库表

  1. 忽略表上下文:应包含您在应用程序数据库中不关心其本地数据库中的数据的所有表(您只需要它们的表结构就可以了!)。这些表的数据永远不会从源数据库中导出。
  2. 固定表上下文:应包含您只需要导入一次数据的应用程序数据库中的所有表。通常,应用程序中的进程只从这些表中读取数据,但不向其中插入或更新数据。
  3. 操作表上下文:应包含包含动态操作数据的所有应用程序数据库表。您将使用此上下文中的主题来导入您需要在本地数据库中导入的选择性数据。

用例

此工具的一些有用应用包括

  • 您想在本地环境中测试一个功能,但没有必要/相关数据。
  • 您无法在生产环境中重现一个错误,因为您的本地数据库缺少必要/相关数据。
  • 您想为生产环境中发生的一些边缘情况编写测试,您需要生产数据库中的确切数据来完成这项工作。
  • 您希望能够在几秒钟内设置本地数据库,并在其中包含所有相关数据!

配置文件

配置文件是您定义源和目标数据库连接数据、上下文、主题和用例的表结构的地方。在包根目录中有一个示例 di_config.php 文件。如果您将此包作为应用程序的依赖项使用,则需要将配置文件复制到应用程序的根目录。

简单在线商店应用程序的配置
// ignored tables context contains all tables which you don't need the data for
'ignored_tables' => [
    'process_logs' => [],
    'product_history' => [],
    // ...
],

// fixed tables context contains all tables which you probabely need their data only once
'fixed_tables' => [
    'categories' => [],
    'countries' => [],
    'languages' => [],
    'users' => [
        'has_many' => [
            'users_roles' => [
                'foreign_key' => 'user_id'
                'belongs_to' => [
                    'roles' => [
                        'foreign_key' => 'role_id'
                    ]
                ]
            ]
        ]
    ],
    // ...
],

// operational tables context contains all tables  which you are going to regularly import the data for specific ids
'operational_tables' => [
    // topic: products
    'products' => [
        // we should add the pk of a table if it's not "id"
        'pk' => 'sku',
        'has_many' => [
            'images' => [
                'foreign_key' => 'product_id'
            ],
            'reviews' => [
                'foreign_key' => 'product_id'
            ]
        ]
    ],
    
    // topic: orders
    'orders' => [
        'has_many' => [
            'order_items' => [
                'foreign_key' => 'order_id',
                'belongs_to' => [
                    // this is how you reference from one topic to another
                    '@products' => [
                        'foreign_key' => 'product_id',
                        // we should add the other_key if the foreign_key does not reference to other tables "id" column
                        'other_key' => 'sku'
                    ]
                ]
            ]
        ]
    ]
]

使用方法

您可以通过以下两种方式之一开始使用此工具

运行命令行命令

该工具包含一个位于 bin/data-importer(如果您将其作为应用程序的依赖项安装,则为 vendor/bin/data-importer)的可执行脚本。以下是您可以使用 cli 命令的几种方式

# clean all tables configured as operational_tables
vendor/bin/data-importer cleanup --context=operational_tables

# clean all tables in database
vendor/bin/data-importer cleanup --context=all
# export table schemas
vendor/bin/data-importer export --table-schemas=true

# export fixed_tables data
vendor/bin/data-importer export --fixed-tables=true

# export all data within posts topic for posts id IN (100,101,1002) 
vendor/bin/data-importer export --topic=posts --ids=100,101,102

# export everything (good for initializing the db for the first time
vendor/bin/data-importer export --topic=posts --ids=100,101,102 --table-schemas=true --fixed-tables=true 
在您的应用程序中使用

您还可以在您的代码中实例化 DataImporterClient 类,并如下使用它

$client = new DataImporter\DataImporterClient\DataImporterClient();
$client->cleanup()
       ->export('posts', [100, 101, 102], false, false)
       ->import();