8 Commits

Author SHA1 Message Date
chaim d5fe6c21a0 release: v1.2.4 2026-03-29 20:31:11 +00:00
chaim 54e8c77325 fix: proper type coercion for raw SQL import columns
Read column metadata (type, nullable, default) from SHOW COLUMNS and
coerce values accordingly: empty string/false → 0 for NOT NULL numeric
columns, null for nullable ones. Fixes is_internal import error.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-29 20:31:11 +00:00
chaim 35cda6e91c release: v1.2.3 2026-03-29 20:28:56 +00:00
chaim ba00e7ecf6 fix: convert empty strings to null for integer/bool columns
MySQL strict mode rejects empty string '' for integer columns like
is_internal. Convert '' and false to null before SQL insert.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-29 20:28:56 +00:00
chaim e3a1be1844 release: v1.2.2 2026-03-29 20:25:54 +00:00
chaim fd279b6c8b fix: skip columns missing in target DB during raw SQL import
When importing entities with autoincrement (Case, Charge, Invoice),
validate each column exists in the target table before inserting.
Columns from modules not installed on the target are silently skipped
and reported in the import log summary.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-29 20:25:54 +00:00
chaim 73b5907539 release: v1.2.1 2026-03-29 20:22:05 +00:00
chaim edeef544d4 fix: mysqldump SSL and tablespace errors in snapshot
Add --ssl=false and --no-tablespaces flags to handle Docker MySQL
connections with self-signed certificates.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-29 20:22:05 +00:00
4 changed files with 119 additions and 15 deletions
@@ -39,7 +39,8 @@ class Snapshot implements Action
$dumpFile = $snapshotDir . '/database.sql';
$cmd = sprintf(
'mysqldump --host=%s --port=%s --user=%s --password=%s --single-transaction --quick %s > %s 2>&1',
'mysqldump --host=%s --port=%s --user=%s --password=%s'
. ' --ssl=false --no-tablespaces --single-transaction --quick %s > %s 2>&1',
escapeshellarg($host),
escapeshellarg($port),
escapeshellarg($user),
@@ -309,9 +309,19 @@ class EntityImporter
return $newId;
}
/** @var array<string, string[]> Cache of existing column names per table */
private array $tableColumnsCache = [];
/** @var array<string, array<string, array>> Cache of column metadata per table */
private array $tableColumnMetaCache = [];
/** @var array<string, string[]> Skipped columns log per entity type */
private array $skippedColumnsLog = [];
private function insertWithRawSql(string $entityType, array $record, string $newId): void
{
$tableName = $this->camelCaseToUnderscore($entityType);
$existingColumns = $this->getTableColumns($tableName);
// Convert camelCase fields to snake_case for SQL
$columns = [];
@@ -325,15 +335,25 @@ class EntityImporter
continue;
}
// Skip non-storable array/object fields that come from getValueMap
if (is_array($value) || is_object($value)) {
// JSON fields should be stored as JSON strings
if (is_array($value) || is_object($value)) {
$value = json_encode($value, JSON_UNESCAPED_UNICODE);
$column = $this->camelCaseToUnderscore($field);
// Skip columns that don't exist in the target table
if (!in_array($column, $existingColumns)) {
if (!isset($this->skippedColumnsLog[$entityType][$column])) {
$this->skippedColumnsLog[$entityType][$column] = true;
}
continue;
}
$column = $this->camelCaseToUnderscore($field);
// JSON fields should be stored as JSON strings
if (is_array($value) || is_object($value)) {
$value = json_encode($value, JSON_UNESCAPED_UNICODE);
}
// Coerce value to match target column type
$value = $this->coerceValue($tableName, $column, $value);
$columns[] = "`{$column}`";
$values[] = $value;
$placeholders[] = '?';
@@ -344,14 +364,85 @@ class EntityImporter
$sql = "INSERT INTO `{$tableName}` ({$columnStr}) VALUES ({$placeholderStr})";
try {
$pdo = $this->entityManager->getPDO();
$stmt = $pdo->prepare($sql);
$stmt->execute($values);
} catch (\Exception $e) {
// If column mismatch, try with just known columns
throw $e;
$pdo = $this->entityManager->getPDO();
$stmt = $pdo->prepare($sql);
$stmt->execute($values);
}
private function getTableColumns(string $tableName): array
{
if (isset($this->tableColumnsCache[$tableName])) {
return $this->tableColumnsCache[$tableName];
}
$pdo = $this->entityManager->getPDO();
$sth = $pdo->query("SHOW COLUMNS FROM `{$tableName}`");
$columns = [];
$meta = [];
while ($row = $sth->fetch(\PDO::FETCH_ASSOC)) {
$columns[] = $row['Field'];
$meta[$row['Field']] = [
'type' => $row['Type'],
'nullable' => $row['Null'] === 'YES',
'default' => $row['Default'],
];
}
$this->tableColumnsCache[$tableName] = $columns;
$this->tableColumnMetaCache[$tableName] = $meta;
return $columns;
}
/**
* Coerce a value to be compatible with the target column type.
*/
private function coerceValue(string $tableName, string $column, mixed $value): mixed
{
$meta = $this->tableColumnMetaCache[$tableName][$column] ?? null;
if ($meta === null) {
return $value;
}
$type = strtolower($meta['type']);
$isNumeric = str_contains($type, 'int') || str_contains($type, 'decimal')
|| str_contains($type, 'float') || str_contains($type, 'double')
|| str_contains($type, 'tinyint') || str_contains($type, 'smallint')
|| str_contains($type, 'bigint');
if ($isNumeric && ($value === '' || $value === false)) {
if ($meta['nullable']) {
return null;
}
return 0;
}
if (!$meta['nullable'] && $value === null) {
if ($isNumeric) {
return 0;
}
return $meta['default'] ?? '';
}
return $value;
}
/**
* @return array<string, string[]> entityType => [skipped column names]
*/
public function getSkippedColumns(): array
{
$result = [];
foreach ($this->skippedColumnsLog as $entityType => $columns) {
$result[$entityType] = array_keys($columns);
}
return $result;
}
private function resetAutoIncrement(string $entityType): void
@@ -179,6 +179,18 @@ class ImportService
}
}
}
// Report skipped columns (missing in target DB)
$skippedColumns = $this->entityImporter->getSkippedColumns();
if (!empty($skippedColumns)) {
$io->writeLine('');
$io->writeLine('WARNING: Skipped columns not found in target database:');
foreach ($skippedColumns as $type => $columns) {
$io->writeLine(" {$type}: " . implode(', ', $columns));
}
}
}
private function entityTypeExists(string $entityType): bool
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "DataMigration",
"version": "1.2.0",
"version": "1.2.4",
"acceptableVersions": [
">=8.0.0"
],