Skip to content

Commit

Permalink
Merge pull request #38 from utopia-php/feat-fix-postgres-attribute-mi…
Browse files Browse the repository at this point in the history
…gration

Drop postgreSQL column default if functional values
  • Loading branch information
abnegate authored Jul 22, 2024
2 parents f0e7ff0 + 006e373 commit f18d44d
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 125 deletions.
21 changes: 20 additions & 1 deletion bin/MigrationCLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function drawFrame()

$statusCounters = $this->transfer->getStatusCounters();

$mask = "| %15.15s | %-7.7s | %10.10s | %7.7s | %7.7s | %8.8s |\n";
$mask = "| %15.15s | %-7.7s | %10.10s | %7.7s | %7.7s | %8.8s | %8.8s |\n";
printf($mask, 'Resource', 'Pending', 'Processing', 'Skipped', 'Warning', 'Error', 'Success');
printf($mask, '-------------', '-------------', '-------------', '-------------', '-------------', '-------------', '-------------');
foreach ($statusCounters as $resource => $data) {
Expand All @@ -58,6 +58,25 @@ public function drawFrame()
echo $error->getResourceGroup().'['.$error->getResourceId().'] - '.$error->getMessage()."\n";
}
}

// Render Warnings
$sourceWarnings = $this->source->getWarnings();
if (! empty($sourceWarnings)) {
echo "\n\nSource Warnings:\n";
foreach ($sourceWarnings as $warning) {
/** @var Utopia\Migration\Warning $warning */
echo $warning->getResourceName().'['.$warning->getResourceId().'] - '.$warning->getMessage()."\n";
}
}

$destWarnings = $this->destination->getWarnings();
if (! empty($destWarnings)) {
echo "\n\nDestination Warnings:\n";
foreach ($destWarnings as $warning) {
/** @var Utopia\Migration\Warning $warning */
echo $warning->getResourceName().'['.$warning->getResourceId().'] - '.$warning->getMessage()."\n";
}
}
}

public function getSource(): Source
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"require-dev": {
"phpunit/phpunit": "9.*",
"vlucas/phpdotenv": "5.*",
"laravel/pint": "1.*"
"laravel/pint": "1.*",
"utopia-php/cli": "^0.18.0"
}
}
102 changes: 0 additions & 102 deletions playground.php

This file was deleted.

52 changes: 52 additions & 0 deletions src/Migration/Sources/NHost.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Utopia\Migration\Resources\Storage\File;
use Utopia\Migration\Source;
use Utopia\Migration\Transfer;
use Utopia\Migration\Warning;

