Skip to content

Commit

Permalink
🚧 Attempting to set up test database with intializer.
Browse files Browse the repository at this point in the history
  • Loading branch information
klondikemarlen committed Aug 8, 2023
1 parent f1022e0 commit e3c3e02
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 6 deletions.
32 changes: 32 additions & 0 deletions db/configure-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

# Wait 60 seconds for SQL Server to start up by ensuring that
# calling SQLCMD does not return an error code, which will ensure that sqlcmd is accessible
# and that system and user databases return "0" which means all databases are in an "online" state
# https://docs.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-databases-transact-sql?view=sql-server-2017

DBSTATUS=1
ERRCODE=1
i=0

while [[ $DBSTATUS -ne 0 ]] && [[ $i -lt 60 ]] && [[ $ERRCODE -ne 0 ]]; do
i=$i+1
DBSTATUS=$(/opt/mssql-tools/bin/sqlcmd -h -1 -t 1 -U sa -P $SA_PASSWORD -Q "SET NOCOUNT ON; Select SUM(state) from sys.databases")
ERRCODE=$?
sleep 1
done

if [ $DBSTATUS -ne 0 ] OR [ $ERRCODE -ne 0 ]; then
echo "SQL Server took more than 60 seconds to start up or one or more databases are not in an ONLINE state"
exit 1
fi

echo "\$SQ_PASSWORD=$SA_PASSWORD"
echo "\$MSSQL_SQ_PASSWORD=$MSSQL_SQ_PASSWORD"
# Run the setup script to create the DB and the schema in the DB
/opt/mssql-tools/bin/sqlcmd \
-S localhost \
-U sa \
-P $SA_PASSWORD \
-d master \
-i setup.sql
18 changes: 18 additions & 0 deletions db/db.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM mcr.microsoft.com/mssql/server:2019-latest

# current user:group => $whoami:$(id -gn) => mssql:root
USER root

RUN mkdir -p /usr/config

WORKDIR /usr/config

COPY . .

RUN chown -R mssql:root /usr/config
RUN chmod +x ./entrypoint.sh
RUN chmod +x ./configure-db.sh

USER mssql

ENTRYPOINT ["./entrypoint.sh"]
7 changes: 7 additions & 0 deletions db/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# Start the script to create the DB and user
/usr/config/configure-db.sh &

# Start SQL Server
/opt/mssql/bin/sqlservr
21 changes: 21 additions & 0 deletions db/setup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Enter custom T-SQL here that would run after SQL Server has started up.
*/

IF NOT EXISTS (
SELECT name
FROM sys.databases
WHERE name = N'SFADB_DEV'
)
CREATE DATABASE SFADB_DEV;
GO

IF NOT EXISTS (
SELECT name
FROM sys.databases
WHERE name = N'sfa_client_test'
)
CREATE DATABASE sfa_client_test;
GO
26 changes: 20 additions & 6 deletions docker-compose.development.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ services:
dockerfile: development.Dockerfile
env_file:
- ./src/api/.env.development
environment:
environment: &environment-definition
NODE_ENV: development
DB_NAME: SFADB_DEV
DB_HOST: db
DB_USER: sa
DB_PASS: &password-definition Testing1122
DB_PORT: 1433
DB_DEFAULT_SCHEMA: sfa
API_PORT: ${API_PORT:-3000}
BASE_URL: "http://localhost:${API_PORT:-3000}"
Expand All @@ -19,19 +23,29 @@ services:
volumes:
- ./src/api:/usr/src/api
depends_on:
- db
db:
condition: service_healthy

db:
image: mcr.microsoft.com/mssql/server:2019-latest
build:
context: .
dockerfile: ./db/db.Dockerfile
ports:
- "1433:1433"
user: root
env_file:
- db/sqlserver.env
- db/sapassword.env
environment:
MSSQL_SA_PASSWORD: *password-definition
ACCEPT_EULA: "Y"
volumes:
- ./db/data:/var/opt/mssql/data
- ./db/log:/var/opt/mssql/log
- ./db/backups:/var/opt/mssql/data/backups
healthcheck: # https://github.com/Microsoft/mssql-docker/issues/133
test: "/opt/mssql-tools/bin/sqlcmd -U sa -P \"$$MSSQL_SA_PASSWORD\" -Q \"SELECT 1\" || exit 1"
interval: 5s
timeout: 10s
retries: 3
start_period: 5s
s3:
image: "minio/minio:latest"
ports:
Expand Down

0 comments on commit e3c3e02

Please sign in to comment.