“Diffing” objects from a relational database

2019-03-13 08:27发布

Our win32 application assembles objects from the data in a number of tables in a MySQL relational database. Of such an object, multiple revisions are stored in the database.

When storing multiple revisions of something, sooner or later you'll ask yourself the question if you can visualize the differences between two revisions :) So my question is: what would be a good way to "diff" two such database objects?

  • Would you do the comparison at the database level? (Doesn't sound like a good idea: too low-level, and too sensitive to the schema).
  • Would you compare the objects?
    • Would you write a function that "manually" compares the properties and fields of two objects?
    • How would you store the diff? In a separate, generic "TDiff" object?
    • Any general recommendations on how to visualize such things in a user interface?

Advice, or stories about your own experiences with this, are very welcome; thanks a bunch!

Extra info on use case (20090515)

In reply to Antony's comment: this specific application is used to schedule training courses, run by teams of teachers. The schedule of a teacher is stored in various tables in the database, and contains info such as "where does she have to go on which day", "who are her colleagues in the team", etc. This information is spread out over multiple tables.

Once in a while, we "publish" the schedule, so the teachers can see it on a webpage. Each "publication" is a revision, and we'd like to be able to show the users (and later also the teachers) what's changed between two publications --- if anything.

Hope that makes the scenario a bit more tangible :)

Some final remarks

Well, the bounty has come to an end, so I've accepted an answer. If it'd somehow be possible to slice a couple of extra 100's off of my rep and give it to some of the other answers, I would do so without hesitation. All your guys' help has been great, and I am very grateful! ~ Onno 20090519

12条回答
Evening l夕情丶
2楼-- · 2019-03-13 08:47

Just an idea, but would it be worthwhile for you to convert the two object versions being compared to some text format and then comparing these text objects using an existing diff program - like diff for example? There are lots of nice diff programs out there that can offer nice visual representations, etc.

So for example

Text version of Object 1:

first_name: Harry
last_name: Lime
address: Wien
version: 0.1

Text version of Object 2:

first_name: Harry
last_name: Lime
address: Vienna
version: 0.2

The diff would be something like:

3,4c3,4
< address: Wien
< version: 0.1
---
> address: Vienna
> version: 0.2
查看更多
放荡不羁爱自由
3楼-- · 2019-03-13 08:49

Have you looked at Open Source DiffKit?

www.diffkit.org

I think it does what you want.

查看更多
Rolldiameter
4楼-- · 2019-03-13 08:51

I'm riffing off of what Harry Lime suggested: Output your properties to text format, then hash the results. That way you can compare the hash values and easily flag the data that has been altered. This way you get the best of both worlds as you can visually see differences but programmatically identify differences. With the has you'll have a good source for an index should you want to store and retrieve the deltas.

查看更多
Lonely孤独者°
5楼-- · 2019-03-13 08:58

Assume that a class has 5 known properties - date, time, subject, outline, location. When I look at my schedule, I'm most interested in the most recent (ie current/accurate) version of these properties. It would also be useful for me to know what, if anything, has changed. (As a side note, if the date, time or location changed, I'd also expect to get an email/sms advising me in case I don't check for an updated schedule :-))

I would suggest that the 'diff' is performed at the time the schedule is amended. So, when version 2 of the class is created, record which values have changed, and store this in two 'changelog' fields on the version 2 object (there must already be one parent table that sits atop all your tables - use that one!). One changelog field is 'human readable text' eg 'Date changed from Mon 1 May to Tues 2 May, Time changed from 10:00am to 10:30am'. The second changelog field is a delimted list of changed fields eg 'date,time' To do this, before saving you would loop over the values submitted by the user, compare to current database values, and concatenate 2 strings, one human readable, one a list of field names. Then, update the data and set your concatenated strings as the 'changelog' values.

When displaying the schedule load the current version by default. Loop through the fields in the changelog field list, and annotate the display to show that the value has changed (a * or a highlight, etc). Then, in a separate panel display the human readable change log.

If a schedule is amended more than once, you would probably want to combine the changelogs between version 1 & 2, and 2 & 3. Say in version 3 only the course outline changed - if that was the only changelog you had when displaying the schedule, the change to date and time wouldn't be displayed.

Note that this denormalised approach won't be great for analysis - eg working out which specific location always has classes changed out of it - but you could extend it using an E-A-V model to store the change log.

查看更多
倾城 Initia
6楼-- · 2019-03-13 08:59

Given you want to create a UI for this and need to indicate where the differences are, it seems to me you can either go custom or create a generic object comparer - the latter being dependent on the language you are using.

For the custom method, you need to create a class that takes to two instances of the classes to be comparied. It then returns differences;

 public class Person
 {
     public string name;
 }

 public class PersonComparer
 {
     public PersonComparer(Person old, Person new)
     {
        ....         
     }

     public bool NameIsDifferent() { return old.Name != new.Name; }
     public string NameDifferentText() { return NameIsDifferent() ? "Name changed from " + old.Name + " to " + new.Name : ""; }
 }

This way you can use the NameComparer object to create your GUI.

The gereric approach would be much the same, just that you generalize the calls, and use object insepection (getObjectProperty call below) to find differences;

 public class ObjectComparer()
 {
    public ObjectComparer(object old, object new)
    {
        ...
    }

    public bool PropertyIsDifferent(string propertyName) { return getObjectProperty(old, propertyName) != getObjectProperty(new, propertyName) };

     public string PropertyDifferentText(string propertyName) { return PropertyIsDifferent(propertyName) ? propertyName + " " + changed from " + getObjectProperty(old, propertyName) + " to " + getObjectProperty(new, propertyName): ""; }
 }
}

I would go for the second, as it makes things really easy to change GUI on needs. The GUI I would try 'yellowing' the differences to make them easy to see - but that depends on how you want to show the differences.

Getting the object to compare would be loading your object with the initial revision and latest revision.

My 2 cents... Not as techy as the database compare stuff already here.

查看更多
闹够了就滚
7楼-- · 2019-03-13 08:59

Example with Oracle.

  • Export ordered objects to text with dbms_metadata
  • Export ordered tables data into CSV or query format
  • Make big text file
  • Diff
查看更多
登录 后发表回答