Sha256: 67bc6d5ca97b789962d66d6368411c7f200b7a88a3ada95ca67e216f61bb3730

Contents?: true

Size: 1.8 KB

Versions: 6

Compression:

Stored size: 1.8 KB

Contents

require 'ardb'
require 'ardb/adapter/base'

class Ardb::Adapter

  class Postgresql < Base

    def public_schema_settings
      self.config_settings.merge({
        'database'           => 'postgres',
        'schema_search_path' => 'public'
      })
    end

    def create_db
      ActiveRecord::Base.establish_connection(self.public_schema_settings)
      ActiveRecord::Base.connection.create_database(self.database, self.config_settings)
      ActiveRecord::Base.establish_connection(self.config_settings)
    end

    def drop_db
      ActiveRecord::Base.establish_connection(self.public_schema_settings)
      ActiveRecord::Base.connection.tap do |conn|
        conn.execute "UPDATE pg_catalog.pg_database"\
                     " SET datallowconn=false WHERE datname='#{self.database}'"
        # this SELECT actually runs a command: it terminates all the connections
        # http://www.postgresql.org/docs/9.2/static/functions-admin.html#FUNCTIONS-ADMIN-SIGNAL-TABLE
        conn.execute "SELECT pg_terminate_backend(pid)"\
                     " FROM pg_stat_activity WHERE datname='#{self.database}'"
        conn.execute "DROP DATABASE IF EXISTS #{self.database}"
      end
    end

    def drop_tables
      ActiveRecord::Base.connection.tap do |conn|
        tables = conn.execute "SELECT table_name"\
                              " FROM information_schema.tables"\
                              " WHERE table_schema = 'public';"
        tables.each{ |row| conn.execute "DROP TABLE #{row['table_name']} CASCADE" }
      end
    end

    def foreign_key_add_sql
      "ALTER TABLE :from_table"\
      " ADD CONSTRAINT :name"\
      " FOREIGN KEY (:from_column)"\
      " REFERENCES :to_table (:to_column)"
    end

    def foreign_key_drop_sql
      "ALTER TABLE :from_table"\
      " DROP CONSTRAINT :name"
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ardb-0.9.0 lib/ardb/adapter/postgresql.rb
ardb-0.8.0 lib/ardb/adapter/postgresql.rb
ardb-0.7.0 lib/ardb/adapter/postgresql.rb
ardb-0.6.0 lib/ardb/adapter/postgresql.rb
ardb-0.5.0 lib/ardb/adapter/postgresql.rb
ardb-0.4.1 lib/ardb/adapter/postgresql.rb