onlyphp/codeigniter3-housekeeping 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

onlyphp/codeigniter3-housekeeping

Composer 安装命令:

composer require onlyphp/codeigniter3-housekeeping

包简介

A powerful and flexible database archiving solution for CodeIgniter 3 applications. This package helps you manage database growth by providing tools to archive and purge data while maintaining data integrity.

README 文档

README

Latest Version on Packagist Software License Total Downloads

A powerful and flexible database archiving solution for CodeIgniter 3 applications. This package helps you manage database growth by providing tools to archive and purge data while maintaining data integrity.

⚠️ Warning

DO NOT USE THIS PACKAGE IN PRODUCTION

This package is under active development and may contain critical bugs. It is primarily intended for personal use and testing. The current version has not undergone rigorous testing and may be unstable.

🚀 Features

  • ✨ Backup and purge operations with transaction support
  • 🔄 Parallel processing support for faster execution
  • 📊 Progress tracking and integrity verification
  • 📝 Detailed logging with customizable paths
  • 🎯 Chunk-based processing to manage memory usage
  • 🛡️ Support for unique constraints
  • 🔌 Multiple database driver support (MySQL, Oracle)
  • 🔍 Real-time progress monitoring
  • 📈 Performance optimization options

💻 Requirements

  • PHP >= 7.4
  • CodeIgniter 3.x
  • MySQL 5.7+ or Oracle 11g+
  • PHP PCNTL extension (for parallel processing)
  • Composer

📦 Installation

Via Composer

composer require onlyphp/codeigniter3-housekeeping

Manual Installation

  1. Download the package
  2. Extract to your application/libraries folder
  3. Add to your autoload.php:
$autoload['libraries'] = array('DatabaseArchiver');

🎓 Basic Concepts

Operation Modes

  • Backup Only (BO): Only copies data to archive table
  • Purge Only (PO): Only deletes data from source table
  • Backup & Purge (BP): Copies data then deletes from source

Processing Methods

  • Sequential: Single-threaded processing
  • Parallel: Multi-threaded processing (Unix/Linux only)

🎯 Usage Examples

Example 1: Basic Backup Operation

Archive records older than 6 months from the 'orders' table:

$archiver = new DatabaseArchiver();
$result = $archiver
    ->driver('mysql')
    ->backupFrom('orders')
    ->primaryKey('order_id')
    ->condition('created_at < DATE_SUB(NOW(), INTERVAL 6 MONTH)')
    ->mode('BO')  // Backup Only
    ->run();

print_r($result);

Example 2: Backup and Purge with Unique Constraints

Archive and delete completed orders while ensuring no duplicate order numbers:

$archiver = new DatabaseArchiver();
$result = $archiver
    ->driver('mysql')
    ->backupFrom('orders')
    ->primaryKey('order_id')
    ->uniqueColumns(['order_number'])
    ->condition('status = "completed" AND created_at < DATE_SUB(NOW(), INTERVAL 1 YEAR)')
    ->mode('BP')  // Backup and Purge
    ->chunk(500)  // Process 500 records at a time
    ->run();

// Verify archive integrity
$verification = $archiver->verifyArchive();
print_r($verification);

Example 3: Parallel Processing with Custom Archive Table

Archive large datasets using parallel processing:

$archiver = new DatabaseArchiver();
$result = $archiver
    ->driver('mysql')
    ->backupFrom('transactions')
    ->backupTo('transactions_archive_2023')
    ->primaryKey('transaction_id')
    ->condition('YEAR(transaction_date) = 2023')
    ->mode('BO')
    ->parallel(4)  // Use 4 parallel processes
    ->chunk(1000)
    ->run();

// Monitor progress
$progress = $archiver->getProgress();
print_r($progress);

Example 4: Oracle Database with Debug Mode

Archive data from an Oracle database with debug logging:

$archiver = new DatabaseArchiver();
$result = $archiver
    ->driver('oci')
    ->backupFrom('EMPLOYEES')
    ->primaryKey('EMPLOYEE_ID')
    ->condition('TERMINATION_DATE IS NOT NULL')
    ->mode('BP')
    ->onDebug()  // Enable debug mode
    ->logPath('/custom/path/archive.log')
    ->sqlHint('/*+ PARALLEL(4) */')  // Oracle-specific hint
    ->run();

Example 5: Purge-Only Operation with Memory Management

Delete old log entries without backing them up:

$archiver = new DatabaseArchiver();
$result = $archiver
    ->driver('mysql')
    ->backupFrom('system_logs')
    ->primaryKey('log_id')
    ->condition('created_at < DATE_SUB(NOW(), INTERVAL 3 MONTH)')
    ->mode('PO')  // Purge Only
    ->chunk(200)  // Smaller chunks to manage memory
    ->run();

Example 6: Interactive Archiving Process with Progress Monitoring

This example shows a complete archiving process with real-time progress monitoring and verification:

class ArchiveManager {
    private $archiver;
    private $maxAttempts = 3;
    private $progressCheckInterval = 5; // seconds

    public function __construct() {
        $this->archiver = new DatabaseArchiver();
    }

