Skip to content

Commit

Permalink
Merge pull request #26 from Snowflake-Labs/support/jvasquez/update_ou…
Browse files Browse the repository at this point in the history
…tput_text_with_more_detailed_information_about_the_execution

[Teradata] Update output text with more detailed information about the execution
  • Loading branch information
sfc-gh-jvasquezrojas authored Feb 28, 2024
2 parents 87744cb + 00fef04 commit e5ebc4a
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 171 deletions.
2 changes: 1 addition & 1 deletion Teradata/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This repository provides some simple scripts to help exporting your Teradata cod

## Version

Release 2023-02-23
Release 2023-02-27

## Usage

Expand Down
88 changes: 69 additions & 19 deletions Teradata/bin/create_ddls.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#
#Version 20201105: Script created
#Version 20211210: Fix error messages
#Version 20230811: Add command to copy the scripts from scripts_template.
#Version 20240201: Add spliting mechanism for output code.
#Version 20240223: Remove spliting mechanism for output code.
#Version 2020-11-05: Script created.
#Version 2021-12-10: Fix error messages.
#Version 2023-08-11: Add command to copy the scripts from scripts_template.
#Version 2024-02-01: Add spliting mechanism for output code.
#Version 2024-02-23: Remove spliting mechanism for output code.
#Version 2024-02-27: Update output text with more detailed information about the execution.

##### PARAMETERS
##### Modify the connection information
connection_string="dbc,dbc"

Expand All @@ -23,43 +25,91 @@ exclude_databases="(UPPER(T1.DATABASENAME) NOT IN ('SYS_CALENDAR','ALL','CONSOLE
##### Use uppercase names.
include_objects="(UPPER(T1.TABLENAME) LIKE ANY ('%'))"

##### Creates directory for output and log files.
##### CONSTANTS
steps="5"

function get_current_timestamp
{
date '+%Y/%m/%d %l:%M:%S%p'
}

echo "[$(get_current_timestamp)] Info: Step 1/${steps} - Creating Directories: Started"
mkdir -p ../log
mkdir -p ../temp
mkdir -p ../output
mkdir -p ../output/object_extracts
mkdir -p ../output/object_extracts/DDL
cp -r ../scripts_template ../scripts
touch -- "../output/object_extracts/DDL/.sc_extracted"
echo "[$(get_current_timestamp)] Info: Step 1/${steps} - Creating Directories: Completed"


echo "[$(get_current_timestamp)] Info: Step 2/${steps} - Extracting DDLs: Started"
declare -a scripts_file=(
[0]="create_databases"
[1]="create_functions"
[2]="create_join_indexes"
[3]="create_macros"
[4]="create_procedures"
[5]="create_schemas"
[6]="create_tables"
[7]="create_triggers"
[8]="create_views"
)

declare -a scripts_name=(
[0]="databases"
[1]="functions"
[2]="join indexes"
[3]="macros"
[4]="procedures"
[5]="schemas"
[6]="tables"
[7]="triggers"
[8]="views"
)

##### Updates BTEQ files with the correct list of databases and connection info.
sed -i "s|include_databases|$include_databases|g" ../scripts/create_ddls.btq
sed -i "s|exclude_databases|$exclude_databases|g" ../scripts/create_ddls.btq
sed -i "s|include_objects|$include_objects|g" ../scripts/create_ddls.btq
sed -i "s|connection_string|$connection_string|g" ../scripts/create_ddls.btq

##### Executes DDL extracts and DDL Reports
echo 'Creating DDLS...'
bteq <../scripts/create_ddls.btq >../log/create_ddls.log 2>&1
for i in "${!scripts_file[@]}"; do
echo "[$(get_current_timestamp)] Info: Start extracting ${scripts_name[$i]}"

echo 'Removing unnecessary comments...'
if [[ ! -f ../scripts/"${scripts_file[$i]}".btq ]]
then
echo "[$(get_current_timestamp)] ERROR: file ${scripts_file[$i]} not found"
fi
sed -i "s|include_databases|$include_databases|g" ../scripts/"${scripts_file[$i]}".btq
sed -i "s|exclude_databases|$exclude_databases|g" ../scripts/"${scripts_file[$i]}".btq
sed -i "s|include_objects|$include_objects|g" ../scripts/"${scripts_file[$i]}".btq
sed -i "s|connection_string|$connection_string|g" ../scripts/"${scripts_file[$i]}".btq
bteq <../scripts/"${scripts_file[$i]}".btq >../log/${scripts_file[$i]}.log 2>&1

echo "[$(get_current_timestamp)] Info: Extracted ${scripts_name[$i]}"

