Drop a table

Ecto for Beginners : part 5 of 22 published on Sep 06, 2015

First we need to create a table to drop because we do plan on using our Posts table.

$ mix ecto.gen.migration create_ads
* creating priv/repo/migrations
* creating priv/repo/migrations/TIMESTAMP_create_ads.exs

And in our migration, we will create an Ads table.

# /priv/repo/migrations/TIMESTAMP_create_ads.exs
defmodule Readdit.Repo.Migrations.CreateAds do
  use Ecto.Migration

  def change do
    create table(:ads) do
      add :message, :string

      timestamps
    end
  end
end

Running our migration will create the ads table.

$ mix ecto.migrate
== Running Readdit.Repo.Migrations.CreateAds.change/0 forward
create table ads
== Migrated in 0.0s

Now we can create our migration to drop the new ads table.

$ mix ecto.gen.migration drop_ads
* creating priv/repo/migrations
* creating priv/repo/migrations/TIMESTAMP_drop_ads.exs

Instead of using create or alter we will use drop to drop the table table from the database.

defmodule Readdit.Repo.Migrations.DropAds do
  use Ecto.Migration

  def change do
    drop table(:ads)
  end
end

We then run the migration and our table is now dropped.

$ mix ecto.migrate
== Running Readdit.Repo.Migrations.DropAds.change/0 forward
drop table ads
== Migrated in 0.0s
Next: Setup our Post Model