-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Italo Israel Baeza Cabrera
committed
Aug 17, 2024
1 parent
039c2ad
commit 321f30b
Showing
10 changed files
with
233 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#!/usr/bin/env bash | ||
|
||
# If there is a command, execute it. | ||
if [ "$#" -ne 0 ]; then | ||
exec "$@" | ||
fi | ||
|
||
SQLCMD="/opt/mssql-tools/bin/sqlcmd" | ||
|
||
if [ -f /opt/mssql-tools18/bin/sqlcmd ]; then | ||
SQLCMD="/opt/mssql-tools18/bin/sqlcmd" | ||
fi | ||
|
||
echo "Laravel Sail: Starting Microsoft SQL Server for init scripts." | ||
|
||
# Start SQL Server | ||
/opt/mssql/bin/sqlservr -mSQLCMD -f -c -x & | ||
|
||
# Function to check if SQL Server is ready | ||
sql_server_is_ready() { | ||
$SQLCMD -No -U sa -P "${MSSQL_SA_PASSWORD}" -Q "SELECT 1" &> /dev/null | ||
return $? | ||
} | ||
|
||
# Wait until SQL Server is ready | ||
until sql_server_is_ready; do | ||
echo "Laravel Sail: Waiting for SQL Server to be available..." | ||
sleep 2 | ||
done | ||
|
||
SQL_INIT=$(cat <<-EOSQL | ||
IF NOT EXISTS(SELECT * FROM sys.databases WHERE name = "$MSSQL_DB_NAME") BEGIN | ||
CREATE DATABASE $MSSQL_DB_NAME; | ||
END | ||
GO | ||
IF NOT EXISTS (SELECT name FROM master.sys.server_principals WHERE name = "$MSSQL_USER") BEGIN | ||
CREATE LOGIN $MSSQL_USER WITH | ||
PASSWORD = "$MSSQL_PASSWORD", | ||
DEFAULT_DATABASE = $MSSQL_DB_NAME, | ||
CHECK_EXPIRATION = OFF, | ||
CHECK_POLICY = OFF; | ||
END | ||
GO | ||
IF NOT EXISTS(SELECT * FROM sys.database_principals WHERE name = "$MSSQL_USER") BEGIN | ||
CREATE USER $MSSQL_USER FOR LOGIN $MSSQL_USER; | ||
ALTER ROLE db_owner ADD MEMBER $MSSQL_USER; | ||
END | ||
GO | ||
EOSQL | ||
) | ||
|
||
echo "Laravel Sail: Initializing ${MSSQL_DB_NAME} database for ${MSSQL_USER}." | ||
|
||
## Set the engine default user and database name | ||
$SQLCMD -No -U sa -No -P "${MSSQL_SA_PASSWORD}" -Q "${SQL_INIT}" | ||
|
||
SQL_INIT_TESTING=$(cat <<-EOSQL | ||
IF NOT EXISTS(SELECT * FROM sys.databases WHERE name = "testing") BEGIN | ||
CREATE DATABASE testing; | ||
END | ||
GO | ||
IF NOT EXISTS(SELECT * FROM sys.database_principals WHERE name = "testing") BEGIN | ||
CREATE USER testing FOR LOGIN testing WITH | ||
PASSWORD = "", | ||
DEFAULT_DATABASE = "testing", | ||
CHECK_EXPIRATION = OFF, | ||
CHECK_POLICY = OFF; | ||
ALTER ROLE db_datareader ADD MEMBER testing; | ||
ALTER ROLE db_datawriter ADD MEMBER testing; | ||
ALTER ROLE db_ddladmin ADD MEMBER testing; | ||
END | ||
GO | ||
EOSQL | ||
) | ||
|
||
echo "Laravel Sail: Initializing testing database." | ||
|
||
## Create the testing database if it doesn't exists | ||
$SQLCMD -No -U sa -P "${MSSQL_SA_PASSWORD}" -Q "${SQL_INIT_TESTING}" | ||
|
||
echo "Laravel Sail: Stopping Microsoft SQL Server for init scripts." | ||
|
||
# Kill the server | ||
pkill sqlservr | ||
|
||
echo "Laravel Sail: Starting Microsoft SQL Server." | ||
|
||
# Start it again. | ||
exec /opt/mssql/bin/sqlservr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
sqlsrv: | ||
image: 'mcr.microsoft.com/mssql/server:2022-latest' | ||
ports: | ||
- '${FORWARD_DB_PORT:-1433}:1433' | ||
environment: | ||
MSSQL_DB_NAME: '${DB_DATABASE}' | ||
MSSQL_USER: '${DB_USERNAME}' | ||
MSSQL_SA_PASSWORD: '${DB_ROOT_PASSWORD}' | ||
MSSQL_PASSWORD: '${DB_PASSWORD}' | ||
ACCEPT_EULA: '${ACCEPT_EULA}' | ||
volumes: | ||
- 'sail-sqlsrv:/var/opt/mssql' | ||
- './vendor/laravel/sail/database/sqlsrv/entrypoint.sh:/entrypoint.sh' | ||
entrypoint: "/entrypoint.sh" | ||
networks: | ||
- sail | ||
healthcheck: | ||
test: ["CMD", "/opt/mssql-tools/bin/sqlcmd", "-No", "-U", "sa", "-P", "${MSSQL_SA_PASSWORD}", "-Q", "\"SELECT 1\"", "-b", "-o", "/dev/null"] | ||
interval: 10s | ||
timeout: 5s | ||
retries: 3 | ||
start_period: 30s |