This repository was archived by the owner on Sep 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
OSAC-1532: Rename organizations table to tenants #730
Merged
openshift-merge-bot
merged 2 commits into
osac-project:main
from
DakCrowder:rename-organizations-table-to-tenants
Jun 22, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
internal/database/migrations/58_rename_organizations_to_tenants.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| -- | ||
| -- Copyright (c) 2026 Red Hat Inc. | ||
| -- | ||
| -- Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| -- the License. You may obtain a copy of the License at | ||
| -- | ||
| -- http://www.apache.org/licenses/LICENSE-2.0 | ||
| -- | ||
| -- Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| -- an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| -- specific language governing permissions and limitations under the License. | ||
| -- | ||
|
|
||
| -- Rename the organizations tables to tenants. The immutable column trigger and all foreign key constraints that | ||
| -- reference the table must be dropped and re-created because they are bound to the table name. | ||
|
|
||
| -- Drop the immutable column trigger (its error messages reference the table name): | ||
| drop trigger check_immutable_columns on organizations; | ||
|
|
||
| -- Drop all foreign key constraints that reference organizations(id): | ||
| do $$ | ||
| declare | ||
| r record; | ||
| begin | ||
| for r in | ||
| select | ||
| con.conname as constraint_name, | ||
| c.relname as table_name | ||
| from | ||
| pg_catalog.pg_constraint con | ||
| join | ||
| pg_catalog.pg_class c on c.oid = con.conrelid | ||
| join | ||
| pg_catalog.pg_class fc on fc.oid = con.confrelid | ||
| join | ||
| pg_catalog.pg_namespace fn on fn.oid = fc.relnamespace | ||
| where | ||
| con.contype = 'f' and | ||
| fn.nspname = 'public' and | ||
| fc.relname = 'organizations' | ||
| loop | ||
| execute format('alter table %I drop constraint %I', r.table_name, r.constraint_name); | ||
| end loop; | ||
| end; | ||
| $$; | ||
|
|
||
| -- Rename the tables: | ||
| alter table organizations rename to tenants; | ||
| alter table archived_organizations rename to archived_tenants; | ||
|
|
||
| -- Rename the indexes: | ||
| alter index organizations_by_name rename to tenants_by_name; | ||
| alter index organizations_by_owner rename to tenants_by_owner; | ||
| alter index organizations_by_tenant rename to tenants_by_tenant; | ||
| alter index organizations_by_label rename to tenants_by_label; | ||
|
|
||
| -- Re-create the immutable column trigger on the renamed table: | ||
| create trigger check_immutable_columns | ||
| before update on tenants | ||
| for each row | ||
| execute function check_immutable_columns('name', 'tenant'); | ||
|
|
||
| -- Re-create all tenant foreign key constraints referencing tenants(id): | ||
| do $$ | ||
| declare | ||
| t text; | ||
| begin | ||
| for t in | ||
| select | ||
| c.table_name | ||
| from | ||
| information_schema.columns c | ||
| join | ||
| information_schema.tables tb on | ||
| tb.table_schema = c.table_schema and | ||
| tb.table_name = c.table_name | ||
| where | ||
| c.table_schema = 'public' and | ||
| c.column_name = 'tenant' and | ||
| tb.table_type = 'BASE TABLE' and | ||
| c.table_name not like 'archived_%' and | ||
| c.table_name not in ('notifications', 'schema_migrations') | ||
| order by | ||
| c.table_name | ||
| loop | ||
| execute format( | ||
| 'alter table %I add constraint %I foreign key (tenant) references tenants (id)', | ||
| t, t || '_tenant_fk' | ||
| ); | ||
| end loop; | ||
| end; | ||
| $$; |
101 changes: 101 additions & 0 deletions
101
internal/database/migrations/58_rename_organizations_to_tenants_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /* | ||
| Copyright (c) 2026 Red Hat Inc. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the | ||
| License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an | ||
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific | ||
| language governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package migrations | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2/dsl/core" | ||
| . "github.com/onsi/gomega" | ||
| ) | ||
|
|
||
| var _ = DescribeMigration("Rename organizations to tenants", func() { | ||
| It("Renames the organizations table to tenants", func(ctx context.Context) { | ||
| err := tool.Migrate(ctx, 58) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
|
|
||
| _, err = conn.Exec(ctx, ` | ||
| insert into tenants (id, name, tenant, data) | ||
| values ('my-tenant', 'my-tenant', 'my-tenant', '{}') | ||
| `) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| }) | ||
|
|
||
| It("Removes the organizations table", func(ctx context.Context) { | ||
| err := tool.Migrate(ctx, 58) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
|
|
||
| var count int | ||
| err = conn.QueryRow(ctx, ` | ||
| select count(*) from pg_catalog.pg_class c | ||
| join pg_catalog.pg_namespace n on n.oid = c.relnamespace | ||
| where n.nspname = 'public' and c.relkind = 'r' and c.relname = 'organizations' | ||
| `).Scan(&count) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| Expect(count).To(Equal(0)) | ||
| }) | ||
|
|
||
| It("Renames the archived table", func(ctx context.Context) { | ||
| err := tool.Migrate(ctx, 58) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
|
|
||
| var count int | ||
| err = conn.QueryRow(ctx, ` | ||
| select count(*) from pg_catalog.pg_class c | ||
| join pg_catalog.pg_namespace n on n.oid = c.relnamespace | ||
| where n.nspname = 'public' and c.relkind = 'r' and c.relname = 'archived_tenants' | ||
| `).Scan(&count) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| Expect(count).To(Equal(1)) | ||
| }) | ||
|
|
||
| It("Preserves tenant foreign key constraints", func(ctx context.Context) { | ||
| err := tool.Migrate(ctx, 58) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
|
|
||
| // Insert a valid tenant first: | ||
| _, err = conn.Exec(ctx, ` | ||
| insert into tenants (id, name, tenant, data) | ||
| values ('my-tenant', 'my-tenant', 'my-tenant', '{}') | ||
| `) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
|
|
||
| // Inserting a cluster with a non-existent tenant should fail: | ||
| _, err = conn.Exec(ctx, ` | ||
| insert into clusters (id, name, tenant, data) | ||
| values ('123', 'my-cluster', 'no-such-tenant', '{}') | ||
| `) | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(err.Error()).To(ContainSubstring("tenant_fk")) | ||
| }) | ||
|
|
||
| It("Preserves the immutable column trigger", func(ctx context.Context) { | ||
| err := tool.Migrate(ctx, 58) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
|
|
||
| // Insert a tenant: | ||
| _, err = conn.Exec(ctx, ` | ||
| insert into tenants (id, name, tenant, data) | ||
| values ('my-tenant', 'my-tenant', 'my-tenant', '{}') | ||
| `) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
|
|
||
| // Updating the name should be rejected: | ||
| _, err = conn.Exec(ctx, ` | ||
| update tenants set name = 'new-name' where id = 'my-tenant' | ||
| `) | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(err.Error()).To(ContainSubstring("immutable")) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.