diff --git a/packages/server/scripts/integrations/mssql/data/Dockerfile b/packages/server/scripts/integrations/mssql/data/Dockerfile new file mode 100644 index 0000000000..8ac56409a0 --- /dev/null +++ b/packages/server/scripts/integrations/mssql/data/Dockerfile @@ -0,0 +1,9 @@ +FROM mcr.microsoft.com/mssql/server + +ENV ACCEPT_EULA=Y +ENV SA_PASSWORD=Passw0rd + +COPY ./data / + +ENTRYPOINT [ "/bin/bash", "entrypoint.sh" ] +CMD [ "/opt/mssql/bin/sqlservr" ] diff --git a/packages/server/scripts/integrations/mssql/data/entrypoint.sh b/packages/server/scripts/integrations/mssql/data/entrypoint.sh new file mode 100644 index 0000000000..04780d085e --- /dev/null +++ b/packages/server/scripts/integrations/mssql/data/entrypoint.sh @@ -0,0 +1,24 @@ +#!/bin/bash +set -e + +if [ "$1" = '/opt/mssql/bin/sqlservr' ]; then + # If this is the container's first run, initialize the application database + if [ ! -f /tmp/app-initialized ]; then + # Initialize the application database asynchronously in a background process. This allows a) the SQL Server process to be the main process in the container, which allows graceful shutdown and other goodies, and b) us to only start the SQL Server process once, as opposed to starting, stopping, then starting it again. + function initialize_app_database() { + # Wait a bit for SQL Server to start. SQL Server's process doesn't provide a clever way to check if it's up or not, and it needs to be up before we can import the application database + sleep 30s + + echo "RUNNING BUDIBASE SETUP" + + #run the setup script to create the DB and the schema in the DB + /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Passw0rd -i setup.sql + + # Note that the container has been initialized so future starts won't wipe changes to the data + touch /tmp/app-initialized + } + initialize_app_database & + fi +fi + +exec "$@" diff --git a/packages/server/scripts/integrations/mssql/data/setup.sql b/packages/server/scripts/integrations/mssql/data/setup.sql new file mode 100644 index 0000000000..f6c94ee2c1 --- /dev/null +++ b/packages/server/scripts/integrations/mssql/data/setup.sql @@ -0,0 +1,34 @@ +USE master; + +IF OBJECT_ID ('dbo.products', 'U') IS NOT NULL + DROP TABLE products; +GO +CREATE TABLE products +( + id int IDENTITY(1,1), + name varchar (20), + description varchar(30) +); +IF OBJECT_ID ('dbo.tasks', 'U') IS NOT NULL + DROP TABLE tasks; +GO +CREATE TABLE tasks +( + taskid int IDENTITY(1,1), + taskname varchar (20) +); + +INSERT products + (name, description) +VALUES + ('Bananas', 'Fruit thing'); + +INSERT products + (name, description) +VALUES + ('Meat', 'Animal thing'); + +INSERT tasks + (taskname) +VALUES + ('Processing'); diff --git a/packages/server/scripts/integrations/mssql/docker-compose.yaml b/packages/server/scripts/integrations/mssql/docker-compose.yaml new file mode 100644 index 0000000000..89222eddaa --- /dev/null +++ b/packages/server/scripts/integrations/mssql/docker-compose.yaml @@ -0,0 +1,12 @@ +version: "3.8" +services: + # password: Passw0rd + # user: sa + # database: master + mssql: + image: bb/mssql + build: + context: . + dockerfile: data/Dockerfile + ports: + - "1433:1433" diff --git a/packages/server/scripts/integrations/mssql/reset.sh b/packages/server/scripts/integrations/mssql/reset.sh new file mode 100755 index 0000000000..32778bd11f --- /dev/null +++ b/packages/server/scripts/integrations/mssql/reset.sh @@ -0,0 +1,3 @@ +#!/bin/bash +docker-compose down +docker volume prune -f