Skip to content

Commit

Permalink
Add migration docs yiisoft/db-migration (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
hacan359 authored Jan 17, 2024
1 parent 69e0f6a commit 85d4cca
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 5 deletions.
4 changes: 2 additions & 2 deletions guide/en/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ Views -
* [Template engines](views/template-engines.md) -


Working with databases -
Working with databases +-
----------------------

* [Database access objects](db-dao.md): Connecting to a database, basic queries, transactions, and schema manipulation
* [Query builder](db-query-builder.md): Querying the database using a simple abstraction layer
* [Active record](db-active-record.md): The Active Record ORM, retrieving and manipulating records, and defining relations

Check notice on line 73 in guide/en/README.md

View workflow job for this annotation

GitHub Actions / vale

[vale] guide/en/README.md#L73

[Microsoft.Acronyms] 'ORM' has no definition.
Raw output
{"message": "[Microsoft.Acronyms] 'ORM' has no definition.", "location": {"path": "guide/en/README.md", "range": {"start": {"line": 73, "column": 59}}}, "severity": "INFO"}
* [Migrations](db-migrations.md): Apply version control to your databases in a team development environment
* [Migrations](databases/db-migrations.md): +

Getting data from users -
-----------------------
Expand Down
102 changes: 102 additions & 0 deletions guide/en/databases/db-migrations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Migrations

To use migrations, install [yiisoft/db-migration](https://github.com/yiisoft/db-migration/) package:

```shell
composer require yiisoft/db-migration
```

### Example usage

First, configure DI container. Create `config/common/db.php` with the following content:

```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'
]
]
];
```

Add the following to `config/params.php`:

```php
...
'yiisoft/db-migration' => [
'newMigrationNamespace' => 'App\\Migration',
'sourceNamespaces' => ['App\\Migration'],
],
...
```

Now test if it works:

```shell
./yii list migrate
```

### Creating a migration

To work with migrations, you can use the provided [view](https://github.com/yiisoft/db-migration/tree/master/resources/views).

```shell
./yii migrate:create my_first_table --command=table --fields=name,example --table-comment=my_first_table
```

That would generate the following:

```php
<?php

declare(strict_types=1);

namespace App\Migration;

use Yiisoft\Db\Migration\MigrationBuilder;
use Yiisoft\Db\Migration\RevertibleMigrationInterface;
use Yiisoft\Db\Migration\TransactionalMigrationInterface;

/**
* Handles the creation of table `my_first_table`.
*/
final class M240115143455CreateMyFirstTableTable implements RevertibleMigrationInterface, TransactionalMigrationInterface
{
public function up(MigrationBuilder $b): void
{
$b->createTable('my_first_table', [
'id' => $b->primaryKey(),
'name',
'example',
]);

$b->addCommentOnTable('my_first_table', 'dest');
}

public function down(MigrationBuilder $b): void
{
$b->dropTable('my_first_table');
}
}
```

For more information [see](https://github.com/yiisoft/db-migration/tree/master/docs/en)

### Upgrading from Yii2

Check notice on line 94 in guide/en/databases/db-migrations.md

View workflow job for this annotation

GitHub Actions / vale

[vale] guide/en/databases/db-migrations.md#L94

[Microsoft.Headings] 'Upgrading from Yii2' should use sentence-style capitalization.
Raw output
{"message": "[Microsoft.Headings] 'Upgrading from Yii2' should use sentence-style capitalization.", "location": {"path": "guide/en/databases/db-migrations.md", "range": {"start": {"line": 94, "column": 5}}}, "severity": "INFO"}

Migrations in Yii2 and the [yiisoft/db-migration](https://github.com/yiisoft/db-migration/) package are not compatible,

Check failure on line 96 in guide/en/databases/db-migrations.md

View workflow job for this annotation

GitHub Actions / vale

[vale] guide/en/databases/db-migrations.md#L96

[Microsoft.Contractions] Use 'aren't' instead of 'are not'.
Raw output
{"message": "[Microsoft.Contractions] Use 'aren't' instead of 'are not'.", "location": {"path": "guide/en/databases/db-migrations.md", "range": {"start": {"line": 96, "column": 101}}}, "severity": "ERROR"}
and the `migration` table is also not
compatible.
A probable solution is to use structure dumps and rename the old `migration` table. Upon the initial execution of
migrations, a new `migration` table with new fields will be created. All subsequent changes in the database schema are

Check notice on line 100 in guide/en/databases/db-migrations.md

View workflow job for this annotation

GitHub Actions / vale

[vale] guide/en/databases/db-migrations.md#L100

[Microsoft.Passive] 'be created' looks like passive voice.
Raw output
{"message": "[Microsoft.Passive] 'be created' looks like passive voice.", "location": {"path": "guide/en/databases/db-migrations.md", "range": {"start": {"line": 100, "column": 58}}}, "severity": "INFO"}

Check notice on line 100 in guide/en/databases/db-migrations.md

View workflow job for this annotation

GitHub Actions / vale

[vale] guide/en/databases/db-migrations.md#L100

[Microsoft.ComplexWords] Consider using 'later' or 'next' instead of 'subsequent'.
Raw output
{"message": "[Microsoft.ComplexWords] Consider using 'later' or 'next' instead of 'subsequent'.", "location": {"path": "guide/en/databases/db-migrations.md", "range": {"start": {"line": 100, "column": 74}}}, "severity": "INFO"}

Check notice on line 100 in guide/en/databases/db-migrations.md

View workflow job for this annotation

GitHub Actions / vale

[vale] guide/en/databases/db-migrations.md#L100

[Microsoft.Passive] 'are applied' looks like passive voice.
Raw output
{"message": "[Microsoft.Passive] 'are applied' looks like passive voice.", "location": {"path": "guide/en/databases/db-migrations.md", "range": {"start": {"line": 100, "column": 116}}}, "severity": "INFO"}
applied using the new `migration` component and recorded in the new migration table.

Check notice on line 101 in guide/en/databases/db-migrations.md

View workflow job for this annotation

GitHub Actions / vale

[vale] guide/en/databases/db-migrations.md#L101

[Microsoft.ComplexWords] Consider using 'part' instead of 'component'.
Raw output
{"message": "[Microsoft.ComplexWords] Consider using 'part' instead of 'component'.", "location": {"path": "guide/en/databases/db-migrations.md", "range": {"start": {"line": 101, "column": 35}}}, "severity": "INFO"}

4 changes: 2 additions & 2 deletions guide/en/security/authorization.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ The command above could be executed from console the following way:

**TODO**: finish it when migrations are implemented.

Check notice on line 209 in guide/en/security/authorization.md

View workflow job for this annotation

GitHub Actions / vale

[vale] guide/en/security/authorization.md#L209

[Microsoft.Passive] 'are implemented' looks like passive voice.
Raw output
{"message": "[Microsoft.Passive] 'are implemented' looks like passive voice.", "location": {"path": "guide/en/security/authorization.md", "range": {"start": {"line": 209, "column": 37}}}, "severity": "INFO"}

You can use [migrations](db-migrations.md)
You can use [migrations](../databases/db-migrations.md)
to initialize and change hierarchy via APIs offered by `\Yiisoft\Rbac\ManagerInterface`.

Create new migration using `./yii migrate/create init_rbac` then implement creating a hierarchy:
Create new migration using `./yii migrate:create init_rbac` then implement creating a hierarchy:

```php
<?php
Expand Down
2 changes: 1 addition & 1 deletion guide/es/security/authorization.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ The command above could be executed from console the following way:
You can use [migrations](db-migrations.md)
to initialize and modify hierarchy via APIs offered by `\Yiisoft\Rbac\ManagerInterface`.

Create new migration using `./vendor/bin/yii migrate/create init_rbac` then impement creating a hierarchy:
Create new migration using `./vendor/bin/yii migrate:create init_rbac` then impement creating a hierarchy:

```php
<?php
Expand Down

0 comments on commit 85d4cca

Please sign in to comment.