khaydarovm/php-clickhouse-migrator

Clickhouse 迁移工具

v2.0.0 2023-04-24 09:54 UTC

This package is auto-updated.

Last update: 2024-09-24 12:56:12 UTC


README

Clickhouse 迁移工具

Daily Downloads Latest Stable Version Coverage Status

需求

PHP 7.0 或更高版本

安装

可以通过 composer 获取

composer require khaydarov\php-clickhouse-migrator

安装完成后,您可以通过脚本 ./vendor/bin/clickhouse-migrator 运行迁移命令

使用方法

运行脚本时,会显示可用命令列表

$ ./clickhouse-migrator
Console Tool

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  create    Create new migration file
  help      Displays help for a command
  init      Initialize Clickhouse migrations project
  list      Lists commands
  migrate   Migrate to specific revision of database

初始化

每个命令都在 composer.json 和 vendor 目录所在的项目根目录下执行。

在运行命令之前,您需要创建一个配置文件。您可以使用自己创建的文件,或者运行 init 创建新的配置文件。

以下命令创建新的 PHP 配置文件

vendor/bin/clickhouse-migrator init -f php

配置

支持两种配置扩展:YAML 和 PHP

配置结构

default: development

paths:
  migrations: migrations

environments:
  development:
    cluster: local
    host: localhost
    port: 8123
    database: db
    username: user
    password: pass

  staging:
    cluster: stage
    host: stage.host
    port: 8123
    database: db
    username: user
    password: pass

  production:
    cluster: production
    host: production.host
    port: 8123
    database: db
    username: user
    password: pass

default 指向环境凭据。此属性值在未传递 -e 时使用

创建新修订版

使用 create 命令创建新修订版

vendor/bin/clickhouse-migrator create RevisionName

RevisionName 是类名,因此它必须使用驼峰命名法。迁移文件将类似于 20200125120301_RevisionName,其中 20200125120301 是 ID,其余部分是类名。

运行命令后,将在迁移路径中出现文件 20200125120301_RevisionName.php

<?php

use Khaydarovm\Clickhouse\Migrator\AbstractMigration;

class RevisionName extends AbstractMigration
{
    public function up()
    {
    }

    public function down()
    {
    }
}

使用 up() 方法进行迁移,使用 down() 进行回滚。

目前尚未实现 rollbackstatus。但很快就会完成。

AbstractMigration 类最初提供三种方法

  • getDatabase() — 从配置文件获取数据库名称
  • getCluster() - 从配置文件获取集群名称
  • execute(string $query) - 执行传入的 SQL 查询的方法

开发

测试