Skip to content

Add feature flag for setting default organization in the model

Rutger Wessels requested to merge 492349-namespace-model-sets-default into master

What does this MR do and why?

When we closed #411832 (closed), all code paths that create a Namespace are now explicitly assigning an organization and no longer relying on the database default value. This means we can clean up the database default of 1 for namespace.organization_id column.

Since namespaces is an important table, we want to derisk this database change. So we want to use a feature flag. However, a database change can not be put behind a feature flag. So we want to move the responsibility of setting the default from the database layer to the model layer.

This MR will introduce a feature flag that will allow us to move the responsibility of setting organization_id on Namespace model from the database to the application. This allows us to remove the (now redundant) default value but turn it back on in case something breaks.

See Implementation Plan in the parent issue for details.

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

How to set up and validate locally

Using Rails console and with feature flag namespace_model_default_org disabled:

[1] pry(main)> Namespace.new.organization
=> #<Organizations::Organization:0x0000000151fbde78
 id: 1,
 created_at: Thu, 03 Oct 2024 09:50:31.809020000 UTC +00:00,
 updated_at: Thu, 03 Oct 2024 09:50:31.809020000 UTC +00:00,
 name: "Default",
 path: "default",
 visibility_level: 20>
[2] pry(main)> Namespace.new.organization_id
=> 1

So the organization is assigned to the new Namespace object.

Now quit the Rails console and, using gdk psql, change the default value of namespaces table:

ALTER TABLE namespaces ALTER COLUMN organization_id SET DEFAULT 100

And in a Rails console:

[1] pry(main)> Namespace.new.organization
=> nil
[2] pry(main)> Namespace.new.organization_id
=> 100
[3] pry(main)> Feature.enable(:namespace_model_default_org)
=> true
[4] pry(main)> Namespace.new.organization
=> #<Organizations::Organization:0x0000000134074bc8
 id: 1,
 created_at: Thu, 03 Oct 2024 09:50:31.809020000 UTC +00:00,
 updated_at: Thu, 03 Oct 2024 09:50:31.809020000 UTC +00:00,
 name: "Default",
 path: "default",
 visibility_level: 20>
[5] pry(main)> Namespace.new.organization_id
=> 1
[6] pry(main)>

We changed the database default to 100, an Organization that (probably) does not exist on the local development environment. After enabling the feature flag, the model default of 1 is used.

Quit Rails console and restore the default value using gdk psql:

 ALTER TABLE namespaces ALTER COLUMN organization_id SET DEFAULT 1;

Related to #492349 (closed)

Edited by Rutger Wessels

Merge request reports

Loading