Module: DataMapper::Adapters::PostgresAdapter::Migration

Includes:
SQL
Included in:
DataMapper::Adapters::PostgresAdapter
Defined in:
lib/gems/dm-core-0.9.9/lib/dm-core/adapters/postgres_adapter.rb

Overview

TODO: move to dm-more/dm-migrations (if possible)

Defined Under Namespace

Modules: ClassMethods, SQL

Instance Method Summary collapse

Instance Method Details

#create_model_storage(repository, model) ⇒ Object

TODO: move to dm-more/dm-migrations



52
53
54
55
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/adapters/postgres_adapter.rb', line 52

def create_model_storage(repository, model)
  add_sequences(repository, model)
  without_notices { super }
end

#destroy_model_storage(repository, model) ⇒ Object

TODO: move to dm-more/dm-migrations



58
59
60
61
62
63
64
65
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/adapters/postgres_adapter.rb', line 58

def destroy_model_storage(repository, model)
  return true unless storage_exists?(model.storage_name(repository.name))
  success = without_notices { super }
  model.properties(repository.name).each do |property|
    drop_sequence(repository, property) if property.serial?
  end
  success
end

#field_exists?(storage_name, column_name) ⇒ Boolean

TODO: move to dm-more/dm-migrations (if possible)

Returns:



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/adapters/postgres_adapter.rb', line 32

def field_exists?(storage_name, column_name)
  statement = <<-EOS.compress_lines
    SELECT COUNT(*)
    FROM "pg_class"
    JOIN "pg_attribute" ON "pg_class"."oid" = "pg_attribute"."attrelid"
    WHERE "pg_attribute"."attname" = ?
    AND "pg_class"."relname" = ?
    AND "pg_attribute"."attnum" >= 0
  EOS

  query(statement, column_name, storage_name).first > 0
end

#storage_exists?(storage_name) ⇒ Boolean

TODO: move to dm-more/dm-migrations (if possible)

Returns:



20
21
22
23
24
25
26
27
28
29
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/adapters/postgres_adapter.rb', line 20

def storage_exists?(storage_name)
  statement = <<-EOS.compress_lines
    SELECT COUNT(*)
    FROM "information_schema"."columns"
    WHERE "table_name" = ?
    AND "table_schema" = current_schema()
  EOS

  query(statement, storage_name).first > 0
end

#upgrade_model_storage(repository, model) ⇒ Object

TODO: move to dm-more/dm-migrations



46
47
48
49
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/adapters/postgres_adapter.rb', line 46

def upgrade_model_storage(repository, model)
  add_sequences(repository, model)
  super
end