# frozen_string_literal: true

module DummyApp
  module Migrations
    extend self

    # Ensure database exists
    def database_exists?
      ActiveRecord::Base.connection
    rescue ActiveRecord::NoDatabaseError
      false
    else
      true
    end

    def needs_migration?
      return true if !database_exists?
      if ActiveRecord::Base.connection.respond_to?(:migration_context)
        # Rails >= 5.2
        ActiveRecord::Base.connection.migration_context.needs_migration?
      else
        ActiveRecord::Migrator.needs_migration?
      end
    end

    def auto_migrate
      if needs_migration?
        puts "Configuration changed. Re-running migrations"
        sh 'rake db:reset VERBOSE=false'

        # We might have a brand new database, so we must re-establish our connection
        ActiveRecord::Base.establish_connection
      end
    end

    private

    def sh(cmd)
      puts cmd
      system cmd
    end
  end
end

# frozen_string_literal: true

module DummyApp
  module Migrations
    extend self

    # Ensure database exists
    def database_exists?
      ActiveRecord::Base.connection
    rescue ActiveRecord::NoDatabaseError
      false
    else
      true
    end

    def needs_migration?
      return true if !database_exists?
      if ActiveRecord::Base.connection.respond_to?(:migration_context)
        # Rails >= 5.2
        ActiveRecord::Base.connection.migration_context.needs_migration?
      else
        ActiveRecord::Migrator.needs_migration?
      end
    end

    def auto_migrate
      if needs_migration?
        puts "Configuration changed. Re-running migrations"

        # Disconnect to avoid "database is being accessed by other users" on postgres
        ActiveRecord::Base.remove_connection

        sh 'rake db:reset VERBOSE=false'

        # We have a brand new database, so we must re-establish our connection
        ActiveRecord::Base.establish_connection
      end
    end

    private

    def sh(cmd)
      puts cmd
      system cmd
    end
  end
end

Fix error dropping postgres db in automigration
Fix error dropping postgres db in automigration

I was getting this error when automigration the datbase

    Caused by:
    PG::ObjectInUse: ERROR:  database "solidus_backend_test" is being accessed by other users
    DETAIL:  There is 1 other session using the database.

This is because we were connecting to the database to check whether
migrations were up to date, and that connection remained open as we
tried to drop the DB from a separate process.

This commit fixes the issue by calling
`ActiveRecord::Base.remove_connection` before shelling out to `rake
db:reset`
