Text edit by users and saving the previous versions - versioning

Hi I'm studying a solution to apply in a web project and I would like to have your opinion.
I'm going to give to my users the possibility to edit some parts of many pages.
The data will be save in a database, I'm wondering what is the best option to maintain the previous version.
Do you think I have to save them in a different table of database?
What is the best option even thinking the worst (or best it depends from the point of views) case scenario where many people will contribute?
Thanks for any advice.

Here is the basic concept that WikiMedia (Wikipedia) uses to track user changes. You may be able to apply this to your project.
There is a page table. This has a title, some other meta data about the page, and a revision id. The revision id references a revision table. The revision table has information about the revision, like the user id and comments about the update. It also references a text table. The text table has the actual text of the page (as edited by the user).

Related

Store about and contacts in database

I want to know if it would make sense to store the website about text and contacts in a database instead of just writing it in the html, and how would I do that?
I've been thinking just one table for each with the information and no id (since I only want to store one version of everything), but that doesn't make a lot of sense, I think.
The about table should contain the text and the contacts should contain an address, coordinates, email and phone number.
Any suggestions?
The question might get flagged as off topic, but I have some thoughts. I would only put stuff in a database that would be updated frequently. Simply putting text in a DB that never really changes doesn't get you any benefit and just adds complexity and burden on your DB. If you find yourself copying the same web text to different pages of your site, make it text you include.
User information like logins,etc. that is added/updated/deleted should clearly live in a DB. But unless your About Us kind of data is going to be changed constantly or you make a tool that allows somebody else to update it, don't put that in the DB.

How to keep track of revisions?

I am trying to design my database and I want to have the ability to keep a track of history of changes.
I will have a table that will have all the nutritional facts of an food. When a user makes changes to the item(say changes calories from 100 to 200). I want to make that as a new revision.
That way a person who comes along can see that it was original 100 calories and then was updated to 200 calories. I guess this would be alot like how stack does it now where you can see what has been edited.
I am wondering what is the best way to do this? I am using sql server and nhibernate.
I was thinking of have another column or something that would be revision number. Then every-time a revision is made the number is incremented. Is this a good way?
NHibernate.Envers helps you with that.
You could just do as you've written, add another revision number.
Another approach would be adding a timestamp and use that as history. Every time an item is updated you add a row with the current values and the current timestamp.
I'd normally use some form of audit table(s) to handle this. What we do where I currently work is have all our audit tables under an audit schema, and we have an audit table for every that we wish to track revisions for. We don't use NHibernate, so we simply utilise triggers to ensure that every update to a given row is recorded in the audit table along with a timestamp and user id so you can get some context and order to the way in which the revisions were made.
It seems like it is difficult using triggers with NHibernate, but you could use something like NHibernate interceptors or events as mentioned in this post. I'd say triggers would be preferable to relying on your code, but if this is the only way to go with NHibernate, then maybe it is worth a look.
Lastly, I've seen it mentioned that you can use SQL Servers native audit or trace capabilities. I've never used this myself, but do remember some post on SO suggesting that this replaced the need for creating your own manual audit tables and associated triggers. It seems to do what you want as illustrated by this quote from the link above;
The auditing of activity of users, roles, or groups on database
objects can be restricted down to the table level. That is, you can
target SQL Server Audit to track specific activities of a user or
users down to the individual table level. For example, SQL Server
Audit allows a record to be made of all the UPDATEs to the Payroll
table by DBO.
To me that sounds more like a true audit rather than just a revision history, but maybe someone with more experience in this area could comment on its feasibility to be used for revision tracking. Of course, you'd have to see what effect it had on NHibernate also.

Should I add a field like MarkedAsDeleted to my table?

I'm building an ASP.NET MVC application with SQL Server. I would like to know what will be a good practice for record deletion operations. I mean, when an item is deleted via web application, I would like to mark it as deleted, and then from an admin console, I will purge them if needed.
Is this a good practice? Should I use or avoid?
Thank you.
There is nothing wrong with this approach if you have no issues with storage space. Typically, we will use this pattern if the object being deleted is tied to other object (for instance, if you were tracking changes by user id, then you would not want to delete the user because you would not be able to pull info for that user later on). Simply mark a bit field showing the record has been deleted and filter those out when you query.
Again, it really depends on what makes sense for you and your application. Will you ever need this object again? Is it tied to other items in the database? Do you want to offer the user the option to 'undelete'? If the answer to any of those questions is yes, then you should probably keep the record. If the answer to all of those is no, then I would ask, why would you not just delete the record at the time it was requested?

What is the best way to record the edit history in a database

I am developing a system with Java EE and JPA where users can make changes to entities. It is needed to trace back to the changes when needed. So the all the changes and the user have to be recorded for each occasion when en update is made. What is the best way to record the changes.
For example, there is an Entity called Investigation. It has attributes like Name, Category, Price, Volume, etc. A user can search a single investigation and change the name in one instance and in another instance, another user can change the price. All these occasions with the change done and the user who did it is needed to be traced back when needed.
One method described in this link is that to label objects as old edited and create a new object with updated values, but the problem there are several other objects from different entities referring to the old one.
Another method as described in this link is to use a versioning field in a new table. Than can be achieved in JPA by creating a new entity that extends the main entity.
Out of these methods what is the best practice? Is there any other optimized way to keep the record editing history in Java Persistence?
EclipseLink supports history.
See,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/History
If you don't mind using Hibernate, Envers might be interesting for you. It performs auditing automatically, optionally appending metadata like current user.
For each audited entity it creates a history table that holds previous versions.

What is best practice for working with DB in Wordpress?

I'm developing a plugin for Wordpress, and I need to store some information from a form into a table in the DB.
There are probably lots of pages explaining how to do this, this being one of them:
http://codex.wordpress.org/Function_Reference/wpdb_Class
But are there any other pages talking about best practice for interacting with th WP DB?
UPDATE
Found a few more pages which could be usefull:
http://wpengineer.com/wordpress-database-functions
http://blue-anvil.com/archives/wordpress-development-techniques-1-running-custom-queries-using-the-wpdb-class
Unless you need to create your own complex table structure I'd suggest using the existing tables for your needs. There's the options table, user meta table, and post meta table to work with. These all have built in apis for quick access.
Options: add_option(), get_option(), update_option(), delete_option()
Usermeta: add_user_meta(), get_user_meta(), update_user_meta(), delete_user_meta()
Postmeta: add_post_meta(), get_post_meta(), update_post_meta(), delete_post_meta()
I've not found much real need to go outside of these tables (yes, there are exceptions, I've done them myself when the data needs are complex) but these meta options all use a text field in the db to store the data, so there's a lot you can store here if its simple data.
If your data is simple then consider storing your information in one of these places as individual options or even as serialized arrays.
One of my BIGGEST pet peeves with plug in developers that leverage the WP database is that if/when a given database driven plugin isn't used anymore, the developer doesn't think to remove the footprint it made in the database.

Resources