diff --git a/lib/mix/tasks/thesis.install.ex b/lib/mix/tasks/thesis.install.ex index cf03730..a4747bd 100644 --- a/lib/mix/tasks/thesis.install.ex +++ b/lib/mix/tasks/thesis.install.ex @@ -2,6 +2,15 @@ defmodule Mix.Tasks.Thesis.Install do use Mix.Task import Mix.Thesis.Utils + @migrations [ + "create_thesis_tables", + "add_meta_to_thesis_page_contents", + "add_indexes_to_tables" + ] + @template_files [ + {"priv/templates/thesis.install/thesis_auth.exs", "lib/thesis_auth.ex" } + ] + @shortdoc "Generates Thesis code in your Phoenix app" @moduledoc """ @@ -29,14 +38,12 @@ defmodule Mix.Tasks.Thesis.Install do @doc false def thesis_templates do migrations = ["create_thesis_tables", "add_meta_to_thesis_page_contents"] - migration_files = migrations + migration_files = @migrations |> Enum.filter(&migration_missing?/1) |> Enum.with_index |> Enum.map(&migration_tuple/1) - template_files = [ {"priv/templates/thesis.install/thesis_auth.exs", "lib/thesis_auth.ex" } ] - - template_files ++ migration_files + @template_files ++ migration_files |> Stream.map(&render_eex/1) |> Stream.map(©_to_target/1) |> Stream.run diff --git a/priv/templates/thesis.install/add_indexes_to_tables.exs b/priv/templates/thesis.install/add_indexes_to_tables.exs new file mode 100644 index 0000000..de7c341 --- /dev/null +++ b/priv/templates/thesis.install/add_indexes_to_tables.exs @@ -0,0 +1,27 @@ +defmodule <%= base %>.Repo.Migrations.AddIndexToTables do + use Ecto.Migration + + def up do + alter table(:thesis_pages) do + modify :title, :string, size: 512 + modify :description, :string, size: 1024 + end + + # Index page slugs, since we're searching on those + create index(:thesis_pages, [:slug]) + + # Create foreign key constraint for page_contents -> page + alter table(:thesis_page_contents) do + modify :page_id, references(:thesis_pages, on_delete: :delete_all) + end + end + + def down do + alter table(:thesis_pages) do + modify :title, :string + modify :description, :string + end + + remove index(:thesis_pages, [:slug]) + end +end