seed.sql 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. -- Copyright Materialize, Inc. and contributors. All rights reserved.
  2. --
  3. -- Licensed under the Apache License, Version 2.0 (the "License");
  4. -- you may not use this file except in compliance with the License.
  5. -- You may obtain a copy of the License in the LICENSE file at the
  6. -- root of this repository, or online at
  7. --
  8. -- http://www.apache.org/licenses/LICENSE-2.0
  9. --
  10. -- Unless required by applicable law or agreed to in writing, software
  11. -- distributed under the License is distributed on an "AS IS" BASIS,
  12. -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. -- See the License for the specific language governing permissions and
  14. -- limitations under the License.
  15. {% materialization seed, adapter='materialize' %}
  16. {%- set identifier = model['alias'] -%}
  17. {%- set full_refresh_mode = (should_full_refresh()) -%}
  18. {%- set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) -%}
  19. {%- set exists_as_table = (old_relation is not none and old_relation.is_table) -%}
  20. {%- set exists_as_view = (old_relation is not none and old_relation.is_view) -%}
  21. {%- set exists_as_materialized_view = (old_relation is not none and old_relation.is_materialized_view) -%}
  22. {%- set exists_as_source = (old_relation is not none and old_relation.is_source) -%}
  23. {%- set exists_as_sink = (old_relation is not none and old_relation.is_sink) -%}
  24. {%- set agate_table = load_agate_table() -%}
  25. {%- do store_result('agate_table', response='OK', agate_table=agate_table) -%}
  26. {{ run_hooks(pre_hooks, inside_transaction=False) }}
  27. {{ run_hooks(pre_hooks, inside_transaction=True) }}
  28. {% set create_table_sql = "" %}
  29. {% if exists_as_view or exists_as_materialized_view or exists_as_source or exists_as_sink %}
  30. {{ exceptions.raise_compiler_error("Cannot seed to '{}', it is a '{}'".format(old_relation.render(),old_relation.type)) }}
  31. {% elif exists_as_table %}
  32. {% set create_table_sql = reset_csv_table(model, full_refresh_mode, old_relation, agate_table) %}
  33. {% else %}
  34. {% set create_table_sql = create_csv_table(model, agate_table) %}
  35. {% endif %}
  36. {% set code = 'CREATE' if full_refresh_mode else 'INSERT' %}
  37. {% set rows_affected = (agate_table.rows | length) %}
  38. {% set sql = load_csv_rows(model, agate_table) %}
  39. {% call noop_statement('main', code ~ ' ' ~ rows_affected, code, rows_affected) %}
  40. {{ get_csv_sql(create_table_sql, sql) }};
  41. {% endcall %}
  42. {% set target_relation = this.incorporate(type='table') %}
  43. {% do persist_docs(target_relation, model) %}
  44. {% if full_refresh_mode or not exists_as_table %}
  45. {% do create_indexes(target_relation) %}
  46. {% endif %}
  47. {{ run_hooks(post_hooks, inside_transaction=True) }}
  48. {{ adapter.commit() }}
  49. {{ run_hooks(post_hooks, inside_transaction=False) }}
  50. {{ return({'relations': [target_relation]}) }}
  51. {% endmaterialization %}