Module tables and migrations
How to describe a module entity, generate a migration with generate:migration, when the core applies it and how to roll it back.
By the end your module has its own table: an entity with fields and a link to the player, and a migration in the module folder that is applied on install and rolled back on uninstall.
Minimal example
The entity lives in the module's database/Entities. The database segment of the namespace is lowercase, as in the path. Entities have their own shape: no declare(strict_types = 1) and not final; a class that extends ActiveRecord and holds public typed properties with #[Column] attributes.
<?php
namespace Flute\Modules\Example\database\Entities;
use Cycle\ActiveRecord\ActiveRecord;
use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\Relation\BelongsTo;
use Cycle\Annotated\Annotation\Table;
use Cycle\Annotated\Annotation\Table\Index;
use Cycle\ORM\Entity\Behavior;
use DateTimeImmutable;
use Flute\Core\Database\Entities\User;
#[Entity(table: 'example_payouts')]
#[Table(indexes: [new Index(columns: ['status', 'created_at'])])]
#[Behavior\CreatedAt(field: 'createdAt', column: 'created_at')]
class Payout extends ActiveRecord
{
#[Column(type: 'primary')]
public int $id;
#[BelongsTo(target: User::class)]
public User $user;
#[Column(type: 'decimal(10,2)', default: 0)]
public float $amount = 0.0;
#[Column(type: 'string', size: 16, default: 'pending')]
public string $status = 'pending';
#[Column(type: 'datetime', name: 'created_at')]
public DateTimeImmutable $createdAt;
}The migration is created by a command. It compares the module's entities with the database and writes up() and down():
php flute generate:migration --module=Example[OK] Migration created: <SITE_DIR>/app/Modules/Example/database/migrations/20261126.120000_0_0_default_create_flute_example_payouts.phpWhen the module is already installed on your site, apply the migration right away:
php flute migrate --module=ExampleWorking with a record:
$payout = new Payout();
$payout->user = $user;
$payout->amount = 150.0;
$payout->saveOrFail();
$pending = rep(Payout::class)->select()->load('user')->where('status', 'pending')->fetchAll();The Tickets module is built the same way: entities in database/Entities, migrations in database/migrations, all of them created by the command.
When the core changes the schema
Tables are not created at boot. A new entity without an applied migration saves nothing and reads nothing, and no error is raised. Four places change the schema:
| When | What happens |
|---|---|
| module install | the migrations in database/migrations run, then the core generates and applies a migration for whatever they do not cover. Then Installer::install() is called. If anything fails, the migrations of this attempt are rolled back |
| module update | the schema the same way first, then the Updater methods between the old and the new version. On failure the schema is rolled back and the previous version is recorded |
php flute migrate | only the existing files of the core and installed modules, nothing is generated |
| module uninstall | Installer::uninstall() first, while the tables still exist, then the whole migration folder is rolled back, newest first |
A migration the core generated on the site during install goes into the module folder. On update such files are carried into the new copy of the module, so an uninstall can drop those tables too.
A generated module migration touches only the module's tables. Differences in core tables go to the database log and never into the module's file. The core does not apply an automatic migration that drops a table or a column: the module ships such a migration as a ready file.
Reference
| Command | What it does |
|---|---|
php flute generate:migration --module=Example | a migration for the module's tables in app/Modules/Example/database/migrations; the module need not be installed |
php flute generate:migration | asks where to write: the core or a module |
php flute migrate --module=Example | apply the migrations of one installed module |
php flute migrate:status | which migrations of the core and installed modules have run |
php flute migrate:rollback --module=Example --steps=1 | roll back the module's latest migrations; asks for confirmation, --force skips it |
| What | How |
|---|---|
| primary key | #[Column(type: 'primary')] public int $id; |
| optional column | #[Column(type: 'string', nullable: true)] public ?string $note = null; |
| JSON | #[Column(type: 'json', nullable: true, typecast: 'json')] public ?array $meta = null; |
| money | decimal(10,2), a float property, never a float column |
| created and updated time | #[Behavior\CreatedAt], #[Behavior\UpdatedAt], never set by hand |
| soft delete | #[Behavior\SoftDelete(field: 'deletedAt', column: 'deleted_at')] plus the Flute\Core\Database\Scope\SoftDeletes trait |
| index | #[Table(indexes: [new Index(columns: ['status'], unique: true)])], column names |
| your own repository | #[Entity(repository: PayoutRepository::class)] |
#[BelongsTo(target: User::class)], #[HasOne], #[HasMany(target: ..., cascade: true)], #[ManyToMany(target: ..., through: ...)] from Cycle\Annotated\Annotation\Relation. Relations are lazy: if the loop after a query reads $payout->user, load the relation in the query with load('user'). Filtering by a field of a relation needs with(). Use cascade: true only for rows the entity owns.
Flute\Modules\Example\Installer extends Flute\Core\Support\AbstractModuleInstaller: install(ModuleInformation &$module): bool and uninstall(ModuleInformation &$module): bool, where false stops the operation. uninstall() is also called when an install fails after install() ran. Put settings writes in installed(): nothing is rolled back after it.
Flute\Modules\Example\Updater holds methods such as update_1_2_0(). The core calls, in ascending order, those whose version is above the installed one and not above the new one. Data a step changed is not rolled back.
Common mistakes
The table never appeared
The core does not create tables at boot, and php flute migrate does not generate migrations. If you added an entity to an installed module, run generate:migration --module=Example, then migrate --module=Example, and ship the migration file in the module archive.
| What you see | Cause |
|---|---|
Nothing was chosen. Say where the migration goes: --global or --module=<Name>. | the command ran without a prompt (a script, CI) and without --module |
There is no module Example in app/Modules. | a typo in the module name; use the folder name |
Schema already in sync with the database — nothing to generate. | the entities already match the database, nothing to change |
No migration directory found for module 'Example'. | the module is not installed, or it has no database/migrations |
Auto-generated schema migration was rejected: its up() contains destructive operations (drop table/column). in the database log | install or update needs a migration that drops something; generate it with the command and ship it in the module |
The update of Example failed, and the database changes it already made could not be undone: ... | a migration has an empty down(), or an Updater step failed and its data changes cannot be rolled back |
| the tables stay after the module is uninstalled | the migration sits in storage/migrations instead of the module folder |
Do not rename a migration file and do not edit an applied migration: a schema change is always a new migration. Test both directions: migrate, then migrate:rollback.