Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update import-data.sh #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
FROM mcr.microsoft.com/mssql/server

FROM mcr.microsoft.com/mssql/server:2022-CU14-ubuntu-22.04
# Switch to root user for access to apt-get install
USER root

Expand Down
4 changes: 2 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#start SQL Server, start the script to create the DB and import the data, start the app
/opt/mssql/bin/sqlservr & /usr/src/app/import-data.sh & npm start
# run mport-data.sh in background and sqlservr in foreground, and start the app
/usr/src/app/import-data.sh & /opt/mssql/bin/sqlservr & npm start
44 changes: 35 additions & 9 deletions import-data.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
#run the setup script to create the DB and the schema in the DB
#do this in a loop because the timing for when the SQL instance is ready is indeterminate
for i in {1..50};
do
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Yukon900 -d master -i setup.sql
if [ $? -eq 0 ]
then
echo "setup.sql completed"
#!/bin/bash

LOG_FILE="/tmp/mssql-log.txt"

# Function to log messages
log_message() {
local message="$1"
echo "$(date +"%Y-%m-%d %T"): $message" >> "$LOG_FILE"
}

# Wait for SQL Server to be started
for i in {1..100}; do
/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P Yukon900 -d master -Q "SELECT 1" -C
if [ $? -eq 0 ]; then
log_message "SQL Server is up and running"
break
else
echo "not ready yet..."
sleep 1
log_message "Waiting for SQL Server to be ready... Attempt $i"
sleep 5
fi
done

# Check if SQL Server is up and running
/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P Yukon900 -d master -Q "SELECT 1" -C
if [ $? -ne 0 ]; then
log_message "SQL Server is not accessible after waiting. Exiting."
exit 1
fi

# Run the init script
/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P Yukon900 -d master -i setup.sql -C
if [ $? -eq 0 ]; then
log_message "init.sql setup completed successfully"
else
log_message "init.sql setup failed"
fi

#import the data from the csv file
/opt/mssql-tools/bin/bcp DemoData.dbo.Products in "/usr/src/app/Products.csv" -c -t',' -S localhost -U sa -P Yukon900
/opt/mssql-tools18/bin/bcp DemoData.dbo.Products in "/usr/src/app/Products.csv" -c -t',' -S localhost -U sa -P Yukon900