🌱
Seeding
Seeding means populating the database with records. We do this in development to show what the web app looks like with data.
We do this in one script file: seeds.exs
seeds.exs
alias PetalPro.Accounts.User
alias PetalPro.Accounts.UserToken
alias PetalPro.Logs.Log
alias PetalPro.Accounts.UserSeeder
alias PetalPro.Orgs.OrgSeeder
alias PetalPro.Orgs.{Org, Membership, Invitation}
​
if Mix.env() == :dev do
PetalPro.Repo.delete_all(Log)
PetalPro.Repo.delete_all(Invitation)
PetalPro.Repo.delete_all(Membership)
PetalPro.Repo.delete_all(Org)
PetalPro.Repo.delete_all(UserToken)
PetalPro.Repo.delete_all(User)
​
admin = UserSeeder.admin()
​
normal_user =
UserSeeder.normal_user(%{
name: "Sarah Cunningham",
password: "password",
confirmed_at: Timex.now() |> Timex.to_naive_datetime()
})
​
org = OrgSeeder.random_org(admin)
PetalPro.Orgs.create_invitation(org, %{email: normal_user.email})
​
UserSeeder.random_users(20)
end
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.
Copy link