ekolotech/database-importer

允许将数据库导入到另一个数据库

2.0.0 2024-06-02 12:59 UTC

This package is auto-updated.

Last update: 2024-10-02 13:36:12 UTC


README

ekolotech/database-importer 是一个命令行组件,允许

  • 将一个称为 source 的数据库导入到另一个称为 destination 的数据库中
  • 将一个称为 source 的数据库导出到一个 .sql 文件中
  • .sql 文件将数据库导入到称为 destination 的数据库中

作者: @igorcyberdyne@EKOLOTECH

支持的数据库

  • Mysql
  • MariaDB

安装

您可以在

https://github.com/igorcyberdyne/DatabaseImporter.git 下载组件

或者在控制台执行以下命令

composer require ekolotech/database-importer

描述

为了执行以下表格中描述的操作,根据需要,您需要实现数据库配置的接口,使用 CommandHandler 实例,添加触发操作的命令(查看 2. 实现示例)。

- 命令列表

1. 根据命令和数据库模型介绍配置接口

接口的方法允许提供源数据库和目标数据库的连接数据。

interface SourceToDestinationDatabaseCommandConfig
{
    public function getSource(): Database;
    public function getDestination(): Database;
}

interface ExportDatabaseCommandConfig
{
    public function getSource(): Database;
}

interface ImportDatabaseCommandConfig
{
    public function getDestination(): Database;
}
class Database
{
    public function __construct(
        string $name, // Nom de la base
        string $host, // le host du serveur
        string $user, // l'utilisateur sur lequel se connecter
        string $password, // le mot de passe de l'utilisateur
    )
    {
    }
}

2. 实现示例

- 主文件(DatabaseImporter > demo > console)

<?php

use DatabaseImporter\Argv;
use DatabaseImporter\CommandHandler;
use DatabaseImporter\model\Database;
use DatabaseImporter\model\ExportDatabaseCommandConfig;
use DatabaseImporter\model\ImportDatabaseCommandConfig;
use DatabaseImporter\model\SourceToDestinationDatabaseCommandConfig;

class ExampleSourceToDestinationDatabaseCommandConfig implements SourceToDestinationDatabaseCommandConfig
{
    public function getSource(): Database
    {
        return new Database(
            "source_database",
            "127.0.0.1",
            "root",
            ""
        );
    }

    public function getDestination(): Database
    {
        return new Database(
            "destination_database",
            "127.0.0.1",
            "root",
            ""
        );
    }
}

class ExampleExportDatabaseCommandConfig implements ExportDatabaseCommandConfig
{
    public function getSource(): Database
    {
        return new Database(
            "source_database",
            "127.0.0.1",
            "root",
            ""
        );
    }
}

class ExampleImportDatabaseCommandConfig implements ImportDatabaseCommandConfig
{
    public function getDestination(): Database
    {
        return new Database(
            "destination_database",
            "127.0.0.1",
            "root",
            ""
        );
    }
}

$commandHandler = new CommandHandler();
$commandHandler->add(new ExampleSourceToDestinationDatabaseCommandConfig());
$commandHandler->add(new ExampleImportDatabaseCommandConfig());
$commandHandler->add(new ExampleExportDatabaseCommandConfig());

try {
    $commandHandler->run(new Argv());
} catch (Exception $e) {
    die($e->getMessage());
}

3. 从项目根目录执行命令

php demo/console [commandName]

示例 1:将数据库 A 导入到数据库 B 中

如果指定了 --migrationDir 选项,则数据库 A 将被记录在指定的目录中的一个 SQL 文件中

php demo/console database:import-from-another-database

示例 2:将数据库导出到文件

导出的文件位于指定的目录中,文件名符合此格式 MigrationV.*.sql。如果未指定选项,则文件将被导出到系统临时目录中,执行结束后将在控制台指示该目录的路径。

php demo/console database:export-to-file --migrationDir='C:\Project\DatabaseImporter'

示例 3:从文件导入数据库

如果指定了 --migrationDir 选项,则数据库 A 将被记录在指定的目录中的一个 SQL 文件中

php demo/console database:import-from-file --dumpFilePath='C:\Project\DatabaseImporter\database.sql'