Skip to content

Commit f107e5c

Browse files
author
Composite PHP
committed
Add migration commands description to documentation
1 parent 04d24a2 commit f107e5c

File tree

1 file changed

+75
-10
lines changed

1 file changed

+75
-10
lines changed

doc/migrations.md

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
> **_NOTE:_** This is experimental feature
44
5-
Migrations enable you to maintain your database schema within your PHP entity classes.
6-
Any modification made in your class triggers the generation of migration files.
7-
These files execute SQL queries which synchronize the schema from the PHP class to the corresponding SQL table.
5+
Migrations enable you to maintain your database schema within your PHP entity classes.
6+
Any modification made in your class triggers the generation of migration files.
7+
These files execute SQL queries which synchronize the schema from the PHP class to the corresponding SQL table.
88
This mechanism ensures consistent alignment between your codebase and the database structure.
99

1010
## Supported Databases
@@ -26,6 +26,9 @@ You need to configure ConnectionManager, see instructions [here](configuration.m
2626
### 3. Configure commands
2727

2828
Add [symfony/console](https://symfony.com/doc/current/components/console.html) commands to your application:
29+
- Composite\Sync\Commands\MigrateCommand
30+
- Composite\Sync\Commands\MigrateNewCommand
31+
- Composite\Sync\Commands\MigrateDownCommand
2932
- Composite\Sync\Commands\GenerateEntityCommand
3033
- Composite\Sync\Commands\GenerateTableCommand
3134

@@ -40,16 +43,78 @@ use Symfony\Component\Console\Application;
4043

4144
//may be changed with .env file
4245
putenv('CONNECTIONS_CONFIG_FILE=/path/to/your/connections/config.php');
46+
putenv('ENTITIES_DIR=/path/to/your/source/dir'); // e.g. "./src"
47+
putenv('MIGRATIONS_DIR=/path/to/your/migrations/dir'); // e.g. "./src/Migrations"
48+
putenv('MIGRATIONS_NAMESPACE=Migrations\Namespace'); // e.g. "App\Migrations"
4349

4450
$app = new Application();
4551
$app->addCommands([
52+
new Commands\MigrateCommand(),
53+
new Commands\MigrateNewCommand(),
54+
new Commands\MigrateDownCommand(),
4655
new Commands\GenerateEntityCommand(),
4756
new Commands\GenerateTableCommand(),
4857
]);
4958
$app->run();
5059
```
5160
## Available commands
5261

62+
* ### composite:migrate
63+
64+
This command performs two primary functions depending on its usage context. Initially, when called for the first time,
65+
it scans all entities located in the `ENTITIES_DIR` directory and generates migration files corresponding to these entities.
66+
This initial step prepares the necessary migration scripts based on the current entity definitions. Upon its second
67+
invocation, the command shifts its role to apply these generated migration scripts to the database. This two-step process
68+
ensures that the database schema is synchronized with the entity definitions, first by preparing the migration scripts
69+
and then by executing them to update the database.
70+
71+
```shell
72+
php cli.php composite:migrate
73+
```
74+
75+
| Option | Short | Description |
76+
|--------------|-------|-----------------------------------------------------------|
77+
| --connection | -c | Check migrations for all entities with desired connection |
78+
| --entity | -e | Check migrations only for entity class |
79+
| --run | -r | Run migrations without asking for confirmation |
80+
| --dry | -d | Dry run mode, no real SQL queries will be executed |
81+
82+
* ### composite:migrate-new
83+
84+
This command generates a new, empty migration file. The file is provided as a template for the user to fill with the
85+
necessary database schema changes or updates. This command is typically used for initiating a new database migration,
86+
where the user can define the specific changes to be applied to the database schema. The generated file needs to be
87+
manually edited to include the desired migration logic before it can be executed with the migration commands.
88+
89+
```shell
90+
php cli.php composite:migrate-new
91+
```
92+
93+
| Argument | Required | Description |
94+
|-------------|----------|------------------------------------------|
95+
| connection | No | Name of connection from your config file |
96+
| description | No | Short description of desired changes |
97+
98+
* ### composite:migrate-down
99+
100+
This command rolls back the most recently applied migration. It is useful for undoing the last schema change made to
101+
the database. This can be particularly helpful during development or testing phases, where you might need to revert
102+
recent changes quickly.
103+
104+
```shell
105+
php cli.php composite:migrate-down
106+
```
107+
108+
| Argument | Required | Description |
109+
|------------|----------|---------------------------------------------------------------------------|
110+
| connection | No | Name of connection from your config file |
111+
| limit | No | Number of migrations should be rolled back from current state, default: 1 |
112+
113+
114+
| Option | Short | Description |
115+
|--------|-------|-----------------------------------------------------|
116+
| --dry | -d | Dry run mode, no real SQL queries will be executed |
117+
53118
* ### composite:generate-entity
54119

55120
The command examines the specific SQL table and generates an [Composite\Entity\AbstractEntity](https://github.com/compositephp/entity) PHP class.
@@ -67,9 +132,9 @@ php cli.php composite:generate-entity connection_name TableName 'App\Models\Enti
67132

68133
Options:
69134

70-
| Option | Description |
71-
|---------|-------------------------|
72-
| --force | Overwrite existing file |
135+
| Option | Short | Description |
136+
|---------|-------|-------------------------|
137+
| --force | -f | Overwrite existing file |
73138

74139
* ### composite:generate-table
75140

@@ -87,7 +152,7 @@ php cli.php composite:generate-table 'App\Models\EntityName'
87152

88153
Options:
89154

90-
| Option | Description |
91-
|----------|--------------------------------------------|
92-
| --cached | Generate cached version of PHP Table class |
93-
| --force | Overwrite existing file |
155+
| Option | Short | Description |
156+
|----------|-------|--------------------------------------------|
157+
| --cached | -c | Generate cached version of PHP Table class |
158+
| --force | -f | Overwrite existing file |

0 commit comments

Comments
 (0)