midas/phil_columns-ex
{ "createdAt": "2016-06-23T00:06:10Z", "defaultBranch": "master", "description": "A data seeding framework for Elixir/Ecto.", "fullName": "midas/phil_columns-ex", "homepage": "", "language": "Elixir", "name": "phil_columns-ex", "pushedAt": "2023-03-10T17:48:13Z", "stargazersCount": 19, "topics": [ "elixir", "seeding" ], "updatedAt": "2025-04-24T11:43:33Z", "url": "https://github.com/midas/phil_columns-ex"}
PhilColumns
Section titled “PhilColumns”A full featured Elixir/Ecto seeding solution providing means for dev and prod seeding.
Installation
Section titled “Installation”Add phil_columns to your list of dependencies in mix.exs:
def deps do [{:phil_columns, "~> 0.1.0"}]endEnsure phil_columns is started before your application:
def application do [applications: [:phil_columns]]endCreate a Seed module for you application:
defmodule MyApp.Seed do defmacro __using__(_opts) do quote do use PhilColumns.Seed
# shared code here ... end endendConfiguration
Section titled “Configuration”If you need to ensure applications are started before seeding, configure them like this:
config :phil_columns, ensure_all_started: ~w(timex)aSeeding Quick Start
Section titled “Seeding Quick Start”Use the generator to create a seed.
$ mix phil_columns.gen.seed add_thingsThe generator puts a seed file in place. Add your seeding logic to the up/1 and/or down/1
functions using any valid Elixir/Ecto code.
defmodule MyApp.Repo.Seeds.AddThings do use MyApp.Seed
def up(_repo) do # seeding logic ... endendExecute the seed(s).
$ mix phil_columns.seedThe Seed Command
Section titled “The Seed Command”The simplest usage of the seed command defaults the environment to dev and the version to all.
$ mix phil_columns.seedThe env can be overridden by providing a switch. The env is used to select only seeds that have been specified for the specified env.
$ mix phil_columns.seed --env=prod$ mix phil_columns.seed -e prodTags and Environments
Section titled “Tags and Environments”Tags and environments can be applied to seeds and filtered in command usage. The seed generator adds the dev
environment by default and no tags. This feature enables efficiency and adaptability in development seeding and
the possibility to use PhilColumns seeding in production (see Production Seeding section below).
Specifying environment(s) for a seed is accomplished with the envs function.
defmodule MyApp.Repo.Seeds.AddThings do use MyApp.Seed
envs [:dev, :prod] # ...endTo change the environment use the env switch. When not specified the env defaults to dev.
$ mix phil_columns.seed -e prodSimilary, applying tag(s) is accomplished using the tags function.
defmodule MyApp.Repo.Seeds.AddThings do use MyApp.Seed
envs [:dev, :prod] tags [:some_tag] # ...endTo change the tag(s) provide them after the command command line.
$ mix phil_columns.seed --tags=users,settings,etc$ mix phil_columns.seed -t users,settings,etcProduction Seeding
Section titled “Production Seeding”Systems often have system level data that must be seeded when bootstraping a system or as new features are rolled out. Some examples are settings, configurations, roles, licenses, etc.
PhilColumns provides the ability to apply these system data seedings and commit them with features, analgous to an Ecto migration. Committing seed data with features or bug fixes communicates the intention of the data more clearly than any other strategy can.
Create a module specifically for dealing with seeding in production.
defmodule MyApp.Deployment.Seeder do import Mix.Ecto import Mix.PhilColumns
def seed(opts, seeder \\ &PhilColumns.Seeder.run/4) do repos = parse_repo(opts) |> List.wrap
# set env with current_env/0 overwriting provided arg opts = Keyword.put( opts, :env, current_env )
opts = if opts[:to] || opts[:step] || opts[:all], do: opts, else: Keyword.put(opts, :all, true)
opts = if opts[:log], do: opts, else: Keyword.put(opts, :log, :info)
opts = if opts[:quiet], do: Keyword.put(opts, :log, false), else: opts
Enum.each(repos, fn repo -> exec_task(repo, opts, fn -> seeder.(repo, seeds_path(repo), :up, opts) end) end) end
defp current_env do # implement this # warning: do not use Mix.env if you are doing an erlang release endendUse the module in the production app’s remote console.
MyApp.Deployment.Seeder.seed(tags: ~w(things stuff)a)