Module: RailsSync::Table
- Included in:
- Analyzer
- Defined in:
- lib/rails_sync/table.rb
Instance Attribute Summary collapse
-
#dest_table_name ⇒ Object
readonly
Returns the value of attribute dest_table_name.
Instance Method Summary collapse
- #create_temp_table ⇒ Object
- #dest_columns ⇒ Object
- #dest_indexes ⇒ Object
- #dest_primary_key ⇒ Object
- #dest_sql_table(only: [], except: [], pure: true) ⇒ Object
- #drop_temp_table ⇒ Object
- #instance_table ⇒ Object
- #reset_temp_table ⇒ Object
- #same_server? ⇒ Boolean
Instance Attribute Details
#dest_table_name ⇒ Object (readonly)
Returns the value of attribute dest_table_name.
3 4 5 |
# File 'lib/rails_sync/table.rb', line 3 def dest_table_name @dest_table_name end |
Instance Method Details
#create_temp_table ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rails_sync/table.rb', line 89 def create_temp_table unless @dest_columns.include?(dest_primary_key) @dest_columns.unshift dest_primary_key end sql = "CREATE TABLE `#{@dest_table_name}` (\n" sql << dest_sql_table(only: @dest_columns) sql << ")" sql << "ENGINE=FEDERATED\n" sql << "CONNECTION='#{adapter.url}/#{@dest_table}';" connection.execute(sql) end |
#dest_columns ⇒ Object
19 20 21 |
# File 'lib/rails_sync/table.rb', line 19 def dest_columns adapter.connection.columns(@dest_table) end |
#dest_indexes ⇒ Object
23 24 25 26 27 28 |
# File 'lib/rails_sync/table.rb', line 23 def dest_indexes results = adapter.connection.indexes(@dest_table) results = results.map { |result| { result.name => result.columns } } results.to_combined_hash # rails_com core ext results end |
#dest_primary_key ⇒ Object
30 31 32 |
# File 'lib/rails_sync/table.rb', line 30 def dest_primary_key adapter.connection.primary_key(@dest_table) end |
#dest_sql_table(only: [], except: [], pure: true) ⇒ Object
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 |
# File 'lib/rails_sync/table.rb', line 34 def dest_sql_table(only: [], except: [], pure: true) if only.size > 0 _columns = dest_columns.select { |column| only.include?(column.name) } else _columns = dest_columns.reject { |column| except.include?(column.name) } end if pure sql = "" else sql = "CREATE TABLE `#{@dest_table}` (\n" end _columns.each do |column| sql << " `#{column.name}` #{column.sql_type}" sql << " COLLATE #{column.collation}" if column.collation.present? sql << " NOT NULL" if column.null.is_a?(FalseClass) if column.default sql << " DEFAULT '#{column.default}',\n" elsif column.default.nil? && column.null sql << " DEFAULT NULL,\n" else sql << ",\n" end end sql << " PRIMARY KEY (`#{dest_primary_key}`)" _indexes = dest_indexes.reject { |_, value| (Array(value) & _columns.map { |col| col.name }).blank? } if _indexes.present? sql << ",\n" else sql << "\n" end _indexes.each do |index, columns| sql << " KEY `#{index}` (" sql << Array(columns).map { |col| "`#{col}`" }.join(',') sql << "),\n" end sql.chomp!(",\n") if pure sql else sql << ")" end end |
#drop_temp_table ⇒ Object
103 104 105 106 107 |
# File 'lib/rails_sync/table.rb', line 103 def drop_temp_table sql = "DROP TABLE IF EXISTS `#{@dest_table_name}`" connection.execute(sql) end |
#instance_table ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/rails_sync/table.rb', line 5 def instance_table if same_server? # `source.table_name` @dest_table_name = @adapter.instance_variable_get(:@adapter_options)[:database].to_s + '.' + @dest_table.to_s else # `source_table_name` @dest_table_name = @dest.to_s + '_' + @table_name + '-' + @dest_table.to_s end end |
#reset_temp_table ⇒ Object
84 85 86 87 |
# File 'lib/rails_sync/table.rb', line 84 def reset_temp_table drop_temp_table create_temp_table end |
#same_server? ⇒ Boolean
15 16 17 |
# File 'lib/rails_sync/table.rb', line 15 def same_server? @server_id == adapter.server_id end |