Adjust database initialization logic

This commit is contained in:
Ubuntu 2025-02-06 00:05:35 +00:00
parent b32845f656
commit b12cc774e2

View File

@ -36,118 +36,113 @@ if [ "${DROP_DATABASE,,}" = "true" ]; then
echo "Error dropping database" echo "Error dropping database"
exit 1 exit 1
fi fi
fi
# Check if database already exists and has tables # Check if database already exists and has tables
echo "Checking if database is already initialized..." echo "Checking if database is already initialized..."
echo "Checking if database is already initialized..."
DB_INITIALIZED=$(sqlcmd -S localhost -U sa -P $SA_PASSWORD -h -1 -Q "
DECLARE @DBExists BIT = 0;
IF EXISTS (SELECT 1 FROM sys.databases WHERE name = 'st-database') # First check if database exists
BEGIN DB_EXISTS=$(sqlcmd -S localhost -U sa -P $SA_PASSWORD -h -1 -Q "
-- Set the context to the target database IF EXISTS (SELECT 1 FROM sys.databases WHERE name = 'st-database')
EXEC('USE [st-database]; SELECT 1
IF EXISTS (SELECT 1 FROM sys.tables WHERE name = ''AMAssumptionView'') ELSE
BEGIN SELECT 0" | tr -d '[:space:]')
SET @DBExists = 1;
END');
END;
SELECT @DBExists;") echo "Database exists check result: '$DB_EXISTS'"
if [ "$DB_INITIALIZED" = "1" ]; then if [ "$DB_EXISTS" = "1" ]; then
echo "Database st-database already exists and is initialized. Skipping setup." echo "Database st-database already exists and is initialized. Skipping setup."
else else
echo "Database needs initialization. Starting setup..." echo "Database needs initialization. Starting setup..."
# Create st-database if it doesn't exist
echo "Creating st-database if it doesn't exist..."
sqlcmd -S localhost -U sa -P $SA_PASSWORD -Q "IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = 'st-database') CREATE DATABASE [st-database]"
if [ $? -ne 0 ]; then
echo "Error creating database"
exit 1
fi
# Run the initialization script
echo "Running initialization script..."
sqlcmd -S localhost -U sa -P $SA_PASSWORD -d st-database -i /usr/src/app/init.sql
if [ $? -ne 0 ]; then
echo "Error executing initialization script"
exit 1
fi
# Clean up existing files in sql_objects directories
echo "Cleaning up existing files..."
rm -f /usr/src/app/sql_objects/*/*.sql
rm -f /usr/src/app/sql_objects/indexes/*/*.sql
# Process each schema file through the extraction scripts
echo "Processing schema files..."
for schema_file in /usr/src/app/schemas/*.sql
do
echo "Processing schema file: $schema_file"
# Extract different types of SQL objects # Create st-database if it doesn't exist
bash /usr/src/app/extract_tables.sh "$schema_file" echo "Creating st-database if it doesn't exist..."
sqlcmd -S localhost -U sa -P $SA_PASSWORD -Q "IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = 'st-database') CREATE DATABASE [st-database]"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Warning: Table extraction had some errors but continuing..." echo "Error creating database"
exit 1
fi fi
bash /usr/src/app/extract_constraints.sh "$schema_file"
bash /usr/src/app/extract_indexes.sh "$schema_file"
bash /usr/src/app/extract_views.sh "$schema_file"
bash /usr/src/app/extract_procedures.sh "$schema_file"
done
# Function to execute SQL files from a directory # Run the initialization script
execute_sql_files() { echo "Running initialization script..."
local dir=$1 sqlcmd -S localhost -U sa -P $SA_PASSWORD -d st-database -i /usr/src/app/init.sql
local type=$2 if [ $? -ne 0 ]; then
echo "Error executing initialization script"
exit 1
fi
# Clean up existing files in sql_objects directories
echo "Cleaning up existing files..."
rm -f /usr/src/app/sql_objects/*/*.sql
rm -f /usr/src/app/sql_objects/indexes/*/*.sql
if [ -d "$dir" ] && [ "$(ls -A $dir 2>/dev/null)" ]; then # Process each schema file through the extraction scripts
echo "Executing $type..." echo "Processing schema files..."
for sql_file in "$dir"/*.sql; do for schema_file in /usr/src/app/schemas/*.sql
if [ -f "$sql_file" ]; then do
echo "Executing $sql_file..." echo "Processing schema file: $schema_file"
sqlcmd -S localhost -U sa -P $SA_PASSWORD -d st-database -i "$sql_file"
if [ $? -ne 0 ]; then # Extract different types of SQL objects
echo "Warning: Error executing $sql_file but continuing..." bash /usr/src/app/extract_tables.sh "$schema_file"
if [ $? -ne 0 ]; then
echo "Warning: Table extraction had some errors but continuing..."
fi
bash /usr/src/app/extract_constraints.sh "$schema_file"
bash /usr/src/app/extract_indexes.sh "$schema_file"
bash /usr/src/app/extract_views.sh "$schema_file"
bash /usr/src/app/extract_procedures.sh "$schema_file"
done
# Function to execute SQL files from a directory
execute_sql_files() {
local dir=$1
local type=$2
if [ -d "$dir" ] && [ "$(ls -A $dir 2>/dev/null)" ]; then
echo "Executing $type..."
for sql_file in "$dir"/*.sql; do
if [ -f "$sql_file" ]; then
echo "Executing $sql_file..."
sqlcmd -S localhost -U sa -P $SA_PASSWORD -d st-database -i "$sql_file"
if [ $? -ne 0 ]; then
echo "Warning: Error executing $sql_file but continuing..."
fi
fi fi
fi done
done else
else echo "No files found in $dir"
echo "No files found in $dir" fi
fi }
}
# Execute SQL objects in the correct order # Execute SQL objects in the correct order
echo "Executing SQL objects in order..." echo "Executing SQL objects in order..."
# 1. Tables # 1. Tables
execute_sql_files "sql_objects/tables" "tables" execute_sql_files "sql_objects/tables" "tables"
# 2. Constraints # 2. Constraints
execute_sql_files "sql_objects/constraints" "constraints" execute_sql_files "sql_objects/constraints" "constraints"
execute_sql_files "sql_objects/foreign_keys" "foreign keys" execute_sql_files "sql_objects/foreign_keys" "foreign keys"
# 3. Indexes # 3. Indexes
execute_sql_files "sql_objects/indexes/clustered" "clustered indexes" execute_sql_files "sql_objects/indexes/clustered" "clustered indexes"
execute_sql_files "sql_objects/indexes/unique_clustered" "unique clustered indexes" execute_sql_files "sql_objects/indexes/unique_clustered" "unique clustered indexes"
execute_sql_files "sql_objects/indexes/nonclustered" "nonclustered indexes" execute_sql_files "sql_objects/indexes/nonclustered" "nonclustered indexes"
# 4. Views # 4. Views
execute_sql_files "sql_objects/views" "views" execute_sql_files "sql_objects/views" "views"
# 5. Stored Procedures # 5. Stored Procedures
execute_sql_files "sql_objects/procedures" "stored procedures" execute_sql_files "sql_objects/procedures" "stored procedures"
# Clean up # Clean up
echo "Cleaning up..." echo "Cleaning up..."
rm -rf sql_objects rm -rf sql_objects
echo "SQL object execution completed" echo "SQL object execution completed"
fi
fi fi
# Keep container running # Keep container running
while true; do while true; do
sleep 1 sleep 1