Skip to content

Instantly share code, notes, and snippets.

@danielplawgo

danielplawgo/database1.sql

Last active Mar 24, 2021
Embed
What would you like to do?
SQL Server i Docker
IF NOT EXISTS(SELECT * FROM sys.databases WHERE name = 'Database1')
BEGIN
CREATE DATABASE [Database1]
END
GO
USE [Database1]
GO
--You need to check if the table exists
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='Users' and xtype='U')
BEGIN
CREATE TABLE Users (
Id INT PRIMARY KEY IDENTITY (1, 1),
Name VARCHAR(100)
)
END
version: "3.9"
services:
db:
container_name: sql-server-db
image: mcr.microsoft.com/mssql/server:2019-latest
environment:
SA_PASSWORD: Your_password123
ACCEPT_EULA: Y
ports:
- 1433:1433
version: "3.9"
services:
db:
container_name: sql-server-db
image: mcr.microsoft.com/mssql/server:2019-latest
environment:
SA_PASSWORD: Your_password123
ACCEPT_EULA: Y
ports:
- 1433:1433
volumes:
- ./sqlserver/data:/var/opt/mssql/data
- ./sqlserver/log:/var/opt/mssql/log
version: "3.9"
services:
db:
container_name: sql-server-db
image: mcr.microsoft.com/mssql/server:2019-latest
environment:
SA_PASSWORD: Your_password123
ACCEPT_EULA: Y
ports:
- 1433:1433
volumes:
- ./sqlserver/data:/var/opt/mssql/data
- ./sqlserver/log:/var/opt/mssql/log
- ./scripts:/scripts/
command:
- /bin/bash
- -c
- |
# Launch MSSQL and send to background
/opt/mssql/bin/sqlservr &
pid=$$!
# Wait for it to be available
echo "Waiting for MS SQL to be available ⏳"
/opt/mssql-tools/bin/sqlcmd -l 30 -S localhost -h-1 -V1 -U sa -P $$SA_PASSWORD -Q "SET NOCOUNT ON SELECT \"YAY WE ARE UP\" , @@servername"
is_up=$$?
while [ $$is_up -ne 0 ] ; do
echo -e $$(date)
/opt/mssql-tools/bin/sqlcmd -l 30 -S localhost -h-1 -V1 -U sa -P $$SA_PASSWORD -Q "SET NOCOUNT ON SELECT \"YAY WE ARE UP\" , @@servername"
is_up=$$?
sleep 5
done
# Run every script in /scripts
# TODO set a flag so that this is only done once on creation,
# and not every time the container runs
for foo in /scripts/*.sql
do /opt/mssql-tools/bin/sqlcmd -U sa -P $$SA_PASSWORD -l 30 -e -i $$foo
done
# trap SIGTERM and send same to sqlservr process for clean shutdown
trap "kill -15 $$pid" SIGTERM
# Wait on the sqlserver process
echo "All scripts have been executed. Waiting for MS SQL(pid $$pid) to terminate."
# Wait on the sqlserver process
wait $$pid
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment