Sha256: 3d6ca86006c212bc142f4f2134ac036abe3275dbd1b2d09c3ec7b519c813b127

Contents?: true

Size: 866 Bytes

Versions: 4

Compression:

Stored size: 866 Bytes

Contents

# NB need to specify a username in the database.yml if you want to use any of these commands
module HerokuTool
  class DbConfiguration
    def config
      db_config_from_file = ERB.new(File.read("config/database.yml")).result
      @config ||= YAML.safe_load(db_config_from_file, permitted_classes: [], permitted_symbols: [], aliases: true)
    end

    def generate_drop_tables_sql
      sql = %(select 'DROP TABLE IF EXISTS \\"' || tablename || '\\" CASCADE;' from pg_tables where schemaname = 'public')
      %(psql #{user_arg} #{database} -t -c "#{sql}")
    end

    def user_arg
      username = db_config["username"]
      username.present? && "-U #{username}" || ""
    end

    def database
      db_config["database"]
    end

    private

    def db_config
      config[env]
    end

    def env
      ENV["RAILS_ENV"] || "development"
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
heroku_tool-0.7.1 lib/heroku_tool/db_configuration.rb
heroku_tool-0.7.0 lib/heroku_tool/db_configuration.rb
heroku_tool-0.6.0 lib/heroku_tool/db_configuration.rb
heroku_tool-0.5.0 lib/heroku_tool/db_configuration.rb