    public function runArchiveProcess() {
        try {
            // Configure the archiver
            $this->archiver
                ->driver('mysql')
                ->backupFrom('orders')
                ->primaryKey('order_id')
                ->condition('created_at < DATE_SUB(NOW(), INTERVAL 6 MONTH)')
                ->mode('BP')
                ->chunk(1000)
                ->onDebug();

            // Start the archiving process
            $processId = uniqid('archive_');
            log_message('info', "Starting archive process: {$processId}");

            // Run the archiving process
            $result = $this->archiver->run();
            
            // Monitor progress until completion
            $this->monitorProgress();

            // Verify the archive
            $verification = $this->verifyArchiveWithRetry();

            return [
                'process_id' => $processId,
                'archive_result' => $result,
                'verification' => $verification
            ];

        } catch (Exception $e) {
            log_message('error', "Archive process failed: " . $e->getMessage());
            throw $e;
        }
    }

    private function monitorProgress() {
        $completed = false;
        $lastPercentage = 0;

        while (!$completed) {
            $progress = $this->archiver->getProgress();
            
            if ($progress['percentage_complete'] != $lastPercentage) {
                $this->displayProgress($progress);
                $lastPercentage = $progress['percentage_complete'];
            }

            if ($progress['percentage_complete'] >= 100) {
                $completed = true;
                echo "\nArchiving process completed!\n";
            } else {
                sleep($this->progressCheckInterval);
            }
        }
    }

    private function displayProgress($progress) {
        $barLength = 50;
        $completed = round(($progress['percentage_complete'] * $barLength) / 100);
        $remaining = $barLength - $completed;
        
        $progressBar = "[" . 
            str_repeat("=", $completed) . 
            ">" . 
            str_repeat(" ", $remaining) . 
            "]";

        echo sprintf(
            "\rProgress: %s %d%% (%d/%d records)", 
            $progressBar,
            $progress['percentage_complete'],
            $progress['archived_records'],
            $progress['total_records']
        );
    }

    private function verifyArchiveWithRetry() {
        $attempts = 0;
        $success = false;
        $lastError = null;

        while ($attempts < $this->maxAttempts && !$success) {
            try {
                $verification = $this->archiver->verifyArchive();
                
                if ($verification['integrity_status'] === 'ok_complete') {
                    return $verification;
                }
                
                // Handle different verification statuses
                switch ($verification['integrity_status']) {
                    case 'warning_duplicates_found':
                        throw new Exception("Duplicate records found in archive");
                    case 'warning_incomplete_backup':
                        throw new Exception("Incomplete backup detected");
                    case 'warning_count_mismatch':
                        throw new Exception("Record count mismatch");
                }

                $success = true;
                return $verification;

            } catch (Exception $e) {
                $lastError = $e;
                $attempts++;
                if ($attempts < $this->maxAttempts) {
                    sleep(5); // Wait before retry
                }
            }
        }

        if (!$success) {
            throw new Exception(
                "Archive verification failed after {$this->maxAttempts} attempts. " .
                "Last error: " . $lastError->getMessage()
            );
        }
    }
}

// Usage example:
try {
    $manager = new ArchiveManager();
    $result = $manager->runArchiveProcess();
    
    echo "\n\nArchive Process Summary:\n";
    echo "Process ID: {$result['process_id']}\n";
    echo "Records Processed: {$result['archive_result']['processed']}\n";
    echo "Execution Time: {$result['archive_result']['execution_time']}\n";
    echo "Memory Usage: {$result['archive_result']['memory']['used']}\n";
    echo "Verification Status: {$result['verification']['integrity_status']}\n";
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}

⚙️ Configuration Options

Method Description
driver(string) Set database driver ('mysql' or 'oci')
backupFrom(string) Set source table name
backupTo(string) Set custom archive table name
primaryKey(string) Set primary key column
condition(string) Set WHERE clause for record selection
mode(string) Set operation mode ('BO', 'PO', or 'BP')
chunk(int) Set chunk size (100-50000)
parallel(int) Set number of parallel threads (1-16)
uniqueColumns(array) Set columns that should be unique in archive
sqlHint(string) Set SQL optimization hint
logPath(string) Set custom log file path
onDebug() Enable debug mode

🔍 Monitoring and Verification

The package provides methods to monitor and verify the archiving process:

// Get current progress
$progress = $archiver->getProgress();

// Verify archive integrity
$verification = $archiver->verifyArchive();

⚠️ Important Notes

  1. Always backup your database before running archiving operations
  2. Test the archiving process on a subset of data first
  3. Monitor system resources during parallel processing
  4. Consider database load and peak hours when scheduling archiving tasks
  5. Verify archive integrity after completion

📄 License

The MIT License (MIT). Please see License File for more information.

onlyphp/codeigniter3-housekeeping 适用场景与选型建议

onlyphp/codeigniter3-housekeeping 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 02 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「mysql」 「codeigniter」 「oracle」 「codeigniter3」 「housekeeping」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 onlyphp/codeigniter3-housekeeping 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 onlyphp/codeigniter3-housekeeping 我们能提供哪些服务?
定制开发 / 二次开发

基于 onlyphp/codeigniter3-housekeeping 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 32
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-08