done
echo "[$(get_current_timestamp)] Info: Step 2/${steps} - Extracting DDLs: Completed"


echo "[$(get_current_timestamp)] Info: Step 3/${steps} - Removing unnecessary comments: Started"
[[ ! -f ../output/object_extracts/DDL/DDL_Tables.sql ]] || sed -i "s|--------------.*--------------||g" ../output/object_extracts/DDL/DDL_Tables.sql
[[ ! -f ../output/object_extracts/DDL/DDL_Join_Indexes.sql ]] || sed -i "s|--------------.*--------------||g" ../output/object_extracts/DDL/DDL_Join_Indexes.sql
[[ ! -f ../output/object_extracts/DDL/DDL_Views.sql ]] || sed -i "s|--------------.*--------------||g" ../output/object_extracts/DDL/DDL_Views.sql
[[ ! -f ../output/object_extracts/DDL/DDL_Functions.sql ]] || sed -i "s|--------------.*--------------||g" ../output/object_extracts/DDL/DDL_Functions.sql
[[ ! -f ../output/object_extracts/DDL/DDL_Macros.sql ]] || sed -i "s|--------------.*--------------||g" ../output/object_extracts/DDL/DDL_Macros.sql
[[ ! -f ../output/object_extracts/DDL/DDL_Procedures.sql ]] || sed -i "s|--------------.*--------------||g" ../output/object_extracts/DDL/DDL_Procedures.sql
[[ ! -f ../output/object_extracts/DDL/DDL_SF_Schemas.sql ]] || sed -i "s| |\n|g" ../output/object_extracts/DDL/DDL_SF_Schemas.sql
echo "[$(get_current_timestamp)] Info: Step 3/${steps} - Removing unnecessary comments: Completed"

echo 'Replacing unicode values...'
echo "[$(get_current_timestamp)] Info: Step 4/${steps} - Replacing unicode values: Started"
[[ ! -f ../output/object_extracts/DDL/DDL_Tables.sql ]] || sed -i -e "s|\U2013|-|g" -e "s|\U00D8|0|g" -e "s|\U00A0| |g" -e "s|\U1680| |g" -e "s|\U180E| |g" -e "s|\U2000| |g" -e "s|\U2001| |g" -e "s|\U2002| |g" -e "s|\U2003| |g" -e "s|\U2004| |g" -e "s|\U2005| |g" -e "s|\U2006| |g" -e "s|\U2007| |g" -e "s|\U2008| |g" -e "s|\U2009| |g" -e "s|\U200A| |g" -e "s|\U200B| |g" -e "s|\U202F| |g" -e "s|\U205F| |g" -e "s|\U3000| |g" -e "s|\UFEFF| |g" ../output/object_extracts/DDL/DDL_Tables.sql
[[ ! -f ../output/object_extracts/DDL/DDL_Join_Indexes.sql ]] || sed -i -e "s|\U2013|-|g" -e "s|\U00D8|0|g" -e "s|\U00A0| |g" -e "s|\U1680| |g" -e "s|\U180E| |g" -e "s|\U2000| |g" -e "s|\U2001| |g" -e "s|\U2002| |g" -e "s|\U2003| |g" -e "s|\U2004| |g" -e "s|\U2005| |g" -e "s|\U2006| |g" -e "s|\U2007| |g" -e "s|\U2008| |g" -e "s|\U2009| |g" -e "s|\U200A| |g" -e "s|\U200B| |g" -e "s|\U202F| |g" -e "s|\U205F| |g" -e "s|\U3000| |g" -e "s|\UFEFF| |g" ../output/object_extracts/DDL/DDL_Join_Indexes.sql
[[ ! -f ../output/object_extracts/DDL/DDL_Views.sql ]] || sed -i -e "s|\U2013|-|g" -e "s|\U00D8|0|g" -e "s|\U00A0| |g" -e "s|\U1680| |g" -e "s|\U180E| |g" -e "s|\U2000| |g" -e "s|\U2001| |g" -e "s|\U2002| |g" -e "s|\U2003| |g" -e "s|\U2004| |g" -e "s|\U2005| |g" -e "s|\U2006| |g" -e "s|\U2007| |g" -e "s|\U2008| |g" -e "s|\U2009| |g" -e "s|\U200A| |g" -e "s|\U200B| |g" -e "s|\U202F| |g" -e "s|\U205F| |g" -e "s|\U3000| |g" -e "s|\UFEFF| |g" ../output/object_extracts/DDL/DDL_Views.sql
[[ ! -f ../output/object_extracts/DDL/DDL_Functions.sql ]] || sed -i -e "s|\U2013|-|g" -e "s|\U00D8|0|g" -e "s|\U00A0| |g" -e "s|\U1680| |g" -e "s|\U180E| |g" -e "s|\U2000| |g" -e "s|\U2001| |g" -e "s|\U2002| |g" -e "s|\U2003| |g" -e "s|\U2004| |g" -e "s|\U2005| |g" -e "s|\U2006| |g" -e "s|\U2007| |g" -e "s|\U2008| |g" -e "s|\U2009| |g" -e "s|\U200A| |g" -e "s|\U200B| |g" -e "s|\U202F| |g" -e "s|\U205F| |g" -e "s|\U3000| |g" -e "s|\UFEFF| |g" ../output/object_extracts/DDL/DDL_Functions.sql
[[ ! -f ../output/object_extracts/DDL/DDL_Macros.sql ]] || sed -i -e "s|\U2013|-|g" -e "s|\U00D8|0|g" -e "s|\U00A0| |g" -e "s|\U1680| |g" -e "s|\U180E| |g" -e "s|\U2000| |g" -e "s|\U2001| |g" -e "s|\U2002| |g" -e "s|\U2003| |g" -e "s|\U2004| |g" -e "s|\U2005| |g" -e "s|\U2006| |g" -e "s|\U2007| |g" -e "s|\U2008| |g" -e "s|\U2009| |g" -e "s|\U200A| |g" -e "s|\U200B| |g" -e "s|\U202F| |g" -e "s|\U205F| |g" -e "s|\U3000| |g" -e "s|\UFEFF| |g" ../output/object_extracts/DDL/DDL_Macros.sql
[[ ! -f ../output/object_extracts/DDL/DDL_Procedures.sql ]] || sed -i -e "s|\U2013|-|g" -e "s|\U00D8|0|g" -e "s|\U00A0| |g" -e "s|\U1680| |g" -e "s|\U180E| |g" -e "s|\U2000| |g" -e "s|\U2001| |g" -e "s|\U2002| |g" -e "s|\U2003| |g" -e "s|\U2004| |g" -e "s|\U2005| |g" -e "s|\U2006| |g" -e "s|\U2007| |g" -e "s|\U2008| |g" -e "s|\U2009| |g" -e "s|\U200A| |g" -e "s|\U200B| |g" -e "s|\U202F| |g" -e "s|\U205F| |g" -e "s|\U3000| |g" -e "s|\UFEFF| |g" ../output/object_extracts/DDL/DDL_Procedures.sql
echo "[$(get_current_timestamp)] Info: Step 4/${steps} - Replacing unicode values: Completed"

