This repository has been archived by the owner on Dec 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration.php
921 lines (802 loc) · 34.3 KB
/
migration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
#!/usr/bin/php
<?php
/**
* PostgreSQL to MariaDB Migration Tool
*
* A robust PHP-based tool for migrating databases from PostgreSQL to MariaDB
* while maintaining data integrity, relationships, and structure.
*
* Features:
* - Automated schema and data migration
* - Foreign key and index preservation
* - Progress tracking and detailed logging
* - Batch processing for optimal performance
*
* @author PixoVoid <contact@pixovoid.net>
* @copyright 2024 PixoVoid
* @license MIT License
* @link https://pixovoid.net
* @link https://github.com/PixoVoid-net/pgsql-mariadb-migrate
*
* @requires PHP >= 8.3
* @requires ext-pdo
* @requires ext-pdo_pgsql
* @requires ext-pdo_mysql
*/
declare(strict_types=1);
// Improved error handling and logging for better user feedback
ini_set('display_errors', '0'); // Disable display errors on screen
ini_set('log_errors', '1'); // Enable error logging
ini_set('error_log', __DIR__ . '/migration_error.log'); // Log errors to the migration log file
// Enhanced script execution time management
set_time_limit(0); // Limit execution time to unlimited to prevent server overload
define('LOG_FILE', __DIR__ . '/migration' . date('Y-m-d-H-i') . '.log');
// Group all database connection functions
function createPDOConnection(string $host, string $port, string $dbname, string $user, string $password, string $engine = 'pgsql'): PDO
{
$dsn = "$engine:host=$host;port=$port;dbname=$dbname";
try {
$pdo = new PDO($dsn, $user, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
logMessage("Successfully connected to the database: $dbname", 'INFO');
return $pdo;
} catch (PDOException $e) {
logMessage("Connection failed: " . $e->getMessage(), 'ERROR');
throw new RuntimeException("Database connection error. Please check the configuration and try again.");
}
}
// Group all migration-related functions
function migrateDatabase(PDO $pgsql, PDO $mariadb): void
{
profileStart('migrateDatabase');
$startTime = microtime(true); // Start timer
try {
ConsoleOutput::showStatus("Analyzing Database", "Getting table dependencies...", 0, 5, 0);
$orderedTables = getTableOrder($pgsql);
$totalTables = count($orderedTables);
// Step 1: Create all tables first (without foreign keys)
foreach ($orderedTables as $index => $tableName) {
$elapsedTime = microtime(true) - $startTime;
ConsoleOutput::showStatus("Creating Tables", "Creating table", 1, 5, $elapsedTime);
$columns = fetchTableColumns($pgsql, $tableName);
createMariaDBTable($mariadb, $tableName, $columns);
}
// Step 2: Migrate data in batches
foreach ($orderedTables as $index => $tableName) {
$elapsedTime = microtime(true) - $startTime;
ConsoleOutput::showStatus("Migrating Data", "Migrating data", 2, 5, $elapsedTime);
$columns = fetchTableColumns($pgsql, $tableName);
transferTableDataInBatches($pgsql, $mariadb, $tableName, $columns);
}
// Step 3: Add foreign keys after all data is migrated
foreach ($orderedTables as $index => $tableName) {
$elapsedTime = microtime(true) - $startTime;
ConsoleOutput::showStatus("Adding Foreign Keys", "Adding foreign keys", 3, 5, $elapsedTime);
$foreignKeys = fetchForeignKeys($pgsql, $tableName);
addForeignKeyConstraints($mariadb, $pgsql, $tableName, $foreignKeys);
}
// Step 4: Add cascade constraints
foreach ($orderedTables as $index => $tableName) {
$elapsedTime = microtime(true) - $startTime;
ConsoleOutput::showStatus("Adding Cascade Constraints", "Adding cascade constraints", 4, 5, $elapsedTime);
$foreignKeys = fetchForeignKeys($pgsql, $tableName);
addCascadeConstraints($mariadb, $pgsql, $tableName, $foreignKeys);
}
// Step 5: Add indexes
foreach ($orderedTables as $index => $tableName) {
$elapsedTime = microtime(true) - $startTime;
ConsoleOutput::showStatus("Creating Indexes", "Creating indexes", 5, 5, $elapsedTime);
createIndexes($pgsql, $mariadb, $tableName);
}
ConsoleOutput::showSuccess("Migration completed successfully!");
} catch (Exception $e) {
ConsoleOutput::showError($e->getMessage());
logMessage("Error during migration: " . $e->getMessage(), 'ERROR');
throw $e;
}
profileEnd('migrateDatabase');
}
function transferTableDataInBatches(PDO $pgsql, PDO $mariadb, string $tableName, array $columns, int $batchSize = 1000): void
{
$batchSize = DatabaseConfig::BATCH_SIZE;
$offset = 0;
do {
$query = "SELECT * FROM $tableName LIMIT $batchSize OFFSET $offset";
$stmt = $pgsql->query($query);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (count($rows) > 0) {
$mariadb->beginTransaction();
foreach ($rows as $row) {
sanitizeRow($row, $columns);
insertRowIntoMariaDB($mariadb, $tableName, $row);
}
$mariadb->commit();
}
$offset += $batchSize;
} while (count($rows) > 0);
$stmt = null; // Close the statement
}
function insertRowIntoMariaDB(PDO $mariadb, string $tableName, array $row): void
{
$columns = implode(", ", array_keys($row));
$placeholders = implode(", ", array_fill(0, count($row), '?'));
$query = "INSERT INTO $tableName ($columns) VALUES ($placeholders)";
$stmt = $mariadb->prepare($query);
$stmt->execute(array_values($row));
$stmt = null; // Close the statement
}
function logMessage(string $message, string $level = 'INFO')
{
$timestamp = date('Y-m-d H:i:s');
$formattedMessage = "[$timestamp] [$level] $message" . PHP_EOL;
file_put_contents(LOG_FILE, $formattedMessage, FILE_APPEND);
}
function loadEnv(string $filePath): void
{
match (true) {
!file_exists($filePath) => throw new InvalidArgumentException('Missing .env file.'),
filesize($filePath) === 0 => throw new InvalidArgumentException('The .env file is empty.'),
default => null
};
$lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)
?: throw new RuntimeException('Failed to read the .env file.');
foreach ($lines as $line) {
$line = trim($line);
if ($line === '' || str_starts_with($line, '#')) {
continue;
}
if (!str_contains($line, '=')) {
throw new InvalidArgumentException(
"The line '$line' in the .env file is not in the correct format."
);
}
[$key, $value] = explode('=', $line, 2);
putenv("$key=$value");
}
}
function fetchTableColumns(PDO $pdo, string $tableName): array
{
static $cache = [];
if (isset($cache[$tableName])) {
return $cache[$tableName];
}
$stmt = $pdo->prepare(
'SELECT column_name, data_type, is_nullable, column_default, ordinal_position,
CASE
WHEN column_default LIKE \'nextval(%::regclass)\' THEN 1
ELSE 0
END as is_auto_increment
FROM information_schema.columns
WHERE table_name = :table_name'
);
$stmt->execute(['table_name' => $tableName]);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$cache[$tableName] = $result;
return $result;
}
function createMariaDBTable(PDO $mariadb, string $tableName, array $columns, string $tableEngine = DatabaseConfig::DEFAULT_ENGINE): void
{
$columnsSql = [];
$primaryKey = null;
foreach ($columns as $col) {
$dataType = match ($col['data_type']) {
'smallint' => DataType::SMALLINT,
'integer' => DataType::INTEGER,
'bigint' => DataType::BIGINT,
'boolean' => DataType::BOOLEAN,
'character varying' => DataType::CHARACTER_VARYING,
'text' => DataType::TEXT,
'timestamp without time zone' => DataType::TIMESTAMP,
'date' => DataType::DATE,
'numeric' => DataType::NUMERIC,
default => DataType::TEXT,
};
$type = $dataType->toMariaDBType();
$nullable = $col['is_nullable'] === 'YES' ? 'NULL' : 'NOT NULL';
$autoIncrement = ($col['is_auto_increment'] == 1) ? 'AUTO_INCREMENT' : '';
if ($autoIncrement && !$primaryKey) {
$primaryKey = $col['column_name'];
$columnsSql[] = sprintf('`%s` %s %s %s PRIMARY KEY', $col['column_name'], $type, $nullable, $autoIncrement);
} else {
$columnsSql[] = sprintf('`%s` %s %s %s', $col['column_name'], $type, $nullable, $autoIncrement);
}
}
if (!$primaryKey) {
$primaryKey = $columns[0]['column_name'];
}
$sql = sprintf(
'CREATE TABLE IF NOT EXISTS `%s` (%s) ENGINE=%s DEFAULT CHARSET=%s COLLATE=%s',
$tableName,
implode(', ', $columnsSql),
$tableEngine,
DatabaseConfig::CHARSET,
DatabaseConfig::COLLATION
);
try {
$mariadb->exec($sql);
logMessage("Created table `$tableName`.", 'INFO');
} catch (PDOException $e) {
logMessage("Error creating table `$tableName`: " . $e->getMessage(), 'ERROR');
echo "Error creating table `$tableName`: " . $e->getMessage() . "\n";
}
}
function sanitizeRow(array &$row, array $columns): void
{
foreach ($columns as $col) {
$colName = $col['column_name'];
$dataType = $col['data_type'];
$isNullable = $col['is_nullable'] === 'YES';
// Handle empty strings and NULL values
if (!isset($row[$colName]) || $row[$colName] === '') {
if (!$isNullable) {
// Default values for NOT NULL columns
if (in_array($dataType, ['integer', 'bigint', 'smallint', 'numeric'], true)) {
$row[$colName] = 0; // Default for numeric types
} elseif ($dataType === 'boolean') {
$row[$colName] = 0; // Default for boolean types
} else {
$row[$colName] = ''; // Default for text types
}
} else {
$row[$colName] = null; // Allow NULL for nullable columns
}
}
// Boolean sanitization: map values to 0/1
if ($dataType === 'boolean') {
$row[$colName] = filter_var($row[$colName], FILTER_VALIDATE_BOOLEAN) ? 1 : 0;
}
}
}
// Utility function for validating and setting default actions
function validateAction(string $action, string $constraintName, string $tableName, string $type): string
{
$validActions = ['NO ACTION', 'CASCADE', 'SET NULL', 'RESTRICT', 'SET DEFAULT'];
$action = strtoupper($action);
if (!in_array($action, $validActions)) {
logMessage("Invalid $type action '$action' for $constraintName in $tableName. Defaulting to 'NO ACTION'.", 'WARNING');
return 'NO ACTION';
}
return $action;
}
function createForeignKeyConstraints(PDO $pgsql, PDO $mariadb, string $tableName): void
{
$query = <<<SQL
SELECT
tc.constraint_name,
kcu.column_name,
ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name,
rc.update_rule,
rc.delete_rule
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage ccu
ON ccu.constraint_name = tc.constraint_name
JOIN information_schema.referential_constraints rc
ON rc.constraint_name = tc.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY'
AND tc.table_name = :tableName
SQL;
$stmt = $pgsql->prepare($query);
$stmt->execute(['tableName' => $tableName]);
$foreignKeys = $stmt->fetchAll();
foreach ($foreignKeys as $fk) {
$constraint = <<<SQL
ALTER TABLE `$tableName`
ADD CONSTRAINT `{$fk['constraint_name']}`
FOREIGN KEY (`{$fk['column_name']}`)
REFERENCES `{$fk['foreign_table_name']}` (`{$fk['foreign_column_name']}`)
SQL;
// Add ON DELETE rule if specified
if (!empty($fk['delete_rule'])) {
$constraint .= " ON DELETE " . strtoupper($fk['delete_rule']);
}
// Add ON UPDATE rule if specified
if (!empty($fk['update_rule'])) {
$constraint .= " ON UPDATE " . strtoupper($fk['update_rule']);
}
try {
$mariadb->exec($constraint);
logMessage("Added foreign key constraint {$fk['constraint_name']} to $tableName", 'INFO');
} catch (PDOException $e) {
if (strpos($e->getMessage(), 'foreign key constraint') !== false) {
logMessage("Foreign key constraint issue for `$tableName`: " . $e->getMessage(), 'WARNING');
} else {
logMessage("Error creating table `$tableName`: " . $e->getMessage(), 'ERROR');
}
}
}
}
function createIndexes(PDO $pgsql, PDO $mariadb, string $tableName): void
{
$query = <<<SQL
SELECT
i.relname AS index_name,
a.attname AS column_name,
ix.indisunique AS is_unique
FROM pg_class t
JOIN pg_index ix ON t.oid = ix.indrelid
JOIN pg_class i ON i.oid = ix.indexrelid
JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(ix.indkey)
WHERE t.relname = :tableName
AND i.relname NOT LIKE '%_pkey'
SQL;
$stmt = $pgsql->prepare($query);
$stmt->execute(['tableName' => $tableName]);
$indexes = $stmt->fetchAll();
foreach ($indexes as $idx) {
$unique = $idx['is_unique'] ? 'UNIQUE' : '';
$indexSql = <<<SQL
CREATE $unique INDEX `{$idx['index_name']}`
ON `$tableName` (`{$idx['column_name']}`)
SQL;
try {
$mariadb->exec($indexSql);
logMessage("Created index {$idx['index_name']} on $tableName", 'INFO');
} catch (PDOException $e) {
logMessage("Failed to create index: " . $e->getMessage(), 'ERROR');
}
}
}
function addCascadeConstraints(PDO $mariadb, PDO $pgsql, string $tableName): void
{
// Check if the table exists in PostgreSQL
$tableExistsSql = "SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = :tableName";
$stmt = $pgsql->prepare($tableExistsSql);
$stmt->execute(['tableName' => $tableName]);
if ($stmt->rowCount() === 0) {
logMessage("Table '$tableName' does not exist in PostgreSQL.", 'ERROR');
return;
}
$query = <<<SQL
SELECT
tc.constraint_name,
kcu.column_name,
ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name,
tc.constraint_type,
tc.table_name
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage ccu
ON ccu.constraint_name = tc.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY'
AND tc.table_name = :tableName
SQL;
$stmt = $pgsql->prepare($query);
$stmt->execute(['tableName' => $tableName]);
$foreignKeys = $stmt->fetchAll();
foreach ($foreignKeys as $fk) {
// Ensure unique constraint name by appending table name and column
$constraintName = "fk_{$tableName}_{$fk['column_name']}";
// Default values for ON UPDATE and ON DELETE
$onUpdate = '';
$onDelete = '';
// Check if ON UPDATE CASCADE is defined in PostgreSQL
$pgsqlUpdateCheckSql = "
SELECT 1
FROM information_schema.referential_constraints rc
JOIN information_schema.key_column_usage kcu
ON rc.constraint_name = kcu.constraint_name
AND rc.constraint_schema = kcu.constraint_schema
WHERE rc.constraint_schema = 'public'
AND rc.update_rule = 'CASCADE'
AND kcu.table_name = :tableName
AND kcu.column_name = :columnName
";
$stmt = $pgsql->prepare($pgsqlUpdateCheckSql);
$stmt->execute(['tableName' => $tableName, 'columnName' => $fk['column_name']]);
if ($stmt->rowCount() > 0) {
$onUpdate = 'ON UPDATE CASCADE';
}
// Check if ON DELETE CASCADE is defined in PostgreSQL
$pgsqlDeleteCheckSql = "
SELECT 1
FROM information_schema.referential_constraints rc
JOIN information_schema.key_column_usage kcu
ON rc.constraint_name = kcu.constraint_name
AND rc.constraint_schema = kcu.constraint_schema
WHERE rc.constraint_schema = 'public'
AND rc.delete_rule = 'CASCADE'
AND kcu.table_name = :tableName
AND kcu.column_name = :columnName
";
$stmt = $pgsql->prepare($pgsqlDeleteCheckSql);
$stmt->execute(['tableName' => $tableName, 'columnName' => $fk['column_name']]);
if ($stmt->rowCount() > 0) {
$onDelete = 'ON DELETE CASCADE';
}
// Construct the ALTER TABLE SQL statement dynamically
$constraintSql = "
ALTER TABLE `$tableName`
ADD CONSTRAINT `$constraintName`
FOREIGN KEY (`{$fk['column_name']}`)
REFERENCES `{$fk['foreign_table_name']}`(`{$fk['foreign_column_name']}`)
$onUpdate
$onDelete;
";
try {
// Verify if the referenced table and column exist in MariaDB
$checkSql = "
SELECT 1
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = :referencedTable
AND COLUMN_NAME = :referencedColumn
";
$stmt = $mariadb->prepare($checkSql);
$stmt->execute([
'referencedTable' => $fk['foreign_table_name'],
'referencedColumn' => $fk['foreign_column_name']
]);
if ($stmt->rowCount() > 0) {
// Ensure the referenced column has an index
$indexCheckSql = "SHOW INDEX FROM `{$fk['foreign_table_name']}` WHERE Column_name = :referenced_column";
$indexStmt = $mariadb->prepare($indexCheckSql);
$indexStmt->execute([
':referenced_column' => $fk['foreign_column_name']
]);
if ($indexStmt->rowCount() === 0) {
$createIndexSql = "CREATE INDEX idx_{$fk['foreign_column_name']} ON `{$fk['foreign_table_name']}`(`{$fk['foreign_column_name']}`)";
$mariadb->exec($createIndexSql);
}
// Add the foreign key constraint
$mariadb->exec($constraintSql);
logMessage("Added cascade constraints for table '$tableName', column '{$fk['column_name']}'", 'INFO');
} else {
logMessage("Referenced table or column does not exist for '$tableName' and column '{$fk['column_name']}'", 'ERROR');
}
} catch (PDOException $e) {
logMessage("Failed to add cascade constraints for table '$tableName', column '{$fk['column_name']}': " . $e->getMessage(), 'ERROR');
}
}
}
function addForeignKeyConstraints(PDO $mariadb, PDO $pgsql, string $tableName, array $foreignKeys): void
{
foreach ($foreignKeys as $foreignKey) {
// Fetch the exact foreign key definition from PostgreSQL
$constraintSql = $foreignKey['definition'];
try {
// Verify the existence of the referenced table and column
$checkSql = "SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = :referenced_table AND COLUMN_NAME = :referenced_column";
$stmt = $mariadb->prepare($checkSql);
$stmt->execute([
':referenced_table' => $foreignKey['referenced_table'],
':referenced_column' => $foreignKey['referenced_column']
]);
if ($stmt->rowCount() > 0) {
// Ensure the referenced column has an index
$indexCheckSql = "SHOW INDEX FROM `{$foreignKey['referenced_table']}` WHERE Column_name = :referenced_column";
$indexStmt = $mariadb->prepare($indexCheckSql);
$indexStmt->execute([
':referenced_column' => $foreignKey['referenced_column']
]);
if ($indexStmt->rowCount() === 0) {
$createIndexSql = "CREATE INDEX idx_{$foreignKey['referenced_column']} ON `{$foreignKey['referenced_table']}`(`{$foreignKey['referenced_column']}`)";
$mariadb->exec($createIndexSql);
}
// Execute the SQL to add the foreign key constraint
$mariadb->exec($constraintSql);
logMessage("Transferred foreign key constraint for $tableName: {$foreignKey['name']}", 'INFO');
} else {
logMessage("Referenced table or column does not exist for $tableName", 'ERROR');
}
} catch (PDOException $e) {
logMessage("Failed to transfer foreign key constraint for $tableName: {$foreignKey['name']} - " . $e->getMessage(), 'ERROR');
}
}
}
function getTableOrder(PDO $pgsql): array
{
$tables = [];
$dependencies = [];
// Get all tables
$stmt = $pgsql->query("SELECT tablename FROM pg_tables WHERE schemaname = 'public'");
$allTables = $stmt->fetchAll(PDO::FETCH_COLUMN);
// Get foreign key dependencies
foreach ($allTables as $table) {
$query = <<<SQL
SELECT
tc.table_name,
kcu.column_name,
ccu.table_name AS foreign_table_name
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage ccu
ON ccu.constraint_name = tc.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY'
AND tc.table_name = :tableName
SQL;
$stmt = $pgsql->prepare($query);
$stmt->execute(['tableName' => $table]);
$foreignKeys = $stmt->fetchAll();
$dependencies[$table] = [];
foreach ($foreignKeys as $fk) {
$dependencies[$table][] = $fk['foreign_table_name'];
}
if (!isset($tables[$table])) {
$tables[$table] = false; // not processed
}
}
// Helper function to process tables in correct order
$orderedTables = [];
$processTable = function ($table) use (&$processTable, &$tables, &$dependencies, &$orderedTables) {
if ($tables[$table]) { // already processed
return;
}
$tables[$table] = true; // mark as being processed
// Process dependencies first
foreach ($dependencies[$table] as $dep) {
if (isset($tables[$dep]) && !$tables[$dep]) {
$processTable($dep);
}
}
$orderedTables[] = $table;
};
// Process all tables
foreach ($tables as $table => $processed) {
if (!$processed) {
$processTable($table);
}
}
return $orderedTables;
}
class ConsoleOutput
{
public const COLORS = [
'green' => "\033[32m",
'red' => "\033[31m",
'yellow' => "\033[33m",
'blue' => "\033[34m",
'purple' => "\033[35m",
'cyan' => "\033[36m",
'white' => "\033[37m",
'reset' => "\033[0m",
];
public static function showStatus(string $step, string $message, int $current, int $total, float $elapsedTime = 0.0): void
{
static $lastProgress = -1;
static $lastErrorCount = -1;
// Prevent division by zero
$total = max($total, 1);
// Calculate progress percentage
$percentage = ($current / $total) * 100;
$percentageDisplay = number_format($percentage, 1);
// Generate progress bar
$progressBar = self::createProgressBar((int)$percentage);
// Count errors in the log file
$errorCount = file_exists(LOG_FILE) ? substr_count(file_get_contents(LOG_FILE), '[ERROR]') : 0;
// Update console only if needed
if ((int)$percentage !== $lastProgress || $errorCount !== $lastErrorCount) {
// Clear screen and move cursor to top
echo "\033[2J\033[;H";
echo "═══════════════════════════════════════════════════════════════════\n";
echo "\033[1m"; // Bold text for emphasis
echo sprintf("Step: %s%s%s\n", self::COLORS['cyan'], $step, self::COLORS['reset']);
echo sprintf("Progress: [%s%s%s] %s%%\n", self::getGradientColor($percentage), $progressBar, self::COLORS['reset'], $percentageDisplay);
echo sprintf("Current Action: %s%-50s%s\n", self::COLORS['yellow'], $message, self::COLORS['reset']);
echo sprintf("Errors Logged: %s%d%s\n", self::COLORS['red'], $errorCount, self::COLORS['reset']);
echo "\033[0m"; // Reset text formatting
echo "═══════════════════════════════════════════════════════════════════\n";
$lastProgress = (int)$percentage;
$lastErrorCount = $errorCount;
}
// Log detailed tracking information
logMessage(sprintf("Step: %s, Action: %s, Progress: %d/%d (%.1f%%), Errors Logged: %d", $step, $message, $current, $total, $percentageDisplay, $errorCount), 'INFO');
// Optional delay for readability in fast loops
usleep(50000); // Sleep for 50 milliseconds
}
public static function createProgressBar(int $percentage): string
{
// Ensure percentage is between 0 and 100
$percentage = max(0, min(100, $percentage));
$width = 50;
$completed = (int)($width * $percentage / 100);
$remaining = $width - $completed;
return self::COLORS['green'] .
str_repeat("█", $completed) .
self::COLORS['white'] .
str_repeat("░", $remaining) .
self::COLORS['reset'];
}
private static function getGradientColor(float $percentage): string
{
// Simple gradient from red to green
if ($percentage < 50) {
return "\033[31m"; // Red
} elseif ($percentage < 75) {
return "\033[33m"; // Yellow
} else {
return "\033[32m"; // Green
}
}
public static function showError(string $message): void
{
echo self::COLORS['red'] . "ERROR: $message" . self::COLORS['reset'] . "\n";
}
public static function showSuccess(string $message): void
{
echo self::COLORS['green'] . "SUCCESS: $message" . self::COLORS['reset'] . "\n";
}
public static function showWarning(string $message): void
{
echo self::COLORS['yellow'] . "WARNING: $message" . self::COLORS['reset'] . "\n";
}
}
enum DataType: string
{
case SMALLINT = 'SMALLINT';
case INTEGER = 'INT';
case BIGINT = 'BIGINT';
case BOOLEAN = 'TINYINT(1)';
case CHARACTER_VARYING = 'VARCHAR(255)';
case TEXT = 'TEXT';
case TIMESTAMP = 'DATETIME';
case DATE = 'DATE';
case NUMERIC = 'DECIMAL(20,6)';
public function toMariaDBType(): string
{
return match ($this) {
self::SMALLINT => 'INT',
self::INTEGER => 'INT',
self::BIGINT => 'BIGINT',
self::BOOLEAN => 'TINYINT(1)',
self::CHARACTER_VARYING => 'VARCHAR(255)',
self::TEXT => 'TEXT',
self::TIMESTAMP => 'DATETIME',
self::DATE => 'DATE',
self::NUMERIC => 'DECIMAL(20,6)',
};
}
}
readonly class DatabaseConfig
{
public const BATCH_SIZE = 100;
public const DEFAULT_ENGINE = 'InnoDB';
public const CHARSET = 'utf8mb4';
public const COLLATION = 'utf8mb4_unicode_ci';
}
function fetchForeignKeyActions(PDO $pgsql, string $tableName, string $columnName): array
{
$query = "SELECT rc.update_rule, rc.delete_rule FROM information_schema.referential_constraints rc JOIN information_schema.key_column_usage kcu ON rc.constraint_name = kcu.constraint_name WHERE kcu.table_name = :table_name AND kcu.column_name = :column_name";
$stmt = $pgsql->prepare($query);
$stmt->execute(['table_name' => $tableName, 'column_name' => $columnName]);
return $stmt->fetch(PDO::FETCH_ASSOC) ?: ['update_rule' => 'RESTRICT', 'delete_rule' => 'RESTRICT'];
}
function fetchForeignKeys(PDO $pgsql, string $tableName): array
{
$query = <<<SQL
SELECT
tc.constraint_name,
kcu.column_name,
ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name,
tc.constraint_type,
tc.table_name
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage ccu
ON ccu.constraint_name = tc.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY'
AND tc.table_name = :tableName
SQL;
$stmt = $pgsql->prepare($query);
$stmt->execute(['tableName' => $tableName]);
$foreignKeys = $stmt->fetchAll();
$result = [];
foreach ($foreignKeys as $fk) {
// Construct the SQL definition for the foreign key
$definition = "ALTER TABLE `$tableName` ADD CONSTRAINT `{$fk['constraint_name']}` FOREIGN KEY (`{$fk['column_name']}`) REFERENCES `{$fk['foreign_table_name']}`(`{$fk['foreign_column_name']}`) ON UPDATE CASCADE ON DELETE CASCADE";
$result[] = [
'name' => $fk['constraint_name'],
'column' => $fk['column_name'],
'referenced_table' => $fk['foreign_table_name'],
'referenced_column' => $fk['foreign_column_name'],
'definition' => $definition,
'on_update' => 'CASCADE',
'on_delete' => 'CASCADE',
];
}
return $result;
}
function fetchCascadeConstraints(PDO $pgsql, string $tableName): array
{
$query = <<<SQL
SELECT
tc.constraint_name,
kcu.column_name,
ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name,
tc.constraint_type,
tc.table_name
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage ccu
ON ccu.constraint_name = tc.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY'
AND tc.table_name = :tableName
SQL;
$stmt = $pgsql->prepare($query);
$stmt->execute(['tableName' => $tableName]);
$foreignKeys = $stmt->fetchAll();
$result = [];
foreach ($foreignKeys as $fk) {
$result[] = [
'name' => $fk['constraint_name'],
'column' => $fk['column_name'],
'referenced_table' => $fk['foreign_table_name'],
'referenced_column' => $fk['foreign_column_name'],
];
}
return $result;
}
const COLORS = [
'reset' => "\033[0m",
'red' => "\033[31m",
'green' => "\033[32m",
'yellow' => "\033[33m",
'cyan' => "\033[36m",
'white' => "\033[37m",
];
function colorize(string $message, string $color): string
{
return COLORS[$color] . $message . COLORS['reset'];
}
function displayWarning(): void
{
echo colorize("############################ WARNING ################################\n", 'red');
echo colorize("This script is designed to migrate data from PostgreSQL to MariaDB.\n", 'yellow');
echo colorize("It is provided as-is, and you assume all risks associated with its use.\n", 'yellow');
echo colorize("The author is not responsible for any data loss or damage.\n", 'yellow');
echo colorize("Before proceeding, please ensure you have read the README.md file thoroughly.\n", 'yellow');
echo colorize("Make sure to perform complete backups of your data to prevent any accidental loss.\n", 'yellow');
echo colorize("######################################################################\n", 'red');
echo colorize("Type 'YES' to confirm you have read and understood this warning: ", 'cyan');
$confirmation = trim(fgets(STDIN));
if (strtoupper($confirmation) !== 'YES') {
exit("Operation cancelled by user.\n");
}
}
function profileStart(string $section): void
{
logMessage("Profiling start: $section", 'DEBUG');
}
function profileEnd(string $section): void
{
logMessage("Profiling end: $section", 'DEBUG');
}
function main()
{
logMessage("Migration started.", 'INFO');
displayWarning();
try {
// Load environment variables
loadEnv(__DIR__ . '/.env');
// Create database connections using individual environment variables
$pgsql = createPDOConnection(getenv('PGSQL_HOST'), getenv('PGSQL_PORT'), getenv('PGSQL_DBNAME'), getenv('PGSQL_USER'), getenv('PGSQL_PASSWORD'), 'pgsql');
$mariadb = createPDOConnection(getenv('MARIADB_HOST'), getenv('MARIADB_PORT'), getenv('MARIADB_DBNAME'), getenv('MARIADB_USER'), getenv('MARIADB_PASSWORD'), 'mysql');
// Perform migration
migrateDatabase($pgsql, $mariadb);
// Drop audit tables
$tableNames = getTableOrder($pgsql);
logMessage("Migration completed successfully.", 'SUCCESS');
echo colorize("Migration completed successfully. Check the log for details.\n", 'green');
} catch (Exception $e) {
logMessage("Migration failed: " . $e->getMessage(), 'ERROR');
echo colorize("Migration failed. Check the log for details.\n", 'red');
}
}
// Check if required PHP extensions are loaded
$requiredExtensions = ['pdo_pgsql', 'pdo_mysql', 'pdo'];
foreach ($requiredExtensions as $extension) {
if (!extension_loaded($extension)) {
ConsoleOutput::showError("Missing required extension: $extension");
logMessage("Missing required extension: $extension.", 'ERROR');
exit(1);
}
}
main();