mgcosta / mysql-to-cloud-spanner
MySQL 解析器到 Google Cloud Spanner
Requires
- php: ^7.3 || ^8.0
- ext-grpc: *
- ext-json: *
- google/cloud-spanner: ^1.44.0
Requires (Dev)
- ext-xdebug: *
- codeception/c3: ^2.4
- codeception/codeception: ^4.1
- codeception/module-asserts: ^1.2
- codeception/module-phpbrowser: ^1.0.0
- mockery/mockery: ^1.4
- phpcompatibility/php-compatibility: ^9.3
- phpunit/phpunit: ^9
- sebastian/phpcpd: ^5.0
- squizlabs/php_codesniffer: ^3.5
README
MySQL 解析器到 Google Cloud Spanner 是一个 PHP 库,提供了一个简单的方法将数据从 MySQL 迁移到 Google Cloud Spanner。
安装
通过 Composer
$ composer require mgcosta/mysql-to-cloud-spanner
使用说明
要使用此工具包,您需要一个包含 MySQL 列和相应外键/索引的数组。
您可以使用 MySQL 的 Describe
命令获取表数组,外键您需要做类似 这样 的操作。
use MgCosta\MysqlParser\Parser; $schemaParser = new Parser(); $tableName = 'users'; $table = [ [ 'Field' => 'id', 'Type' => 'biginteger unsigned', 'Null' => 'NO', 'Key' => 'PRI', 'Default' => null, 'Extra' => 'auto_increment' ], [ 'Field' => 'name', 'Type' => 'varchar(255)', 'Null' => 'NO', 'Key' => '', 'Default' => null, 'Extra' => '' ], [ 'Field' => 'email', 'Type' => 'varchar(255)', 'Null' => 'NO', 'Key' => 'UNI', 'Default' => null, 'Extra' => '' ], [ 'Field' => 'password', 'Type' => 'varchar(255)', 'Null' => 'NO', 'Key' => '', 'Default' => null, 'Extra' => '' ] ]; $keys = [ [ 'TABLE_NAME' => 'users', 'COLUMN_NAME' => 'id', 'CONSTRAINT_NAME' => 'PRIMARY', 'REFERENCED_TABLE_NAME' => null, 'REFERENCED_COLUMN_NAME' => null ], [ 'TABLE_NAME' => 'users', 'COLUMN_NAME' => 'email', 'CONSTRAINT_NAME' => 'users_email_unique', 'REFERENCED_TABLE_NAME' => null, 'REFERENCED_COLUMN_NAME' => null ] ]; $ddl = $schemaParser->setTableName($tableName) ->setDescribedTable($table) ->setKeys($keys) ->toDDL(); // it will output an array of DDL statements required to create // the necessary elements to compose the table // ------------------------------------------- // array(3) { // ['tables'] => array { // [0] => string(145) "CREATE TABLE users ( // id INT64 NOT NULL, // name STRING(255) NOT NULL, // email STRING(255) NOT NULL, // password STRING(255) NOT NULL // ) PRIMARY KEY (id)" // } // ['indexes'] => array { // [0] => string(55) "CREATE UNIQUE INDEX users_email_unique ON users (email)" // } // ['constraints'] => array {}
该库输出一个包含以下键的多维数组:'tables', 'indexes', 'constraints' 以插入到 Google Cloud Spanner 引擎中。
注意:您可能想要存储约束键,以便在所有表和索引之后运行,以防止为尚未创建的表运行约束。
返回不带分号的 DDL 语句
如果您需要每个语句末尾不带分号,可以使用 shouldAssignPrimaryKey
方法。
$schemaParser = (new Parser())->shouldAssignSemicolon(false); $ddl = $schemaParser->setTableName($tableName) ->setDescribedTable($table) ->setKeys($keys) ->toDDL();
处理没有主键的模式
由于 Cloud Spanner 上的主键几乎是 必需的,默认情况下,如果没有主键的表模式,将生成一个默认列 id,它将是一个 int64 类型。但是,您可以修改创建此默认列的方式或完全禁用它,以下是一个示例
use MgCosta\MysqlParser\Parser; use MgCosta\MysqlParser\Dialect; use MgCosta\MysqlParser\Exceptions\PrimaryKeyNotFoundException; $schemaParser = new Parser(); $tableName = 'users'; $table = [ [ 'Field' => 'name', 'Type' => 'varchar(255)', 'Null' => 'NO', 'Key' => '', 'Default' => null, 'Extra' => '' ], [ 'Field' => 'email', 'Type' => 'varchar(255)', 'Null' => 'NO', 'Key' => '', 'Default' => null, 'Extra' => '' ] ]; // define the default column id for a specific table $ddl = $schemaParser->setDefaultID('column_id') ->setTableName($tableName) ->setDescribedTable($table) ->setKeys($keys) ->toDDL(); // disable the generation of default id // it can lead on an exception try { $schemaParser = (new Parser())->shouldAssignPrimaryKey(false); $ddl = $schemaParser->setTableName($tableName) ->setDescribedTable($table) ->setKeys($keys) ->toDDL(); } catch(PrimaryKeyNotFoundException $e) { }
使用 MySQL 的方言服务
为了帮助您从 MySQL 获取创建 spanner 语句所需的数据,您可以使用您 PDO 或 ORM / 查询构建器 上的可用服务。
这将简单地生成一个有效的查询字符串,您可以使用它来获取列和键的详细信息。
以下示例是基于 Laravel 的,但您可以轻松地修改它。
use Illuminate\Support\Facades\DB; use MgCosta\MysqlParser\Parser; use MgCosta\MysqlParser\Dialect; $schemaParser = new Parser(); $mysqlDialect = new Dialect(); $databaseName = 'my_database'; $tableName = 'users'; // you can extract the table details doing the following $table = DB::select( DB::raw($mysqlDialect->generateTableDetails($tableName)) ); $keys = DB::select( DB::raw($mysqlDialect->generateTableKeysDetails($databaseName, $tableName)) ); $ddl = $schemaParser->setTableName($tableName) ->setDescribedTable($table) ->setKeys($keys) ->toDDL();
准备迁移数据
有时,当您从 PHP PDOs 系统获取数据时,您可能需要将意外的类型值插入到 Cloud Spanner 中,为此您可以使用可用的转换器,该转换器将准备与描述的表映射的数据。
以下是一个示例
use MgCosta\MysqlParser\Transformer\SpannerTransformer; $table = [ [ 'Field' => 'name', 'Type' => 'varchar(255)', 'Null' => 'NO', 'Key' => '', 'Default' => null, 'Extra' => '' ], [ 'Field' => 'value', 'Type' => 'decimal(8,2)', 'Null' => 'NO', 'Key' => '', 'Default' => null, 'Extra' => '' ] ]; $rows = [ [ 'name' => 'product', 'value' => '50.20' ] ]; $transformer = (new SpannerTransformer())->setDescribedTable($table); $results = $transformer->setRows($rows)->transform();
路线图
您可以在以下 链接 上获取此早期版本计划的更多详细信息。
贡献
有关详细信息,请参阅 CONTRIBUTING。
鸣谢
- [Miguel Costa][https://github.com/mgcostaParedes]
- [Mike Slowik][https://github.com/sl0wik]
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件。