View Source Versioned.Migration (Versioned v0.4.0)
Allows creating tables for versioned schemas.
example
Example
defmodule MyApp.Repo.Migrations.CreateCar do
use Versioned.Migration
def change do
create_versioned_table(:cars) do
add :name, :string
end
end
end
Link to this section Summary
Functions
Add a new column to both the main table and the versions table.
Create a table whose data is versioned by also creating a secondary table with the immutable, append-only history.
Modify orig_field
column in table table_name
and its versioned
counterpart.
Removes a column from a table and its versioned counterpart.
Rename orig_field
column in table table_name
to a new name.
Renames a table table and its versioned counterpart.
Link to this section Functions
Add a new column to both the main table and the versions table.
Any foreign key constraint via references/2
will be stripped for
the versions table.
Create a table whose data is versioned by also creating a secondary table with the immutable, append-only history.
modify_versioned_column(table_name, column, type, opts \\ [])
View Source (macro)Modify orig_field
column in table table_name
and its versioned
counterpart.
example
Example
defmodule MyApp.Repo.Migrations.RenameFooToBar do
use Versioned.Migration
def change do
modify_versioned_column("my_table", :foo, :text, null: true)
end
end
Removes a column from a table and its versioned counterpart.
example
Example
defmodule MyApp.Repo.Migrations.RemoveFooToBar do
use Versioned.Migration
def change do
remove_versioned_column("my_table", :foo)
end
end
Rename orig_field
column in table table_name
to a new name.
Note that this is indeed changing the field names as well in the complimenting and generally immutable "versions" table.
example
Example
defmodule MyApp.Repo.Migrations.RenameFooToBar do
use Versioned.Migration
def change do
rename_versioned_column("my_table", :foo, to: :bar)
end
end
Renames a table table and its versioned counterpart.
other-considerations
Other Considerations
If the table is cars
, then cars_versions
has a car_id
field. If
the table is being renamed to automobiles
, then after using this macro,
you'll want to also do something like the following in order to rename the
car_id
field in the versions table to automobile_id
.
rename table("automobiles_versions"), :car_id, to: :automobile_id
If your table has additional foreign key constraints such as those set up with
Ecto.Migration.references/2
, you'll want to move those over, too, with
something like this. The versions table won't have any such constraints.
execute(
"ALTER TABLE automobiles RENAME CONSTRAINT cars_garage_id_fkey TO automobiles_garage_id_fkey;",
"ALTER TABLE automobiles RENAME CONSTRAINT automobiles_garage_id_fkey TO cars_garage_id_fkey;"
)
example
Example
defmodule MyApp.Repo.Migrations.RenameCarsToAutomobiles do
use Versioned.Migration
def change do
rename_versioned_table("cars", "automobiles")
rename table("automobiles_versions"), :car_id, to: :automobile_id
end
end