Module: ChronoModel::Adapter::Migrations

Included in:
ChronoModel::Adapter
Defined in:
lib/chrono_model/adapter/migrations.rb

Instance Method Summary collapse

Instance Method Details

#add_column(table_name) ⇒ Object

If adding a column to a temporal table, creates it in the table in the temporal schema and updates the triggers.



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/chrono_model/adapter/migrations.rb', line 137

def add_column(table_name, *)
  return super unless is_chrono?(table_name)

  transaction do
    # Add the column to the temporal table
    on_temporal_schema { super }

    # Update the triggers
    chrono_public_view_ddl(table_name)
  end
end

#add_index(table_name, column_name, options = {}) ⇒ Object

If adding an index to a temporal table, add it to the one in the temporal schema and to the history one. If the ‘:unique` option is present, it is removed from the index created in the history table.



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/chrono_model/adapter/migrations.rb', line 109

def add_index(table_name, column_name, options = {})
  return super unless is_chrono?(table_name)

  transaction do
    on_temporal_schema { super }

    # Uniqueness constraints do not make sense in the history table
    options = options.dup.tap {|o| o.delete(:unique)} if options[:unique].present?

    on_history_schema { super table_name, column_name, options }
  end
end

#change_column(table_name) ⇒ Object

If removing a column from a temporal table, we are forced to drop the view, then change the column from the table in the temporal schema and eventually recreate the triggers.



169
170
171
172
# File 'lib/chrono_model/adapter/migrations.rb', line 169

def change_column(table_name, *)
  return super unless is_chrono?(table_name)
  drop_and_recreate_public_view(table_name) { super }
end

#change_column_default(table_name) ⇒ Object

Change the default on the temporal schema table.



176
177
178
179
# File 'lib/chrono_model/adapter/migrations.rb', line 176

def change_column_default(table_name, *)
  return super unless is_chrono?(table_name)
  on_temporal_schema { super }
end

#change_column_null(table_name) ⇒ Object

Change the null constraint on the temporal schema table.



183
184
185
186
# File 'lib/chrono_model/adapter/migrations.rb', line 183

def change_column_null(table_name, *)
  return super unless is_chrono?(table_name)
  on_temporal_schema { super }
end

#change_table(table_name, options = {}, &block) ⇒ Object

If changing a temporal table, redirect the change to the table in the temporal schema and recreate views.

If the ‘:temporal` option is specified, enables or disables temporal features on the given table. Please note that you’ll lose your history when demoting a temporal table to a plain one.



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
# File 'lib/chrono_model/adapter/migrations.rb', line 66

def change_table(table_name, options = {}, &block)
  transaction do

    # Add an empty proc to support calling change_table without a block.
    #
    block ||= proc { }

    case options[:temporal]
    when true
      if !is_chrono?(table_name)
        chrono_make_temporal_table(table_name, options)
      end

      drop_and_recreate_public_view(table_name, options) do
        super table_name, options, &block
      end

    when false
      if is_chrono?(table_name)
        chrono_undo_temporal_table(table_name)
      end

      super table_name, options, &block
    end

  end
end

#create_table(table_name, options = {}) ⇒ Object

Creates the given table, possibly creating the temporal schema objects if the ‘:temporal` option is given and set to true.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/chrono_model/adapter/migrations.rb', line 8

def create_table(table_name, options = {})
  # No temporal features requested, skip
  return super unless options[:temporal]

  if options[:id] == false
    logger.warn "ChronoModel: Temporal Temporal tables require a primary key."
    logger.warn "ChronoModel: Adding a `__chrono_id' primary key to #{table_name} definition."

    options[:id] = '__chrono_id'
  end

  transaction do
    on_temporal_schema { super }
    on_history_schema { chrono_history_table_ddl(table_name) }

    chrono_public_view_ddl(table_name, options)
  end
end

#drop_table(table_name) ⇒ Object

If dropping a temporal table, drops it from the temporal schema adding the CASCADE option so to delete the history, view and triggers.



97
98
99
100
101
102
103
# File 'lib/chrono_model/adapter/migrations.rb', line 97

def drop_table(table_name, *)
  return super unless is_chrono?(table_name)

  on_temporal_schema { execute "DROP TABLE #{table_name} CASCADE" }

  chrono_drop_trigger_functions_for(table_name)
end

#remove_column(table_name) ⇒ Object

If removing a column from a temporal table, we are forced to drop the view, then drop the column from the table in the temporal schema and eventually recreate the triggers.



192
193
194
195
# File 'lib/chrono_model/adapter/migrations.rb', line 192

def remove_column(table_name, *)
  return super unless is_chrono?(table_name)
  drop_and_recreate_public_view(table_name) { super }
end

#remove_index(table_name) ⇒ Object

If removing an index from a temporal table, remove it both from the temporal and the history schemas.



125
126
127
128
129
130
131
132
# File 'lib/chrono_model/adapter/migrations.rb', line 125

def remove_index(table_name, *)
  return super unless is_chrono?(table_name)

  transaction do
    on_temporal_schema { super }
    on_history_schema { super }
  end
end

#rename_column(table_name) ⇒ Object

If renaming a column of a temporal table, rename it in the table in the temporal schema and update the triggers.



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/chrono_model/adapter/migrations.rb', line 152

def rename_column(table_name, *)
  return super unless is_chrono?(table_name)

  # Rename the column in the temporal table and in the view
  transaction do
    on_temporal_schema { super }
    super

    # Update the triggers
    chrono_public_view_ddl(table_name)
  end
end

#rename_table(name, new_name) ⇒ Object

If renaming a temporal table, rename the history and view as well.



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
# File 'lib/chrono_model/adapter/migrations.rb', line 29

def rename_table(name, new_name)
  return super unless is_chrono?(name)

  clear_cache!

  transaction do
    # Rename tables
    #
    on_temporal_schema { rename_table_and_pk(name, new_name) }
    on_history_schema  { rename_table_and_pk(name, new_name) }

    # Rename indexes
    #
    chrono_rename_history_indexes(name, new_name)
    chrono_rename_temporal_indexes(name, new_name)

    # Drop view
    #
    execute "DROP VIEW #{name}"

    # Drop functions
    #
    chrono_drop_trigger_functions_for(name)

    # Create view and functions
    #
    chrono_public_view_ddl(new_name)
  end
end