Class: DataAPI::DatabaseMaintainer

Inherits:
Object
  • Object
show all
Defined in:
app/models/data_api/database_maintainer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ DatabaseMaintainer


4
5
6
7
# File 'app/models/data_api/database_maintainer.rb', line 4

def initialize(model)
  self.model = model
  self.migration = new_migration(model)
end

Instance Attribute Details

#migrationObject

Returns the value of attribute migration


2
3
4
# File 'app/models/data_api/database_maintainer.rb', line 2

def migration
  @migration
end

#modelObject

Returns the value of attribute model


2
3
4
# File 'app/models/data_api/database_maintainer.rb', line 2

def model
  @model
end

Instance Method Details

#create_table(table_name, schema) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/models/data_api/database_maintainer.rb', line 9

def create_table(table_name, schema)
  migration.transaction do
    migration.create_table table_name do |t|
      schema.each do |column_name, column_attrs|
        t.send(column_attrs[:type], column_name)
      end
    end

    schema.each do |column_name, column_attrs|
      migration.add_index table_name, column_name if column_attrs[:index]
    end
  end
end

#drop_table(table_name) ⇒ Object


23
24
25
26
27
# File 'app/models/data_api/database_maintainer.rb', line 23

def drop_table(table_name)
  migration.transaction do
    migration.drop_table table_name
  end
end

#update_table(old_table_name, new_table_name, old_schema, new_schema, test_run: false) ⇒ Object


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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/data_api/database_maintainer.rb', line 29

def update_table(old_table_name, new_table_name, old_schema, new_schema, test_run: false)
  changes = []

  migration.transaction do

    # Rename the table if needed

    if new_table_name != old_table_name
      migration.rename_table old_table_name, new_table_name unless test_run
      changes << ['rename_table', [old_table_name, new_table_name]]
    end

    table_name = new_table_name

    # Get column data

    new_column_data = new_schema.to_hash_indexed_with_uuid
    old_column_data = old_schema.to_hash_indexed_with_uuid

    # Deal with deleted columns

    deleted_columns = {}

    old_column_data.each do |uuid, column_attrs|
      deleted_columns[uuid] = column_attrs unless new_column_data.key?(uuid)
    end

    deleted_columns.each do |_uuid, column_attrs|
      migration.remove_column table_name, column_attrs[:name] unless test_run
      changes << ['remove_column', [table_name, column_attrs[:name]]]
    end

    # Create new columns

    new_columns = {}

    new_column_data.each do |uuid, column_attrs|
      new_columns[uuid] = column_attrs unless old_column_data.key?(uuid)
    end

    new_columns.each do |_uuid, column_attrs|
      migration.add_column table_name, column_attrs[:name], column_attrs[:type] unless test_run
      changes << ['add_column', [table_name, column_attrs[:name], column_attrs[:type]]]
    end

    # Deal with renamed columns

    renamed_columns = {}

    new_column_data.each do |uuid, column_attrs|
      renamed_columns[uuid] = column_attrs if old_column_data[uuid].present? &&
                                              column_attrs[:name] != old_column_data[uuid][:name]
    end

    renamed_columns.each do |uuid, _column_attrs|
      migration.rename_column table_name, old_column_data[uuid][:name], new_column_data[uuid][:name] unless test_run
      changes << ['rename_column', [table_name, old_column_data[uuid][:name], new_column_data[uuid][:name]]]
    end

    # Deal with added indexes

    added_indexes = {}

    new_column_data.each do |uuid, column_attrs|
      added_indexes[uuid] = column_attrs if old_column_data[uuid].present? &&
                                            column_attrs[:index] &&
                                            !old_column_data[uuid][:index]
    end

    added_indexes.each do |_uuid, column_attrs|
      migration.add_index table_name, column_attrs[:name] unless test_run
      changes << ['add_index', [table_name, column_attrs[:name]]]
    end

    # Deal with removed indexes

    removed_indexes = {}

    new_column_data.each do |uuid, column_attrs|
      removed_indexes[uuid] = column_attrs if old_column_data[uuid].present? &&
                                              !column_attrs[:index] &&
                                              old_column_data[uuid][:index]
    end

    removed_indexes.each do |_uuid, column_attrs|
      migration.remove_index table_name, column_attrs[:name] unless test_run
      changes << ['remove_index', [table_name, column_attrs[:name]]]
    end

    # Deal with type changed columns

    # type_changed_columns = {}

    # new_column_data.each do |uuid, column_attrs|
    #   type_changed_columns[uuid] = column_attrs if old_column_data[uuid].present? &&
    #                                                column_attrs[:type] != old_column_data[uuid][:type]
    # end

    # type_changed_columns.each do |uuid, _column_attrs|
    #   migration.change_column table_name, new_column_data[uuid][:name], new_column_data[uuid][:type]
    # end

  end

  changes
end