echo '...DDL Creation Complete'

echo "[$(get_current_timestamp)] Info: Step 5/${steps} - Removing temporal files: Started"
rm -r ../temp
rm -r ../scripts
rm -r ../scripts

echo "[$(get_current_timestamp)] Info: Step 5/${steps} - Removing temporal files: Completed"
15 changes: 15 additions & 0 deletions Teradata/scripts_template/create_databases.btq
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.LOGON connection_string;

.EXPORT RESET

.SET WIDTH 65531
.set titledashes off

.OS rm ../output/object_extracts/DDL/DDL_Databases.sql
.EXPORT FILE = ../output/object_extracts/DDL/DDL_Databases.sql
.SET WIDTH 65531
SELECT 'CREATE DATABASE ' || TRIM(T1.DATABASENAME) || ' FROM DBC AS PERM = 100000000;' "--" FROM DBC.DATABASESV T1
WHERE include_databases AND exclude_databases GROUP BY 1 ORDER BY 1;
.EXPORT RESET

.quit 0;
151 changes: 0 additions & 151 deletions Teradata/scripts_template/create_ddls.btq

This file was deleted.

19 changes: 19 additions & 0 deletions Teradata/scripts_template/create_functions.btq
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.LOGON connection_string;

.EXPORT RESET

.SET WIDTH 65531
.set titledashes off

.EXPORT FILE = ../temp/SHOW_Functions.sql
.SET WIDTH 65531
SELECT 'SELECT ''/* <sc-function> '' || ''' || TRIM(T1.DATABASENAME) || '.' || TRIM(T1.SpecificNAME) || ' </sc-function> */'' as "--"; ' || 'SHOW FUNCTION ' || TRIM(T1.DATABASENAME) || '.' || TRIM(T1.FUNCTIONNAME) || ';' "---"
FROM DBC.FUNCTIONSV T1 WHERE include_databases AND exclude_databases GROUP BY 1;
.EXPORT RESET
.OS rm ../output/object_extracts/DDL/DDL_Functions.sql
.EXPORT FILE = ../output/object_extracts/DDL/DDL_Functions.sql
.SET WIDTH 65531
.RUN FILE = ../temp/SHOW_Functions.sql
.EXPORT RESET

