Class: Caboose::Utilities::Schema

Inherits:
Object
  • Object
show all
Defined in:
app/models/caboose/utilities/schema.rb

Direct Known Subclasses

Schema

Class Method Summary collapse

Class Method Details

.create_indexesObject

Verifies (non-destructively) that the given indexes exist in the database.



96
97
98
99
100
101
102
103
104
# File 'app/models/caboose/utilities/schema.rb', line 96

def self.create_indexes
  return if self.indexes.nil?
  c = ActiveRecord::Base.connection      
  self.indexes.each do |model, arr|
    arr.each do |index|
      c.add_index model.table_name, index if !c.index_exists?(model.table_name, index)
    end             
  end
end

.create_schemaObject

Verifies (non-destructively) that the given schema exists in the database.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/caboose/utilities/schema.rb', line 39

def self.create_schema
  return if self.schema.nil?
  
  rename_tables
  rename_columns
  remove_columns    
  
  c = ActiveRecord::Base.connection
  self.schema.each do |model, columns|
    tbl = model.table_name
    puts "Creating table #{tbl}..."      
    if !c.table_exists?(tbl)
      if model.primary_key != 'id'          
        c.create_table tbl, { :id => false, :force => true }
      else
        c.create_table tbl
      end          
    end            
    columns.each do |col|
      puts "Creating column #{tbl}.#{col[0]}..."
      
      # Special case for attachments
      if col[1] == :attachment          
        c.add_attachment tbl, col[0] if !c.column_exists?(tbl, "#{col[0]}_file_size")            
        next
      end
              
      # Skip if the column exists with the proper data type
      next if c.column_exists?(tbl, col[0], col[1])
      
      # If the column doesn't exists, add it
      if !c.column_exists?(tbl, col[0])
        if col.count > 2                      
          c.add_column tbl, col[0], col[1], col[2]
        else          
          c.add_column tbl, col[0], col[1] 
        end
        
      # Column exists, but not with the correct data type, try to change it
      else                    
        c.execute("alter table #{tbl} alter column #{col[0]} type #{col[1]} using cast(#{col[0]} as #{col[1]})")
  
      end        
      if col[0].to_s == model.primary_key
        c.execute("alter table #{tbl} add primary key (\"#{col[0].to_s}\")")
      end
    end                  
  end
      
  create_indexes
  
  self.schema.each do |model, columns|
    model.reset_column_information
  end
end

.indexesObject

Any column indexes that need to exist in the database



20
21
22
# File 'app/models/caboose/utilities/schema.rb', line 20

def self.indexes
  return nil      
end

.load_dataObject

Loads initial data into the database



31
32
# File 'app/models/caboose/utilities/schema.rb', line 31

def self.load_data
end

.remove_columnsObject

Removes a set of tables



130
131
132
133
134
135
136
137
138
139
# File 'app/models/caboose/utilities/schema.rb', line 130

def self.remove_columns
  return if self.removed_columns.nil?
  c = ActiveRecord::Base.connection
  self.removed_columns.each do |model, columns|
    next if !c.table_exists?(model.table_name)
    columns.each do |col|
      c.remove_column model.table_name, col if c.column_exists?(model.table_name, col)
    end
  end
end

.removed_columnsObject

Columns (in order) that were removed in the development of the gem.



15
16
17
# File 'app/models/caboose/utilities/schema.rb', line 15

def self.removed_columns
  return nil
end

.rename_columnsObject

Renames a set of columns



117
118
119
120
121
122
123
124
125
126
127
# File 'app/models/caboose/utilities/schema.rb', line 117

def self.rename_columns
  return if self.renamed_columns.nil?
  c = ActiveRecord::Base.connection
  self.renamed_columns.each do |model, cols|
    next if !c.table_exists? model.table_name
    cols.each do |old_name, new_name|
      next if !c.column_exists?(model.table_name, old_name) || c.column_exists?(model.table_name, new_name)
      c.rename_column model.table_name, old_name, new_name
    end
  end
end

.rename_tablesObject

Renames a set of tables



107
108
109
110
111
112
113
114
# File 'app/models/caboose/utilities/schema.rb', line 107

def self.rename_tables
  return if self.renamed_tables.nil?
  c = ActiveRecord::Base.connection
  self.renamed_tables.each do |old_name, new_name|
    next if !c.table_exists?(old_name) || c.table_exists?(new_name)      
    c.rename_table old_name, new_name
  end
end

.renamed_columnsObject

Columns (in order) that were renamed in the development of the gem.



10
11
12
# File 'app/models/caboose/utilities/schema.rb', line 10

def self.renamed_columns
  return nil
end

.renamed_tablesObject

Tables (in order) that were renamed in the development of the gem.



5
6
7
# File 'app/models/caboose/utilities/schema.rb', line 5

def self.renamed_tables
  return nil
end

.schemaObject

The schema of the database { Model => [[name, data_type, options]] }

Raises:

  • (NotImplementedError)


26
27
28
# File 'app/models/caboose/utilities/schema.rb', line 26

def self.schema
  raise NotImplementedError.new("You must implement this")
end