This package provides a Spark command to convert SQL dump files (e.g., from phpMyAdmin) into CodeIgniter 4 migration files. It supports table creation (CREATE TABLE), foreign keys (FOREIGN KEY), triggers, and database prefixes (database.default.DBPrefix).
- Table Creation: Generates migration files from
CREATE TABLEstatements. - Foreign Keys: Converts
ALTER TABLEforeign key definitions into separate migration files, supportingSET NULL,NO ACTION,CASCADE,RESTRICT, andSET DEFAULT. - Triggers: Creates migration files for
CREATE TRIGGERstatements. - Prefix Support: Automatically strips prefixes (e.g.,
ci4ms_) from table names based ondatabase.default.DBPrefixin the.envfile. - Debugging: Provides detailed CLI logs for troubleshooting.
Add the package to your CodeIgniter 4 project:
composer require bertugfahriozer/sql-to-migrationCheck if the Spark command is registered:
php spark listYou should see sql2migration in the list.
To convert an SQL file into CodeIgniter 4 migration files:
php spark sql2migration /path/to/your/database.sql- Input: Path to the SQL file (e.g.,
/path/to/database.sql). - Output: Migration files are generated in
app/Database/Migrations/:- Table migrations (e.g.,
20250907223600_CreateUsersTable.php) - Foreign key migration (e.g.,
20250907223602_AddForeignKeys.php) - Trigger migrations (e.g.,
20250907223603_CreateUpdateTimestampTrigger.php)
- Table migrations (e.g.,
Apply the migrations:
php spark migrate-
Table Definitions:
- Supports
CREATE TABLEstatements with column types (INT,VARCHAR, etc.),UNSIGNED,NOT NULL,AUTO_INCREMENT, andPRIMARY KEY. - Example:
CREATE TABLE `ci4ms_users` ( `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, `username` VARCHAR(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB;
- Supports
-
Foreign Keys:
- Supports
ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEYstatements. - Handles
ON DELETEandON UPDATEwith:CASCADE,SET NULL,NO ACTION,RESTRICT,SET DEFAULT. - Example:
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;
- Supports
-
Triggers:
- Supports
CREATE TRIGGERstatements (BEFORE/AFTER,INSERT/UPDATE/DELETE). - Example:
CREATE TRIGGER `ci4ms_update_timestamp` BEFORE UPDATE ON `ci4ms_users` FOR EACH ROW SET NEW.updated_at = NOW();
- Supports
-
Prefix Handling:
- Strips prefixes (e.g.,
ci4ms_) from table names based ondatabase.default.DBPrefixin.env. - Example
.env:database.default.DBPrefix = ci4ms_
- Strips prefixes (e.g.,
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 php spark sql2migration database.sql produces the following files:
-
Table Migration (
20250907223600_CreateUsersTable.php):<?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'); } }
-
Foreign Key Migration (
20250907223602_AddForeignKeys.php):<?php namespace App\Database\Migrations; use CodeIgniter\Database\Migration; class AddForeignKeys extends Migration { public function up() { $this->forge->addForeignKey('user_id', 'users', 'id', 'SET NULL', 'NO ACTION'); } public function down() { $this->forge->dropForeignKey('orders', 'fk_orders_users'); } }
-
Trigger Migration (
20250907223603_CreateUpdateTimestampTrigger.php):<?php namespace App\Database\Migrations; use CodeIgniter\Database\Migration; class CreateUpdateTimestampTrigger extends Migration { public function up() { $this->db->query( 'CREATE TRIGGER `ci4ms_update_timestamp` BEFORE UPDATE ON `users` FOR EACH ROW SET NEW.updated_at = NOW();' ); } public function down() { $this->db->query('DROP TRIGGER IF EXISTS `ci4ms_update_timestamp`;'); } }
- "Command Not Found" Error:
- Ensure the namespace is correctly added in
app/Config/Autoload.php. - Run
composer dump-autoload.
- Ensure the namespace is correctly added in
- Foreign Keys Not Parsed:
- Verify the
ALTER TABLEformat in your SQL file. The package supports:ALTER TABLE `table_name` ADD CONSTRAINT `constraint_name` FOREIGN KEY (`field`) REFERENCES `ref_table` (`ref_field`) ON DELETE SET NULL ON UPDATE NO ACTION; ALTER TABLE `table_name` ADD FOREIGN KEY (`field`) REFERENCES `ref_table` (`ref_field`);
- Check CLI output for debugging:
Look for
php spark sql2migration /path/to/database.sqlDebug: Found ALTER TABLE statementsandDebug: Found foreign keysin the output.
- Verify the
- Table Prefix Issues:
- Ensure
database.default.DBPrefixis correctly set in.env. Example:database.default.DBPrefix = ci4ms_
- Ensure
- Migration Errors:
- If migrations fail, roll back with
php spark migrate:rollbackand verify the SQL file.
- If migrations fail, roll back with
- Report issues on GitHub: github.com/bertugfahriozer/sql2migration
- Contribute via Pull Requests.
- The package is licensed under the MIT License.
MIT License. See the LICENSE file for details.