.quit 0;
18 changes: 18 additions & 0 deletions Teradata/scripts_template/create_join_indexes.btq
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.LOGON connection_string;

.EXPORT RESET

.SET WIDTH 65531
.set titledashes off

.EXPORT FILE = ../temp/SHOW_Join_Indexes.sql
.SET WIDTH 65531
SELECT 'SELECT ''/* <sc-joinindex> '' || ''' || TRIM(T1.DATABASENAME) || '.' || TRIM(T1.TABLENAME) || ' </sc-joinindex> */'' as "--"; ' || 'SHOW JOIN INDEX ' || TRIM(T1.DATABASENAME) || '.' ||TRIM(T1.TABLENAME) || ';' "--" FROM DBC.TABLESV T1 WHERE T1.TABLEKIND IN ('I') AND include_databases AND exclude_databases AND include_objects GROUP BY 1;
.EXPORT RESET
.OS rm ../output/object_extracts/DDL/DDL_Join_Indexes.sql
.EXPORT FILE = ../output/object_extracts/DDL/DDL_Join_Indexes.sql
.SET WIDTH 65531
.RUN FILE = ../temp/SHOW_Join_Indexes.sql
.EXPORT RESET

.quit 0;
18 changes: 18 additions & 0 deletions Teradata/scripts_template/create_macros.btq
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.LOGON connection_string;

.EXPORT RESET

.SET WIDTH 65531
.set titledashes off

.EXPORT FILE = ../temp/SHOW_Macros.sql
.SET WIDTH 65531
SELECT 'SELECT ''/* <sc-macro> '' || ''' || TRIM(T1.DATABASENAME) || '.' || TRIM(T1.TABLENAME) || ' </sc-macro> */'' as "--"; ' || 'SHOW MACRO ' || TRIM(T1.DATABASENAME) || '.' || TRIM(T1.TABLENAME) || ';' "--" FROM DBC.TABLESV T1 WHERE T1.TABLEKIND = 'M' AND include_databases AND exclude_databases AND include_objects GROUP BY 1;
.EXPORT RESET
.OS rm ../output/object_extracts/DDL/DDL_Macros.sql
.EXPORT FILE = ../output/object_extracts/DDL/DDL_Macros.sql
.SET WIDTH 65531
.RUN FILE = ../temp/SHOW_Macros.sql
.EXPORT RESET

.quit 0;
18 changes: 18 additions & 0 deletions Teradata/scripts_template/create_procedures.btq
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.LOGON connection_string;

.EXPORT RESET

.SET WIDTH 65531
.set titledashes off

.EXPORT FILE = ../temp/SHOW_Procedures.sql
.SET WIDTH 65531
SELECT 'SELECT ''/* <sc-procedure> '' || ''' || TRIM(T1.DATABASENAME) || '.' || TRIM(T1.TABLENAME) || ' </sc-procedure> */'' as "--"; ' || 'SHOW PROCEDURE ' || TRIM(T1.DATABASENAME) || '.' || TRIM(T1.TABLENAME) || ';' "--" FROM DBC.TABLESV T1 WHERE T1.TABLEKIND = 'P' AND include_databases AND exclude_databases AND include_objects GROUP BY 1;
.EXPORT RESET
.OS rm ../output/object_extracts/DDL/DDL_Procedures.sql
.EXPORT FILE = ../output/object_extracts/DDL/DDL_Procedures.sql
.SET WIDTH 65531
.RUN FILE = ../temp/SHOW_Procedures.sql
.EXPORT RESET

.quit 0;
14 changes: 14 additions & 0 deletions Teradata/scripts_template/create_schemas.btq
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.LOGON connection_string;

.EXPORT RESET

.SET WIDTH 65531
.set titledashes off

.OS rm ../output/object_extracts/DDL/DDL_SF_Schemas.sql
.EXPORT FILE = ../output/object_extracts/DDL/DDL_SF_Schemas.sql
.SET WIDTH 65531
SELECT '/* <sc-schema> ' || TRIM(T1.DATABASENAME) || ' </sc-schema> */ ' || 'CREATE SCHEMA ' || TRIM(T1.DATABASENAME) || ';' "--" FROM DBC.DATABASESV T1 WHERE include_databases AND exclude_databases GROUP BY 1 ORDER BY 1;
.EXPORT RESET

.quit 0;
Loading

0 comments on commit e5ebc4a

Please sign in to comment.