|
| 1 | +# SQL to Migration for CodeIgniter 4 - Documentation |
| 2 | + |
| 3 | +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`). |
| 4 | + |
| 5 | +## Features |
| 6 | +- **Table Creation**: Generates migration files from `CREATE TABLE` statements. |
| 7 | +- **Foreign Keys**: Converts `ALTER TABLE` foreign key definitions into separate migration files, supporting `SET NULL`, `NO ACTION`, `CASCADE`, `RESTRICT`, and `SET DEFAULT`. |
| 8 | +- **Triggers**: Creates migration files for `CREATE TRIGGER` statements. |
| 9 | +- **Prefix Support**: Automatically strips prefixes (e.g., `ci4ms_`) from table names based on `database.default.DBPrefix` in the `.env` file. |
| 10 | +- **Debugging**: Provides detailed CLI logs for troubleshooting. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +### 1. Install via Composer |
| 15 | +Add the package to your CodeIgniter 4 project: |
| 16 | +```bash |
| 17 | +composer require yourvendor/sql-to-migration |
| 18 | +``` |
| 19 | + |
| 20 | +### 2. Configure Autoloading |
| 21 | +Open `app/Config/Autoload.php` and add the package to the PSR-4 autoloading: |
| 22 | +```php |
| 23 | +public $psr4 = [ |
| 24 | + APP_NAMESPACE => APPPATH, // Existing namespaces |
| 25 | + 'Config' => APPPATH . 'Config', |
| 26 | + 'YourVendor\SqlToMigration' => VENDORPATH . 'yourvendor/sql-to-migration/src', |
| 27 | +]; |
| 28 | +``` |
| 29 | + |
| 30 | +**Note**: Replace `yourvendor` with your actual vendor name (e.g., `acme`). |
| 31 | + |
| 32 | +### 3. Verify Command Availability |
| 33 | +Check if the Spark command is registered: |
| 34 | +```bash |
| 35 | +php spark list |
| 36 | +``` |
| 37 | +You should see `sql2migration` in the list. |
| 38 | + |
| 39 | +## Usage |
| 40 | +To convert an SQL file into CodeIgniter 4 migration files: |
| 41 | +```bash |
| 42 | +php spark sql2migration /path/to/your/database.sql |
| 43 | +``` |
| 44 | + |
| 45 | +- **Input**: Path to the SQL file (e.g., `/path/to/database.sql`). |
| 46 | +- **Output**: Migration files are generated in `app/Database/Migrations/`: |
| 47 | + - Table migrations (e.g., `20250907223600_CreateUsersTable.php`) |
| 48 | + - Foreign key migration (e.g., `20250907223602_AddForeignKeys.php`) |
| 49 | + - Trigger migrations (e.g., `20250907223603_CreateUpdateTimestampTrigger.php`) |
| 50 | + |
| 51 | +Apply the migrations: |
| 52 | +```bash |
| 53 | +php spark migrate |
| 54 | +``` |
| 55 | + |
| 56 | +## Supported SQL Structures |
| 57 | +- **Table Definitions**: |
| 58 | + - Supports `CREATE TABLE` statements with column types (`INT`, `VARCHAR`, etc.), `UNSIGNED`, `NOT NULL`, `AUTO_INCREMENT`, and `PRIMARY KEY`. |
| 59 | + - Example: |
| 60 | + ```sql |
| 61 | + CREATE TABLE `ci4ms_users` ( |
| 62 | + `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 63 | + `username` VARCHAR(255) NOT NULL, |
| 64 | + PRIMARY KEY (`id`) |
| 65 | + ) ENGINE=InnoDB; |
| 66 | + ``` |
| 67 | + |
| 68 | +- **Foreign Keys**: |
| 69 | + - Supports `ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY` statements. |
| 70 | + - Handles `ON DELETE` and `ON UPDATE` with: `CASCADE`, `SET NULL`, `NO ACTION`, `RESTRICT`, `SET DEFAULT`. |
| 71 | + - Example: |
| 72 | + ```sql |
| 73 | + ALTER TABLE `ci4ms_orders` |
| 74 | + ADD CONSTRAINT `fk_orders_users` FOREIGN KEY (`user_id`) REFERENCES `ci4ms_users` (`id`) ON DELETE SET NULL ON UPDATE NO ACTION; |
| 75 | + ``` |
| 76 | + |
| 77 | +- **Triggers**: |
| 78 | + - Supports `CREATE TRIGGER` statements (`BEFORE`/`AFTER`, `INSERT`/`UPDATE`/`DELETE`). |
| 79 | + - Example: |
| 80 | + ```sql |
| 81 | + CREATE TRIGGER `ci4ms_update_timestamp` BEFORE UPDATE ON `ci4ms_users` FOR EACH ROW SET NEW.updated_at = NOW(); |
| 82 | + ``` |
| 83 | + |
| 84 | +- **Prefix Handling**: |
| 85 | + - Strips prefixes (e.g., `ci4ms_`) from table names based on `database.default.DBPrefix` in `.env`. |
| 86 | + - Example `.env`: |
| 87 | + ```env |
| 88 | + database.default.DBPrefix = ci4ms_ |
| 89 | + ``` |
| 90 | + |
| 91 | +## Example SQL File |
| 92 | +The following SQL file demonstrates the package’s capabilities: |
| 93 | +```sql |
| 94 | +CREATE TABLE `ci4ms_users` ( |
| 95 | + `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 96 | + `username` VARCHAR(255) NOT NULL, |
| 97 | + PRIMARY KEY (`id`) |
| 98 | +) ENGINE=InnoDB; |
| 99 | +
|
| 100 | +CREATE TABLE `ci4ms_orders` ( |
| 101 | + `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 102 | + `user_id` INT(11) UNSIGNED NULL, |
| 103 | + PRIMARY KEY (`id`) |
| 104 | +) ENGINE=InnoDB; |
| 105 | +
|
| 106 | +ALTER TABLE `ci4ms_orders` |
| 107 | + ADD CONSTRAINT `fk_orders_users` FOREIGN KEY (`user_id`) REFERENCES `ci4ms_users` (`id`) ON DELETE SET NULL ON UPDATE NO ACTION; |
| 108 | +
|
| 109 | +CREATE TRIGGER `ci4ms_update_timestamp` BEFORE UPDATE ON `ci4ms_users` FOR EACH ROW SET NEW.updated_at = NOW(); |
| 110 | +``` |
| 111 | + |
| 112 | +### Generated Migration Files |
| 113 | +Running `php spark sql2migration database.sql` produces the following files: |
| 114 | + |
| 115 | +- **Table Migration** (`20250907223600_CreateUsersTable.php`): |
| 116 | + ```php |
| 117 | + <?php |
| 118 | + namespace App\Database\Migrations; |
| 119 | + use CodeIgniter\Database\Migration; |
| 120 | +
|
| 121 | + class CreateUsersTable extends Migration |
| 122 | + { |
| 123 | + public function up() |
| 124 | + { |
| 125 | + $this->forge->addField([ |
| 126 | + 'id' => [ |
| 127 | + 'type' => 'INT', |
| 128 | + 'constraint' => '11', |
| 129 | + 'unsigned' => true, |
| 130 | + 'auto_increment' => true, |
| 131 | + ], |
| 132 | + 'username' => [ |
| 133 | + 'type' => 'VARCHAR', |
| 134 | + 'constraint' => '255', |
| 135 | + ], |
| 136 | + ]); |
| 137 | + $this->forge->addKey('id', true); |
| 138 | + $this->forge->createTable('users'); |
| 139 | + } |
| 140 | +
|
| 141 | + public function down() |
| 142 | + { |
| 143 | + $this->forge->dropTable('users'); |
| 144 | + } |
| 145 | + } |
| 146 | + ``` |
| 147 | + |
| 148 | +- **Foreign Key Migration** (`20250907223602_AddForeignKeys.php`): |
| 149 | + ```php |
| 150 | + <?php |
| 151 | + namespace App\Database\Migrations; |
| 152 | + use CodeIgniter\Database\Migration; |
| 153 | +
|
| 154 | + class AddForeignKeys extends Migration |
| 155 | + { |
| 156 | + public function up() |
| 157 | + { |
| 158 | + $this->forge->addForeignKey('user_id', 'users', 'id', 'SET NULL', 'NO ACTION'); |
| 159 | + } |
| 160 | +
|
| 161 | + public function down() |
| 162 | + { |
| 163 | + $this->forge->dropForeignKey('orders', 'fk_orders_users'); |
| 164 | + } |
| 165 | + } |
| 166 | + ``` |
| 167 | + |
| 168 | +- **Trigger Migration** (`20250907223603_CreateUpdateTimestampTrigger.php`): |
| 169 | + ```php |
| 170 | + <?php |
| 171 | + namespace App\Database\Migrations; |
| 172 | + use CodeIgniter\Database\Migration; |
| 173 | +
|
| 174 | + class CreateUpdateTimestampTrigger extends Migration |
| 175 | + { |
| 176 | + public function up() |
| 177 | + { |
| 178 | + $this->db->query( |
| 179 | + 'CREATE TRIGGER `ci4ms_update_timestamp` BEFORE UPDATE ON `users` FOR EACH ROW SET NEW.updated_at = NOW();' |
| 180 | + ); |
| 181 | + } |
| 182 | +
|
| 183 | + public function down() |
| 184 | + { |
| 185 | + $this->db->query('DROP TRIGGER IF EXISTS `ci4ms_update_timestamp`;'); |
| 186 | + } |
| 187 | + } |
| 188 | + ``` |
| 189 | + |
| 190 | +## Troubleshooting |
| 191 | +- **"Command Not Found" Error**: |
| 192 | + - Ensure the namespace is correctly added in `app/Config/Autoload.php`. |
| 193 | + - Run `composer dump-autoload`. |
| 194 | +- **Foreign Keys Not Parsed**: |
| 195 | + - Verify the `ALTER TABLE` format in your SQL file. The package supports: |
| 196 | + ```sql |
| 197 | + ALTER TABLE `table_name` ADD CONSTRAINT `constraint_name` FOREIGN KEY (`field`) REFERENCES `ref_table` (`ref_field`) ON DELETE SET NULL ON UPDATE NO ACTION; |
| 198 | + ALTER TABLE `table_name` ADD FOREIGN KEY (`field`) REFERENCES `ref_table` (`ref_field`); |
| 199 | + ``` |
| 200 | + - Check CLI output for debugging: |
| 201 | + ``` |
| 202 | + php spark sql2migration /path/to/database.sql |
| 203 | + ``` |
| 204 | + Look for `Debug: Found ALTER TABLE statements` and `Debug: Found foreign keys` in the output. |
| 205 | +- **Table Prefix Issues**: |
| 206 | + - Ensure `database.default.DBPrefix` is correctly set in `.env`. Example: |
| 207 | + ```env |
| 208 | + database.default.DBPrefix = ci4ms_ |
| 209 | + ``` |
| 210 | +- **Migration Errors**: |
| 211 | + - If migrations fail, roll back with `php spark migrate:rollback` and verify the SQL file. |
| 212 | + |
| 213 | +## Support and Contribution |
| 214 | +- Report issues on GitHub: [github.com/bertugfahriozer/sql2migration](https://github.com/bertugfahriozer/sql2migration) |
| 215 | +- Contribute via Pull Requests. |
| 216 | +- The package is licensed under the MIT License. |
| 217 | + |
| 218 | +## License |
| 219 | +MIT License. See the `LICENSE` file for details. |
0 commit comments