From eb2de1ff122581b92f47f0d7132e0e65facadd9c Mon Sep 17 00:00:00 2001 From: Jorge Burgos Date: Mon, 3 Feb 2025 20:38:08 -0500 Subject: [PATCH] adjust setup and init files --- .../infrastructure/sql-server/init.sql | 36 +++++++++---- .../infrastructure/sql-server/setup.sh | 52 +++++++++++++++++++ 2 files changed, 77 insertions(+), 11 deletions(-) diff --git a/ef-migration/infrastructure/sql-server/init.sql b/ef-migration/infrastructure/sql-server/init.sql index 1c752a2..c433681 100644 --- a/ef-migration/infrastructure/sql-server/init.sql +++ b/ef-migration/infrastructure/sql-server/init.sql @@ -8,23 +8,37 @@ GO USE [st-database] GO --- Create schemas if they don't exist -IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'dbo') - EXEC('CREATE SCHEMA [dbo]') -GO +-- Create schemas (excluding dbo since it already exists) +DECLARE @SQL nvarchar(max) IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'fp') - EXEC('CREATE SCHEMA [fp]') -GO +BEGIN + SET @SQL = 'CREATE SCHEMA fp AUTHORIZATION dbo' + EXEC sp_executesql @SQL + PRINT 'Created schema fp' +END IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'fw') - EXEC('CREATE SCHEMA [fw]') -GO +BEGIN + SET @SQL = 'CREATE SCHEMA fw AUTHORIZATION dbo' + EXEC sp_executesql @SQL + PRINT 'Created schema fw' +END IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'int') - EXEC('CREATE SCHEMA [int]') -GO +BEGIN + SET @SQL = 'CREATE SCHEMA int AUTHORIZATION dbo' + EXEC sp_executesql @SQL + PRINT 'Created schema int' +END IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'ob') - EXEC('CREATE SCHEMA [ob]') +BEGIN + SET @SQL = 'CREATE SCHEMA ob AUTHORIZATION dbo' + EXEC sp_executesql @SQL + PRINT 'Created schema ob' +END + +-- Verify schemas +SELECT name FROM sys.schemas WHERE name IN ('fp', 'fw', 'int', 'ob') GO \ No newline at end of file diff --git a/ef-migration/infrastructure/sql-server/setup.sh b/ef-migration/infrastructure/sql-server/setup.sh index e69de29..e3a732a 100644 --- a/ef-migration/infrastructure/sql-server/setup.sh +++ b/ef-migration/infrastructure/sql-server/setup.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +# Start SQL Server +/opt/mssql/bin/sqlservr & + +# Wait for SQL Server to start +echo "Waiting for SQL Server to start..." +sleep 30s + +# Initialize database +for i in {1..50}; +do + sqlcmd -S localhost -U sa -P $SA_PASSWORD -Q "SELECT 1" &> /dev/null + if [ $? -eq 0 ] + then + echo "SQL Server is ready" + break + else + echo "SQL Server is not ready yet..." + sleep 1 + fi +done + +# Run the initialization script +echo "Running initialization script..." +sqlcmd -S localhost -U sa -P $SA_PASSWORD -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..." + sqlcmd -S localhost -U sa -P $SA_PASSWORD -i "$f" -b + if [ $? -ne 0 ]; then + echo "Error executing schema script $f" + exit 1 + fi + + # Verify schema creation + sqlcmd -S localhost -U sa -P $SA_PASSWORD -Q "SELECT name FROM sys.schemas WHERE name IN ('fp', 'fw', 'int', 'ob')" -h -1 +done + +echo "All scripts executed. SQL Server is ready." + +# Keep container running +while true; do + sleep 1 +done \ No newline at end of file