slogan3

Help me met het implementeren van geavanceerde CodeIgniter database migratiescripts

description

Met deze prompt kan ik veilige en efficiënte migratiescripts ontwikkelen die helpen mijn database schema en data consistent te houden tijdens updates. Het lost problemen op met handmatige migraties en helpt fouten voorkomen door geautomatiseerde en versiebeheerde aanpak. Dit verbetert de onderhoudbaarheid van mijn CodeIgniter projecten aanzienlijk.

prompt

Help me bij het ontwerpen en implementeren van geavanceerde database migratiescripts voor mijn CodeIgniter project. Beschrijf de beste praktijken voor het schrijven, beheren en uitvoeren van migratiescripts die schemawijzigingen en data- ...

try_prompt

generate_helper
disclaimerOnPageApi

ai_answers

provider: openai
model: gpt-4.1-nano
Natuurlijk! Hier zijn de beste praktijken voor het ontwerpen, beheren en uitvoeren van geavanceerde database migratiescripts in CodeIgniter 3.1.11 voor jouw project, met een voorbeeld voor het toevoegen van een nieuwe tabel 'customers': 1. Structuur en Best practices voor migratiescripts - Gebruik de Migration Class: Maak gebruik van de ingebouwde migratieklasse van CodeIgniter. - Consistente bestandsnaamgeving: Geef migratiebestanden een duidelijke naam en een datum/tijd-stempel, bijvoorbeeld: `001_create_customers_table.php`. - Versiebeheer: Houd migraties in een aparte map (`application/migrations/`) en volg een duidelijke volgorde. - Transactioneel: Als je database ondersteunt, voer migraties uit binnen transacties om partiale wijzigingen te voorkomen. - Back-up: Maak altijd een back-up van je database vóór het uitvoeren van migraties, vooral in productie. 2. Schrijven van migratiescripts - Gebruik de Schema Builder: Maak gebruik van de `dbforge` klasse van CodeIgniter om schemawijzigingen te definiëren. - Definieer duidelijk alle kolommen en constraints. - Voeg eventueel data-transformaties toe na de schemawijziging indien nodig. 3. Beheren en uitvoeren van migraties - Gebruik de CLI of via PHP: Voer migraties uit met `php index.php migrate` of via de CLI. - Test migraties op staging-omgeving alvorens in productie uit te voeren. - Documenteer alle migraties en wijzigingen. 4. Voorbeeld migratie voor het toevoegen van 'customers' tabel: ```php <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Migration_Create_customers_table extends CI_Migration { public function up() { // Definieer de tabelstructuur $this->load->dbforge(); $fields = array( 'id' => array( 'type' => 'INT', 'unsigned' => TRUE, 'auto_increment' => TRUE ), 'name' => array( 'type' => 'VARCHAR', 'constraint' => '255', 'null' => FALSE ), 'email' => array( 'type' => 'VARCHAR', 'constraint' => '255', 'null' => FALSE, 'unique' => TRUE ), 'phone_number' => array( 'type' => 'VARCHAR', 'constraint' => '20', 'null' => TRUE ), 'created_at' => array( 'type' => 'TIMESTAMP', 'default' => 'CURRENT_TIMESTAMP' ) ); $this->dbforge->add_field($fields); $this->dbforge->add_key('id', TRUE); // Primaire sleutel $this->dbforge->create_table('customers', TRUE); } public function down() { // Verwijder de tabel bij rollback $this->dbforge->drop_table('customers', TRUE); } } ``` 5. Voer de migratie uit: - Zorg dat migraties zijn ingeschakeld in `application/config/migration.php` (set `$config['migration_enabled'] = TRUE;`) - Voer de migratie uit via CLI: ```bash php index.php migrate ``` 6. Extra tips: - Gebruik seeders voor testgegevens. - Maak gebruik van rollback scripts (`down()`) voor veilige reversies. - Documenteer elke migratie en wijziging voor teamleden. - Overweeg het gebruik van conditionele migraties voor bestaande gegevens. Door deze best practices te volgen, zorg je voor veilige, efficiënte en onderhoudbare database migraties binnen je CodeIgniter project.
provider: deepseek
model: deepseek-chat
Hier is een complete handleiding voor geavanceerde database migraties in CodeIgniter 3.1.11: ## 1. Migratie Configuratie Eerst activeer je migraties in `application/config/migration.php`: ```php <?php defined('BASEPATH') OR exit('No direct script access allowed'); $config['migration_enabled'] = TRUE; $config['migration_type'] = 'timestamp'; $config['migration_table'] = 'migrations'; $config['migration_auto_latest'] = FALSE; $config['migration_version'] = 0; $config['migration_path'] = APPPATH . 'migrations/'; ``` ## 2. Migratie Script Structuur Maak een nieuwe migratie aan via CLI: ```bash php index.php migrate create Add_customers_table ``` ## 3. Geavanceerde Migratie Script `application/migrations/20231020120000_Add_customers_table.php`: ```php <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Migration_Add_customers_table extends CI_Migration { public function up() { // Transaction start voor data integriteit $this->db->trans_start(); // Nieuwe tabel aanmaken $this->dbforge->add_field([ 'id' => [ 'type' => 'INT', 'constraint' => 11, 'unsigned' => TRUE, 'auto_increment' => TRUE ], 'name' => [ 'type' => 'VARCHAR', 'constraint' => '100', 'null' => FALSE ], 'email' => [ 'type' => 'VARCHAR', 'constraint' => '100', 'null' => FALSE, 'unique' => TRUE ], 'phone' => [ 'type' => 'VARCHAR', 'constraint' => '20', 'null' => TRUE ], 'created_at' => [ 'type' => 'TIMESTAMP', 'default' => 'CURRENT_TIMESTAMP' ], 'updated_at' => [ 'type' => 'TIMESTAMP', 'default' => 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP' ] ]); $this->dbforge->add_key('id', TRUE); $this->dbforge->add_key('email'); $this->dbforge->create_table('customers', TRUE); // Optionele initiële data $seed_data = [ [ 'name' => 'Test Klant', 'email' => 'test@voorbeeld.nl', 'phone' => '0612345678' ] ]; $this->db->insert_batch('customers', $seed_data); // Transaction complete $this->db->trans_complete(); if ($this->db->trans_status() === FALSE) { log_message('error', 'Migratie Add_customers_table failed'); throw new Exception('Migratie mislukt'); } } public function down() { // Rollback procedure $this->db->trans_start(); // Tabel verwijderen $this->dbforge->drop_table('customers', TRUE); $this->db->trans_complete(); } } ``` ## 4. Beste Praktijken ### Veiligheid & Robustheid ```php // Always use transactions $this->db->trans_start(); // Je migratie code hier $this->db->trans_complete(); // Error handling if ($this->db->trans_status() === FALSE) { log_message('error', 'Migratie failed: ' . get_class($this)); return FALSE; } ``` ### Data Validatie ```php // Voor data transformaties private function _validate_email($email) { if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { throw new Exception("Ongeldig email format: {$email}"); } return $email; } ``` ### Index Management ```php // Indexen toevoegen voor performance $this->db->query('CREATE INDEX idx_customers_email ON customers(email)'); $this->db->query('CREATE INDEX idx_customers_created ON customers(created_at)'); ``` ## 5. Migratie Uitvoering ```bash # Laatste migratie uitvoeren php index.php migrate # Naar specifieke versie php index.php migrate version 20231020120000 # Rollback laatste migratie php index.php migrate rollback # Status checken php index.php migrate current ``` ## 6. Geavanceerde Features ### Batch Verwerking voor Grote Datasets ```php public function up() { $batch_size = 1000; $offset = 0; do { $this->db->trans_start(); // Batch verwerking code $users = $this->db->get('users', $batch_size, $offset)->result(); foreach ($users as $user) { // Transformatie logica } $this->db->trans_complete(); $offset += $batch_size; } while (count($users) === $batch_size); } ``` ### Environment-specifieke Migraties ```php if (ENVIRONMENT === 'production') { // Production-safe migratie $this->db->query('ALTER TABLE customers ADD COLUMN backup_email VARCHAR(100) NULL'); } else { // Development-only changes $this->dbforge->add_column('customers', [ 'test_field' => ['type' => 'VARCHAR', 'constraint' => 50] ]); } ``` ## 7. Monitoring en Logging Voeg logging toe aan je migraties: ```php log_message('info', 'Starting migration: ' . get_class($this)); // Je migratie code log_message('info', 'Migration completed successfully: ' . get_class($this)); ``` Deze aanpak zorgt voor veilige, efficiënte en onderhoudbare database migraties in je CodeIgniter project.