diff --git a/ef-migration/infrastructure/docker-compose-infra.yml b/ef-migration/infrastructure/docker-compose-infra.yml index dc86278..c51a07a 100644 --- a/ef-migration/infrastructure/docker-compose-infra.yml +++ b/ef-migration/infrastructure/docker-compose-infra.yml @@ -12,6 +12,7 @@ services: - ACCEPT_EULA=Y - MSSQL_PID=Express - SA_PASSWORD=YourStrong@Passw0rd + - DROP_DATABASE=${DROP_DATABASE:-false} volumes: - sqlserver_data:/var/opt/mssql deploy: diff --git a/ef-migration/infrastructure/sql-server/Dockerfile b/ef-migration/infrastructure/sql-server/Dockerfile index 6118e15..af53d90 100644 --- a/ef-migration/infrastructure/sql-server/Dockerfile +++ b/ef-migration/infrastructure/sql-server/Dockerfile @@ -5,6 +5,7 @@ FROM mcr.microsoft.com/mssql/server:2022-latest ENV ACCEPT_EULA=Y \ MSSQL_PID=Express \ SA_PASSWORD=YourStrong@Passw0rd \ + DROP_DATABASE=false \ PATH="/opt/mssql-tools/bin:${PATH}" # Install sqlcmd utility diff --git a/ef-migration/infrastructure/sql-server/full_schemas/dbo_schema_database_statements.sql b/ef-migration/infrastructure/sql-server/schemas/dbo_schema_database_statements.sql similarity index 100% rename from ef-migration/infrastructure/sql-server/full_schemas/dbo_schema_database_statements.sql rename to ef-migration/infrastructure/sql-server/schemas/dbo_schema_database_statements.sql diff --git a/ef-migration/infrastructure/sql-server/full_schemas/fp_schema_database_statements.sql b/ef-migration/infrastructure/sql-server/schemas/fp_schema_database_statements.sql similarity index 100% rename from ef-migration/infrastructure/sql-server/full_schemas/fp_schema_database_statements.sql rename to ef-migration/infrastructure/sql-server/schemas/fp_schema_database_statements.sql diff --git a/ef-migration/infrastructure/sql-server/full_schemas/fw_schema_database_statements.sql b/ef-migration/infrastructure/sql-server/schemas/fw_schema_database_statements.sql similarity index 100% rename from ef-migration/infrastructure/sql-server/full_schemas/fw_schema_database_statements.sql rename to ef-migration/infrastructure/sql-server/schemas/fw_schema_database_statements.sql diff --git a/ef-migration/infrastructure/sql-server/full_schemas/int_schema_database_statements.sql b/ef-migration/infrastructure/sql-server/schemas/int_schema_database_statements.sql similarity index 100% rename from ef-migration/infrastructure/sql-server/full_schemas/int_schema_database_statements.sql rename to ef-migration/infrastructure/sql-server/schemas/int_schema_database_statements.sql diff --git a/ef-migration/infrastructure/sql-server/full_schemas/ob_schema_database_statements.sql b/ef-migration/infrastructure/sql-server/schemas/ob_schema_database_statements.sql similarity index 100% rename from ef-migration/infrastructure/sql-server/full_schemas/ob_schema_database_statements.sql rename to ef-migration/infrastructure/sql-server/schemas/ob_schema_database_statements.sql diff --git a/ef-migration/infrastructure/sql-server/setup.sh b/ef-migration/infrastructure/sql-server/setup.sh index 9e5d456..3730046 100644 --- a/ef-migration/infrastructure/sql-server/setup.sh +++ b/ef-migration/infrastructure/sql-server/setup.sh @@ -21,47 +21,87 @@ do fi done -# 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 - -# Run all schema scripts in the schemas directory -echo "Running schema scripts..." -for f in /usr/src/app/schemas/*.sql -do - echo "Processing $f..." - - # Create a temporary file - temp_file=$(mktemp) - - # Process the file: add newlines around GO statements - sed 's/GO/\nGO\n/g' "$f" > "$temp_file" - - # Execute the processed file - sqlcmd -S localhost -U sa -P $SA_PASSWORD -d st-database -i "$temp_file" - if [ $? -ne 0 ]; then - echo "Warning: Script $f had some errors but continuing..." - rm "$temp_file" # Clean up temp file - continue +# Check if we should drop the database +if [ "${DROP_DATABASE,,}" = "true" ]; then + echo "DROP_DATABASE is true. Attempting to drop database..." + sqlcmd -S localhost -U sa -P $SA_PASSWORD -Q " + IF EXISTS (SELECT 1 FROM sys.databases WHERE name = 'st-database') + BEGIN + ALTER DATABASE [st-database] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; + DROP DATABASE [st-database]; + END" + if [ $? -eq 0 ]; then + echo "Database dropped successfully." + else + echo "Error dropping database" + exit 1 fi - - # Clean up temp file - rm "$temp_file" -done +fi -echo "All scripts executed. SQL Server is ready." +# Check if database already exists and has tables +echo "Checking if database is already initialized..." +DB_INITIALIZED=$(sqlcmd -S localhost -U sa -P $SA_PASSWORD -Q " +IF EXISTS ( + SELECT 1 + FROM sys.databases + WHERE name = 'st-database' + AND EXISTS ( + SELECT 1 + FROM st-database.sys.tables + WHERE name = 'AMAssumptionView' + ) +) +SELECT 1 +ELSE +SELECT 0" -h -1) + +if [ "$DB_INITIALIZED" = "1" ]; then + echo "Database st-database already exists and is initialized. Skipping setup." +else + 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 + + # Run all schema scripts in the schemas directory + echo "Running schema scripts..." + for f in /usr/src/app/schemas/*.sql + do + echo "Processing $f..." + + # Create a temporary file + temp_file=$(mktemp) + + # Process the file: add newlines around GO statements + sed 's/GO/\nGO\n/g' "$f" > "$temp_file" + + # Execute the processed file + sqlcmd -S localhost -U sa -P $SA_PASSWORD -d st-database -i "$temp_file" + if [ $? -ne 0 ]; then + echo "Warning: Script $f had some errors but continuing..." + rm "$temp_file" # Clean up temp file + continue + fi + + # Clean up temp file + rm "$temp_file" + done + + echo "All scripts executed. SQL Server is ready." +fi # Keep container running while true; do diff --git a/ef-migration/infrastructure/sql-server/schemas/dbo_schema_database_statements_small.sql b/ef-migration/infrastructure/sql-server/small_schemas/dbo_schema_database_statements_small.sql similarity index 100% rename from ef-migration/infrastructure/sql-server/schemas/dbo_schema_database_statements_small.sql rename to ef-migration/infrastructure/sql-server/small_schemas/dbo_schema_database_statements_small.sql diff --git a/ef-migration/infrastructure/sql-server/schemas/fp_schema_database_statements_small.sql b/ef-migration/infrastructure/sql-server/small_schemas/fp_schema_database_statements_small.sql similarity index 100% rename from ef-migration/infrastructure/sql-server/schemas/fp_schema_database_statements_small.sql rename to ef-migration/infrastructure/sql-server/small_schemas/fp_schema_database_statements_small.sql diff --git a/ef-migration/infrastructure/sql-server/schemas/fw_schema_database_statements_small.sql b/ef-migration/infrastructure/sql-server/small_schemas/fw_schema_database_statements_small.sql similarity index 100% rename from ef-migration/infrastructure/sql-server/schemas/fw_schema_database_statements_small.sql rename to ef-migration/infrastructure/sql-server/small_schemas/fw_schema_database_statements_small.sql diff --git a/ef-migration/infrastructure/sql-server/schemas/int_schema_database_statements_small.sql b/ef-migration/infrastructure/sql-server/small_schemas/int_schema_database_statements_small.sql similarity index 100% rename from ef-migration/infrastructure/sql-server/schemas/int_schema_database_statements_small.sql rename to ef-migration/infrastructure/sql-server/small_schemas/int_schema_database_statements_small.sql diff --git a/ef-migration/infrastructure/sql-server/schemas/ob_schema_database_statements_small.sql b/ef-migration/infrastructure/sql-server/small_schemas/ob_schema_database_statements_small.sql similarity index 100% rename from ef-migration/infrastructure/sql-server/schemas/ob_schema_database_statements_small.sql rename to ef-migration/infrastructure/sql-server/small_schemas/ob_schema_database_statements_small.sql