Files
HRM-System/scratch/import_sql.php

47 lines
1.4 KiB
PHP

<?php
require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
echo "Dropping all tables from database...\n";
Schema::disableForeignKeyConstraints();
$dbName = config('database.connections.mysql.database');
$tables = DB::select('SHOW TABLES');
$keyName = "Tables_in_" . $dbName;
foreach ($tables as $table) {
if (isset($table->$keyName)) {
$name = $table->$keyName;
Schema::drop($name);
echo "Dropped table: $name\n";
}
}
Schema::enableForeignKeyConstraints();
echo "\nImporting u693027326_seb (1).sql...\n";
$sqlPath = __DIR__ . '/../u693027326_seb (1).sql';
if (!file_exists($sqlPath)) {
echo "SQL file not found at: $sqlPath\n";
exit(1);
}
$sql = file_get_contents($sqlPath);
try {
// Disable foreign key checks for the session during SQL import
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
DB::unprepared($sql);
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
echo "Import completed successfully! ✅\n";
} catch (\Exception $e) {
// Try to re-enable checks if it failed
try { DB::statement('SET FOREIGN_KEY_CHECKS=1;'); } catch (\Exception $ex) {}
echo "Error during import: " . $e->getMessage() . "\n";
exit(1);
}