Class: Volt::Sql::TableReconcile
- Inherits:
-
Object
- Object
- Volt::Sql::TableReconcile
- Includes:
- SqlLogger
- Defined in:
- app/sql/lib/table_reconcile.rb
Instance Attribute Summary collapse
-
#field_updater ⇒ Object
readonly
Returns the value of attribute field_updater.
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Instance Method Summary collapse
-
#db_fields_for_table(table_name) ⇒ Object
Pulls the db_fields out of sequel.
-
#ensure_table ⇒ Object
Create an empty table if one does not exist.
-
#initialize(adaptor, db, model_class) ⇒ TableReconcile
constructor
A new instance of TableReconcile.
- #run ⇒ Object
- #sequel_class_and_opts_from_db(db_field) ⇒ Object
- #update_fields(model_class, table_name) ⇒ Object
Methods included from SqlLogger
Constructor Details
#initialize(adaptor, db, model_class) ⇒ TableReconcile
Returns a new instance of TableReconcile.
14 15 16 17 18 19 20 |
# File 'app/sql/lib/table_reconcile.rb', line 14 def initialize(adaptor, db, model_class) @model_class = model_class @adaptor = adaptor @db = db @table_name = @model_class.collection_name @field_updater ||= FieldUpdater.new(@db, self) end |
Instance Attribute Details
#field_updater ⇒ Object (readonly)
Returns the value of attribute field_updater.
12 13 14 |
# File 'app/sql/lib/table_reconcile.rb', line 12 def field_updater @field_updater end |
#table_name ⇒ Object (readonly)
Returns the value of attribute table_name.
12 13 14 |
# File 'app/sql/lib/table_reconcile.rb', line 12 def table_name @table_name end |
Instance Method Details
#db_fields_for_table(table_name) ⇒ Object
Pulls the db_fields out of sequel
62 63 64 65 66 67 68 69 |
# File 'app/sql/lib/table_reconcile.rb', line 62 def db_fields_for_table(table_name) db_fields = {} result = @db.schema(table_name).each do |col| db_fields[col[0].to_sym] = col[1] end db_fields end |
#ensure_table ⇒ Object
Create an empty table if one does not exist
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/sql/lib/table_reconcile.rb', line 31 def ensure_table # Check if table exists if !@db.tables || !@db.tables.include?(table_name) log("Creating Table #{table_name}") adaptor_name = @adaptor.adaptor_name @db.create_table(table_name) do # guid id column :id, String, :unique => true, :null => false, :primary_key => true # When using underscore notation on a field that does not exist, the # data will be stored in extra. if adaptor_name == 'postgres' # Use jsonb column :extra, 'json' else column :extra, String end end # TODO: there's some issue with @db.schema and no clue why, but I # have to run this again to get .schema to work later. @db.tables end end |
#run ⇒ Object
22 23 24 25 26 27 28 |
# File 'app/sql/lib/table_reconcile.rb', line 22 def run update_fields(@model_class, table_name) IndexUpdater.new(@db, @model_class, table_name) @model_class.reconciled = true end |
#sequel_class_and_opts_from_db(db_field) ⇒ Object
56 57 58 59 |
# File 'app/sql/lib/table_reconcile.rb', line 56 def sequel_class_and_opts_from_db(db_field) vclasses, vopts = Helper.(db_field) return Helper.(vclasses, vopts) end |
#update_fields(model_class, table_name) ⇒ Object
71 72 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 109 110 111 112 113 114 115 116 117 |
# File 'app/sql/lib/table_reconcile.rb', line 71 def update_fields(model_class, table_name) if (fields = model_class.fields) db_fields = db_fields_for_table(table_name) db_fields.delete(:id) db_fields.delete(:extra) orphan_fields = db_fields.keys - fields.keys new_fields = fields.keys - db_fields.keys # If a single field was renamed, we can auto-migrate if (orphan_fields.size == 1 && new_fields.size == 1) from_name = orphan_fields[0] to_name = new_fields[0] @field_updater.auto_migrate_field_rename(table_name, from_name, to_name) # Move in start fields db_fields[to_name] = db_fields.delete(from_name) end if new_fields.size == 0 && orphan_fields.size > 0 # one or more fields were removed orphan_fields.each do |field_name| @field_updater.auto_migrate_remove_field(table_name, field_name, db_fields[field_name]) end end if orphan_fields.size == 0 fields.each do |name, klass_and_opts| name = name.to_sym klasses, opts = klass_and_opts # Get the original state for the field db_field = db_fields.delete(name) @field_updater.update_field(model_class, table_name, db_field, name, klasses, opts) end # remove any fields we didn't see in the models end else # Either >1 orphaned fields, or more than one new fields raise "Could not auto migrate #{table_name}" end end |