Module: DataMapper::Migrations::OracleAdapter

Includes:
SQL
Defined in:
lib/dm-core/migrations.rb

Overview

module Sqlite3Adapter

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

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.

TODO: document



820
821
822
# File 'lib/dm-core/migrations.rb', line 820

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

Instance Method Details

#create_model_storage(model) ⇒ Object

TODO: document



880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
# File 'lib/dm-core/migrations.rb', line 880

def create_model_storage(model)
  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

      statement = create_table_statement(connection, model, properties)
      command   = connection.create_command(statement)
      command.execute_non_query

      (create_index_statements(model) + create_unique_index_statements(model)).each do |statement|
        command   = connection.create_command(statement)
        command.execute_non_query
      end

      # added creation of sequence
      create_sequence_statements(model).each do |statement|
        command   = connection.create_command(statement)
        command.execute_non_query
      end
    end

  end

  true
end

#destroy_model_storage(model, forced = false) ⇒ Object

TODO: document



923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
# File 'lib/dm-core/migrations.rb', line 923

def destroy_model_storage(model, forced = false)
  table_name = model.storage_name(name)
  truncate_or_delete = self.class.auto_migrate_with
  if storage_exists?(table_name)
    if truncate_or_delete && !forced
      statement = case truncate_or_delete
      when :truncate
        truncate_table_statement(model)
      when :delete
        delete_table_statement(model)
      else
        raise ArgumentError, "Unsupported auto_migrate_with option"
      end
      execute(statement)
      @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 = self.class.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))
      if table_is_truncated && !forced
        statement = reset_sequence_statement(model)
      else
        statement = drop_sequence_statement(model)
      end
      execute(statement) if statement
    end
  end
  true
end

#field_exists?(storage_name, field_name) ⇒ Boolean

TODO: document

Returns:

  • (Boolean)


853
854
855
856
857
858
859
860
861
862
863
# File 'lib/dm-core/migrations.rb', line 853

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

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

#sequence_exists?(sequence_name) ⇒ Boolean

TODO: document

Returns:

  • (Boolean)


839
840
841
842
843
844
845
846
847
848
849
# File 'lib/dm-core/migrations.rb', line 839

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

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

#storage_exists?(storage_name) ⇒ Boolean

TODO: document

Returns:

  • (Boolean)


826
827
828
829
830
831
832
833
834
835
# File 'lib/dm-core/migrations.rb', line 826

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

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

#storage_fields(storage_name) ⇒ Object

TODO: document



867
868
869
870
871
872
873
874
875
876
# File 'lib/dm-core/migrations.rb', line 867

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

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