Class: Gitlab::Database::Triggers::AssignDesiredShardingKey
- Inherits:
-
Object
- Object
- Gitlab::Database::Triggers::AssignDesiredShardingKey
- Includes:
- SchemaHelpers
- Defined in:
- lib/gitlab/database/triggers/assign_desired_sharding_key.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #create ⇒ Object
- #drop ⇒ Object
-
#initialize(table:, sharding_key:, parent_table:, parent_sharding_key:, foreign_key:, connection:, parent_table_primary_key: nil, trigger_name: nil) ⇒ AssignDesiredShardingKey
constructor
A new instance of AssignDesiredShardingKey.
Methods included from SchemaHelpers
#assert_not_in_transaction_block, #create_comment, #create_trigger, #create_trigger_function, #drop_function, #drop_trigger, #find_all_id_columns_sql, #function_exists?, #object_name, #reset_all_trigger_functions, #reset_trigger_function, #tmp_table_name, #trigger_exists?
Constructor Details
#initialize(table:, sharding_key:, parent_table:, parent_sharding_key:, foreign_key:, connection:, parent_table_primary_key: nil, trigger_name: nil) ⇒ AssignDesiredShardingKey
Returns a new instance of AssignDesiredShardingKey.
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/gitlab/database/triggers/assign_desired_sharding_key.rb', line 13 def initialize( table:, sharding_key:, parent_table:, parent_sharding_key:, foreign_key:, connection:, parent_table_primary_key: nil, trigger_name: nil ) @table = table @sharding_key = sharding_key @parent_table = parent_table @parent_table_primary_key = parent_table_primary_key @parent_sharding_key = parent_sharding_key @foreign_key = foreign_key @name = trigger_name || generated_name @connection = connection end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/gitlab/database/triggers/assign_desired_sharding_key.rb', line 9 def name @name end |
Instance Method Details
#create ⇒ Object
27 28 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 |
# File 'lib/gitlab/database/triggers/assign_desired_sharding_key.rb', line 27 def create quoted_table_name = quote_table_name(table) quoted_parent_table = quote_table_name(parent_table) quoted_sharding_key = quote_column_name(sharding_key) quoted_parent_sharding_key = quote_column_name(parent_sharding_key) quoted_primary_key = quote_column_name(parent_table_primary_key || 'id') quoted_foreign_key = quote_column_name(foreign_key) create_trigger_function(name) do " IF NEW.\#{quoted_sharding_key} IS NULL THEN\n SELECT \#{quoted_parent_sharding_key}\n INTO NEW.\#{quoted_sharding_key}\n FROM \#{quoted_parent_table}\n WHERE \#{quoted_parent_table}.\#{quoted_primary_key} = NEW.\#{quoted_foreign_key};\n END IF;\n\n RETURN NEW;\n SQL\n end\n\n # Postgres 14 adds the `OR REPLACE` option to trigger creation, so\n # this line can be removed and `OR REPLACE` added to `#create_trigger`\n # when the minimum supported version is updated to 14 (milestone 17.0).\n drop_trigger(quoted_table_name, name)\n\n create_trigger(quoted_table_name, name, name, fires: 'BEFORE INSERT OR UPDATE')\nend\n" |
#drop ⇒ Object
56 57 58 59 |
# File 'lib/gitlab/database/triggers/assign_desired_sharding_key.rb', line 56 def drop drop_trigger(quote_table_name(table), name) drop_function(name) end |