Giter Club home page Giter Club logo

Comments (17)

laserlemon avatar laserlemon commented on June 29, 2024

Thank you! This is a known issue. We use Rails' standard hooks to initialize Figaro before Rails initializes. Unfortunately, this hook apparently occurs after database.yml is read. Trying to circumvent the standard hooks and tie into Rails' initialization elsewhere introduces a host of new problems and makes it very difficult to account for every situation in which the Rails environment is loaded.

I'm afraid this falls into the "won't fix" bucket until Rails provides a better (and supported) hook that Figaro can tie into. In my apps, I Git-ignore database.yml like I ignore application.yml. Not ideal, but it seems like the best we can do for now. Please feel free to investigate possible solutions yourself, as I'd love for Figaro to work as you expected. In the meantime, I'll close this issue. Thank you for the report! 👏

from figaro.

scaryguy avatar scaryguy commented on June 29, 2024

Uhm I see.

I love Figaro, it should even be merged to Rails project I think.

Thanks for this great job! :)

But this issue is very important and deserves to be mentioned in README I guess. I'd encourage you to add it.

from figaro.

ideasasylum avatar ideasasylum commented on June 29, 2024

I'm a bit confused by this issue.

I have exactly the same setup (database user/passwords in application.yml, inserted into database.yml using erb) and it's working fine so far. The server is working, rake db:migrate is working, tests are still working (running rspec).

from figaro.

laserlemon avatar laserlemon commented on June 29, 2024

@ideasasylum: Interesting! Would you be willing to put up a small Git repo to step through how you build/configure a Rails app so I could take a look?

from figaro.

scaryguy avatar scaryguy commented on June 29, 2024

Oh really? How? :/

from figaro.

ideasasylum avatar ideasasylum commented on June 29, 2024

Hmm... I'm not sure if a small app would really capture how this app is setup. It's not massive but it is slightly unusual.

Here's some items of interest: I'm using Rails 3.2.13 with postgres. Fairly normal. I'm using Unicorn (in dev and production) with preload_app true. This means that in my after_fork handler I'm calling ActiveRecord::Base.establish_connection. Could this really be reloading the database.yml? I'd be surprised.

Additionally, I'm using the Apartment gem to switch postgres schemas based on the subdomain, which would also be changing the AR connection.

Do you know where in Rails database.yml gets loaded?

from figaro.

ideasasylum avatar ideasasylum commented on June 29, 2024

ok, probably nothing to do with Unicorn etc.

I can put <% binding.pry %> into my database.yml file, run rails c and in the pry console I'll be able to see all my Figaro env variables in ENV

Obviously, the question is why but I also needed to convince myself that I wasn't going mad!

from figaro.

ideasasylum avatar ideasasylum commented on June 29, 2024

I've done some digging around this but I still can't figure out why it works in my case but not @scaryguy's

Figaro hooks into config.before_configuration but, I'm wondering, could Figaro be an initializer instead? Then you can specify that it should load before "active_record.initialize_database"?

So, I did it: ideasasylum@1eeb7a9

You can try this fork with gem "figaro", git: 'https://github.com/ideasasylum/figaro.git'. And if works then it can be pulled in.

And if you want to explore the Rails/Figaro/database initialisation process… Here's a gist of a pry session I used to verify that the Figaro initializer was being loaded before the activerecord database initialization

from figaro.

miharekar avatar miharekar commented on June 29, 2024

Could you try my PR #69 which fixed the Rails 4.1 issue by hooking into first initializer. This would then solve all the issues 🎉

from figaro.

ideasasylum avatar ideasasylum commented on June 29, 2024

@mrfoto yeah, it looks like we both took the same approach of using initializer. I think that definitely the way forward. The only difference is when it happens (and I don't mind, just as long as it's before database initialisation and is compatible with Rails 3.2+)

from figaro.

miharekar avatar miharekar commented on June 29, 2024

Judging by your gist, the load_environment_hook is included in your initializers list so it should play nicely

from figaro.

rgdenis avatar rgdenis commented on June 29, 2024

I face the same problem.
Alternatively can be used ENV["DATABASE_URL"] instead of database.yml config. If both are present, DATABASE_URL silently wins (database.yml just ignored).

from figaro.

ideasasylum avatar ideasasylum commented on June 29, 2024

@laserlemon Any update on this? I think we have a few solutions that could be merged in

from figaro.

laserlemon avatar laserlemon commented on June 29, 2024

@ideasasylum Before I change the hook, I'll need to be able to add a failing test. I need a case where database.yml is loaded before application.yml. Please feel free to add a test to a branch of off master if you can!

from figaro.

laserlemon avatar laserlemon commented on June 29, 2024

I just pushed a test that I think might cover this for all versions of Rails 3-4. Please take a look: afc6699

from figaro.

exchgr avatar exchgr commented on June 29, 2024

That commit works for me on Rails 3.2.11 using Unicorn.

from figaro.

purushothamana avatar purushothamana commented on June 29, 2024

Eventhough I set me environment, it overrides with the other environment. What would be the problem behind it?

from figaro.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.