An advanced CLI tool for converting SQL dump files into CodeIgniter 4 migration files with support for tables, foreign keys, and triggers.
Get StartedThe SQL to Migration
is a CodeIgniter 4 CLI command designed to convert SQL dump files (e.g., from phpMyAdmin) into CodeIgniter 4 migration files. It supports table creation, foreign keys, triggers, and database prefixes.
Add the package to your CodeIgniter 4 project:
Check if the Spark command is registered:
You should see sql2migration
in the list.
To convert an SQL file into CodeIgniter 4 migration files:
Path to the SQL file (e.g., /path/to/database.sql
)
Migration files are generated in app/Database/Migrations/
Apply the migrations:
Generates migration files from CREATE TABLE
statements with various column types and constraints.
Converts ALTER TABLE
foreign key definitions into separate migration files with various actions.
Creates migration files for CREATE TRIGGER
statements with different timing and event combinations.
Automatically strips table prefixes based on your CodeIgniter database configuration.
Provides detailed CLI logs for troubleshooting during the conversion process.
The following SQL file demonstrates the package's capabilities:
CREATE TABLE `ci4ms_users` ( `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, `username` VARCHAR(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB; CREATE TABLE `ci4ms_orders` ( `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, `user_id` INT(11) UNSIGNED NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB; ALTER TABLE `ci4ms_orders` ADD CONSTRAINT `fk_orders_users` FOREIGN KEY (`user_id`) REFERENCES `ci4ms_users` (`id`) ON DELETE SET NULL ON UPDATE NO ACTION; CREATE TRIGGER `ci4ms_update_timestamp` BEFORE UPDATE ON `ci4ms_users` FOR EACH ROW SET NEW.updated_at = NOW();
Running the command produces the following migration files:
<?php namespace App\Database\Migrations; use CodeIgniter\Database\Migration; class CreateUsersTable extends Migration { public function up() { $this->forge->addField([ 'id' => [ 'type' => 'INT', 'constraint' => '11', 'unsigned' => true, 'auto_increment' => true, ], 'username' => [ 'type' => 'VARCHAR', 'constraint' => '255', ], ]); $this->forge->addKey('id', true); $this->forge->createTable('users'); } public function down() { $this->forge->dropTable('users'); } }
app/Config/Autoload.php
composer dump-autoload
ALTER TABLE
format in your SQL filedatabase.default.DBPrefix
is correctly set in .env
php spark migrate:rollback