-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sergei Predvoditelev <[email protected]>
- Loading branch information
1 parent
1212ef4
commit 83f35d8
Showing
8 changed files
with
206 additions
and
13 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
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,5 @@ | ||
# Yii DB Migration | ||
|
||
- [Yii Console](usage-with-yii-console.md) | ||
- [Symfony application](usage-with-symfony.md) | ||
- [Standalone](usage-standalone.md) |
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,71 @@ | ||
# Standalone usage | ||
|
||
## Com arquivo de configuração | ||
|
||
1. Copie o arquivo de configuração `./vendor/yiisoft/db-migration/bin/yii-db-migration.php` para a pasta raiz do seu projeto: | ||
|
||
```shell | ||
cp ./vendor/yiisoft/db-migration/bin/yii-db-migration.php ./yii-db-migration.php | ||
``` | ||
|
||
2. Defina a conexão do banco de dados no arquivo de configuração (consulte a | ||
[documentação do Yii DB](https://github.com/yiisoft/db/blob/master/docs/en/README.md#create-connection)). | ||
Por exemplo, conexão MySQL: | ||
|
||
```php | ||
'db' => new \Yiisoft\Db\Mysql\Connection( | ||
new \Yiisoft\Db\Mysql\Driver('mysql:host=mysql;dbname=mydb', 'user', 'q1w2e3r4'), | ||
new \Yiisoft\Db\Cache\SchemaCache(new \Yiisoft\Cache\ArrayCache()), | ||
), | ||
``` | ||
|
||
3. Opcionalmente, modifique outras opções no arquivo de configuração. Cada opção possui um comentário com descrição. | ||
|
||
4. Execute o comando do console sem argumentos para ver a lista de comandos de migração disponíveis: | ||
|
||
```shell | ||
./vendor/bin/yii-db-migration | ||
``` | ||
|
||
## Sem arquivo de configuração | ||
|
||
Isso pode ser útil em ambientes de teste e/ou quando vários RDBMS são usados. | ||
|
||
Configure todas as dependências manualmente: | ||
|
||
```php | ||
use Yiisoft\Db\Connection\ConnectionInterface; | ||
use Yiisoft\Db\Migration\Informer\NullMigrationInformer; | ||
use Yiisoft\Db\Migration\Migrator; | ||
use Yiisoft\Db\Migration\Service\MigrationService; | ||
use Yiisoft\Injector\Injector; | ||
/** @var ConnectionInterface $database */ | ||
$migrator = new Migrator($database, new NullMigrationInformer()); | ||
$migrationService = new MigrationService($database, new Injector(), $migrator); | ||
$migrationService->setSourcePaths([dirname(__DIR__, 2), 'migrations']); | ||
``` | ||
Em seguida, inicialize o comando para usar sem CLI. Por exemplo, para aplicar migrações será `UpdateCommand`: | ||
```php | ||
use Symfony\Component\Console\Helper\HelperSet; | ||
use Symfony\Component\Console\Helper\QuestionHelper; | ||
use Yiisoft\Db\Migration\Command\UpdateCommand; | ||
use Yiisoft\Db\Migration\Runner\UpdateRunner; | ||
$command = new UpdateCommand(new UpdateRunner($migrator), $migrationService, $migrator); | ||
$command->setHelperSet(new HelperSet(['queestion' => new QuestionHelper()])); | ||
``` | ||
E, por fim, execute o comando: | ||
```php | ||
use Symfony\Component\Console\Input\ArrayInput; | ||
use Symfony\Component\Console\Output\NullOutput; | ||
$input = new ArrayInput([]); | ||
$input->setInteractive(false); | ||
$this->getMigrateUpdateCommand()->run($input, new NullOutput()); | ||
``` |
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,55 @@ | ||
# Uso com Symfony | ||
|
||
Requer [Yii DB Migration](https://github.com/yiisoft/db-migration) e driver de banco de dados. Vamos usar [Yii DB SQLite](https://github.com/yiisoft/db-sqlite) para este exemplo: | ||
|
||
```shell | ||
composer require yiisoft/db-migration | ||
composer require yiisoft/db-sqlite | ||
``` | ||
|
||
Configure migração e a conexão de banco de dados em seu `config/services.yml`: | ||
|
||
```yaml | ||
Yiisoft\Db\Migration\: | ||
resource: '../vendor/yiisoft/db-migration/src/' | ||
|
||
Yiisoft\Db\Migration\Informer\MigrationInformerInterface: | ||
class: 'Yiisoft\Db\Migration\Informer\ConsoleMigrationInformer' | ||
|
||
Yiisoft\Injector\Injector: | ||
arguments: | ||
- '@service_container' | ||
|
||
Yiisoft\Db\Migration\Service\MigrationService: | ||
calls: | ||
- setNewMigrationNamespace: ['App\Migrations'] | ||
- setNewMigrationPath: [''] | ||
- setSourceNamespaces: [['App\Migrations']] | ||
- setSourcePaths: [[]] | ||
|
||
Yiisoft\Db\: | ||
resource: '../vendor/yiisoft/db/src/' | ||
exclude: | ||
- '../vendor/yiisoft/db/src/Debug/' | ||
|
||
cache.app.simple: | ||
class: 'Symfony\Component\Cache\Psr16Cache' | ||
arguments: | ||
- '@cache.app' | ||
|
||
Yiisoft\Db\Cache\SchemaCache: | ||
arguments: | ||
- '@cache.app.simple' | ||
|
||
Yiisoft\Db\Connection\ConnectionInterface: | ||
class: '\Yiisoft\Db\Sqlite\Connection' | ||
arguments: | ||
- '@sqlite_driver' | ||
|
||
sqlite_driver: | ||
class: '\Yiisoft\Db\Sqlite\Driver' | ||
arguments: | ||
- 'sqlite:./var/migrations.sq3' | ||
``` | ||
É isso. Agora você pode usar os comandos `bin/console migrate:*`. |
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,46 @@ | ||
# Uso com Yii Console | ||
|
||
Neste exemplo, usamos [yiisoft/app](https://github.com/yiisoft/app). | ||
|
||
Primeiro, configure o contêiner DI. Crie `config/common/db.php` com o seguinte conteúdo: | ||
|
||
```php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Yiisoft\Db\Connection\ConnectionInterface; | ||
use Yiisoft\Db\Sqlite\Connection as SqliteConnection; | ||
|
||
return [ | ||
ConnectionInterface::class => [ | ||
'class' => SqliteConnection::class, | ||
'__construct()' => [ | ||
'dsn' => 'sqlite:' . __DIR__ . '/Data/yiitest.sq3' | ||
] | ||
] | ||
]; | ||
``` | ||
|
||
Adicione em `config/params.php`: | ||
|
||
```php | ||
... | ||
'yiisoft/db-migration' => [ | ||
'newMigrationNamespace' => 'App\\Migration', | ||
'sourceNamespaces' => ['App\\Migration'], | ||
], | ||
... | ||
``` | ||
|
||
Agora o `MigrationService::class` usa o `View` da aplicação que já está registrada em `yiisoft/view`. | ||
|
||
Execute `composer du` no console para reconstruir a configuração. | ||
|
||
Agora temos o pacote [`yiisoft/db-migration`](https://github.com/yiisoft/db-migration) configurado e ele pode ser chamado no console. | ||
|
||
Veja a lista de comandos disponíveis com `./yii list`: | ||
|
||
```shell | ||
./yii list | ||
``` |