class NHost extends Source
{
Expand Down Expand Up @@ -511,20 +512,71 @@ private function convertAttribute(array $column, Collection $collection): Attrib
return new Boolean($column['column_name'], $collection, $column['is_nullable'] === 'NO', $isArray, $column['column_default']);
case 'smallint':
case 'int2':
if (! is_numeric($column['column_default']) && ! is_null($column['column_default'])) {
$this->addWarning(new Warning(
Resource::TYPE_COLLECTION,
Transfer::GROUP_DATABASES,
'Functional default values are not supported. Default value for attribute '.$column['column_name'].' will be set to null.',
$collection->getId()
));

$collection->setStatus(Resource::STATUS_WARNING);

$column['column_default'] = null;
}

return new Integer($column['column_name'], $collection, $column['is_nullable'] === 'NO', $isArray, $column['column_default'], -32768, 32767);
case 'integer':
case 'int4':
if (! is_numeric($column['column_default']) && ! is_null($column['column_default'])) {
$this->addWarning(new Warning(
Resource::TYPE_COLLECTION,
Transfer::GROUP_DATABASES,
'Functional default values are not supported. Default value for attribute '.$column['column_name'].' will be set to null.',
$collection->getId()
));

$collection->setStatus(Resource::STATUS_WARNING);

$column['column_default'] = null;
}

return new Integer($column['column_name'], $collection, $column['is_nullable'] === 'NO', $isArray, $column['column_default'], -2147483648, 2147483647);
case 'bigint':
case 'int8':
case 'numeric':
if (! is_numeric($column['column_default']) && ! is_null($column['column_default'])) {
$this->addWarning(new Warning(
Resource::TYPE_COLLECTION,
Transfer::GROUP_DATABASES,
'Functional default values are not supported. Default value for attribute '.$column['column_name'].' will be set to null.',
$collection->getId()
));
$collection->setStatus(Resource::STATUS_WARNING);

$column['column_default'] = null;
}

return new Integer($column['column_name'], $collection, $column['is_nullable'] === 'NO', $isArray, $column['column_default']);
case 'decimal':
case 'real':
case 'double precision':
case 'float4':
case 'float8':
case 'money':
if (! is_numeric($column['column_default']) && ! is_null($column['column_default'])) {
$this->addWarning(new Warning(
Resource::TYPE_COLLECTION,
Transfer::GROUP_DATABASES,
'Functional default values are not supported. Default value for attribute '.$column['column_name'].' will be set to null.',
$collection->getId()
));

$collection->setStatus(Resource::STATUS_WARNING);

$column['column_default'] = null;
}

return new Decimal($column['column_name'], $collection, $column['is_nullable'] === 'NO', $isArray, $column['column_default']);
// Time (Conversion happens with documents)
case 'timestamp with time zone':
Expand Down
37 changes: 30 additions & 7 deletions src/Migration/Target.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,20 @@ abstract class Target

public $cache;

/**
* Errors
*
* @var array<Exception>
*/
public $errors = [];

/**
* Warnings
*
* @var array<Warning>
*/
public $warnings = [];

protected $endpoint = '';

abstract public static function getName(): string;
Expand Down Expand Up @@ -160,25 +172,36 @@ protected function flatten(array $data, string $prefix = ''): array
/**
* Get Errors
*
* @returns Error[]
* @returns array<Exception>
*/
public function getErrors(): array
{
return $this->errors;
}

/**
* Set Errors
* Add Error
*/
public function addError(Exception $error): void
{
$this->errors[] = $error;
}

/**
* Get Warnings
*
* @param Error[] $errors
* @returns array<Warning>
*/
public function setErrors(array $errors): void
public function getWarnings(): array
{
$this->errors = $errors;
return $this->warnings;
}

public function addError(Exception $error): void
/**
* Add Warning
*/
public function addWarning(Warning $warning): void
{
$this->errors[] = $error;
$this->warnings[] = $warning;
}
}
11 changes: 3 additions & 8 deletions src/Migration/Transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ public function __construct(Source $source, Destination $destination)
*/
protected Cache $cache;

protected array $options = [];

protected array $callbacks = [];

protected array $events = [];

protected array $resources = [];

public function getStatusCounters()
Expand Down Expand Up @@ -115,8 +109,9 @@ public function getStatusCounters()

// Process Source Errprs
foreach ($this->source->getErrors() as $error) {
if (isset($status[$error->getResourceType()])) {
$status[$error->getResourceType()][Resource::STATUS_ERROR]++;
/** @var Exception $error */
if (isset($status[$error->getResourceGroup()])) {
$status[$error->getResourceGroup()][Resource::STATUS_ERROR]++;
}
}

Expand Down
42 changes: 42 additions & 0 deletions src/Migration/Warning.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Utopia\Migration;

class Warning
{
public string $resourceName;

public string $resourceGroup;

public string $resourceId;

public string $message;

public function __construct(string $resourceName, string $resourceGroup, string $message, string $resourceId = '')
{
$this->resourceName = $resourceName;
$this->resourceId = $resourceId;
$this->resourceGroup = $resourceGroup;
$this->message = $message;
}

public function getResourceName(): string
{
return $this->resourceName;
}

public function getResourceGroup(): string
{
return $this->resourceGroup;
}

public function getResourceId(): string
{
return $this->resourceId;
}

public function getMessage(): string
{
return $this->message;
}
}
Loading

0 comments on commit f18d44d

Please sign in to comment.