Skip to content

Resolve "Add namespace_id column to the broadcast_messages table to relate these 2 tables"

What does this MR do and why?

We want to implement the Broadcast messages feature at the group level. This is a feature that already exists at the instance level.

But, since this available only at the instance level, it is available only to self-managed customers, and not to (group) admins on gitlab.com, which is why we want to introduce it at the group level too.

Since the BroadcastMessage model and its table already exists, it contains all the necessary settings required for the feature. By adding a namespace_id column to this existing table, we can extend this instance level feature to group level relatively easily.

This MR is the first step towards this change, where we:

  • add a new column namespace_id to the broadcast_messages table.
  • sets up an index and foreign key on the new namespace_id column.

Migration output

rake db:migrate
main: == 20220822102651 AddNamespaceIdToBroadcastMessage: migrating =================
main: -- add_column(:broadcast_messages, :namespace_id, :bigint)
main:    -> 0.0018s
main: == 20220822102651 AddNamespaceIdToBroadcastMessage: migrated (0.0024s) ========

main: == 20220822103638 AddIndexAndForeignKeyToBroadcastMessage: migrating ==========
main: -- transaction_open?()
main:    -> 0.0000s
main: -- index_exists?(:broadcast_messages, :namespace_id, {:name=>"index_broadcast_messages_on_namespace_id", :algorithm=>:concurrently})
main:    -> 0.0035s
main: -- execute("SET statement_timeout TO 0")
main:    -> 0.0001s
main: -- add_index(:broadcast_messages, :namespace_id, {:name=>"index_broadcast_messages_on_namespace_id", :algorithm=>:concurrently})
main:    -> 0.0013s
main: -- execute("RESET statement_timeout")
main:    -> 0.0003s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- foreign_keys(:broadcast_messages)
main:    -> 0.0020s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- execute("ALTER TABLE broadcast_messages\nADD CONSTRAINT fk_7bf2ec43da\nFOREIGN KEY (namespace_id)\nREFERENCES namespaces (id)\nON DELETE CASCADE\nNOT VALID;\n")
main:    -> 0.0013s
main: -- execute("ALTER TABLE broadcast_messages VALIDATE CONSTRAINT fk_7bf2ec43da;")
main:    -> 0.0018s
main: == 20220822103638 AddIndexAndForeignKeyToBroadcastMessage: migrated (0.0155s) =

ci: == 20220822102651 AddNamespaceIdToBroadcastMessage: migrating =================
ci: -- add_column(:broadcast_messages, :namespace_id, :bigint)
ci:    -> 0.0005s
ci: == 20220822102651 AddNamespaceIdToBroadcastMessage: migrated (0.0006s) ========

ci: == 20220822103638 AddIndexAndForeignKeyToBroadcastMessage: migrating ==========
ci: -- transaction_open?()
ci:    -> 0.0000s
ci: -- index_exists?(:broadcast_messages, :namespace_id, {:name=>"index_broadcast_messages_on_namespace_id", :algorithm=>:concurrently})
ci:    -> 0.0023s
ci: -- execute("SET statement_timeout TO 0")
ci:    -> 0.0002s
ci: -- add_index(:broadcast_messages, :namespace_id, {:name=>"index_broadcast_messages_on_namespace_id", :algorithm=>:concurrently})
ci:    -> 0.0017s
ci: -- execute("RESET statement_timeout")
ci:    -> 0.0002s
ci: -- transaction_open?()
ci:    -> 0.0000s
ci: -- foreign_keys(:broadcast_messages)
ci:    -> 0.0022s
ci: -- transaction_open?()
ci:    -> 0.0000s
ci: -- execute("ALTER TABLE broadcast_messages\nADD CONSTRAINT fk_7bf2ec43da\nFOREIGN KEY (namespace_id)\nREFERENCES namespaces (id)\nON DELETE CASCADE\nNOT VALID;\n")
ci:    -> 0.0013s
ci: -- execute("ALTER TABLE broadcast_messages VALIDATE CONSTRAINT fk_7bf2ec43da;")
ci:    -> 0.0041s
ci: == 20220822103638 AddIndexAndForeignKeyToBroadcastMessage: migrated (0.0153s) =
rake db:rollback:main STEP=2
main: == 20220822103638 AddIndexAndForeignKeyToBroadcastMessage: reverting ==========
main: -- foreign_keys(:broadcast_messages)
main:    -> 0.0048s
main: -- remove_foreign_key(:broadcast_messages, {:column=>:namespace_id})
main:    -> 0.0026s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- indexes(:broadcast_messages)
main:    -> 0.0021s
main: -- execute("SET statement_timeout TO 0")
main:    -> 0.0001s
main: -- remove_index(:broadcast_messages, {:algorithm=>:concurrently, :name=>"index_broadcast_messages_on_namespace_id"})
main:    -> 0.0010s
main: -- execute("RESET statement_timeout")
main:    -> 0.0002s
main: == 20220822103638 AddIndexAndForeignKeyToBroadcastMessage: reverted (0.0156s) =

main: == 20220822102651 AddNamespaceIdToBroadcastMessage: reverting =================
main: -- remove_column(:broadcast_messages, :namespace_id, :bigint)
main:    -> 0.0004s
main: == 20220822102651 AddNamespaceIdToBroadcastMessage: reverted (0.0015s) ========

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #368774 (closed)

Edited by Manoj M J

Merge request reports

Loading