ringlesoft/db-archive
Composer 安装命令:
composer require ringlesoft/db-archive
包简介
A Laravel package for archiving old database entries automatically
README 文档
README
Easily archive your Laravel database tables periodically to keep your application database lean and performant.
Introduction
Laravel DB Archive is a package that provides a simple and efficient way to archive old records from your database tables in Laravel applications. It helps maintain your application's database performance by moving historical data to archive tables, while keeping your primary tables focused on recent and relevant information.
Laravel 10.x and above
Installation
You can install the package via composer:
composer require ringlesoft/db-archive
Configuration
Publish the configuration file with:
php artisan vendor:publish --provider="RingleSoft\DbArchive\DbArchiveServiceProvider" --tag="config"
Configuration Options:
connection:
- The database connection name to be used for creating archive tables and moving data.
- Ensure this connection is defined in your
config/database.phpfile. - I recommend using a different connection from your application's default connection.
- Defaults to
mysql_archiveand can be overridden using theARCHIVE_DB_CONNECTIONenvironment variable.
settings:
-
table_prefix:- Prefix to be added to the archived tables (e.g., archive_).
- Set to
nullfor no prefix.
-
batch_size:- Number of records to process in each batch during archiving.
- Adjust this value based on your server resources and table size.
- Defaults to 1000.
-
date_column:- The database column used to determine the age of records for archiving (e.g.,
created_at,updated_at). - Defaults to
created_at.
- The database column used to determine the age of records for archiving (e.g.,
-
archive_older_than_days:- Number of days after which records are considered old enough to be archived.
- Records with a date in the
date_columnolder than this value will be archived. - Defaults to
365days .
-
conditions:- An array of additional where conditions to filter records for archiving.
- Allows for more specific criteria for selecting records to archive.
- Defaults to an empty array
[]. - Example:
[['status', 'active']]or[['id', '<=', 100]]
enable_logging:
- Boolean value to enable or disable logging of the archiving process.
- Logs are stored in the default Laravel log file.
- Defaults to true.
notifications:
email:- Email address to receive notifications about the archiving process (success or failure).
- Set to null to disable email notifications.
- Defaults to
admin@example.com. - This only works if batching is enabled (for now).
tables:
- An array defining the tables to be archived.
- User plain table names array for default settings or associative array for specific settings.
Setting Up
To setup the package, run the following command:
php artisan db-archive:setup
This will create the backup database and tables if not already present.
Queueing and Batching
- This package supports job queuing and job batches.
- To use jobs and batches, make sure you have both jobs and batches enabled in your application.
Jobs Table
php artisan queue:table php artisan migrate
Batches table
php artisan queue:batches-table php artisan migrate
Usage
Artisan Command
Run the archive command to process tables defined in your configuration:
php artisan db-archive:archive
This command will:
- Check Configuration: Load the
db-archive.phpconfiguration file. - Process Tables: Iterate through the tables defined in the tables array.
- Archive Records: Move records from the original table to the archive table based on the configured settings (age, conditions, etc.).
- Logging and Notifications: Log the archiving process and send notifications if enabled.
Scheduling
To automate the archiving process, schedule the db-archive:archive command in your Kernel.php file:
// app/Console/Kernel.php
protected function schedule(Schedule $schedule) { $schedule->command('db-archive:archive')->dailyAt('01:00'); // Run daily at 1:00 AM }
Adjust the scheduling as per your requirements (e.g., daily, weekly, monthly).
Examples
Basic Usage
(Archive orders table with default settings)
// config/db-archive.php
'connection' => 'mysql_archive', ... 'tables' => [ 'orders', 'comments' ],
This configuration will archive records from the users table in your default connection that are older than 365 days (based on the created_at column) to users table in the mysql_archive connection.
Custom Settings
// config/db-archive.php
'connection' => 'mysql_archive', ... 'tables' => [ 'orders' => [ 'archive_older_than_days' => 90, // Archive orders older than 90 days 'date_column' => 'order_date', // Use 'order_date' column for age check 'batch_size' => 5000, // Process in batches of 5000 'conditions' => [ // Additional conditions ['status', '=', 'completed'], ], ], ];
This configuration will archive records from the orders table that are older than 90 days (based on the order_date column), processed in batches of 5000, and only for orders with a status of 'completed'.
Enjoy!
Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues to suggest improvements or report bugs.
License
The Laravel DB Archive package is open-sourced software licensed under the MIT license.
Support
Contacts
Follow me on X: @ringunger
Email me: ringunger@gmail.com
Website: https://ringlesoft.com
ringlesoft/db-archive 适用场景与选型建议
ringlesoft/db-archive 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 538 次下载、GitHub Stars 达 4, 最近一次更新时间为 2025 年 03 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「backup」 「laravel」 「archive」 「old」 「ringlesoft」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ringlesoft/db-archive 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ringlesoft/db-archive 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ringlesoft/db-archive 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
A SDK for working with B2 cloud storage.
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A sleek PHP wrapper around rclone with Laravel-style fluent API syntax
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 538
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-08