Class: Caboose::Utilities::Schema
- Inherits:
-
Object
- Object
- Caboose::Utilities::Schema
- Defined in:
- app/models/caboose/utilities/schema.rb
Direct Known Subclasses
Class Method Summary collapse
-
.create_indexes ⇒ Object
Verifies (non-destructively) that the given indexes exist in the database.
-
.create_schema ⇒ Object
Verifies (non-destructively) that the given schema exists in the database.
-
.indexes ⇒ Object
Any column indexes that need to exist in the database.
-
.load_data ⇒ Object
Loads initial data into the database.
-
.rename_tables ⇒ Object
Renames a set of tables.
-
.renamed_tables ⇒ Object
Tables (in order) that were renamed in the development of the gem.
-
.schema ⇒ Object
The schema of the database { Model => [[name, data_type, options]] }.
Class Method Details
.create_indexes ⇒ Object
Verifies (non-destructively) that the given indexes exist in the database.
91 92 93 94 95 96 97 98 99 |
# File 'app/models/caboose/utilities/schema.rb', line 91 def self.create_indexes return if self.indexes.nil? c = ActiveRecord::Base.connection self.indexes.each do |table, indexes| indexes.each do |index| c.add_index table, index if !c.index_exists?(table, index) end end end |
.create_schema ⇒ Object
Verifies (non-destructively) that the given schema exists in the database.
29 30 31 32 33 34 35 36 37 38 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 |
# File 'app/models/caboose/utilities/schema.rb', line 29 def self.create_schema return if self.schema.nil? rename_tables c = ActiveRecord::Base.connection self.schema.each do |model, columns| tbl = model.table_name #puts "Creating table #{tbl}..." c.create_table tbl if !c.table_exists?(tbl) columns.each do |col| #puts "Creating column #{tbl}.#{col[0]}..." # 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 # Add a temp column if col.count > 2 c.add_column tbl, "#{col[0]}_temp", col[1], col[2] else c.add_column tbl, "#{col[0]}_temp", col[1] end # Copy the old column and cast with correct data type to the new column model.all.each do |m| m["#{col[0]}_temp"] = case col[1] when :integer then m[col[0]].to_i when :string then m[col[0]].to_s when :text then m[col[0]].to_s when :numeric then m[col[0]].to_f when :datetime then DateTime.parse(m[col[0]]) when :boolean then m[col[0]].to_i == 1 else nil end m.save end # Remove the old column and rename the new one c.remove_column tbl, col[0] c.rename_column tbl, "#{col[0]}_temp", col[0] end end end create_indexes self.schema.each do |model, columns| model.reset_column_information end end |
.indexes ⇒ Object
Any column indexes that need to exist in the database
10 11 12 |
# File 'app/models/caboose/utilities/schema.rb', line 10 def self.indexes return nil end |
.load_data ⇒ Object
Loads initial data into the database
21 22 |
# File 'app/models/caboose/utilities/schema.rb', line 21 def self.load_data end |
.rename_tables ⇒ Object
Renames a set of tables
102 103 104 105 106 107 108 |
# File 'app/models/caboose/utilities/schema.rb', line 102 def self.rename_tables return if self.renamed_tables.nil? c = ActiveRecord::Base.connection self.renamed_tables.each do |old_name, new_name| c.rename_table old_name, new_name if c.table_exists?(old_name) end end |
.renamed_tables ⇒ Object
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 |
.schema ⇒ Object
The schema of the database { Model => [[name, data_type, options]] }
16 17 18 |
# File 'app/models/caboose/utilities/schema.rb', line 16 def self.schema raise NotImplementedError.new("You must implement this") end |