views:

1017

answers:

6

Hi,

We're about to run side-by-side testing to compare a legacy system with a new shiny version. We have an Oracle database table, A, that stores data for the legacy system, and an equivalent table, B, that stores data for the new system, so for the duration of the test, the database is denormalized. (Also, the legacy system and table A are fixed - no changes allowed)

What I want to do is to allow the infrequent DML operations on A to propagate to B, and vice-versa. I started with a pair of triggers to do this, but hit the obvious problem that when the triggers run, the tables are mutating, and an exception is thrown.

Is there a standard way of handling this issue? I've read differing reports on whether or not using dbms_scheduler is the way to go...

Thanks,

Andy

Update: I've ended up chickening out of the whole issue and ensured that all stored procedures that update A, also update B, and vice versa.

I've marked Quassnoi's answer as accepted, because I'd follow his suggestions if faced with the same issue in the future.

I've marked up JosephStyon's answer, because I briefly got things working by adding two insert/update statement level triggers on tables A and B, then doing his merge procedure using A or B as the master table, depending on which trigger ran (although first I checked that the target table would be changed by the merge, earlying out if not).

+1  A: 

How tight does the synchronization between tables have to be? Can you point your triggers instead to staging tables (Aqueue/Bqueue) and pull their results in at a later time?

John Pirie
not very tight. Also, DML changes don't come in too quickly, so synchronizing using the scheduler seemed like a viable option.
Andy
+3  A: 

I'd create A and B as views over a single normalized (or denormalized) table, and created an INSTEAD OF trigger over these views to handle DML operations.

If the query plans matter, it's better to keep two copies of tables: A_underlying and B_underlying and create the views just like this:

CREATE VIEW A
AS
SELECT  *
FROM    A_underlying

CREATE VIEW B
AS
SELECT  *
FROM    B_underlying

The predicates will be pushed into the views, and the query plans for actual tables and views will be the same.

In INSTEAD OF triggers over both view, you should put the data into both underlying tables.

Quassnoi
that's actually a very nice idea. Unfortunately, the legacy application and database schema are fixed. I've updated the question to reflect this. Thanks.
Andy
The database schema will remain the same to the outside world. You can even create the underlying tables in another schema. By creating the triggers you change your schema no less than by creating views over underlying tables.
Quassnoi
true. To be more specific: I can add triggers without affecting the legacy system. If I wanted to create a view onto a single unified table AB, I'd have to modify the legacy application to make use of the view onto it.
Andy
@Andy: you can rename the old table A into A_underlying (or even backup_schema.A_underlying) and create the view with the same name A that just selects everything from A_underlying. Your legacy application won't notice anything.
Quassnoi
I see what you mean - nice! Unfortunately, I'm too chicken to do this - the codebase of the legacy system is so convoluted that it may do direct inserts into table A... if all else fails I will go down this route, though. Thank you :)
Andy
@Andy: I'll play Biff Tannen yet a little and tell you that with an INSTEAD OF trigger everything will look just like direct inserts into A.
Quassnoi
no-one calls me chicken. etc. ok, I'll give it a go.
Andy
+1 for simplicity... you could also abstract a bit further with synonyms.
DCookie
@DCookie: you can't create INSTEAD OF triggers on synonyms, only on views
Quassnoi
@Quassnoi, understand, but you can create a synonym for what your app is looking for and point it at whatever you want to call your view.
DCookie
@DCookie: in that sense, sure you can, right
Quassnoi
+1  A: 

Do you really mean DDL, not DML?

With DML you can have a look at Oracles Multi Master Replication to keep the tables in synch or you could also have a look at the tool SymmetricDS for this purpose.

With DDL the only solution I'm aware of is again the Oracle advanced replication.

Kosi2801
gah! DML of course - well spotted :)
Andy
+1  A: 

Put the below three statements in a stored procedure, then run it as a scheduled job as often as you like:

--Assume that "A" is a master, and "B" needs to be synched

--If no match in "A", delete from "B"
DELETE FROM B
WHERE NOT EXISTS(
                SELECT *
                FROM A
                WHERE A.PRIMARY_KEY = B.PRIMARY_KEY
                );

--If there is a match, but they are different, then update "B"
update 
  (
  select
    a.field1 as new_value1
   ,b.field1 as old_value1
   ,a.field2 as new_value2
   ,b.field2 as old_value2
   ,....
   ,a.fieldN as new_valueN
   ,b.fieldN as old_valueN
  from
    a
   ,b
 where a.primary_key = b.primary_key
 )
set
  old_value1 = new_value1
 ,old_value2 = new_value2
 ,....
 ,old_valueN = new_valueN;


--if the record is new to "A", then insert it into "B"
INSERT INTO B
SELECT *
FROM A 
WHERE NOT EXISTS(
                SELECT *
                FROM B 
                WHERE B.PRIMARY_KEY = A.PRIMARY_KEY
                );
JosephStyons
Hi Joseph. There is no master/slave relationship between the tables. Changes to A should propagate to B, and vice versa. I suppose I could add a timestamp to your code to achieve this, as changes are user-initiated and concurrency issues are unlikely. Thank you.
Andy
+1  A: 

Oracle 10g and above have implemented Change Notification as an asynchronous process. It's automatic and the package is included with the server install of Oracle 10g and above.

You can see here for some info.

Jonathan
A: 

Perfect solution, JosephStyons ;-)