- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
假设我想将用户和组存储在 MySQL 数据库中。他们有一个关系 n:m。为了跟踪所有更改,每个表都有一个审计表 user_journal、group_journal 和 user_group_journal。 MySQL 触发器在每次 INSERT 或 UPDATE 时将当前记录复制到日志表(不支持 DELETES,因为我需要应用程序用户删除记录的信息——所以有一个标记 active
将被设置为 0
而不是删除)。
我的问题是:假设我一次将 10 个用户添加到一个组中。当我稍后在应用程序的用户界面中单击该组的历史记录时,我希望将这 10 个用户的添加视为一步,而不是 10 个独立的步骤。 是否有一个很好的解决方案来将这些更改组合在一起?也许可以有一个计数器,每次触发器被...触发时都会递增?我从未使用过触发器。
最好的解决方案是将在事务中所做的所有更改放在一起。因此,当用户一步更新组名并添加 10 个用户(一次表单 Controller 调用)时,这将是历史记录中的一步。也许可以定义一个随机散列或在每次事务开始时递增一个全局计数器并在触发器中访问该值?
我不想让表设计比为每个“真实”表设计一个日志表更复杂。我不想在每个数据库表中添加事务哈希(意思是“真实”表,而不是审计表——当然可以)。此外,我希望在数据库中有一个解决方案——而不是在应用程序中。
最佳答案
我试了一下,现在我找到了一个很好的解决方案:
# First of all I create the database and the basic table:
DROP DATABASE `mytest`;
CREATE DATABASE `mytest`;
USE `mytest`;
CREATE TABLE `test` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`something` VARCHAR(255) NOT NULL
);
# Then I add an audit table to the database:
CREATE TABLE `audit_trail_test` (
`_id` INT PRIMARY KEY AUTO_INCREMENT,
`_revision_id` VARCHAR(255) NOT NULL,
`id` INT NOT NULL,
`something` VARCHAR(255) NOT NULL
);
# I added a field _revision_id to it. This is
# the ID that groups together all changes a
# user made within a request of that web
# application (written in PHP). So we need a
# third table to store the time and the user
# that made the changes of that revision:
CREATE TABLE `audit_trail_revisions` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`user_id` INT NOT NULL,
`time` DATETIME NOT NULL
);
# Now we need a procedure that creates a
# record in the revisions table each time an
# insert or update trigger will be called.
DELIMITER $$
CREATE PROCEDURE create_revision_record()
BEGIN
IF @revision_id IS NULL THEN
INSERT INTO `audit_trail_revisions`
(user_id, `time`)
VALUES
(@user_id, @time);
SET @revision_id = LAST_INSERT_ID();
END IF;
END;
# It checks if a user defined variable
# @revision_id is set and if not it creates
# the row and stores the generated ID (auto
# increment) into that variable.
#
# Next I wrote the two triggers:
CREATE TRIGGER `test_insert` AFTER INSERT ON `test`
FOR EACH ROW BEGIN
CALL create_revision_record();
INSERT INTO `audit_trail_test`
(
id,
something,
_revision_id
)
VALUES
(
NEW.id,
NEW.something,
@revision_id
);
END;
$$
CREATE TRIGGER `test_update` AFTER UPDATE ON `test`
FOR EACH ROW BEGIN
CALL create_revision_record();
INSERT INTO `audit_trail_test`
(
id,
something,
_revision_id
)
VALUES
(
NEW.id,
NEW.something,
@revision_id
);
END;
$$
$iUserId = 42;
$Database = new \mysqli('localhost', 'root', 'root', 'mytest');
if (!$Database->query('SET @user_id = ' . $iUserId . ', @time = NOW()'))
die($Database->error);
if (!$Database->query('INSERT INTO `test` VALUES (NULL, "foo")'))
die($Database->error);
if (!$Database->query('UPDATE `test` SET `something` = "bar"'))
die($Database->error);
// To simulate a second request we close the connection,
// sleep 2 seconds and create a second connection.
$Database->close();
sleep(2);
$Database = new \mysqli('localhost', 'root', 'root', 'mytest');
if (!$Database->query('SET @user_id = ' . $iUserId . ', @time = NOW()'))
die($Database->error);
if (!$Database->query('UPDATE `test` SET `something` = "baz"'))
die($Database->error);
mysql> select * from test;
+----+-----------+
| id | something |
+----+-----------+
| 1 | baz |
+----+-----------+
1 row in set (0.00 sec)
mysql> select * from audit_trail_test;
+-----+--------------+----+-----------+
| _id | _revision_id | id | something |
+-----+--------------+----+-----------+
| 1 | 1 | 1 | foo |
| 2 | 1 | 1 | bar |
| 3 | 2 | 1 | baz |
+-----+--------------+----+-----------+
3 rows in set (0.00 sec)
mysql> select * from audit_trail_revisions;
+----+---------+---------------------+
| id | user_id | time |
+----+---------+---------------------+
| 1 | 42 | 2013-02-03 17:13:20 |
| 2 | 42 | 2013-02-03 17:13:22 |
+----+---------+---------------------+
2 rows in set (0.00 sec)
如果我遗漏了一点,请告诉我。我将不得不向审计表中添加一个 action
列,以便能够记录删除。
关于mysql - 数据变更历史与审计表 : Grouping changes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14112314/
在我正在开发的应用程序中,我使用 spring、hibernate 和 envers 进行审计。 envers 适用于 hibernateTemplate.insert、hibernateTempla
我可以审核 Oracle 约束违规吗? 我在表中创建了一个约束,我想在某些进程违反此约束时对其进行审核。这可能吗?我该怎么做? 我正在使用 Oracle 11g。 最佳答案 作为选项之一,您可以创建架
我正在使用 IPostUpdateEventListener现在进行更新审计日志的接口(interface),获取旧值和新值,然后将每个更新的字段存储在“审计”表中以及所有这些内容。效果很好,但是我很
我目前正在尝试对公司 Web 应用程序的 UI 进行性能调整。该应用程序只会被员工访问,因此服务器和客户端之间的连接速度将始终比在互联网上快得多。 我一直在使用 Y Slow 等性能审计工具!和谷歌浏
如何获取 MySQL 中特定数据库的审核日志文件。由于数据库位于服务器中,我没有任何管理员权限。但我需要审核日志文件。是否有查询或方法来获取所有审核文件 最佳答案 据我所知,无法通过 sql 检索审核
我正在处理一个非常大的 PHP 代码库(主要是程序代码库),该代码库是在过去 10 年中由我之前的开发人员拼凑而成的,现在我们正承受着沉重的数据库负载。查看一些文件会发现执行了 40 到 60 个以上
我想知道是否有人成功地使用 Hibernate Envers 审核了 native 查询 (SQL)?我知道这可能是错误的,但它会节省我很多重构时间。 干杯 尼克 最佳答案 我只想在这里留下我的想法,
Python 3.8 引入了 PEP 578 -- Python Runtime Audit Hooks它 promise “使 Python 运行时采取的操作对审计工具可见。” 此 pep 提供了一
什么是适合以下的数据库?我对你在非关系领域的经历特别感兴趣 NoSQL系统。 它们对这种用法有好处吗,您使用过并推荐哪种系统,还是应该使用普通的关系数据库 (DB2)? 我需要从一堆来源收集审计跟踪/
据我了解,安全事件日志无法复制到 WAD 诊断存储,并且 SQL Server 审核不适用于 SQL Azure。 您提出了哪些解决方案来审核对 SQL Azure 数据库的访问? 至少应该是登录名、
我们正在使用 kafka 构建异常管理工具。将有源连接器 - 它将从物理文件中提取记录。另一方面,将会有接收器连接(mongodb-sinkconnect),它将从主题中拉取记录并将其推送到 mong
我试图实现一种跟踪数据更改并为我的应用程序创建历史日志的方法。因为我正在使用 EclipseLink,所以应该很容易并且有可能获得像他们这样的更改 write on the EclipseLink F
我有一个对象,在将它发送到 Spring Cloud Stream 之前我试图对其进行审核,当我将对象作为 application/json 发送时出现问题,因此拦截器将对象作为序列化接收字符串。是否
是否有审计工具可以检查 Linux 上打开和关闭哪些端口以及何时打开和关闭?我的目标是运行我的应用程序并检查其端口使用情况。lsof 或 netstat 不适合,因为它们只是告诉我当前打开了哪些端口,
TLDR:是否可以利用npm audit的漏洞检测能力?作为一种 Restful 服务而不是当前的 CLI 实现? npm针对 Node 安全平台 (NSP) 漏洞数据库,针对每个安装请求提供自动漏洞
背景我有一个生产 SQL Server 2005 服务器,4 个不同的应用程序连接到该服务器并进行更改。没有外键,在某些情况下也没有主键。不幸的是,放弃整个事情并从头开始不是一种选择。所以我的解决方案
我有项目要维护,使用 node-sass npm 模块。 从 node 10.x 开始,有一个工具(称为 npm audit)会在我们每次执行 npm install 时运行。这似乎是防止漏洞问题的好
我刚刚为我们的开发团队设置了一个 Phabricator 服务器,我已经很喜欢了!首先,我们仅将其用于代码审查。 UI 在长度上远远优于 GitHub 的评论。 无论如何,因为我想在我们的团队中简化它
作为新 PCI-DSS 服务器部署的一部分,我正在配置一个完全可审计的 NTP 时间更改历史记录。一切都按预期工作,但是我现在看到每一秒都写入与时间更改操作相关的审计日志。经过大量搜索后,我仍然无法理
我有一个带有数据的表格。任何更改或插入,这些数据都应该在两个不同的表中更新,例如一个表中的姓名、薪水和另一个表中的地址、邮件 ID。 像上面的例子一样,我在两个表中都有几列。 现在我想审核表格。所以我
我是一名优秀的程序员,十分优秀!