localspot/priv/repo/seeds.exs
Kevin Sivic 94cb0870ff Add duplicate detection to business import
- Skip businesses that already exist (by slug)
- Track skipped count in import results
- Display skipped count in admin import UI

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-01 08:14:42 -05:00

85 lines
2 KiB
Elixir

# Script for populating the database. You can run it as:
#
# mix run priv/repo/seeds.exs
#
# Inside the script, you can read and write to any of your
# repositories directly:
#
# Localspot.Repo.insert!(%Localspot.SomeSchema{})
#
# We recommend using the bang functions (`insert!`, `update!`
# and so on) as they will fail if something goes wrong.
alias Localspot.Repo
alias Localspot.Businesses.Category
# Create categories (only if they don't exist)
categories_data = [
%{
name: "Restaurants",
slug: "restaurants",
description: "Local dining establishments",
icon: "hero-cake"
},
%{
name: "Coffee Shops",
slug: "coffee-shops",
description: "Cafes and coffee houses",
icon: "hero-cup-soda"
},
%{name: "Retail", slug: "retail", description: "Shops and stores", icon: "hero-shopping-bag"},
%{
name: "Services",
slug: "services",
description: "Professional services",
icon: "hero-wrench-screwdriver"
},
%{
name: "Arts & Entertainment",
slug: "arts-entertainment",
description: "Galleries, theaters, and venues",
icon: "hero-paint-brush"
},
%{
name: "Breweries",
slug: "breweries",
description: "Craft breweries and taprooms",
icon: "hero-beaker"
},
%{
name: "Wineries",
slug: "wineries",
description: "Wineries and vineyards",
icon: "hero-sparkles"
},
%{
name: "Outdoor Recreation",
slug: "outdoor-recreation",
description: "Outdoor gear, guides, and adventure",
icon: "hero-sun"
},
%{
name: "Farm Markets",
slug: "farm-markets",
description: "Farm stands, orchards, and local produce",
icon: "hero-shopping-cart"
}
]
created_count =
categories_data
|> Enum.reduce(0, fn attrs, count ->
case Repo.get_by(Category, slug: attrs.slug) do
nil ->
%Category{}
|> Category.changeset(attrs)
|> Repo.insert!()
count + 1
_existing ->
count
end
end)
IO.puts("Seeded #{created_count} new categories (#{length(categories_data)} total defined)")