Module: Roomer::Helpers::PostgresHelper

Included in:
Schema
Defined in:
lib/roomer/helpers/postgres_helper.rb

Instance Method Summary collapse

Instance Method Details

#create_schema(schema_name) ⇒ Object

Creates a schema in PostgreSQL

Parameters:

  • schema_name (#to_s)

    declaring the name of the schema



7
8
9
# File 'lib/roomer/helpers/postgres_helper.rb', line 7

def create_schema(schema_name)
  ActiveRecord::Base.connection.execute "CREATE SCHEMA \"#{schema_name.to_s}\""
end

#create_sequence(table_name, pk = "id") ⇒ Object

Creates sequence for given table name

Parameters:

  • table (table_name)

    for which sequence will be created

  • primary (pk)

    key for table. Defaults to id



60
61
62
63
64
# File 'lib/roomer/helpers/postgres_helper.rb', line 60

def create_sequence(table_name, pk="id")
  ActiveRecord::Base.connection.execute(%{
    CREATE SEQUENCE #{table_name}_#{pk}_seq OWNED BY #{table_name}.#{pk}
  })
end

#drop_schema(schema_name) ⇒ Object

Drops a schema and all it’s objects (Cascades)

Parameters:

  • schema_name (#to_s)

    declaring the name of the schema to drop



13
14
15
# File 'lib/roomer/helpers/postgres_helper.rb', line 13

def drop_schema(schema_name)
  ActiveRecord::Base.connection.execute("DROP SCHEMA IF EXISTS \"#{schema_name.to_s}\" CASCADE")
end

#ensure_prefix(prefix, &block) ⇒ Object

Note:

All the Models will have the same prefix, caution is advised

Ensures the same ActiveRecord::Base#table_name_prefix for all the models executed in the block Example:

ensure_prefix(:global) do
   Person.find(1)  # => will execute "SELECT id FROM 'global.person' where 'id' = 1"
end

Parameters:

  • A (#to_s)

    Symbol declaring the table name prefix

  • code (#call)

    to execute



77
78
79
80
81
82
# File 'lib/roomer/helpers/postgres_helper.rb', line 77

def ensure_prefix(prefix, &block)
  old_prefix = ActiveRecord::Base.table_name_prefix
  ActiveRecord::Base.table_name_prefix = "#{prefix.to_s}#{Roomer.schema_seperator.to_s}"
  yield
  ActiveRecord::Base.table_name_prefix = old_prefix
end

#ensure_schema_migrationsObject

Ensures schema_migrations table exists and creates otherwise

See Also:

  • ActiveRecord::Base.connection#initialize_schema_migrations_table


86
87
88
# File 'lib/roomer/helpers/postgres_helper.rb', line 86

def ensure_schema_migrations
  ActiveRecord::Base.connection.initialize_schema_migrations_table
end

#ensuring_schema(schema_name, &block) ⇒ Object

Ensures the schema and schema_migrations exist(creates them otherwise) and executes the code block Example:

ensuring_schema(:global) do
   ActiveRecord::Migrator.migrate('/db/migrate', '20110812012536')
end

Parameters:

  • schema_name (#to_s)

    declaring name to ensure

  • &block (#call)

    code to execute

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
55
# File 'lib/roomer/helpers/postgres_helper.rb', line 47

def ensuring_schema(schema_name, &block)
  raise ArgumentError.new("schema_name not present") unless schema_name
  ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
  create_schema(schema_name) unless schemas.include?(schema_name.to_s)
  ensure_prefix(schema_name) do
    ensure_schema_migrations
    yield
  end
end

#schemasArray

lists the schemas available

Returns:

  • (Array)

    list of schemas



19
20
21
22
23
# File 'lib/roomer/helpers/postgres_helper.rb', line 19

def schemas
  ActiveRecord::Base.connection.query(%{
    SELECT nspname FROM pg_namespace WHERE nspname !~ '^pg_.*'
  }).flatten.map(&:to_s)
end

#shared_migrations_pending?Boolean

Determine if there are any pending migrations in the shared migrations directory

Returns:

  • (Boolean)


92
93
94
# File 'lib/roomer/helpers/postgres_helper.rb', line 92

def shared_migrations_pending?
  ActiveRecord::Migrator.new(:up,Roomer.shared_migrations_directory)
end

#stored_procedures(schema_name) ⇒ Array

lists all stored procedures for given schema

Returns:

  • (Array)

    list of stored procedures



27
28
29
30
31
32
33
34
35
# File 'lib/roomer/helpers/postgres_helper.rb', line 27

def stored_procedures(schema_name)
  ActiveRecord::Base.connection.select_values(%{
    SELECT  proname
    FROM    pg_catalog.pg_namespace n
    JOIN    pg_catalog.pg_proc p
      ON    pronamespace = n.oid
    WHERE   nspname = '#{schema_name.to_s}'
  })
end

#view_definitions(schema_name) ⇒ Object

Get view definitions for given schema



98
99
100
101
102
103
104
# File 'lib/roomer/helpers/postgres_helper.rb', line 98

def view_definitions(schema_name)
  ActiveRecord::Base.connection.select_all(%{
    SELECT definition 
    FROM   pg_views
    WHERE  schemaname = '#{schema_name}';
  })
end