Files
codecanyon-rest-api-for-per…/api/migration_template.php
Oussama Douhou 23cc6629f1 license
2025-10-29 19:20:51 +01:00

146 lines
4.2 KiB
PHP

<?php
/**
* Migration Template for API Module
*
* Copy this file and rename it to: {version}_version_{version}.php
* Example: 212_version_212.php
*
* Replace {VERSION} with the actual version number
* Replace {DESCRIPTION} with a brief description of what this migration does
*/
defined('BASEPATH') or exit('No direct script access allowed');
class Migration_Version_{VERSION} extends App_module_migration
{
/** @var CI_DB_query_builder */
protected $db;
public function __construct()
{
parent::__construct();
// Properly initialize the database once the migration is constructed
$CI = &get_instance();
$CI->load->database();
$this->db = $CI->db;
}
public function up()
{
// {DESCRIPTION}
// Add your database upgrade logic here
// Example: Create a new table
/*
$this->db->query("
CREATE TABLE IF NOT EXISTS `" . db_prefix() . "api_custom_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`description` text,
`created_at` datetime NOT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
*/
// Example: Add a new column to existing table
/*
if (!$this->db->field_exists('new_column', db_prefix() . 'existing_table')) {
$this->db->query("
ALTER TABLE `" . db_prefix() . "existing_table`
ADD `new_column` varchar(255) NULL AFTER `existing_column`
");
}
*/
// Example: Create an index
/*
$this->db->query("
ALTER TABLE `" . db_prefix() . "api_usage_logs`
ADD INDEX `idx_api_logs_user` (`user_id`, `created_at`)
");
*/
// Example: Insert default data
/*
$this->db->insert(db_prefix() . 'api_settings', [
'setting_name' => 'new_feature_enabled',
'setting_value' => '1',
'created_at' => date('Y-m-d H:i:s')
]);
*/
}
public function down()
{
// Reverse the changes made in up()
// This method should undo everything that up() did
// Example: Drop a table
/*
$this->db->query("DROP TABLE IF EXISTS `" . db_prefix() . "api_custom_table`");
*/
// Example: Remove a column
/*
if ($this->db->field_exists('new_column', db_prefix() . 'existing_table')) {
$this->db->query("
ALTER TABLE `" . db_prefix() . "existing_table`
DROP COLUMN `new_column`
");
}
*/
// Example: Drop an index
/*
$this->db->query("
ALTER TABLE `" . db_prefix() . "api_usage_logs`
DROP INDEX `idx_api_logs_user`
");
*/
// Example: Remove inserted data
/*
$this->db->delete(db_prefix() . 'api_settings', ['setting_name' => 'new_feature_enabled']);
*/
}
}
/*
MIGRATION BEST PRACTICES:
1. Always check if fields/tables exist before creating/altering
2. Use db_prefix() for all table names
3. Include both up() and down() methods
4. Test migrations on a copy of production data first
5. Keep migrations atomic (each migration does one thing)
6. Document what each migration does in comments
7. Use transactions for complex operations when possible
VERSION NUMBERING:
- Use incremental version numbers (101, 102, 103, etc.)
- Major version jumps (200, 201) for significant changes
- Keep version numbers unique across all migrations
TESTING MIGRATIONS:
1. Backup your database before running migrations
2. Test on development environment first
3. Run migrations using: php migration_runner.php
4. Verify data integrity after migration
5. Test rollback functionality: php migration_runner.php --rollback
EXAMPLE USAGE:
# Run all pending migrations
php migration_runner.php
# Run migrations up to specific version
php migration_runner.php --target=205
# Rollback to specific version
php migration_runner.php --rollback --target=200
# Rollback one version
php migration_runner.php --rollback
*/