Skip to content

Add database_clone_grant_ownership macro#32

Merged
jonhopper-dataengineers merged 1 commit into
mainfrom
feature/database-clone-grant-ownership
Jun 23, 2026
Merged

Add database_clone_grant_ownership macro#32
jonhopper-dataengineers merged 1 commit into
mainfrom
feature/database-clone-grant-ownership

Conversation

@jonhopper-dataengineers

@jonhopper-dataengineers jonhopper-dataengineers commented Jun 23, 2026

Copy link
Copy Markdown
Member

Summary

  • Added database_clone_grant_ownership macro to grant ownership of a cloned database and all its schemas, tables, and views to a specified role
  • Added macro documentation in database.yml and README.md
  • Bumped version from 1.0.5 to 1.0.6 with changelog entry

Test plan

  • Run dbt compile to verify macro parses without errors
  • Run dbt run-operation database_clone_grant_ownership --args '{"destination_database": "<test_db>", "new_owner_role": "<test_role>"}' against a test clone
  • Verify ownership transferred on database, schemas, tables, and views

.... Generated with Cortex Code

Summary by Sourcery

Add a new macro to transfer ownership of cloned databases and update project metadata.

New Features:

  • Introduce database_clone_grant_ownership macro to transfer ownership of a cloned database and all contained objects to a specified role.

Enhancements:

  • Document the new database_clone_grant_ownership macro in macro metadata and the README macro reference table.
  • Bump the package version to 1.0.6 with a corresponding changelog entry.

@sourcery-ai

sourcery-ai Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Introduces a new dbt macro database_clone_grant_ownership that programmatically transfers database and object ownership in a cloned Snowflake database to a target role, and updates package metadata and documentation for the new functionality and version 1.0.6 release.

Sequence diagram for database_clone_grant_ownership macro execution

sequenceDiagram
    actor User
    participant dbt
    participant database_clone_grant_ownership
    participant Snowflake

    User->>dbt: dbt run-operation database_clone_grant_ownership
    dbt->>database_clone_grant_ownership: database_clone_grant_ownership(destination_database, new_owner_role)

    database_clone_grant_ownership->>Snowflake: GRANT OWNERSHIP ON DATABASE destination_database TO ROLE new_owner_role REVOKE CURRENT GRANTS;

    database_clone_grant_ownership->>Snowflake: run_query(list_schemas_query)
    Snowflake-->>database_clone_grant_ownership: schemata_list

    loop schemata_list
        database_clone_grant_ownership->>Snowflake: GRANT OWNERSHIP ON SCHEMA destination_database.schema_name TO ROLE new_owner_role REVOKE CURRENT GRANTS;
        database_clone_grant_ownership->>Snowflake: GRANT OWNERSHIP ON ALL TABLES/VIEWS AND OTHER OBJECTS IN SCHEMA
    end
Loading

File-Level Changes

Change Details Files
Add database_clone_grant_ownership macro to grant ownership of a cloned database and all child objects to a specified role.
  • Define a dbt macro that validates required arguments destination_database and new_owner_role, raising a compiler error if missing.
  • Grant ownership on the target database to the specified role using GRANT OWNERSHIP ON DATABASE ... REVOKE CURRENT GRANTS.
  • Query INFORMATION_SCHEMA.SCHEMATA in the destination database (excluding INFORMATION_SCHEMA) to dynamically enumerate schemas.
  • Iterate over all discovered schemas and issue GRANT OWNERSHIP statements for the schema and all contained objects (views, materialized views, tables, external tables, dynamic tables, stages, file formats, functions, procedures, streams, and tasks) using separate dbt statement blocks without auto-begin.
  • Log progress messages for database-level and per-schema ownership grants to aid observability when running dbt run-operation.
  • Guard access to query results using if execute to keep dbt compile safe while still enabling runtime behavior.
macros/database/database_clone_grant_ownership.sql
Document the new macro and expose it in package metadata.
  • Register the database_clone_grant_ownership macro and its arguments in the database.yml macro manifest with concise descriptions.
  • Add the macro to the README macro overview table with its signature and high-level behavior.
  • Record the addition in the changelog under a new 1.0.6 section describing the macro and behavior at a high level.
macros/database/database.yml
README.md
CHANGELOG.md
Version bump for the dbt package to 1.0.6.
  • Update the declared package version from 1.0.5 to 1.0.6 in dbt_project.yml.
  • Synchronize the README version badge/text to 1.0.6.
  • Note the version bump in the changelog under the 1.0.6 entry.
dbt_project.yml
README.md
CHANGELOG.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The macro description and changelog mention COPY CURRENT GRANTS, but the SQL uses REVOKE CURRENT GRANTS on all GRANT OWNERSHIP statements; consider aligning the behavior and documentation or explicitly calling out that existing grants will be revoked.
  • The macro builds identifiers like {{ destination_database }}.{{ schema_name }} without quoting; if callers ever pass mixed‑case or special‑character names, this will fail, so consider using adapter.quote/api.Relation or otherwise quoting identifiers consistently.
  • run_query(list_schemas_query) is executed unconditionally and the first statement call uses fetch_result=True even though the result is never used; you can simplify by only calling run_query inside an if execute block and dropping unused fetch_result to avoid unnecessary work.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The macro description and changelog mention `COPY CURRENT GRANTS`, but the SQL uses `REVOKE CURRENT GRANTS` on all GRANT OWNERSHIP statements; consider aligning the behavior and documentation or explicitly calling out that existing grants will be revoked.
- The macro builds identifiers like `{{ destination_database }}.{{ schema_name }}` without quoting; if callers ever pass mixed‑case or special‑character names, this will fail, so consider using `adapter.quote`/`api.Relation` or otherwise quoting identifiers consistently.
- `run_query(list_schemas_query)` is executed unconditionally and the first `statement` call uses `fetch_result=True` even though the result is never used; you can simplify by only calling `run_query` inside an `if execute` block and dropping unused `fetch_result` to avoid unnecessary work.

## Individual Comments

### Comment 1
<location path="macros/database/database_clone_grant_ownership.sql" line_range="19" />
<code_context>
+    WHERE schema_name != 'INFORMATION_SCHEMA'
+  {% endset %}
+
+  {% set results = run_query(list_schemas_query) %}
+
+  {% if execute %}
</code_context>
<issue_to_address>
**issue (bug_risk):** Guard the `run_query` call with `if execute` to avoid parse-time failures.

In dbt, `run_query` can’t be used during parse-only phases (e.g. `dbt compile`, docs generation, some adapter metadata calls). Here it’s always executed, and only the *use* of `results` is wrapped in `if execute`. Please also guard the `run_query` call with `if execute`, and in the `else` branch initialize `results`/`schemata_list` to safe defaults so parse-only commands don’t fail.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread macros/database/database_clone_grant_ownership.sql Outdated
Adds a new macro to grant ownership of a cloned database and all its
schemas, tables, and views to a specified role. Includes macro
documentation, README update, changelog entry, and version bump to 1.0.6.

.... Generated with [Cortex Code](https://docs.snowflake.com/en/user-guide/cortex-code/cortex-code)

Co-Authored-By: Cortex Code <noreply@snowflake.com>
@jonhopper-dataengineers jonhopper-dataengineers force-pushed the feature/database-clone-grant-ownership branch from a507b7f to 770e6ef Compare June 23, 2026 08:18
@jonhopper-dataengineers jonhopper-dataengineers merged commit 4b96575 into main Jun 23, 2026
2 checks passed
@jonhopper-dataengineers jonhopper-dataengineers deleted the feature/database-clone-grant-ownership branch June 25, 2026 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant