diff --git a/.devcontainer/db-init.sql b/.devcontainer/db-init.sql index 0960e6d..ea1a65d 100644 --- a/.devcontainer/db-init.sql +++ b/.devcontainer/db-init.sql @@ -1,19 +1,22 @@ -CREATE DATABASE heroes; -GO; - +IF NOT EXISTS(SELECT * FROM sys.databases WHERE name = 'heroes') +BEGIN + CREATE DATABASE heroes; +END +GO USE heroes; -GO; - +GO -CREATE TABLE Heroes ( - Id INT PRIMARY KEY, - Name NVARCHAR(100), - AlterEgo NVARCHAR(100), - Description NVARCHAR(255) -); - -GO; +IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Heroes') +BEGIN + CREATE TABLE Heroes ( + Id INT PRIMARY KEY, + Name NVARCHAR(100), + AlterEgo NVARCHAR(100), + Description NVARCHAR(255) + ); +END +GO INSERT INTO Heroes (Id, Name, AlterEgo, Description) VALUES (1, 'Batman', 'Bruce Wayne', 'A wealthy American playboy, philanthropist, and owner of Wayne Enterprises.'), @@ -21,5 +24,4 @@ INSERT INTO Heroes (Id, Name, AlterEgo, Description) VALUES (3, 'Wonder Woman', 'Diana Prince', 'A demigoddess and warrior princess of the Amazons.'), (4, 'Flash', 'Barry Allen', 'A superhero with the power of super speed.'), (5, 'Green Lantern', 'Hal Jordan', 'A test pilot who was chosen to become the first human member of the Green Lantern Corps.'); - -GO; \ No newline at end of file +GO \ No newline at end of file diff --git a/.devcontainer/docker-compose.yaml b/.devcontainer/docker-compose.yaml index 2a85c62..296d40f 100644 --- a/.devcontainer/docker-compose.yaml +++ b/.devcontainer/docker-compose.yaml @@ -27,17 +27,18 @@ services: ACCEPT_EULA: Y init-db: image: mcr.microsoft.com/azure-sql-edge - command: > - /bin/bash -c " - # Comando para esperar a que SQL esté listo (pseudocódigo) - while ! sqlcmd -U sa -P P@ssword -Q 'SELECT 1'; do - sleep 1 - done; - # Comando para inicializar la base de datos, reemplazar con tu script real - sqlcmd -U sa -P P@ssword -i /init/db-init.sql; - " + command: > + /bin/bash -c " + while ! /opt/mssql-tools/bin/sqlcmd -S db -U sa -P 'P@ssword' -Q 'SELECT * FROM sys.databases' > /dev/null 2>&1; do + echo 'Waiting for SQL Server to become available...' + sleep 1 + done; + echo 'SQL Server is now available.' + + /opt/mssql-tools/bin/sqlcmd -S db -U sa -P 'P@ssword' -i /tmp/db-init.sql; + " volumes: - - ./db-init.sql:/init/db-init.sql + - ./db-init.sql:/tmp/db-init.sql environment: SA_PASSWORD: P@ssword ACCEPT_EULA: Y