Skip to content

Commit 63995e7

Browse files
jr-rkclaude
andcommitted
fix(db): reassign CLARIN table ownership without the public. prefix
Revert the in-place edits to V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sql and V7.6_2024.08.05__Added_Preview_Tables.sql -- both are already applied, so changing their content breaks their Flyway checksum and every existing install fails validation on the next startup. Add V7.6_2026.09.11__Clarin_tables_owner_schema_agnostic.sql instead, which restates the same `ALTER TABLE ... OWNER TO dspace` intent for the 17 CLARIN and preview objects, resolving each name through search_path via to_regclass() rather than a hardcoded `public.` qualifier. The reassignment is best-effort: it is skipped when the `dspace` role is absent, when the object is already owned by it, or when the statement lacks the required privileges (the target role needs CREATE on the object's schema). A tenant-owned schema therefore keeps its current owner instead of failing the migration. Verified against PostgreSQL 15 for: non-public schema with and without the schema grant, repeated runs, a database with no CLARIN tables, a classic public-schema install, and a cluster with no `dspace` role. Only the search_path schema is touched -- a same-named table in `public` is left alone. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent def9aa4 commit 63995e7

3 files changed

Lines changed: 90 additions & 17 deletions

File tree

dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sql

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ CREATE TABLE license_definition (
3131
required_info varchar(256)
3232
);
3333

34-
ALTER TABLE license_definition OWNER TO dspace;
34+
ALTER TABLE public.license_definition OWNER TO dspace;
3535

3636
--
3737
-- Name: license_definition_license_id_seq; Type: SEQUENCE; Schema: public; Owner: dspace
@@ -44,7 +44,7 @@ CREATE SEQUENCE license_definition_license_id_seq
4444
NO MINVALUE
4545
CACHE 1;
4646

47-
ALTER TABLE license_definition_license_id_seq OWNER TO dspace;
47+
ALTER TABLE public.license_definition_license_id_seq OWNER TO dspace;
4848

4949
--
5050
-- Name: license_definition_license_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dspace
@@ -65,7 +65,7 @@ CREATE TABLE license_label (
6565
);
6666

6767

68-
ALTER TABLE license_label OWNER TO dspace;
68+
ALTER TABLE public.license_label OWNER TO dspace;
6969

7070
--
7171
-- Name: license_label_extended_mapping; Type: TABLE; Schema: public; Owner: dspace; Tablespace:
@@ -77,7 +77,7 @@ CREATE TABLE license_label_extended_mapping (
7777
label_id integer
7878
);
7979

80-
ALTER TABLE license_label_extended_mapping OWNER TO dspace;
80+
ALTER TABLE public.license_label_extended_mapping OWNER TO dspace;
8181

8282
--
8383
-- Name: license_label_extended_mapping_mapping_id_seq; Type: SEQUENCE; Schema: public; Owner: dspace
@@ -91,7 +91,7 @@ CREATE SEQUENCE license_label_extended_mapping_mapping_id_seq
9191
CACHE 1;
9292

9393

94-
ALTER TABLE license_label_extended_mapping_mapping_id_seq OWNER TO dspace;
94+
ALTER TABLE public.license_label_extended_mapping_mapping_id_seq OWNER TO dspace;
9595

9696
--
9797
-- Name: license_label_extended_mapping_mapping_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dspace
@@ -118,7 +118,7 @@ CREATE SEQUENCE license_label_label_id_seq
118118
CACHE 1;
119119

120120

121-
ALTER TABLE license_label_label_id_seq OWNER TO dspace;
121+
ALTER TABLE public.license_label_label_id_seq OWNER TO dspace;
122122

123123
--
124124
-- Name: license_label_label_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dspace
@@ -144,7 +144,7 @@ CREATE TABLE license_resource_mapping (
144144
);
145145

146146

147-
ALTER TABLE license_resource_mapping OWNER TO dspace;
147+
ALTER TABLE public.license_resource_mapping OWNER TO dspace;
148148

149149
--
150150
-- Name: license_resource_mapping_mapping_id_seq; Type: SEQUENCE; Schema: public; Owner: dspace
@@ -158,7 +158,7 @@ CREATE SEQUENCE license_resource_mapping_mapping_id_seq
158158
CACHE 1;
159159

160160

161-
ALTER TABLE license_resource_mapping_mapping_id_seq OWNER TO dspace;
161+
ALTER TABLE public.license_resource_mapping_mapping_id_seq OWNER TO dspace;
162162

163163
--
164164
-- Name: license_resource_mapping_mapping_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dspace
@@ -186,7 +186,7 @@ CREATE TABLE license_resource_user_allowance (
186186
token varchar(256)
187187
);
188188

189-
ALTER TABLE license_resource_user_allowance OWNER TO dspace;
189+
ALTER TABLE public.license_resource_user_allowance OWNER TO dspace;
190190

191191
--
192192
-- Name: license_resource_user_allowance_transaction_id_seq; Type: SEQUENCE; Schema: public; Owner: dspace
@@ -199,7 +199,7 @@ CREATE SEQUENCE license_resource_user_allowance_transaction_id_seq
199199
NO MINVALUE
200200
CACHE 1;
201201

202-
ALTER TABLE license_resource_user_allowance_transaction_id_seq OWNER TO dspace;
202+
ALTER TABLE public.license_resource_user_allowance_transaction_id_seq OWNER TO dspace;
203203

204204
--
205205
-- Name: license_resource_user_allowance_transaction_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dspace
@@ -224,7 +224,7 @@ CREATE TABLE user_registration (
224224
confirmation boolean DEFAULT true
225225
);
226226

227-
ALTER TABLE user_registration OWNER TO dspace;
227+
ALTER TABLE public.user_registration OWNER TO dspace;
228228

229229
CREATE SEQUENCE user_registration_user_registration_id_seq
230230
START WITH 1
@@ -233,7 +233,7 @@ CREATE SEQUENCE user_registration_user_registration_id_seq
233233
NO MINVALUE
234234
CACHE 1;
235235

236-
ALTER TABLE user_registration_user_registration_id_seq OWNER TO dspace;
236+
ALTER TABLE public.user_registration_user_registration_id_seq OWNER TO dspace;
237237

238238
--
239239
-- Name: user_registration_user_registration_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dspace
@@ -254,7 +254,7 @@ CREATE TABLE user_metadata (
254254
);
255255

256256

257-
ALTER TABLE user_metadata OWNER TO dspace;
257+
ALTER TABLE public.user_metadata OWNER TO dspace;
258258

259259
--
260260
-- Name: user_metadata_user_metadata_id_seq; Type: SEQUENCE; Schema: public; Owner: dspace
@@ -268,7 +268,7 @@ CREATE SEQUENCE user_metadata_user_metadata_id_seq
268268
CACHE 1;
269269

270270

271-
ALTER TABLE user_metadata_user_metadata_id_seq OWNER TO dspace;
271+
ALTER TABLE public.user_metadata_user_metadata_id_seq OWNER TO dspace;
272272

273273
--
274274
-- Name: user_metadata_user_metadata_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dspace

dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.6_2024.08.05__Added_Preview_Tables.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ CREATE TABLE previewcontent (
1919
size varchar(256)
2020
);
2121

22-
ALTER TABLE previewcontent OWNER TO dspace;
22+
ALTER TABLE public.previewcontent OWNER TO dspace;
2323

2424
--
2525
-- Name: previewcontent_previewcontent_id_seq; Type: SEQUENCE; Schema: public; Owner: dspace
@@ -32,7 +32,7 @@ CREATE SEQUENCE previewcontent_previewcontent_id_seq
3232
NO MINVALUE
3333
CACHE 1;
3434

35-
ALTER TABLE previewcontent_previewcontent_id_seq OWNER TO dspace;
35+
ALTER TABLE public.previewcontent_previewcontent_id_seq OWNER TO dspace;
3636

3737
--
3838
-- Name: previewcontent_previewcontent_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: dspace
@@ -64,7 +64,7 @@ CREATE TABLE preview2preview (
6464
name varchar(2000)
6565
);
6666

67-
ALTER TABLE preview2preview OWNER TO dspace;
67+
ALTER TABLE public.preview2preview OWNER TO dspace;
6868

6969
--
7070
-- Name: preview2preview_pkey; Type: CONSTRAINT; Schema: public; Owner: dspace; Tablespace:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
--
2+
-- The contents of this file are subject to the license and copyright
3+
-- detailed in the LICENSE and NOTICE files at the root of the source
4+
-- tree and available online at
5+
--
6+
-- http://www.dspace.org/license/
7+
--
8+
9+
-----------------------------------------------------------------------------------
10+
-- Re-apply the CLARIN / preview object ownership without the hardcoded `public.`
11+
-- schema qualifier.
12+
--
13+
-- V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sql and
14+
-- V7.6_2024.08.05__Added_Preview_Tables.sql issue `ALTER TABLE public.<name> OWNER TO
15+
-- dspace`, binding the statement to the `public` schema instead of the connection's
16+
-- search_path. Those files are already applied, so editing them would break their
17+
-- Flyway checksum; this migration restates the same intent schema-agnostically.
18+
--
19+
-- Ownership is best-effort: the statement needs the current role to own the object and
20+
-- to be a member of `dspace`, and `dspace` to hold CREATE on the object's schema. Any
21+
-- install that does not satisfy that (different role name, tenant-owned schema) is left
22+
-- untouched with a notice rather than failing the migration.
23+
-----------------------------------------------------------------------------------
24+
DO $$
25+
DECLARE
26+
target_owner CONSTANT text := 'dspace';
27+
objects CONSTANT text[] := ARRAY[
28+
'license_definition',
29+
'license_definition_license_id_seq',
30+
'license_label',
31+
'license_label_label_id_seq',
32+
'license_label_extended_mapping',
33+
'license_label_extended_mapping_mapping_id_seq',
34+
'license_resource_mapping',
35+
'license_resource_mapping_mapping_id_seq',
36+
'license_resource_user_allowance',
37+
'license_resource_user_allowance_transaction_id_seq',
38+
'user_registration',
39+
'user_registration_user_registration_id_seq',
40+
'user_metadata',
41+
'user_metadata_user_metadata_id_seq',
42+
'previewcontent',
43+
'previewcontent_previewcontent_id_seq',
44+
'preview2preview'
45+
];
46+
object_name text;
47+
object_oid oid;
48+
BEGIN
49+
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = target_owner) THEN
50+
RAISE NOTICE 'Role % does not exist, leaving object ownership unchanged.', target_owner;
51+
RETURN;
52+
END IF;
53+
54+
FOREACH object_name IN ARRAY objects LOOP
55+
-- Unqualified to_regclass() resolves against search_path, so the object is found
56+
-- in whichever schema this install actually uses.
57+
object_oid := to_regclass(object_name);
58+
CONTINUE WHEN object_oid IS NULL;
59+
60+
CONTINUE WHEN EXISTS (
61+
SELECT 1 FROM pg_class c
62+
JOIN pg_roles r ON r.oid = c.relowner
63+
WHERE c.oid = object_oid AND r.rolname = target_owner
64+
);
65+
66+
BEGIN
67+
EXECUTE format('ALTER TABLE %s OWNER TO %I', object_oid::regclass, target_owner);
68+
EXCEPTION WHEN insufficient_privilege THEN
69+
RAISE NOTICE 'Cannot reassign % to %, leaving its current owner in place: %',
70+
object_oid::regclass, target_owner, SQLERRM;
71+
END;
72+
END LOOP;
73+
END $$;

0 commit comments

Comments
 (0)