Skip to content

Commit

Permalink
Cast to integer to avoid Spanner GCP incompatibility (#1060)
Browse files Browse the repository at this point in the history
* Cast to integer to avoid Spanner GCP incompatibility

I am currently receiving error from GCP when trying to persist data using Spanner:

```
An exception occurred while executing 'INSERT INTO doctrine_migration_versions (version, executed_at, execution_time) VALUES (?, ?, ?)' with params ["oat\\generis\\migrations\\Version202009301828472348_generis", "2020-10-02T11:54:13.671062Z", 130]:

  {
      "message": "Invalid value for bind parameter param3: Expected INT64.",
      "code": 3,
      "status": "INVALID_ARGUMENT",
      "details": [
          {
              "@type": "grpc-server-stats-bin",
              "data": "<Unknown Binary Data>"
          }
      ]
  }
```
Cause the 130 is actually `130.0` (a float).

Since migrations always expects an integer here I would like to cast this value to avoid driver incompatibilities
  • Loading branch information
gabrielfs7 authored Oct 19, 2020
1 parent a86953f commit 5a7295f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function complete(ExecutionResult $result) : void
$this->connection->insert($this->configuration->getTableName(), [
$this->configuration->getVersionColumnName() => (string) $result->getVersion(),
$this->configuration->getExecutedAtColumnName() => $result->getExecutedAt(),
$this->configuration->getExecutionTimeColumnName() => $result->getTime() === null ? null : round($result->getTime()*1000),
$this->configuration->getExecutionTimeColumnName() => $result->getTime() === null ? null : (int) round($result->getTime()*1000),
], [
Types::STRING,
Types::DATETIME_MUTABLE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
use DateTime;
use DateTimeImmutable;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\PDOSqlite\Driver as SQLiteDriver;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\DateTimeType;
use Doctrine\DBAL\Types\IntegerType;
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Types\Types;
use Doctrine\Migrations\Exception\MetadataStorageError;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorage;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
Expand Down Expand Up @@ -186,6 +188,47 @@ public function testCompleteWithFloatTime() : void
], $rows);
}

public function testCompleteWillAlwaysCastTimeToInteger() : void
{
$config = new TableMetadataStorageConfiguration();
$executedAt = new DateTimeImmutable('2010-01-05 10:30:21');

$connection = $this->getMockBuilder(Connection::class)
->setConstructorArgs([
['pdo' => $this->getSqliteConnection()->getWrappedConnection()],
new SQLiteDriver(),
])
->onlyMethods(['insert'])
->getMock();

$connection
->expects(self::once())
->method('insert')
->willReturnCallback(static function ($table, $params, $types) use ($config, $executedAt) : int {
self::assertSame($config->getTableName(), $table);
self::assertSame([
$config->getVersionColumnName() => '1230',
$config->getExecutedAtColumnName() => $executedAt,
$config->getExecutionTimeColumnName() => 31000,
], $params);
self::assertSame([
Types::STRING,
Types::DATETIME_MUTABLE,
Types::INTEGER,
], $types);

return 1;
});

$storage = new TableMetadataStorage($connection, new AlphabeticalComparator(), $config);
$storage->ensureInitialized();

$result = new ExecutionResult(new Version('1230'), Direction::UP, $executedAt);
$result->setTime(31.0);

$storage->complete($result);
}

public function testRead() : void
{
$this->storage->ensureInitialized();
Expand Down

0 comments on commit 5a7295f

Please sign in to comment.