Viewing latest docs.
Switch version: v3

Seeding

Seeding means populating the database with records. We do this in development to show what the web app looks like with data.

priv/repo/seeds.exs just picks a seed module (mix setup and mix ecto.reset run it):

seeds.exs
if Mix.env() == :prod do
PetalPro.Seeds.Production.run()
else
PetalPro.Seeds.Development.run()
end

The dev data lives in lib/petal_pro/seeds/development.ex. It wipes the tables, then seeds:

def run do
clean()
admin = UserSeeder.admin()
normal_user =
UserSeeder.normal_user(%{
email: "user@example.com",
name: "Sarah Cunningham",
password: "password",
confirmed_at: DateTime.truncate(DateTime.utc_now(), :second)
})
UserSeeder.fake_subscription(normal_user)
org = OrgSeeder.random_org(admin)
PetalPro.Orgs.create_invitation(org, %{email: normal_user.email})
UserSeeder.random_users(20)
all_users = PetalPro.Accounts.list_users()
FileSeeder.create_files(admin)
PostSeeder.create_posts(admin)
FeedbackSeeder.create_feedback(all_users)
end

Add your own tables to run/0, and to clean/0 so a re-seed starts fresh.

We use the faker library for randomizing data:

def random_user_attributes(attrs \\ %{}) do
Enum.into(attrs, %{
name: Faker.Person.En.first_name() <> " " <> Faker.Person.En.last_name(),
email: unique_user_email()
})
end

Seeds should be regularly updated with any new tables you create. This allows you to better test and develop your app.