Skip to content

Instantly share code, notes, and snippets.

@rgrzegorczyk
Last active July 5, 2023 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgrzegorczyk/11f3f48913e3d23f307040f3dc42e0b8 to your computer and use it in GitHub Desktop.
Save rgrzegorczyk/11f3f48913e3d23f307040f3dc42e0b8 to your computer and use it in GitHub Desktop.
SQLcl Liquibase generate-schema - "enhanced" by changesSince. Export only those that changed.
/*
This script should be executed using your SQLcl connecting to your schema.
Create a new folder called e.g. "changesSince" and execute script generated by this query inside of it.
It will create subfolders as needed.
At the end move new objects to your folder with previously captured schema
You still need to add new files into controller.xml
Use CHAT GPT e.g and ask him:
I will send you values for further use.
(send him values generated with below DBMS_OUTPUT
Take values starting with lb.Value after obt is your type, after obn is name.
Create new lines with such pattern:
<include file="type/name_type.xml"/> Sort values by type
(manually add new lines to controller.xml)
*/
--RUN USING e.g. SQL DEVELOPER to your schema
--rm lb.sql
--set define on
ACCEPT num_units NUMBER PROMPT 'Number of hours: '
SET TERMOUT ON
set feedback ON;
--set heading ON;
declare
-- Set variables
v_owner varchar2(30) := 'RAFAL';
object_name varchar2(100);
object_type varchar2(100);
v_last_directory varchar2(100);
v_first_iteration number := 1;
v_filename varchar2(255);
begin
-- Take objects that changed in last hours
for cur in (
select lower(object_name) as object_name, lower(object_type) as object_type
FROM
(
select object_name as object_name,
case
when object_type = 'PACKAGE' then
'PACKAGE_SPEC'
else
TRANSLATE(lower(object_type), ' ', '_')
end as object_type
from all_objects
where owner = v_owner
and TO_DATE(all_objects.timestamp, 'YYYY-MM-DD HH24:MI:SS') > SYSDATE - INTERVAL '&num_units' HOUR
and object_type != 'LOB'
and object_type != 'JOB'
and object_name not like 'ISEQ$$' --skip autogenerated sequences
and object_name NOT LIKE 'DATABASECHANGELOG%'
order by object_type, object_name
)
)
-- Loop through the cursor results and generate SQLcl Liquibase statements for each row
loop
if cur.object_type != v_last_directory or v_last_directory is null then
if v_first_iteration = 1 then
--Switch to the directory with the same name as the object_type
DBMS_OUTPUT.PUT_LINE('!mkdir ' || cur.object_type);
DBMS_OUTPUT.PUT_LINE('cd ' || cur.object_type);
v_first_iteration := 0;
else
-- Switch back to the /changesSince/ directory
DBMS_OUTPUT.PUT_LINE('cd ..');
-- Then Switch to the directory with the same name as the object_type
DBMS_OUTPUT.PUT_LINE('!mkdir ' || cur.object_type);
DBMS_OUTPUT.PUT_LINE('cd ' || cur.object_type);
end if;
-- Save the last directory to avoid switching unnecessarily
v_last_directory := cur.object_type;
end if;
-- Generate the SQLcl Liquibase command
DBMS_OUTPUT.PUT_LINE('lb generate-object --obt '
|| cur.object_type
|| ' --obn '
|| cur.object_name||' --sql');
v_last_directory := cur.object_type;
end loop;
-- Switch back to the /changesSince/ directory
DBMS_OUTPUT.PUT_LINE('cd ..');
end;
/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment