Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't create databases after you drop them #193

Open
Quintasan opened this issue Feb 3, 2023 · 5 comments
Open

Can't create databases after you drop them #193

Quintasan opened this issue Feb 3, 2023 · 5 comments

Comments

@Quintasan
Copy link

Quintasan commented Feb 3, 2023

backend on try-sequel [!?] via πŸ’Ž v3.2.0
❯ bundle exec rails sequel:create:all
rails aborted!
Sequel::DatabaseConnectionError: PG::ConnectionBad: connection to server at "::1", port 5432 failed: Connection refused
        Is the server running on that host and accepting TCP/IP connections?
connection to server at "127.0.0.1", port 5432 failed: FATAL:  database "backend_development" does not exist
/home/quintasan/sauce/lndb/backend/config/environment.rb:7:in `<main>'

Caused by:
PG::ConnectionBad: connection to server at "::1", port 5432 failed: Connection refused
        Is the server running on that host and accepting TCP/IP connections?
connection to server at "127.0.0.1", port 5432 failed: FATAL:  database "backend_development" does not exist
/home/quintasan/sauce/lndb/backend/config/environment.rb:7:in `<main>'
Tasks: TOP => sequel:create:all => environment
(See full trace by running task with --trace)

I'm pretty much it's because it will try to connect to the database specified in config/database.yml which will not exist since we just dropped it :)

@Quintasan
Copy link
Author

I think the problem is in https://github.com/TalentBox/sequel-rails/blob/master/lib/sequel_rails/railties/database.rake#L103

It tries to load the environment which (probably) calls SequelRails::Configuration#connect (https://github.com/TalentBox/sequel-rails/blob/master/lib/sequel_rails/configuration.rb#L51) which tries to connect to a non-existing database.

I did verify that by monkeypatching the #connect method to:

    def connect(environment)
      normalized_config = environment_for environment

      unless (normalized_config.keys & %w[adapter url]).any?
        raise "Database not configured.\n" \
            'Please create config/database.yml or set DATABASE_URL in environment.'
      end

      begin
        config_with_symbolized_keys = SequelRails.deep_symbolize_keys(normalized_config)

        if normalized_config['url']
          ::Sequel.connect normalized_config['url'], config_with_symbolized_keys
        else
          ::Sequel.connect config_with_symbolized_keys
        end.tap { after_connect.call if after_connect.respond_to?(:call) }
      rescue Sequel::DatabaseConnectionError => e
        regex = /database "(?<database_name>\w+)" does not exist/
        matches = e.message.match(regex)
        if matches[:database_name].eql?(config_with_symbolized_keys[:database])
          continue
        else
          raise e
        end
      end
    end

which makes things work:

backend on ξ‚  try-sequel [!?] via πŸ’Ž v3.2.0
❯ bundle exec rails sequel:drop:all
[sequel] Dropped database 'backend_development'
[sequel] Dropped database 'backend_test'
[sequel] Dropped database 'backend_production'

backend on ξ‚  try-sequel [!?] via πŸ’Ž v3.2.0
❯ bundle exec rails sequel:create:all
[sequel] Created database 'backend_development'
[sequel] Created database 'backend_test'
[sequel] Created database 'backend_production'

@Quintasan
Copy link
Author

Of course, I realized that if you config.sequel.skip_connect = true the issue goes away, but I'm not sure why that's not true by default

@dawidof
Copy link

dawidof commented Jul 20, 2023

Use rake db:create not rails db:create

@opensourceame
Copy link

opensourceame commented Oct 17, 2023

I add this to config/application.rb

if defined?(Rake.application) && Rake.application.top_level_tasks.include?('sequel:create')
  config.sequel.skip_connect = true
end

You can also check if top_level_tasks includes db:create for Rails < 7

@artofhuman
Copy link

I think it fixed in last release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants