Module: DataMapper::Migrations::OracleAdapter

Includes:
DataObjectsAdapter, SQL
Defined in:
lib/dm-migrations/adapters/dm-oracle-adapter.rb

Defined Under Namespace

Modules: ClassMethods, SQL

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SQL

#create_sequence_statements, #delete_table_statement, #drop_sequence_statement, #reset_sequence_statement, #schema_name, #truncate_table_statement

Methods included from DataObjectsAdapter

#upgrade_model_storage

Methods included from DataObjectsAdapter::SQL

#add_column_statement, #alter_table_add_column_statement, #create_index_statement, #create_index_statements, #create_table_statement, #create_unique_index_statements, #indexes, #property_schema_hash, #property_schema_statement, #schema_name, #supports_drop_table_if_exists?, #supports_serial?, #unique_indexes

Class Method Details

.included(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
14
# File 'lib/dm-migrations/adapters/dm-oracle-adapter.rb', line 11

def self.included(base)
  base.extend DataObjectsAdapter::ClassMethods
  base.extend ClassMethods
end

Instance Method Details

#create_model_storage(model) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dm-migrations/adapters/dm-oracle-adapter.rb', line 73

def create_model_storage(model)
  name       = self.name
  properties = model.properties_with_subclasses(name)
  table_name = model.storage_name(name)
  truncate_or_delete = self.class.auto_migrate_with
  table_is_truncated = truncate_or_delete && @truncated_tables && @truncated_tables[table_name]

  return false if storage_exists?(table_name) && !table_is_truncated
  return false if properties.empty?

  with_connection do |connection|
    # if table was truncated then check if all columns for properties are present
    # TODO: check all other column definition options
    if table_is_truncated && storage_has_all_fields?(table_name, properties)
      @truncated_tables[table_name] = nil
    else
      # forced drop of table if properties are different
      if truncate_or_delete
        destroy_model_storage(model, true)
      end

      statements = [ create_table_statement(connection, model, properties) ]
      statements.concat(create_index_statements(model))
      statements.concat(create_unique_index_statements(model))
      statements.concat(create_sequence_statements(model))

      statements.each do |statement|
        command   = connection.create_command(statement)
        command.execute_non_query
      end
    end

  end

  true
end

#destroy_model_storage(model, forced = false) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/dm-migrations/adapters/dm-oracle-adapter.rb', line 111

def destroy_model_storage(model, forced = false)
  table_name = model.storage_name(name)
  klass      = self.class
  truncate_or_delete = klass.auto_migrate_with
  if storage_exists?(table_name)
    if truncate_or_delete && !forced
      case truncate_or_delete
      when :truncate
        execute(truncate_table_statement(model))
      when :delete
        execute(delete_table_statement(model))
      else
        raise ArgumentError, "Unsupported auto_migrate_with option"
      end
      @truncated_tables ||= {}
      @truncated_tables[table_name] = true
    else
      execute(drop_table_statement(model))
      @truncated_tables[table_name] = nil if @truncated_tables
    end
  end
  # added destroy of sequences
  reset_sequences = klass.auto_migrate_reset_sequences
  table_is_truncated = @truncated_tables && @truncated_tables[table_name]
  unless truncate_or_delete && !reset_sequences && !forced
    if sequence_exists?(model_sequence_name(model))
      statement = if table_is_truncated && !forced
        reset_sequence_statement(model)
      else
        drop_sequence_statement(model)
      end
      execute(statement) if statement
    end
  end
  true
end

#drop_table_statement(model) ⇒ Object



66
67
68
69
# File 'lib/dm-migrations/adapters/dm-oracle-adapter.rb', line 66

def drop_table_statement(model)
  table_name = quote_name(model.storage_name(name))
  "DROP TABLE #{table_name} CASCADE CONSTRAINTS"
end

#field_exists?(storage_name, field_name) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dm-migrations/adapters/dm-oracle-adapter.rb', line 42

def field_exists?(storage_name, field_name)
  statement = DataMapper::Ext::String.compress_lines(<<-SQL)
    SELECT COUNT(*)
    FROM all_tab_columns
    WHERE owner = ?
    AND table_name = ?
    AND column_name = ?
  SQL

  select(statement, schema_name, oracle_upcase(storage_name), oracle_upcase(field_name)).first > 0
end

#sequence_exists?(sequence_name) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dm-migrations/adapters/dm-oracle-adapter.rb', line 29

def sequence_exists?(sequence_name)
  return false unless sequence_name
  statement = DataMapper::Ext::String.compress_lines(<<-SQL)
    SELECT COUNT(*)
    FROM all_sequences
    WHERE sequence_owner = ?
    AND sequence_name = ?
  SQL

  select(statement, schema_name, oracle_upcase(sequence_name)).first > 0
end

#storage_exists?(storage_name) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
# File 'lib/dm-migrations/adapters/dm-oracle-adapter.rb', line 17

def storage_exists?(storage_name)
  statement = DataMapper::Ext::String.compress_lines(<<-SQL)
    SELECT COUNT(*)
    FROM all_tables
    WHERE owner = ?
    AND table_name = ?
  SQL

  select(statement, schema_name, oracle_upcase(storage_name)).first > 0
end

#storage_fields(storage_name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/dm-migrations/adapters/dm-oracle-adapter.rb', line 55

def storage_fields(storage_name)
  statement = DataMapper::Ext::String.compress_lines(<<-SQL)
    SELECT column_name
    FROM all_tab_columns
    WHERE owner = ?
    AND table_name = ?
  SQL

  select(statement, schema_name, oracle_upcase(storage_name))
end