PHPackages                             mgcosta/mysql-to-cloud-spanner - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. mgcosta/mysql-to-cloud-spanner

ActiveLibrary

mgcosta/mysql-to-cloud-spanner
==============================

Mysql parser to Google Cloud Spanner

v0.4.4(4y ago)230811MITPHPPHP ^7.3 || ^8.0

Since Aug 3Pushed 4y ago1 watchersCompare

[ Source](https://github.com/mgcostaParedes/php-mysql-to-cloud-spanner)[ Packagist](https://packagist.org/packages/mgcosta/mysql-to-cloud-spanner)[ RSS](/packages/mgcosta-mysql-to-cloud-spanner/feed)WikiDiscussions main Synced 4w ago

READMEChangelog (10)Dependencies (10)Versions (26)Used By (1)

MySQL to Google Cloud Spanner Migration Tool for PHP
----------------------------------------------------

[](#mysql-to-google-cloud-spanner-migration-tool-for-php)

[![License](https://camo.githubusercontent.com/fdaff030d1efa0e9c482e6fe62352498083b9428ca43d39be87386c4da241abf/687474703a2f2f706f7365722e707567782e6f72672f6d67636f7374612f6d7973716c2d746f2d636c6f75642d7370616e6e65722f6c6963656e7365)](https://packagist.org/packages/mgcosta/mysql-to-cloud-spanner)[![Actions Status](https://github.com/mgcostaParedes/php-mysql-to-cloud-spanner/workflows/CI/badge.svg)](https://github.com/mgcostaParedes/php-mysql-to-cloud-spanner/actions)[![codecov](https://camo.githubusercontent.com/c908fa41028546738c9cea951efaca34a57bd913b2abb4d64586695c3250e2f3/68747470733a2f2f636f6465636f762e696f2f67682f6d67636f737461506172656465732f7068702d6d7973716c2d746f2d636c6f75642d7370616e6e65722f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d4c32304e325559395836)](https://codecov.io/gh/mgcostaParedes/php-mysql-to-cloud-spanner)[![Total Downloads](https://camo.githubusercontent.com/378e9136407ab62c54966b90daa5b5ba9748eaf6faa516248c9ccda07ac0458e/687474703a2f2f706f7365722e707567782e6f72672f6d67636f7374612f6d7973716c2d746f2d636c6f75642d7370616e6e65722f646f776e6c6f616473)](https://packagist.org/packages/mgcosta/mysql-to-cloud-spanner)

The MySQL Parser to Google Cloud Spanner is a library for PHP, providing an easy way to migrate the data from MySQL to **Google Cloud Spanner**.

Install
-------

[](#install)

Via Composer

```
$ composer require mgcosta/mysql-to-cloud-spanner
```

Usage Instructions
------------------

[](#usage-instructions)

To use this toolkit, you will need an array of the columns from MySQL and the respective foreign keys / indexes.

The table array you can use the `Describe` from MySQL, the foreign keys you will need to do something like [that](https://dev.mysql.com/doc/refman/8.0/en/information-schema-key-column-usage-table.html).

```
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 {}
```

The library outputs a multidimensional array with following keys '**tables**', '**indexes**', '**constraints**' to insert on the Google Cloud Spanner engine.

**Note**: You may want to store the constraint keys to run at the end of all tables and indexes to prevent running a constraint for a table which is not created.

### Returning DDL statements without semicolons

[](#returning-ddl-statements-without-semicolons)

If for some reason you need each statement without semicolon at the end, you can use the method `shouldAssignPrimaryKey`:

```
$schemaParser = (new Parser())->shouldAssignSemicolon(false);

$ddl = $schemaParser->setTableName($tableName)
                  ->setDescribedTable($table)
                  ->setKeys($keys)
                  ->toDDL();
```

### Dealing with schemas without Primary Keys

[](#dealing-with-schemas-without-primary-keys)

Since the primary key on cloud spanner is like almost [required](https://cloud.google.com/spanner/docs/schema-and-data-model#choosing_a_primary_key), by default if there's a table schema without **PK** it will generate a default column called by **id** which will be an **int64** type. However you can modify the way this default column is created or disable it at all, for that check the following example:

```
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) {

}
```

### Using the Dialect Service for MySQL

[](#using-the-dialect-service-for-mysql)

To help your life to fetch the data we need from MySQL to create the spanner statements, you can use the available service on your **PDO** or **ORM / Query Builder**:

This will simply generate a valid query string which you can use to fetch the columns &amp; keys details.

The example below is **[Laravel](https://laravel.com/docs/8.x/queries)** based, but you can adapt it easily.

```
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();
```

### Prepare the data for migration

[](#prepare-the-data-for-migration)

Sometimes you may have unexpected typed values to insert to Cloud Spanner, when you fetch the data from **PHP PDOs** system's, for that you can use the available transformer, which will prepare the data mapped with the described table.

For that follow the example below:

```
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();
```

Roadmap
-------

[](#roadmap)

You can get more details of the plans for this early version on the following [link](https://github.com/mgcostaParedes/php-mysql-to-cloud-spanner/projects/1).

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

- \[Miguel Costa\]\[\]
- \[Mike Slowik\]\[\]

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 98.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~4 days

Total

25

Last Release

1628d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ba33beb898ddbda3f74be8a9db2dcb9629dfab34f5e94c710385ad0dfd9bd982?d=identicon)[mgcostaParedes](/maintainers/mgcostaParedes)

---

Top Contributors

[![mgcostaParedes](https://avatars.githubusercontent.com/u/16031444?v=4)](https://github.com/mgcostaParedes "mgcostaParedes (60 commits)")[![sl0wik](https://avatars.githubusercontent.com/u/2696038?v=4)](https://github.com/sl0wik "sl0wik (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/mgcosta-mysql-to-cloud-spanner/health.svg)

```
[![Health](https://phpackages.com/badges/mgcosta-mysql-to-cloud-spanner/health.svg)](https://phpackages.com/packages/mgcosta-mysql-to-cloud-spanner)
```

###  Alternatives

[colopl/laravel-spanner

Laravel database driver for Google Cloud Spanner

101124.2k1](/packages/colopl-laravel-spanner)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
