When releasing database code to non-development databases , I use such approach - I create release sqlplus script that runs multiple create table/view/sequence/package/etc statements in a sequence. I also should create rollback script which performs drop and other statements if would be needed during deployment or further use. But it is quite annoying always to create rollback scripts manually. I.E. - when I put
alter table table_a add column some_column number(5);
into release script. I have to put
alter table table_a drop column some_column;
into the rollback script. And vice-versa.
Is there way to optimize(or semi-optimize) it? Maybe some there are some Java/Python/etc libraries that allow to parse ddl statements into logical parts?
Maybe there are some better approaches for release/rollback pl/sql code?
DBMS_METADATA_DIFF and a few metadata queries can automate this process.
This example demonstrates 6 types of changes: 1) adding a column 2) incrementing a sequence 3) dropping a table 4) creating a table 5) changing a view 6) allocating an extent.
You are correct that DBMS_METADATA_DIFF does not work for
CREATE
orDROP
. Trying to diff an object that only exists in one schema will generate an error message like this:However, dropping and adding objects may be easy to script with the following:
The alters can be handled with a SQL statement like this:
Some notes about these results:
RESTART START WITH
syntax can be very helpful. You don't need to drop or re-create the indexes, or mess with theincrement by
multiple times. This syntax is not in the 12c manual. In fact, I cannot find it anywhere on Google. Looks like this package is using undocumented features.Some other notes:
It is possible to fully automate this process. But based on the issues above, and my experience with all such automated tools, you should not trust it 100%.