How to add seed data to postgres database, rails app

Hi,

I have a rails app, and I have deployed the application using fly deploy. I want to add some seed data like categories or tags in database tables.

I have added the following task definition to fly.rake file to create seed data in Postgres database,

 # fly.rake
  namespace :db do
    task seed: :environment do
      sh 'bin/rails db:recreate:seed'
    end
  end

And added the following to Dockerfile generated by fly,

#Dockerfile
ARG DB_SEED_COMMAND="bin/rails fly:db:seed RAILS_ENV=production"
RUN ${DB_SEED_COMMAND}

Question: After deployment is done, I don’t see the changes in Postgres. Where i am doing wrong,


Here is my database.yml file

# config/database.yml
production:
  <<: *default
  database: db/production.sqlite3

I’m not sure how fly will pick up postgres without modifying the configuration in database.yml.

The builder does not have access to the database. While this can be done as a part of every release, you generally only want this to be done once. Try removing those lines from your Dockerfile and after you have deployed (i.e., without seed data), try running:

flyctl ssh console -C "/app/bin/rails db:seed"
1 Like

Thank you @rubys :pray:, Can I use this command in GitHub actions? Is that a good idea?

Yes, and as long as you trust github (and I don’t see why you shouldn’t). The only caution is that the more places you have your API token available, the greater the chances of it leaking.

It worked, Thank you @rubys :heart_eyes:

This works for me, but only for my first 10 or so seeds, out of 100s…
fly ssh console -C "rails/bin/rails db:seed"

From How To to Questions / Help

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.