Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 49275d0fc9 | |||
| ec759b89c1 | |||
| d5fe6c21a0 | |||
| 54e8c77325 |
@@ -116,6 +116,19 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{{! === Snapshots === }}
|
||||||
|
<div class="panel panel-info">
|
||||||
|
<div class="panel-heading">
|
||||||
|
<h4 class="panel-title">
|
||||||
|
<i class="fas fa-camera"></i>
|
||||||
|
{{translate 'Database Snapshots' scope='DataMigration'}}
|
||||||
|
</h4>
|
||||||
|
</div>
|
||||||
|
<div class="panel-body" style="padding: 0;">
|
||||||
|
<div class="snapshots-list list-group" style="margin-bottom: 0;"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
{{! === Import === }}
|
{{! === Import === }}
|
||||||
<div class="import-section" style="display: none;">
|
<div class="import-section" style="display: none;">
|
||||||
<div class="panel panel-warning">
|
<div class="panel panel-warning">
|
||||||
|
|||||||
@@ -72,6 +72,11 @@ define('modules/data-migration/views/admin/data-migration', ['view'], function (
|
|||||||
const name = $(e.currentTarget).data('name');
|
const name = $(e.currentTarget).data('name');
|
||||||
this.$el.find('.backup-details[data-name="' + name + '"]').toggle();
|
this.$el.find('.backup-details[data-name="' + name + '"]').toggle();
|
||||||
},
|
},
|
||||||
|
'click [data-action="deleteSnapshot"]': function (e) {
|
||||||
|
e.stopPropagation();
|
||||||
|
const name = $(e.currentTarget).data('name');
|
||||||
|
this.actionDeleteSnapshot(name);
|
||||||
|
},
|
||||||
'click [data-action="dismissExportLog"]': function () {
|
'click [data-action="dismissExportLog"]': function () {
|
||||||
this.exportLog = null;
|
this.exportLog = null;
|
||||||
this.$el.find('.export-log-container').hide();
|
this.$el.find('.export-log-container').hide();
|
||||||
@@ -144,8 +149,10 @@ define('modules/data-migration/views/admin/data-migration', ['view'], function (
|
|||||||
Espo.Ajax.getRequest('DataMigration/action/listBackups')
|
Espo.Ajax.getRequest('DataMigration/action/listBackups')
|
||||||
.then(response => {
|
.then(response => {
|
||||||
this.backups = response.backups || [];
|
this.backups = response.backups || [];
|
||||||
|
this.snapshots = response.snapshots || [];
|
||||||
this.isLoadingBackups = false;
|
this.isLoadingBackups = false;
|
||||||
this.renderBackups();
|
this.renderBackups();
|
||||||
|
this.renderSnapshots();
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
this.isLoadingBackups = false;
|
this.isLoadingBackups = false;
|
||||||
@@ -406,6 +413,60 @@ define('modules/data-migration/views/admin/data-migration', ['view'], function (
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
renderSnapshots: function () {
|
||||||
|
const $container = this.$el.find('.snapshots-list');
|
||||||
|
|
||||||
|
if (!this.snapshots || this.snapshots.length === 0) {
|
||||||
|
$container.html(
|
||||||
|
'<div class="text-muted" style="padding: 15px; text-align: center;">' +
|
||||||
|
this.translate('No snapshots', 'labels', 'DataMigration') +
|
||||||
|
'</div>'
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let html = '';
|
||||||
|
|
||||||
|
this.snapshots.forEach(snap => {
|
||||||
|
const date = snap.createdAt ? new Date(snap.createdAt).toLocaleString() : 'Unknown';
|
||||||
|
|
||||||
|
html += '<div class="list-group-item">';
|
||||||
|
html += '<div class="pull-right">';
|
||||||
|
html += '<button class="btn btn-danger btn-xs" data-action="deleteSnapshot" data-name="' + snap.name + '" title="' + this.translate('Delete') + '">';
|
||||||
|
html += '<i class="fas fa-trash"></i></button>';
|
||||||
|
html += '</div>';
|
||||||
|
html += '<h5 class="list-group-item-heading" style="margin: 0 0 4px;">';
|
||||||
|
html += '<i class="fas fa-camera text-info"></i> ' + snap.name;
|
||||||
|
html += '</h5>';
|
||||||
|
html += '<p class="list-group-item-text text-muted" style="margin: 0; font-size: 0.9em;">';
|
||||||
|
html += '<i class="fas fa-clock"></i> ' + date;
|
||||||
|
html += ' | <i class="fas fa-database"></i> ' + snap.database;
|
||||||
|
html += ' | <i class="fas fa-hdd"></i> ' + snap.sizeFormatted;
|
||||||
|
html += '</p>';
|
||||||
|
html += '</div>';
|
||||||
|
});
|
||||||
|
|
||||||
|
$container.html(html);
|
||||||
|
},
|
||||||
|
|
||||||
|
actionDeleteSnapshot: function (name) {
|
||||||
|
this.confirm(
|
||||||
|
this.translate('deleteSnapshotConfirmation', 'messages', 'DataMigration'),
|
||||||
|
() => {
|
||||||
|
Espo.Ajax.postRequest('DataMigration/action/deleteBackup', {
|
||||||
|
backupName: name,
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
Espo.Ui.success(this.translate('Deleted'));
|
||||||
|
this.loadBackups();
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
Espo.Ui.error(this.translate('Failed to delete snapshot', 'labels', 'DataMigration'));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
renderLog: function (selector, lines, isSuccess) {
|
renderLog: function (selector, lines, isSuccess) {
|
||||||
const $container = this.$el.find(selector);
|
const $container = this.$el.find(selector);
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ class DeleteBackup implements Action
|
|||||||
// Sanitize to prevent directory traversal
|
// Sanitize to prevent directory traversal
|
||||||
$safeName = basename($backupName);
|
$safeName = basename($backupName);
|
||||||
|
|
||||||
if (!str_starts_with($safeName, 'datamigration-')) {
|
if (!str_starts_with($safeName, 'datamigration-') && !str_starts_with($safeName, 'snapshot-')) {
|
||||||
throw new Forbidden('Invalid backup name.');
|
throw new Forbidden('Invalid backup name.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -73,7 +73,43 @@ class ListBackups implements Action
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
return ResponseComposer::json(['backups' => $backups]);
|
// Scan for snapshots
|
||||||
|
$snapshots = [];
|
||||||
|
|
||||||
|
foreach ($dirs as $dir) {
|
||||||
|
if ($dir === '.' || $dir === '..') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!str_starts_with($dir, 'snapshot-')) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$fullPath = $backupsDir . '/' . $dir;
|
||||||
|
|
||||||
|
if (!is_dir($fullPath)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$manifest = $this->manifestBuilder->read($fullPath);
|
||||||
|
$dumpFile = $fullPath . '/database.sql';
|
||||||
|
$dumpSize = file_exists($dumpFile) ? filesize($dumpFile) : 0;
|
||||||
|
|
||||||
|
$snapshots[] = [
|
||||||
|
'name' => $dir,
|
||||||
|
'createdAt' => $manifest['createdAt'] ?? null,
|
||||||
|
'sourceHost' => $manifest['sourceHost'] ?? 'unknown',
|
||||||
|
'espoVersion' => $manifest['espoVersion'] ?? 'unknown',
|
||||||
|
'database' => $manifest['database'] ?? 'unknown',
|
||||||
|
'dumpSizeBytes' => $dumpSize,
|
||||||
|
'sizeFormatted' => $this->formatBytes($dumpSize),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResponseComposer::json([
|
||||||
|
'backups' => $backups,
|
||||||
|
'snapshots' => $snapshots,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function formatBytes(int $bytes): string
|
private function formatBytes(int $bytes): string
|
||||||
|
|||||||
@@ -16,6 +16,9 @@
|
|||||||
"Import failed": "Import failed",
|
"Import failed": "Import failed",
|
||||||
"Select a backup first": "Please select a backup first",
|
"Select a backup first": "Please select a backup first",
|
||||||
"Failed to delete backup": "Failed to delete backup",
|
"Failed to delete backup": "Failed to delete backup",
|
||||||
|
"Failed to delete snapshot": "Failed to delete snapshot",
|
||||||
|
"Database Snapshots": "Database Snapshots",
|
||||||
|
"No snapshots": "No snapshots found",
|
||||||
"Selected backup": "Selected backup",
|
"Selected backup": "Selected backup",
|
||||||
"Import Options": "Import Options",
|
"Import Options": "Import Options",
|
||||||
"Preserve original IDs": "Preserve original IDs",
|
"Preserve original IDs": "Preserve original IDs",
|
||||||
@@ -44,6 +47,7 @@
|
|||||||
"importConfirmation": "Are you sure you want to import this backup? This will add data to the database. This action cannot be easily undone.",
|
"importConfirmation": "Are you sure you want to import this backup? This will add data to the database. This action cannot be easily undone.",
|
||||||
"dryRunConfirmation": "Run a dry import? This will simulate the import without making any changes to the database.",
|
"dryRunConfirmation": "Run a dry import? This will simulate the import without making any changes to the database.",
|
||||||
"deleteBackupConfirmation": "Are you sure you want to delete this backup? This action cannot be undone.",
|
"deleteBackupConfirmation": "Are you sure you want to delete this backup? This action cannot be undone.",
|
||||||
|
"deleteSnapshotConfirmation": "Are you sure you want to delete this database snapshot? This action cannot be undone.",
|
||||||
"noFilesHint": "faster export, without attachment files",
|
"noFilesHint": "faster export, without attachment files",
|
||||||
"preserveIdsHint": "keep original record IDs (recommended for same-instance restore)",
|
"preserveIdsHint": "keep original record IDs (recommended for same-instance restore)",
|
||||||
"importNoFilesHint": "import only database records, skip attachment files",
|
"importNoFilesHint": "import only database records, skip attachment files",
|
||||||
|
|||||||
@@ -16,6 +16,9 @@
|
|||||||
"Import failed": "הייבוא נכשל",
|
"Import failed": "הייבוא נכשל",
|
||||||
"Select a backup first": "נא לבחור גיבוי קודם",
|
"Select a backup first": "נא לבחור גיבוי קודם",
|
||||||
"Failed to delete backup": "מחיקת הגיבוי נכשלה",
|
"Failed to delete backup": "מחיקת הגיבוי נכשלה",
|
||||||
|
"Failed to delete snapshot": "מחיקת צילום המצב נכשלה",
|
||||||
|
"Database Snapshots": "צילומי מצב של מסד הנתונים",
|
||||||
|
"No snapshots": "לא נמצאו צילומי מצב",
|
||||||
"Selected backup": "גיבוי נבחר",
|
"Selected backup": "גיבוי נבחר",
|
||||||
"Import Options": "אפשרויות ייבוא",
|
"Import Options": "אפשרויות ייבוא",
|
||||||
"Preserve original IDs": "שמור מזהים מקוריים",
|
"Preserve original IDs": "שמור מזהים מקוריים",
|
||||||
@@ -44,6 +47,7 @@
|
|||||||
"importConfirmation": "האם אתה בטוח שברצונך לייבא גיבוי זה? פעולה זו תוסיף נתונים למסד הנתונים ולא ניתן לבטלה בקלות.",
|
"importConfirmation": "האם אתה בטוח שברצונך לייבא גיבוי זה? פעולה זו תוסיף נתונים למסד הנתונים ולא ניתן לבטלה בקלות.",
|
||||||
"dryRunConfirmation": "להריץ ייבוא ניסיון? פעולה זו תדמה את הייבוא מבלי לבצע שינויים במסד הנתונים.",
|
"dryRunConfirmation": "להריץ ייבוא ניסיון? פעולה זו תדמה את הייבוא מבלי לבצע שינויים במסד הנתונים.",
|
||||||
"deleteBackupConfirmation": "האם אתה בטוח שברצונך למחוק גיבוי זה? לא ניתן לבטל פעולה זו.",
|
"deleteBackupConfirmation": "האם אתה בטוח שברצונך למחוק גיבוי זה? לא ניתן לבטל פעולה זו.",
|
||||||
|
"deleteSnapshotConfirmation": "האם אתה בטוח שברצונך למחוק צילום מצב זה? לא ניתן לבטל פעולה זו.",
|
||||||
"noFilesHint": "ייצוא מהיר יותר, ללא קבצים מצורפים",
|
"noFilesHint": "ייצוא מהיר יותר, ללא קבצים מצורפים",
|
||||||
"preserveIdsHint": "שמור מזהי רשומות מקוריים (מומלץ לשחזור באותה מערכת)",
|
"preserveIdsHint": "שמור מזהי רשומות מקוריים (מומלץ לשחזור באותה מערכת)",
|
||||||
"importNoFilesHint": "ייבוא רשומות בלבד, ללא קבצים מצורפים",
|
"importNoFilesHint": "ייבוא רשומות בלבד, ללא קבצים מצורפים",
|
||||||
|
|||||||
@@ -309,9 +309,12 @@ class EntityImporter
|
|||||||
return $newId;
|
return $newId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var array<string, string[]> Cache of existing columns per table */
|
/** @var array<string, string[]> Cache of existing column names per table */
|
||||||
private array $tableColumnsCache = [];
|
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 */
|
/** @var array<string, string[]> Skipped columns log per entity type */
|
||||||
private array $skippedColumnsLog = [];
|
private array $skippedColumnsLog = [];
|
||||||
|
|
||||||
@@ -348,10 +351,8 @@ class EntityImporter
|
|||||||
$value = json_encode($value, JSON_UNESCAPED_UNICODE);
|
$value = json_encode($value, JSON_UNESCAPED_UNICODE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fix empty strings for integer/bool columns (MySQL strict mode)
|
// Coerce value to match target column type
|
||||||
if ($value === '' || $value === false) {
|
$value = $this->coerceValue($tableName, $column, $value);
|
||||||
$value = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
$columns[] = "`{$column}`";
|
$columns[] = "`{$column}`";
|
||||||
$values[] = $value;
|
$values[] = $value;
|
||||||
@@ -377,16 +378,59 @@ class EntityImporter
|
|||||||
$pdo = $this->entityManager->getPDO();
|
$pdo = $this->entityManager->getPDO();
|
||||||
$sth = $pdo->query("SHOW COLUMNS FROM `{$tableName}`");
|
$sth = $pdo->query("SHOW COLUMNS FROM `{$tableName}`");
|
||||||
$columns = [];
|
$columns = [];
|
||||||
|
$meta = [];
|
||||||
|
|
||||||
while ($row = $sth->fetch(\PDO::FETCH_ASSOC)) {
|
while ($row = $sth->fetch(\PDO::FETCH_ASSOC)) {
|
||||||
$columns[] = $row['Field'];
|
$columns[] = $row['Field'];
|
||||||
|
$meta[$row['Field']] = [
|
||||||
|
'type' => $row['Type'],
|
||||||
|
'nullable' => $row['Null'] === 'YES',
|
||||||
|
'default' => $row['Default'],
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->tableColumnsCache[$tableName] = $columns;
|
$this->tableColumnsCache[$tableName] = $columns;
|
||||||
|
$this->tableColumnMetaCache[$tableName] = $meta;
|
||||||
|
|
||||||
return $columns;
|
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]
|
* @return array<string, string[]> entityType => [skipped column names]
|
||||||
*/
|
*/
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "DataMigration",
|
"name": "DataMigration",
|
||||||
"version": "1.2.3",
|
"version": "1.3.0",
|
||||||
"acceptableVersions": [
|
"acceptableVersions": [
|
||||||
">=8.0.0"
|
">=8.0.0"
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user