How to Generate a Migration File: Difference between revisions
From ElixirBlocks
No edit summary |
No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
A migration file lets you configure database tables. | A migration file lets you configure database tables. | ||
==Generate a migration file== | ==Generate a migration file== | ||
A migration file is generated using a console command. After it is generated, you write code that configures table fields | A migration file is generated using a console command. After it is generated, you write code to it that configures database table fields. | ||
The command to create a migration file is: | The command to create a migration file is: | ||
Line 34: | Line 33: | ||
To initiate the migration | To initiate the migration, run this command: | ||
<source> | |||
mix ecto.migrate | mix ecto.migrate | ||
</source> |
Latest revision as of 16:17, 19 October 2023
A migration file lets you configure database tables.
Generate a migration file
A migration file is generated using a console command. After it is generated, you write code to it that configures database table fields.
The command to create a migration file is:
mix ecto.gen.migration name_of_migration
For this example, I will add a new field named owner
mix ecto.gen.migration create_owner_field
The migration file after it is created appears in app/priv/repo/migrations
You now need to type code that performs table specific database actions.
In this example below, a field is added to a preexisting table named :testbeds.
def change do alter table(:testbeds) do # customize to your code add :owner, :string end end
To initiate the migration, run this command:
mix ecto.migrate