Skip to content

Commit 58f59d2

Browse files
committed
rework docs to have more smaller pages
1 parent f93fd72 commit 58f59d2

24 files changed

+1846
-5138
lines changed

docs/.vitepress/toc_en.json

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,43 @@
11
{
22
"/": [
33
{
4-
"text": "Migrations",
4+
"text": "Getting Started",
55
"collapsed": false,
66
"items": [
7-
{ "text": "Writing Migrations", "link": "/writing-migrations" },
8-
{ "text": "Using the Query Builder", "link": "/using-the-query-builder" },
9-
{ "text": "Executing Queries", "link": "/executing-queries" },
10-
{ "text": "Database Seeding", "link": "/seeding" }
7+
{ "text": "Migrations 5.x", "link": "/" },
8+
{ "text": "Installation and Overview", "link": "/getting-started/installation-and-overview" },
9+
{ "text": "Creating Migrations", "link": "/getting-started/creating-migrations" },
10+
{ "text": "Snapshots and Diffs", "link": "/getting-started/snapshots-and-diffs" },
11+
{ "text": "Running and Managing Migrations", "link": "/getting-started/running-and-managing-migrations" }
12+
]
13+
},
14+
{
15+
"text": "Guides",
16+
"collapsed": false,
17+
"items": [
18+
{ "text": "Integration and Deployment", "link": "/advanced/integration-and-deployment" },
19+
{
20+
"text": "Writing Migrations",
21+
"link": "/guides/writing-migrations/",
22+
"items": [
23+
{ "text": "Migration Methods", "link": "/guides/writing-migrations/migration-methods" },
24+
{ "text": "Columns and Table Operations", "link": "/guides/writing-migrations/columns-and-table-operations" },
25+
{ "text": "Indexes and Constraints", "link": "/guides/writing-migrations/indexes-and-constraints" },
26+
{ "text": "Schema Introspection and Platform Limitations", "link": "/guides/writing-migrations/schema-introspection-and-platform-limitations" }
27+
]
28+
},
29+
{ "text": "Using the Query Builder", "link": "/guides/using-the-query-builder" },
30+
{ "text": "Executing Queries", "link": "/guides/executing-queries" },
31+
{ "text": "Inserting Data", "link": "/guides/inserting-data" },
32+
{ "text": "Database Seeding", "link": "/guides/seeding" }
1133
]
1234
},
1335
{
1436
"text": "Upgrade Guides",
1537
"collapsed": true,
1638
"items": [
17-
{ "text": "Upgrading from 4.x to 5.x", "link": "/upgrading" },
18-
{ "text": "Upgrading to the Builtin Backend", "link": "/upgrading-to-builtin-backend" }
39+
{ "text": "Upgrading from 4.x to 5.x", "link": "/upgrades/upgrading-from-4-x" },
40+
{ "text": "Upgrading to the Builtin Backend", "link": "/upgrades/upgrading-to-builtin-backend" }
1941
]
2042
}
2143
]
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
# Integration and Deployment
2+
3+
This guide covers test integration, plugin usage, programmatic execution, and
4+
deployment-related operational concerns.
5+
6+
## Using Migrations for Tests
7+
8+
If you are using migrations for your application schema, you can also use those
9+
same migrations to build schema in your tests. In your application's
10+
`tests/bootstrap.php` file you can use the `Migrator` class to build schema
11+
when tests are run. The `Migrator` will use existing schema if it is current,
12+
and if the migration history in the database differs from what is in the
13+
filesystem, all tables will be dropped and migrations will be rerun from the
14+
beginning:
15+
16+
```php
17+
// in tests/bootstrap.php
18+
use Migrations\TestSuite\Migrator;
19+
20+
$migrator = new Migrator();
21+
22+
// Simple setup with no plugins
23+
$migrator->run();
24+
25+
// Run a non-test database
26+
$migrator->run(['connection' => 'test_other']);
27+
28+
// Run migrations for plugins
29+
$migrator->run(['plugin' => 'Contacts']);
30+
31+
// Run the Documents migrations on the test_docs connection
32+
$migrator->run(['plugin' => 'Documents', 'connection' => 'test_docs']);
33+
```
34+
35+
If you need to run multiple sets of migrations, use `runMany()`:
36+
37+
```php
38+
$migrator->runMany([
39+
['plugin' => 'Contacts'],
40+
['plugin' => 'Documents', 'connection' => 'test_docs'],
41+
]);
42+
```
43+
44+
If your database also contains tables that are not managed by your application,
45+
such as those created by PostGIS, you can exclude those tables from drop and
46+
truncate behavior using the `skip` option:
47+
48+
```php
49+
$migrator->run(['connection' => 'test', 'skip' => ['postgis*']]);
50+
```
51+
52+
The `skip` option accepts an `fnmatch()` compatible pattern.
53+
54+
## Using Migrations in Plugins
55+
56+
Plugins can also provide migration files. All commands in the Migrations plugin
57+
support the `--plugin` or `-p` option to scope execution to the migrations
58+
relative to that plugin:
59+
60+
```bash
61+
bin/cake migrations status -p PluginName
62+
bin/cake migrations migrate -p PluginName
63+
```
64+
65+
## Running Migrations in a Non-shell Environment
66+
67+
While typical usage of migrations is from the command line, you can also run
68+
migrations from a non-shell environment by using the `Migrations\Migrations`
69+
class. This can be handy when you are developing a plugin installer for a CMS,
70+
for instance.
71+
72+
The `Migrations` class allows you to run the following commands:
73+
74+
- `migrate`
75+
- `rollback`
76+
- `markMigrated`
77+
- `status`
78+
- `seed`
79+
80+
Each of these commands has a corresponding method defined in the
81+
`Migrations\Migrations` class.
82+
83+
```php
84+
use Migrations\Migrations;
85+
86+
$migrations = new Migrations();
87+
88+
$status = $migrations->status();
89+
$migrate = $migrations->migrate();
90+
$rollback = $migrations->rollback();
91+
$markMigrated = $migrations->markMigrated(20150804222900);
92+
$seeded = $migrations->seed();
93+
```
94+
95+
The methods can accept an array of parameters that match the shell command
96+
options:
97+
98+
```php
99+
use Migrations\Migrations;
100+
101+
$migrations = new Migrations();
102+
$status = $migrations->status([
103+
'connection' => 'custom',
104+
'source' => 'MyMigrationsFolder',
105+
]);
106+
```
107+
108+
The only exception is `markMigrated()`, which expects the version number as the
109+
first argument and the options array as the second.
110+
111+
Optionally, you can pass default parameters in the constructor:
112+
113+
```php
114+
use Migrations\Migrations;
115+
116+
$migrations = new Migrations([
117+
'connection' => 'custom',
118+
'source' => 'MyMigrationsFolder',
119+
]);
120+
121+
$status = $migrations->status();
122+
$migrate = $migrations->migrate();
123+
```
124+
125+
If you need to override one or more default parameters for one call, pass them
126+
to the method:
127+
128+
```php
129+
use Migrations\Migrations;
130+
131+
$migrations = new Migrations([
132+
'connection' => 'custom',
133+
'source' => 'MyMigrationsFolder',
134+
]);
135+
136+
$status = $migrations->status();
137+
$migrate = $migrations->migrate(['connection' => 'default']);
138+
```
139+
140+
## Feature Flags
141+
142+
Migrations offers a few feature flags for compatibility. These features are
143+
disabled by default but can be enabled if required:
144+
145+
- `unsigned_primary_keys`: Should Migrations create primary keys as unsigned
146+
integers? Default: `false`
147+
- `unsigned_ints`: Should Migrations create all integer columns as unsigned?
148+
Default: `false`
149+
- `column_null_default`: Should Migrations create columns as nullable by
150+
default? Default: `false`
151+
- `add_timestamps_use_datetime`: Should Migrations use `DATETIME` type columns
152+
for the columns added by `addTimestamps()`?
153+
154+
Set them via `Configure`, for example in `config/app.php`:
155+
156+
```text
157+
'Migrations' => [
158+
'unsigned_primary_keys' => true,
159+
'unsigned_ints' => true,
160+
'column_null_default' => true,
161+
],
162+
```
163+
164+
> [!NOTE]
165+
> The `unsigned_primary_keys` and `unsigned_ints` options only affect MySQL
166+
> databases. When generating migrations with `bake migration_snapshot` or
167+
> `bake migration_diff`, the `signed` attribute will only be included in the
168+
> output for unsigned columns as `'signed' => false`.
169+
170+
## Skipping the `schema.lock` File Generation
171+
172+
In order for the diff feature to work, a `.lock` file is generated every time
173+
you migrate, roll back, or bake a snapshot, to keep track of the state of your
174+
database schema at any given point in time. You can skip this file generation,
175+
for instance when deploying to production, by using the `--no-lock` option:
176+
177+
```bash
178+
bin/cake migrations migrate --no-lock
179+
bin/cake migrations rollback --no-lock
180+
bin/cake bake migration_snapshot MyMigration --no-lock
181+
```
182+
183+
## Deployment
184+
185+
You should update your deployment scripts to run migrations when new code is
186+
deployed. Ideally, run migrations after the code is on your servers, but before
187+
the application code becomes active.
188+
189+
After running migrations, remember to clear the ORM cache so it renews the
190+
column metadata of your tables. Otherwise, you might end up with errors about
191+
columns not existing when performing operations on those new columns. The
192+
CakePHP core includes a [Schema Cache Shell](https://book.cakephp.org/5/en/console-and-shells/schema-cache.html) that you can use:
193+
194+
```bash
195+
bin/cake migration migrate
196+
bin/cake schema_cache clear
197+
```
198+
199+
## Alert of Missing Migrations
200+
201+
You can use the `Migrations.PendingMigrations` middleware in local development
202+
to alert developers about new migrations that have not been applied:
203+
204+
```php
205+
use Migrations\Middleware\PendingMigrationsMiddleware;
206+
207+
$config = [
208+
'plugins' => [
209+
// Optionally include a list of plugins with migrations to check.
210+
],
211+
];
212+
213+
$middlewareQueue
214+
// ErrorHandler middleware
215+
->add(new PendingMigrationsMiddleware($config));
216+
```
217+
218+
You can add the `'app'` config key set to `false` if you are only interested in
219+
checking plugin migrations.
220+
221+
You can temporarily disable the migration check by adding
222+
`skip-migration-check=1` to the URL query string.
223+
224+
## IDE Autocomplete Support
225+
226+
The [IdeHelper plugin](https://github.com/dereuromark/cakephp-ide-helper) can
227+
help you get more IDE support for tables, their column names, and possible
228+
column types. Specifically, PHPStorm understands the meta information and can
229+
help you autocomplete those.

docs/en/contents.md

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)