File: sql_helpers.rb

package info (click to toggle)
ruby-cancancan 3.0.1%2Bgh-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 580 kB
  • sloc: ruby: 4,195; makefile: 3
file content (31 lines) | stat: -rw-r--r-- 1,165 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# frozen_string_literal: true

module SQLHelpers
  def normalized_sql(adapter)
    adapter.database_records.to_sql.strip.squeeze(' ')
  end

  def connect_db
    # ActiveRecord::Base.logger = Logger.new(STDOUT)
    ActiveRecord::Base.logger = nil
    if ENV['DB'] == 'sqlite'
      ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
    elsif ENV['DB'] == 'postgres'
      connect_postgres
    else
      raise StandardError, 'database not supported'
    end
  end

  private

  def connect_postgres
    ActiveRecord::Base.establish_connection(adapter: 'postgresql', host: 'localhost',
                                            database: 'postgres', schema_search_path: 'public')
    ActiveRecord::Base.connection.drop_database('cancan_postgresql_spec')
    ActiveRecord::Base.connection.create_database('cancan_postgresql_spec', 'encoding' => 'utf-8',
                                                                            'adapter' => 'postgresql')
    ActiveRecord::Base.establish_connection(adapter: 'postgresql', host: 'localhost',
                                            database: 'cancan_postgresql_spec')
  end
end