Search code examples
databaseoracle-databaserefactoring-databases

Is there something like a "column symlink" in Oracle?


I would like to have a column in my DB accessible via two column names temporarily.

Why? The column name was badly chosen, I would like to refactor it. As I want my webapp to remain stable while changing the column name, it would be good to

  1. have a (let's call it) symlink named better_column_name pointing to the column bad_column_name
  2. change the webapplication to use better_column_name
  3. drop the symlink and rename column to better_column_name

"Refactoring Databases" suggests to actually add a second column which is synchronized on commit in order to achieve this. I am just hoping that there might be an easier way with Oracle, with less work and less overhead.


Solution

  • As long as you have code that uses both column names, I don't see a way to get around the fact that you'll have two (real) columns in that table.

    I would add the new column with the correct name and then create a trigger that checks which column has been modified and updates the "other" column correspondingly. So whatever is being updated, the value is synch'ed with the other column.

    Once all the code that uses the old column has been migrated, remove the trigger and drop the old column.

    Edit

    The trigger would so do something like this:

    CREATE OR REPLACE TRIGGER ...
        ...
        UPDATE OF bad_column_name, better_column_name ON the_table 
        ...
    BEGIN
      IF UPDATING ('BAD_COLUMN_NAME') THEN 
         :new.better_column_name = :new.bad_column_name
      END IF;
    
      IF UPDATING ('BETTER_COLUMN_NAME') THEN 
         :new.bad_column_name = :new.better_column_name
      END IF;
    END;
    

    The order of the IF statements controls which change has a "higher priority" in case someone updated both columns at the same time.