sql >> Base de Datos >  >> RDS >> Mysql

Laravel Sail reconstruye la base de datos predeterminada

Caso:reutilización de un volumen de Docker ya creado

Si dejas de Navegar con sail down , el volumen de datos permanece en el host de Docker sin eliminarse.

Cuando se detiene la navegación, use sail down -v para eliminar los datos de volumen de Docker existentes.

Primero sail up , DB_DATABASE=forjar

Cuando inicia Sail por primera vez, se crea un volumen en el host de Docker.

grep DB_DATABASE .env
DB_DATABASE=forge

docker volume ls
DRIVER    VOLUME NAME

sail up -d
Creating network "test_sail" with driver "bridge"
Creating volume "test_sailmysql" with local driver
Creating volume "test_sailredis" with local driver
Creating test_mailhog_1 ... done
Creating test_mysql_1   ... done
Creating test_redis_1   ... done
Creating test_laravel.test_1 ... done

docker volume ls
DRIVER    VOLUME NAME
local     test_sailmysql
local     test_sailredis

sail mysql
mysql> show databases;
| forge              |

Sin embargo, cuando salgo de Sail, el contenedor Docker se elimina pero el volumen no se elimina.

sail down
Stopping test_laravel.test_1 ... done
Stopping test_mailhog_1      ... done
Stopping test_redis_1        ... done
Stopping test_mysql_1        ... done
Removing test_laravel.test_1 ... done
Removing test_mailhog_1      ... done
Removing test_redis_1        ... done
Removing test_mysql_1        ... done
Removing network test_sail

docker volume ls
DRIVER    VOLUME NAME
local     test_sailmysql
local     test_sailredis

Segundo sail up , DB_DATABASE=prueba

Si inicia un segundo Sail en el mismo nombre de directorio, se reutilizará el volumen de Docker ya creado.

grep DB_DATABASE .env
DB_DATABASE=test

sail up -d
Creating network "test_sail" with driver "bridge"
Creating test_mysql_1   ... done
Creating test_redis_1   ... done
Creating test_mailhog_1 ... done
Creating test_laravel.test_1 ... done

docker volume ls
DRIVER    VOLUME NAME
local     test_sailmysql
local     test_sailredis

Dado que los datos existen en test_sailmysql , que es el volumen creado en la primera ejecución, no se ejecuta una nueva tarea de creación de base de datos.

sail mysql
  ERROR 1049 (42000): Unknown database 'test'
sail artisan migrate
  Illuminate\Database\QueryException
  SQLSTATE[HY000] [1049] Unknown database 'test' (SQL: select * from information_schema.tables where table_schema = test and table_name = migrations and table_type = 'BASE TABLE')

Comenzar después de eliminar el volumen existente

sail down -v
...
Removing volume test_sailmysql
Removing volume test_sailredis
sail up -d
...
Creating volume "test_sailmysql" with local driver
Creating volume "test_sailredis" with local driver
sail mysql
mysql> show databases;
| test               |
sail artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (214.30ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (99.56ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (151.61ms)

sail down opciones

sail down -h

    -v, --volumes           Remove named volumes declared in the `volumes`
                            section of the Compose file and anonymous volumes
                            attached to containers.