Skip to Content
Module developmentData

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.

app/Modules/Example/database/Entities/Payout.php
<?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.php

When the module is already installed on your site, apply the migration right away:

php flute migrate --module=Example

Working 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:

WhenWhat happens
module installthe 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 updatethe 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 migrateonly the existing files of the core and installed modules, nothing is generated
module uninstallInstaller::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

CommandWhat it does
php flute generate:migration --module=Examplea migration for the module's tables in app/Modules/Example/database/migrations; the module need not be installed
php flute generate:migrationasks where to write: the core or a module
php flute migrate --module=Exampleapply the migrations of one installed module
php flute migrate:statuswhich migrations of the core and installed modules have run
php flute migrate:rollback --module=Example --steps=1roll back the module's latest migrations; asks for confirmation, --force skips it

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 seeCause
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 loginstall 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 uninstalledthe 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.

Learn more

On this page