|
26 | 26 | {%- set preexisting_backup_relation = load_cached_relation(backup_relation) -%} |
27 | 27 | -- grab current tables grants config for comparision later on |
28 | 28 | {% set grant_config = config.get('grants') %} |
| 29 | + {% set preserved_grants = {} %} |
| 30 | + {% set should_skip_view_update = false %} |
| 31 | + {% set build_sql = none %} |
| 32 | + |
| 33 | + {% if existing_relation is not none and existing_relation.type != 'view' %} |
| 34 | + {% set current_grants_table = run_query(get_show_grant_sql(existing_relation)) %} |
| 35 | + {% set current_grants_dict = adapter.standardize_grants_dict(current_grants_table) %} |
| 36 | + {% set preserved_grants = {} %} |
| 37 | + {% for privilege, grantees in diff_of_two_dicts(current_grants_dict, grant_config).items() %} |
| 38 | + {% if privilege | lower in ['select', 'insert', 'update', 'delete'] %} |
| 39 | + {% do preserved_grants.update({privilege: grantees}) %} |
| 40 | + {% endif %} |
| 41 | + {% endfor %} |
| 42 | + {% set build_sql = get_create_view_as_sql(intermediate_relation, sql) %} |
| 43 | + {% elif existing_relation is not none and existing_relation.type == 'view' %} |
| 44 | + {% set current_view_definition_table = run_query(get_view_definition_sql(existing_relation)) %} |
| 45 | + {% if current_view_definition_table is not none and current_view_definition_table.rows | length > 0 %} |
| 46 | + {% set normalized_relation = target_relation.include(database=False) | lower | replace('\n', '') | replace('\r', '') | replace('\t', '') | replace(' ', '') | replace(';', '') %} |
| 47 | + {% set normalized_sql = sql | lower | replace('\n', '') | replace('\r', '') | replace('\t', '') | replace(' ', '') | replace(';', '') %} |
| 48 | + {% set normalized_definition = current_view_definition_table.rows[0][0] | lower | replace('\n', '') | replace('\r', '') | replace('\t', '') | replace(' ', '') | replace(';', '') %} |
| 49 | + {% set should_skip_view_update = normalized_definition.endswith(normalized_sql) %} |
| 50 | + {% endif %} |
| 51 | + {% if should_skip_view_update %} |
| 52 | + {% set build_sql = 'declare @dbt_sqlserver_noop int;' %} |
| 53 | + {% else %} |
| 54 | + {% set build_sql = get_create_view_as_sql(target_relation, sql) %} |
| 55 | + {% endif %} |
| 56 | + {% else %} |
| 57 | + {% set build_sql = get_create_view_as_sql(target_relation, sql) %} |
| 58 | + {% endif %} |
29 | 59 |
|
30 | 60 | {{ run_hooks(pre_hooks, inside_transaction=False) }} |
31 | 61 |
|
|
36 | 66 | -- `BEGIN` happens here: |
37 | 67 | {{ run_hooks(pre_hooks, inside_transaction=True) }} |
38 | 68 |
|
39 | | - -- build model |
40 | | - {% call statement('main') -%} |
41 | | - {{ get_create_view_as_sql(intermediate_relation, sql) }} |
42 | | - {%- endcall %} |
| 69 | + {% if existing_relation is not none and existing_relation.type != 'view' %} |
| 70 | + -- build model |
| 71 | + {% call statement('main') -%} |
| 72 | + {{ build_sql }} |
| 73 | + {%- endcall %} |
43 | 74 |
|
44 | | - -- cleanup |
45 | | - -- move the existing view out of the way |
46 | | - {% if existing_relation is not none %} |
47 | | - /* Do the equivalent of rename_if_exists. 'existing_relation' could have been dropped |
48 | | - since the variable was first set. */ |
| 75 | + -- cleanup |
| 76 | + -- move the existing relation out of the way |
49 | 77 | {% set existing_relation = load_cached_relation(existing_relation) %} |
50 | 78 | {% if existing_relation is not none %} |
51 | 79 | {{ adapter.rename_relation(existing_relation, backup_relation) }} |
52 | 80 | {% endif %} |
| 81 | + |
| 82 | + {{ adapter.rename_relation(intermediate_relation, target_relation) }} |
| 83 | + {% else %} |
| 84 | + -- build model |
| 85 | + {% call statement('main') -%} |
| 86 | + {{ build_sql }} |
| 87 | + {%- endcall %} |
53 | 88 | {% endif %} |
54 | | - {{ adapter.rename_relation(intermediate_relation, target_relation) }} |
55 | 89 |
|
56 | 90 | {% set should_revoke = should_revoke(existing_relation, full_refresh_mode=True) %} |
57 | 91 | {% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %} |
58 | 92 |
|
| 93 | + {% if preserved_grants %} |
| 94 | + {% do apply_grants(target_relation, preserved_grants, should_revoke=False) %} |
| 95 | + {% endif %} |
| 96 | + |
59 | 97 | {% do persist_docs(target_relation, model) %} |
60 | 98 |
|
61 | 99 | {{ run_hooks(post_hooks, inside_transaction=True) }} |
|
0 commit comments