[Port to dtq-dev] Issue dspace-customers#903: fix CLARIN table ownership for non-public schemas - #1406
[Port to dtq-dev] Issue dspace-customers#903: fix CLARIN table ownership for non-public schemas#1406jr-rk wants to merge 2 commits into
Conversation
The V7.2 Lindat/Clarin and V7.6 Preview migrations qualified their `ALTER TABLE ... OWNER TO dspace` statements with `public.`, so instances whose DSpace tables live in a non-public schema could not run them. Remove the qualifier (17 statements across the two files) so each resolves against the connection's search_path. The unrelated bitstore.xml storage-bean swap from the source commit is omitted -- dtq-dev already uses SyncBitstreamStorageServiceImpl. Port of dataquest-dev/dspace-customers#903 (item 5). Source: customer/vsb-tuo e2fb083. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR updates two CLARIN-specific PostgreSQL Flyway migrations to avoid hardcoding the public. schema in ALTER TABLE … OWNER TO dspace statements, allowing the migrations to run correctly when DSpace tables live in a non-public schema (via the connection search_path).
Changes:
- Removed
public.schema qualification from 14ALTER TABLE … OWNER TO dspacestatements in the Lindat/Clarin upgrade migration (V7.2). - Removed
public.schema qualification from 3ALTER TABLE … OWNER TO dspacestatements in the preview tables migration (V7.6).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sql |
Drops public. qualifiers from ALTER TABLE … OWNER TO dspace so ownership changes resolve via search_path. |
dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.6_2024.08.05__Added_Preview_Tables.sql |
Drops public. qualifiers from ALTER TABLE … OWNER TO dspace for preview tables/sequences to support non-public schemas. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
milanmajchrak
left a comment
There was a problem hiding this comment.
We cannot update already existing flyway scripts because there will be mismatch with the Flyway checksum. You need to create a new Flyway script.
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>
There was a problem hiding this comment.
🔵 Needs a closer look
Object lookup must explicitly resolve the configured DSpace schema before approval.
Review details
Suppressed comments (2)
dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.6_2026.09.11__Clarin_tables_owner_schema_agnostic.sql:57
- This lookup is not limited to the DSpace schema.
to_regclasssearches the entiresearch_path, while Flyway is configured with the DataSource directly and does not apply DSpace'sdb.schema; a customdb.schemawith the defaultpublicpath will therefore match nothing, andsearch_path = tenant, publiccan fall through to a same-named public decoy when the tenant object is missing. Resolve against the configured/current DSpace schema explicitly (or configure Flyway's search path) before changing ownership.
-- Unqualified to_regclass() resolves against search_path, so the object is found
-- in whichever schema this install actually uses.
object_oid := to_regclass(object_name);
dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.6_2026.09.11__Clarin_tables_owner_schema_agnostic.sql:14
- The comment says the migrations “issue” these ALTER statements, which is grammatically incorrect and makes the explanation unclear; “use” is intended here.
-- V7.6_2024.08.05__Added_Preview_Tables.sql issue `ALTER TABLE public.<name> OWNER TO
- Files reviewed: 1/1 changed files
- Comments generated: 0 new
- Review effort level: Lite
Problem
Two CLARIN Flyway migrations hardcode the
public.schema prefix in theirALTER TABLE … OWNER TO dspacestatements, binding them to thepublicschema instead of the connection'ssearch_path. Instances whose DSpace tables live in another schema do not get those objects assigned to thedspacerole.Port of dataquest-dev/dspace-customers#903 (item 5). Source:
customer/vsb-tuoe2fb08358e.Approach (changed after review)
The first revision edited
V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sqlandV7.6_2024.08.05__Added_Preview_Tables.sqlin place. That was reverted: both migrations are already applied, so changing their content changes their Flyway checksum, andDatabaseUtils.updateDatabase()— reached fromContext.init()on every boot — would fail validation at the first Context on every existing install until a manual./dspace database repair.The fix is now a new forward migration; the two existing files are byte-identical to
dtq-dev(verified: empty diff against the base commit). No checksum change, no repair step, no upgrade action for existing installs.Change set
V7.6_2026.09.11__Clarin_tables_owner_schema_agnostic.sql— a plpgsqlDOblock that reassigns the same 17 CLARIN and preview objects todspace, resolving each unqualified name withto_regclass()so it matches againstsearch_pathinstead of a hardcodedpublic..dspacerole does not exist, when the object is not present, when it is already owned bydspace, or when the statement lacks privileges (insufficient_privilegeis trapped per object, leaving the current owner in place).ALTER … OWNER TOalso requires the target role to holdCREATEon the object's schema, not just role membership. That was an outright migration failure in the first draft of the script, found while testing against a tenant-owned schema, and is now one of the guarded cases.The unrelated
bitstore.xmlhunk in the source commit (BitstreamStorageServiceImpl→SyncBitstreamStorageServiceImpl) remains excluded — a storage-bean swap unrelated to schema qualification, anddtq-devalready usesSyncBitstreamStorageServiceImpl.Test evidence
PostgreSQL 15, throwaway container, script executed directly:
publicschemavsbtuo,dspacehas no rights on itGRANT CREATE ON SCHEMA vsbtuo TO dspacedspace; same-named decoypublic.license_definitionuntoucheddspacerole in the clusterpublic-schema install, objects already owned bydspaceT2 is the proof that resolution is schema-agnostic: only the
search_pathschema is touched.Known limitation
A fresh install into a non-
publicschema still aborts inside V7.2 itself —ALTER TABLE public.license_definition OWNER TO dspaceraisesrelation "public.license_definition" does not existbefore any later migration runs, so this script is never reached there. Fixing that case would require editing V7.2, which is exactly what this revision avoids.What this migration does cover: installs where V7.2 is already recorded (baseline or dump-restore into a non-
publicschema), and keeping the codebase schema-agnostic from here on.Risk & rollback
Low. No DDL beyond object ownership, no schema or data change, no checksum impact on existing migrations. Rollback is dropping the new file — nothing to undo in already-migrated databases beyond ownership, which is restored with
ALTER TABLE … OWNER TO <role>.🤖 Generated with Claude Code