Db host
#!/bin/bash
# Load configuration
source config.cfg
# Function to log messages
log() {
echo "$(date +'%Y%m%d') $1" >> $log_file
}
log "Starting data load process."
# Create control file for SQL*Loader
control_file="control_file.ctl"
cat <<EOF > $control_file
LOAD DATA
INFILE '$csv_file'
INTO TABLE $child_table
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
(
parent,
parent.sys_class_name,
child.sys_class_name,
child.u_cmdb_ci_used_for,
parent.correlation_id,
type,
child.correlation_id,
child,
modified_date "SYSDATE"
)
EOF
log "Truncating child table."
sqlplus -s $username/$password@$database <<EOF
TRUNCATE TABLE $child_table;
EXIT;
EOF
if [ $? -eq 0 ]; then
log "Child table truncated successfully."
else
log "Failed to truncate the child table."
exit 1
fi
# Load data into child table
sqlldr $username/$password@$database control=$control_file log="$log_location/child_loader.log" BAD="$log_location/child_loader.bad"
if [ $? -eq 0 ]; then
log "Data loaded successfully into child table."
else
log "Failed to load data into child table."
exit 1
fi
log "Comparing and updating master table."
# Compare and update SQL script
sql_compare_update="compare_update_master.sql"
cat <<EOF > $sql_compare_update
-- Insert new records into master table
INSERT INTO $master_table (
parent,
parent.sys_class_name,
child.sys_class_name,
child.u_cmdb_ci_used_for,
parent.correlation_id,
type,
child.correlation_id,
child,
modified_date
)
SELECT
c.parent,
c.parent.sys_class_name,
c.child.sys_class_name,
c.child.u_cmdb_ci_used_for,
c.parent.correlation_id,
c.type,
c.child.correlation_id,
c.child,
c.modified_date
FROM $child_table c
LEFT JOIN $master_table m ON c.child.correlation_id = m.child.correlation_id
WHERE m.child.correlation_id IS NULL;
-- Log inserted rows
SPOOL $inserted_rows_file
SELECT
c.parent,
c.parent.sys_class_name,
c.child.sys_class_name,
c.child.u_cmdb_ci_used_for,
c.parent.correlation_id,
c.type,
c.child.correlation_id,
c.child,
c.modified_date
FROM $child_table c
LEFT JOIN $master_table m ON c.child.correlation_id = m.child.correlation_id
WHERE m.child.correlation_id IS NULL;
SPOOL OFF;
-- Update modified records in master table
MERGE INTO $master_table m
USING $child_table c
ON (m.child.correlation_id = c.child.correlation_id)
WHEN MATCHED THEN
UPDATE SET
m.parent = c.parent,
m.parent.sys_class_name = c.parent.sys_class_name,
m.child.sys_class_name = c.child.sys_class_name,
m.child.u_cmdb_ci_used_for = c.child.u_cmdb_ci_used_for,
m.parent.correlation_id = c.parent.correlation_id,
m.type = c.type,
m.child = c.child,
m.modified_date = SYSDATE
WHERE
m.parent != c.parent OR
m.parent.sys_class_name != c.parent.sys_class_name OR
m.child.sys_class_name != c.child.sys_class_name OR
m.child.u_cmdb_ci_used_for != c.child.u_cmdb_ci_used_for OR
m.parent.correlation_id != c.parent.correlation_id OR
m.type != c.type OR
m.child != c.child;
-- Log updated rows
SPOOL $updated_rows_file
SELECT
c.parent,
c.parent.sys_class_name,
c.child.sys_class_name,
c.child.u_cmdb_ci_used_for,
c.parent.correlation_id,
c.type,
c.child.correlation_id,
c.child,
c.modified_date
FROM $child_table c
JOIN $master_table m ON c.child.correlation_id = m.child.correlation_id
WHERE
m.parent != c.parent OR
m.parent.sys_class_name != c.parent.sys_class_name OR
m.child.sys_class_name != c.child.sys_class_name OR
m.child.u_cmdb_ci_used_for != c.child.u_cmdb_ci_used_for OR
m.parent.correlation_id != c.parent.correlation_id OR
m.type != c.type OR
m.child != c.child;
SPOOL OFF;
EOF
# Execute SQL script
sqlplus -s $username/$password@$database <<EOF
@$sql_compare_update
EXIT;
EOF
if [ $? -eq 0 ]; then
log "Comparison and update executed successfully."
else
log "Failed to execute comparison and update."
exit 1
fi
rm $control_file
rm $sql_compare_update
log "Data load process completed."
Comments
Post a Comment