We are developing many application where we are auditing, archiving and purging data also for many tables. Most of the application will use Water Flow SDLC for development. While modifying main table, we also need to maintain archival and purge objects.
There many places where we need to archive child table also if we are archiving parent table based on foreign key relationship with tables.
We are also using configuration table to maintain archival and purge period for different tables and also entering different key if we want to maintain archival or purge with different period.
Here, we tried to automate all above scenario. We will go through all above scenario one by one and their configuration. Before that we need to configure some objects. We need to create below objects to configure it. (Oracle Configure Archival Process.sql)
Tables
To understand this scenario, I have taken Employee and Department example. Below is E-R Diagram for that. Below are the details of tables. (Oracle Configure Archival Process Employee & Department tables.sql)
Now, our requirement is to archive and purge on Department & Error_Log tables which also should archive it’s child tables if exist based on relationship created between them. Below is configuration required in dbo.Archive_Purge_Config table to archive that. We also need to add config_key “Archive_Purge_Max_Queue_Size” in dbo.Config table. It will used to setup batch size while deleting records from table inside procedure. (Configure Archival Process Data.sql)
Once job dbo.job_archive_purge will execute, it will create new _Archive tables for each tables configured in dbo.archive_purge_config if it’s not exists with their child relationship tables. Below are the list of archive tables will be created by dbo.cp_archive_purge procedure.
There many places where we need to archive child table also if we are archiving parent table based on foreign key relationship with tables.
We are also using configuration table to maintain archival and purge period for different tables and also entering different key if we want to maintain archival or purge with different period.
Here, we tried to automate all above scenario. We will go through all above scenario one by one and their configuration. Before that we need to configure some objects. We need to create below objects to configure it. (Oracle Configure Archival Process.sql)
Tables
- Error_Log (Check blog http://www.sqlscientist.com/2013/07/error-logging-and-alerts-in-oracle.html)
- Config (Check blog http://www.sqlscientist.com/2013/07/error-logging-and-alerts-in-oracle.html)
- Archive_Purge_Config
- Columns
- is_archive
- 1 = Archive is enable from ERROR_LOG to ERROR_LOG_ARCHIVE.
- 0 = Archive is disable.
- archive_column_name
- a. Archive is happening based on this datetime column.
- archive_day
- Archive is happening based on configured days in this column.
- archive_rule_sql
- Archive and Purge will happened on filtered records configured in this rule.
- NULL – It will happened to whole table.
- is_purge
- 1 = Purge is enable from ERROR_LOG_ARCHIVE.
- 0 = Purge is disable.
- purge_days
- Purge is happening based on configured days in this column.
- is_purge_on_source
- 1 = Purge is enable from ERROR_LOG. In this case, It will ignore values in is_archive and is_purge columns.
- 0 = Purge is disable.
- is_active
- 1 = Record is active.
- 0 = Record is inactive.
- is_archive
- Columns
- cp_archive_purge
- cp_error_log_alert (Check blog http://www.sqlscientist.com/2013/07/error-logging-and-alerts-in-oracle.html)
- JOB_ARCHIVE_PURGE
- Execute procedure cp_archive_purge daily once at 1:00 AM.
- JOB_ERROR_LOG_ALERT (Check blog http://www.sqlscientist.com/2013/07/error-logging-and-alerts-in-oracle.html)
To understand this scenario, I have taken Employee and Department example. Below is E-R Diagram for that. Below are the details of tables. (Oracle Configure Archival Process Employee & Department tables.sql)
- Department
- There may be many departments in database.
- Employee
- There are many employees for each departments
- Contact
- For each employee, there may be many contact details.
- Address
- For each employee, there may be many address details.
- AddressDetails
- For each address, there may or may not be additional address details.
Now, our requirement is to archive and purge on Department & Error_Log tables which also should archive it’s child tables if exist based on relationship created between them. Below is configuration required in dbo.Archive_Purge_Config table to archive that. We also need to add config_key “Archive_Purge_Max_Queue_Size” in dbo.Config table. It will used to setup batch size while deleting records from table inside procedure. (Configure Archival Process Data.sql)
- Department – It will archive 30 days in Department_Archive table and purge(permanently) on 30 days from Department_Archive table.
- Error_Log - It will archive 30 days in Error_Log_Archive table and purge(permanently) on 30 days from Error_Log_Archive table.
Once job dbo.job_archive_purge will execute, it will create new _Archive tables for each tables configured in dbo.archive_purge_config if it’s not exists with their child relationship tables. Below are the list of archive tables will be created by dbo.cp_archive_purge procedure.
- Department_archive
- Contact_Archive
- Employee_Archive
- Address_Archive
- AddressDetail_Archive
- Error_Log_Archive
1. New Column
I want to add one more column “DepartmentType varchar(5)” in dbo.Department table. Once job dbo.job_archive_purge will execute, it will also add column in _Archive table. Below is screenshot of Department_Archive table after job execution.
Query:
ALTER TABLE DEPARTMENT ADD DEPARTMENTTYPE VARCHAR2(5)
2. Rename Column
I want to rename column “AddressDetail” to “Address1” in dbo.AddressDetail table. Once job dbo.job_archive_purge will execute, it will drop old column and create new column in _Archive table. Below is screenshot of AddressDetail_Archive table after job execution.
Query:
ALTER TABLE ADDRESSDETAIL RENAME COLUMN ADDRESSDETAIL TO ADDRESS1;
3. Drop Column
If we extra column in archive table, it will not break archive process so we are not dropping extra columns for each run. Procedure will drop extra columns from _Archive table with any other fixes for that table like we did Rename Column as above.
4. Data Type Change of Column
After adding column “DepartmentType” in dbo.Department table, I realize that it should be varchar(10). While doing this conversion, we should make sure that _Archive column should be convertible with new data type. If it’s require data fixes then do that first before execution of job. To test this scenario we should have some data to archive. (Oracle Configure Archival Process Employee & Department tables Data.sql)
Query:
ALTER TABLE DEPARTMENT MODIFY DEPARTMENTTYPE VARCHAR2(10);
Summary
- Archive Process can support below features.
- Auto create archive table.
- Auto create and delete additional columns on archive table.
- Auto modify data type of column.
- Archive Process can create new column without any constraint.
- Archive table will be created with one more column “archive_created_date” with default getdate().
- Archive feature with their child tables.
- Purge on archive table based on days setup in dbo.Archive_Purge_Config table.
- Purge from Source directly based on days setup in dbo.Archive_Purge_Config table.
- Oracle Configure Archival Process.sql
- Oracle Configure Archival Process Employee & Department tables.sql
- Oracle Configure Archival Process Data.sql
- Oracle Configure Archival Process Employee & Department tables Data.sql
No comments:
Post a Comment