mvf-tech/db-data-eraser

在30天后截断数据库中的数据行。

1.0.6 2021-02-11 11:16 UTC

This package is auto-updated.

Last update: 2024-09-11 23:08:41 UTC


README

一个用于在30天后截断数据库数据行的包。

版本

  • 0.0.3 - 使用此版本用于PHP 5。

安装和设置

  1. 使用上述首选版本运行 composer require mvf-tech/db-data-eraser 来安装包。

  2. 您需要使用配置文件来存储数据库连接细节和表格信息,以便包可以使用,例如:

return [
    'connection' => [
        'host' => 'localhost',
        'database' => 'database',
        'username' => 'root',
        'password' =>'password',
    ],
    'tables' => [
        [
            'name' => 'table_name_one',             // name of table containing the rows to delete
            'datetime_column' => 'created_at',      // name of column in the table that contains the datetime
            'max_age' => '-30 days',                // the max age at which we want the data to be deleted
            'date_format' => 'Y-m-d H:i:s',         // (optional) the default value is 'Y-m-d H:i:s'
            'limit' => 1000,                        // (optional) the default value is 1000
            'order_by' => 'created_at',             // (optional) the default value is what is entered for the 'datetime_column'
        ],
        [
            'name' => 'table_name_two',
            'datetime_column' => 'created_at',
            'max_age' => '-30 days',
        ],
    ],
];

基本使用

use MVF\DataEraser\DataEraser; 

$dbEraser = new DataEraser($config);    // use the config file you created above

创建一个新的 "DataEraser" 实例。这将使用配置文件中定义的连接细节连接到数据库。

$dbEraser->run();

然后您可以使用 run() 函数,它将遍历配置文件中列出的表格并删除超过 max_age 指定时间的行。

$dbEraser->getResultsArray();

为了日志记录,有一个 $results 数组,可以通过 getResultsArray() 函数访问。

$resultsArray = [
    'log_messages' => [
        'success' => true
    ],
    'eagle_queue' => [
        'success' => true
    ],
    'lead_duplicate' => [
        'success' => false
    ]
];

运行 $dbEraser->run() 后,这将返回一个包含表名和成功代码的结果数组,其中 1 表示删除成功,0 表示删除失败。

在版本 1.0.6 中增加了日志记录,以捕获包含 NULL 时间戳的任何行

$dbEraser->getRowsWithNullTimestampArray();

运行 $dbEraser->run() 后,此函数将返回一个包含表名和包含 NULL 时间戳的行总数的数组(如果有的话)。

$rowsWithNullTimestamps = [
    'log_messages' => [
        'number_of_rows_with_null_timestamps' => 1
    ],
    'eagle_queue' => [
        'number_of_rows_with_null_timestamps' => 3
    ],
];