Generators
active-record generate scaffolds the files you would otherwise hand-write, and
active-record destroy removes what a matching generate produced. Both take a type
(model, migration, scope, validator) followed by a name and optional
field specs. g and d are short aliases.
Generated files land under the current project:
- models in
app/models/ - validators in
app/validators/ - migrations in
db/migrate/, with a timestamped filename so they sort and run after the existing ones.
Field specs
Fields are name:type tokens. The type defaults to string when omitted, and
maps to a migration column adverb (string, integer, bigint, boolean,
decimal, datetime, text, uuid, json, ...). Two extras:
name:references(orbelongs_to) adds a reference column and, in a model, abelongs-to.- a trailing
:uniqmakes the column unique; a trailing:indexadds an index.
1 | |
active-record generate model
1 | |
Writes app/models/Article.rakumod:
1 2 3 4 5 6 7 8 9 | |
and a create-table migration db/migrate/<timestamp>-create-articles.raku:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
The table name follows the ORM's own rule (the class name snake-cased, then
plus s, so PageTag becomes page_tags), so the model and its table always
agree.
active-record generate migration
The migration body is inferred from the name:
1 2 3 4 | |
Create<Things>builds acreate-tablewith the fields and adrop-tabledown.Add<...>To<Table>adds the fields, and removes them on the way down.Remove<...>From<Table>removes the fields, and restores them on the way down.- anything else gets an empty
up/downstub to fill in.
active-record generate scope
Inserts a named scope into an existing model file:
1 | |
adds to app/models/Article.rakumod:
1 2 3 4 | |
Field tokens become the where conditions. A numeric or True / False value
is emitted bare; anything else is quoted.
active-record generate validator
1 | |
Writes app/validators/NotEvilValidator.rakumod with a validate($record)
skeleton, ready to use with self.validates-with(NotEvilValidator). Validator
is appended to the name when it is not already there.
active-record destroy
active-record destroy is the inverse:
1 2 3 4 | |
Migrations are matched by the kebab-cased name regardless of their timestamp
prefix, so active-record destroy migration CreateProducts removes
<timestamp>-create-products.raku.