Module: ChronoModel::Adapter::Migrations
- Included in:
- ChronoModel::Adapter
- Defined in:
- lib/chrono_model/adapter/migrations.rb
Instance Method Summary collapse
-
#add_column(table_name, column_name, type, **options) ⇒ Object
If adding a column to a temporal table, creates it in the table in the temporal schema and updates the triggers.
-
#change_column(table_name, column_name, type, **options) ⇒ 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.
-
#change_column_default(table_name) ⇒ Object
Change the default on the temporal schema table.
-
#change_column_null(table_name) ⇒ Object
Change the null constraint on the temporal schema table.
-
#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.
-
#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.
-
#drop_table(table_name, **options) ⇒ Object
If dropping a temporal table, drops it from the temporal schema adding the CASCADE option so to delete the history, view and triggers.
-
#remove_column(table_name, column_name, type = nil, **options) ⇒ 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.
-
#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.
-
#rename_table(name, new_name, **options) ⇒ Object
If renaming a temporal table, rename the history and view as well.
Methods included from ChronoModel::Adapter::MigrationsModules::Stable
Instance Method Details
#add_column(table_name, column_name, type, **options) ⇒ Object
If adding a column to a temporal table, creates it in the table in the temporal schema and updates the triggers.
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/chrono_model/adapter/migrations.rb', line 110 def add_column(table_name, column_name, type, **) 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 |
#change_column(table_name, column_name, type, **options) ⇒ 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.
142 143 144 145 146 |
# File 'lib/chrono_model/adapter/migrations.rb', line 142 def change_column(table_name, column_name, type, **) 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.
150 151 152 153 154 |
# File 'lib/chrono_model/adapter/migrations.rb', line 150 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.
158 159 160 161 162 |
# File 'lib/chrono_model/adapter/migrations.rb', line 158 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.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/chrono_model/adapter/migrations.rb', line 71 def change_table(table_name, **, &block) transaction do # Add an empty proc to support calling change_table without a block. # block ||= proc {} if [:temporal] unless is_chrono?(table_name) chrono_make_temporal_table(table_name, ) end drop_and_recreate_public_view(table_name, ) do super(table_name, **, &block) end else if is_chrono?(table_name) chrono_undo_temporal_table(table_name) end super(table_name, **, &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.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/chrono_model/adapter/migrations.rb', line 9 def create_table(table_name, **) # No temporal features requested, skip return super unless [:temporal] if [: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." [: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, ) end end |
#drop_table(table_name, **options) ⇒ Object
If dropping a temporal table, drops it from the temporal schema adding the CASCADE option so to delete the history, view and triggers.
99 100 101 102 103 104 105 |
# File 'lib/chrono_model/adapter/migrations.rb', line 99 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, column_name, type = nil, **options) ⇒ 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.
168 169 170 171 172 |
# File 'lib/chrono_model/adapter/migrations.rb', line 168 def remove_column(table_name, column_name, type = nil, **) return super unless is_chrono?(table_name) drop_and_recreate_public_view(table_name) { super } 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.
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/chrono_model/adapter/migrations.rb', line 125 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, **options) ⇒ Object
If renaming a temporal table, rename the history and view as well.
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 |
# File 'lib/chrono_model/adapter/migrations.rb', line 30 def rename_table(name, new_name, **) unless is_chrono?(name) return super(name, new_name) if method(:rename_table).super_method.arity == 2 return super end 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 |