ADD: Se agregaron los comandos php artisan para las migraciones y los seeders, para el YAML de prod y dev

This commit is contained in:
Juan Felipe Zapata Moreno 2025-09-11 16:22:03 -06:00
parent 7fff56f592
commit 7503b2beda
7 changed files with 139 additions and 11 deletions

View File

@ -34,7 +34,10 @@ DB_PORT=3306
DB_DATABASE=holos-backend DB_DATABASE=holos-backend
DB_USERNAME=notsoweb DB_USERNAME=notsoweb
DB_PASSWORD= DB_PASSWORD=
PMA_PORT=8081 # Port para phpMyAdmin PMA_PORT=8081 # Puerto para phpMyAdmin
REDIS_PORT=6379 # Puerto para Redis
NGINX_PORT=8080 # Puerto para Nginx
SESSION_DRIVER=database SESSION_DRIVER=database
SESSION_LIFETIME=120 SESSION_LIFETIME=120

33
bd-init.sh Normal file
View File

@ -0,0 +1,33 @@
#!/bin/bash
# Esperar a que MySQL esté disponible
echo "Esperando conexión a MySQL..."
wait_for_mysql() {
echo "Esperando a que MySQL esté disponible..."
until php -r "
try {
\$pdo = new PDO('mysql:host=${DB_HOST};port=${DB_PORT}', '${DB_USERNAME}', '${DB_PASSWORD}');
echo 'MySQL está disponible' . PHP_EOL;
exit(0);
} catch (PDOException \$e) {
echo 'MySQL no disponible: ' . \$e->getMessage() . PHP_EOL;
exit(1);
}
"; do
echo "Esperando MySQL..."
sleep 2
done
}
# Esperar a MySQL
wait_for_mysql
echo "MySQL conectado, ejecutando migraciones..."
# Ejecutar migraciones y seeders
php artisan migrate:fresh --seeder=DevSeeder --force
php artisan passport:client --personal --name=Holos
# Iniciar PHP-FPM
exec php-fpm

View File

@ -0,0 +1,61 @@
<?php
use Database\Seeders\DevSeeder;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
// Solo ejecutar en desarrollo y si no existen datos
if ($this->shouldSeed()) {
(new DevSeeder)->run();
$this->createPassportClient();
}
}
/**
* Reverse the migrations.
*/
public function down(): void
{
}
/**
* Verificar si debe ejecutar el seeding
*/
private function shouldSeed(): bool
{
// Verificar si ya existen usuarios del sistema
$usersExist = DB::table('users')
->whereIn('email', ['developer@notsoweb.com', 'admin@notsoweb.com'])
->exists();
return !$usersExist;
}
/**
* Crear cliente Passport si no existe
*/
private function createPassportClient(): void
{
// Verificar si ya existe un cliente personal con el nombre "Holos"
$clientExists = DB::table('oauth_clients')
->where('name', 'Holos')
->where('personal_access_client', true)
->exists();
if (!$clientExists) {
Artisan::call('passport:client', [
'--personal' => true,
'--name' => 'Holos',
'--no-interaction' => true
]);
}
}
};

View File

@ -10,13 +10,15 @@ services:
networks: networks:
- holos-network - holos-network
depends_on: depends_on:
- mysql mysql:
- redis condition: service_healthy
redis:
condition: service_healthy
nginx: nginx:
image: nginx:alpine image: nginx:alpine
ports: ports:
- "8080:80" - "${NGINX_PORT}:80"
volumes: volumes:
- ./public:/var/www/holos.backend/public - ./public:/var/www/holos.backend/public
- ./Docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf - ./Docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
@ -38,6 +40,10 @@ services:
- mysql_data:/var/lib/mysql - mysql_data:/var/lib/mysql
networks: networks:
- holos-network - holos-network
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
timeout: 15s
retries: 10
phpmyadmin: phpmyadmin:
image: phpmyadmin/phpmyadmin image: phpmyadmin/phpmyadmin
@ -53,10 +59,16 @@ services:
redis: redis:
image: redis:alpine image: redis:alpine
ports:
- "${REDIS_PORT}:6379"
volumes: volumes:
- redis_data:/data - redis_data:/data
networks: networks:
- holos-network - holos-network
healthcheck:
test: ["CMD", "redis-cli", "ping"]
timeout: 5s
retries: 5
volumes: volumes:
mysql_data: mysql_data:

View File

@ -10,13 +10,15 @@ services:
networks: networks:
- holos-network - holos-network
depends_on: depends_on:
- mysql mysql:
- redis condition: service_healthy
redis:
condition: service_healthy
nginx: nginx:
image: nginx:alpine image: nginx:alpine
ports: ports:
- "8080:80" - "${NGINX_PORT}:80"
volumes: volumes:
- ./public:/var/www/holos.backend/public - ./public:/var/www/holos.backend/public
- ./Docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf - ./Docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
@ -38,6 +40,10 @@ services:
- mysql_data:/var/lib/mysql - mysql_data:/var/lib/mysql
networks: networks:
- holos-network - holos-network
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
timeout: 15s
retries: 10
phpmyadmin: phpmyadmin:
image: phpmyadmin/phpmyadmin image: phpmyadmin/phpmyadmin
@ -53,10 +59,16 @@ services:
redis: redis:
image: redis:alpine image: redis:alpine
ports:
- "${REDIS_PORT}:6379"
volumes: volumes:
- redis_data:/data - redis_data:/data
networks: networks:
- holos-network - holos-network
healthcheck:
test: ["CMD", "redis-cli", "ping"]
timeout: 5s
retries: 5
volumes: volumes:
mysql_data: mysql_data:

View File

@ -24,6 +24,11 @@ RUN composer install --no-dev --no-scripts --optimize-autoloader --no-interactio
COPY . . COPY . .
RUN php artisan package:discover --ansi \
&& php artisan storage:link --force || true
RUN chown -R www-data:www-data /var/www/holos.backend/storage /var/www/holos.backend/bootstrap/cache
EXPOSE 9000 EXPOSE 9000
CMD ["php-fpm"] CMD ["sh", "-c", "sleep 10 && php artisan migrate --force && php artisan migrate --path=database/migrations/path --force && php-fpm"]

View File

@ -25,9 +25,11 @@ RUN composer install --optimize-autoloader --no-interaction --no-scripts
COPY . . COPY . .
# Ejecutar los scripts de Laravel después de copiar el código RUN php artisan package:discover --ansi \
RUN composer run-script post-autoload-dump && php artisan storage:link --force || true
RUN chown -R www-data:www-data /var/www/holos.backend/storage /var/www/holos.backend/bootstrap/cache
EXPOSE 9000 EXPOSE 9000
CMD ["php-fpm"] CMD ["sh", "-c", "sleep 10 && php artisan migrate --force && php artisan migrate --path=database/migrations/path --force && php-fpm"]