简体   繁体   English

批准数据库编辑-mysql

[英]Database Edit with Approval - mysql

I currently have a database which many users can access and make changes to. 我目前有一个可供许多用户访问和更改的数据库。 The is also a log database that stores all changes to tables within the database using triggers. 也是一个日志数据库,它使用触发器将所有更改存储到数据库中的表。

I would like to add the ability to approve edits before they are changed in the database. 我想添加在数据库中更改之前批准编辑的功能。

What would be the best way to go about this? 最好的方法是什么?

We have something similar on one of our sites, we've added a bunch of tables: 我们的一个站点上有类似的东西,我们添加了一堆表:

users sites ... etc users sites ...等

Then we have a bunch of shadow tables: 然后,我们有一堆影子表:

users-shadow sites-shadow ... etc users-shadow sites-shadow ...等

The shadow tables have identical structures to the real tables except for an added line for the user who made the change. 影子表具有与实际表相同的结构,只是为进行更改的用户增加了一行。 So first we use this query when a change is submitted by a user who needs to have his/her database actions approved: 因此,当用户需要批准其数据库操作的更改提交后,我们首先使用此查询:

REPLACE INTO users-shadow (user_mod,id,username,password,salt...) VALUES (16,50,'bob','stuff','salt'...);

Obviously, make sure this isn't open to injection, use prepared statements etc. 显然,请确保此方法不适合注入,请使用准备好的语句等。

When approved, a row in the shadow table is simply removed from the shadow table, the user_mod value dropped and changes (non-null values) inserted into the real table (or updated if an id is specified, using REPLACE syntax). 当批准,在一个行shadow表简单地从除去shadow表中, user_mod值下降和变化(非空值)插入到真实表(或是否有更新的id被指定,使用REPLACE语法)。 We do this logic in perl so sadly don't have any SQL on hand for it. 我们在perl中执行此逻辑,因此遗憾的是手头没有任何SQL。

Remember that SQL REPLACE does a DELETE and an INSERT rather than an UPDATE . 请记住,SQL REPLACE进行DELETEINSERT而不是UPDATE You will need to amend any triggers to allow for this behaviour. 您将需要修改任何触发器以允许这种行为。

Note: The reason we didn't use an 'approve' flag was that we needed the ability to amend existing records, of course we couldn't have multiple records with the same primary key. 注意:之所以不使用'approve'标志,是因为我们需要修改现有记录的能力,当然我们不能有多个具有相同主键的记录。

well i have made this system once and here is my solution for DB structure and over all algorithm: 好吧,我曾经制作过这个系统,这是我针对数据库结构和所有算法的解决方案:

there should be a sub system of admin panel which different users can manage their products but every change should be approved by administrator before going affecting the main Product table. 应该有一个管理面板子系统,不同的用户可以管理他们的产品,但是每次更改都必须得到管理员的批准,才可以影响主产品表。 there is three main table: 有三个主表:

1.Product : store products that have final approved and are used in entire system 2.Changes_versions : a table with One To Many Relation with Product Table that indicates each change version is committed by who , when ,and is approved/rejected by admin or still is in Pending state .table structure is as following : 1.Product:存储最终批准并在整个系统中使用的产品。2.Changes_versions:与产品表具有一对多关系的表,该表指示每个变更版本由谁,何时,由admin或何时批准/拒绝提交。仍处于Pending状态。表结构如下:

CREATE TABLE changes_versions(
xid int(11) unsigned NOT NULL AUTO_INCREMENT,
xcreated_date datetime DEFAULT NULL,
xupdated_date timestamp NULL DEFAULT NULL,
xversion int(11) DEFAULT NULL,
xobject_id int(11) DEFAULT NULL,
xobject_type varchar(255) DEFAULT NULL,
xstate enum('PENDING','ACCEPTED','REJECTED') DEFAULT 'PENDING',
  PRIMARY KEY (xid)
) ENGINE=InnoDB AUTO_INCREMENT=165 DEFAULT CHARSET=utf8

3.Changes : a table that have One To Many relation with Changes_versions table that keep every column change record of the main Table (here i mean product table) and by approving a change_version record by admin its related changes records will be placed in main table column. 3.Changes:一个与Changes_versions表具有一对多关系的表,该表保留主表(此处指产品表)的每列更改记录,并通过管理员批准change_version记录,其相关更改记录将被放置在主表中柱。 table structure is as following : 表结构如下:

CREATE TABLE changes(
xid int(11) unsigned NOT NULL AUTO_INCREMENT,
xcreated_date datetime DEFAULT NULL,
xcreated_by varchar(255) DEFAULT NULL,
xupdated_date timestamp NULL DEFAULT NULL,
xupdated_by varchar(255) DEFAULT NULL,
xversion_id int(11) DEFAULT NULL,
xcolumn_name varchar(255) DEFAULT NULL,
xcolumn_value varchar(255) DEFAULT NULL,
xstate enum('PENDING','ACCEPTED','REJECTED') DEFAULT 'PENDING',
xadmin_review text,
    PRIMARY KEY (xid)
) ENGINE=InnoDB AUTO_INCREMENT=764 DEFAULT CHARSET=utf8

with this system and table schema i handled to work with record changes, user fetch list of records ,if user have any Pending state change_version, system will pull its related changes records and place them in the right column in the fetched product row(temporary just for displaying) , so even if user has any pending state changes he/she can see its changes in his/her panel(not main system, only his/her panel). 使用此系统和表模式,我处理了记录更改,用户获取了记录列表,如果用户具有任何待处理状态change_version,则系统将拉出其相关的更改记录,并将其放在获取的产品行的右侧列中(临时(用于显示),因此即使用户有任何待处理的状态更改,他/她也可以在其面板(不是主系统,只有其面板)中看到其更改。

at the end if system administrator accept a user changes_version version and its related changes records ,system should place each changes table record in the right column of product table(for example i used product table, with this system you can versioning and admin approving any table).and change version record state to approved and its changes related records to approved to. 最后,如果系统管理员接受用户changes_version版本及其相关的更改记录,则系统应将每个更改表记录放置在产品表的右列中(例如,我使用了产品表,通过该系统,您可以对任何表进行版本控制和管理批准)。并将版本记录状态更改为“已批准”,并将其更改相关记录更改为“已批准”。 so with this structure you can save and versioning different tables and keep log of each version changes. 因此,通过这种结构,您可以保存和版本化不同的表,并保留每个版本更改的日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM