source_table.sql 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 source_table, adapter='materialize' %}
  16. {%- set identifier = model['alias'] -%}
  17. {%- set old_relation = adapter.get_relation(identifier=identifier,
  18. schema=schema,
  19. database=database) -%}
  20. {%- set target_relation = api.Relation.create(identifier=identifier,
  21. schema=schema,
  22. database=database,
  23. type='table') -%}
  24. {% if old_relation %}
  25. {{ adapter.drop_relation(old_relation) }}
  26. {% endif %}
  27. {{ run_hooks(pre_hooks, inside_transaction=False) }}
  28. {{ run_hooks(pre_hooks, inside_transaction=True) }}
  29. {% call statement('main') -%}
  30. {{ materialize__create_source_table(target_relation, sql) }}
  31. {%- endcall %}
  32. {{ create_indexes(target_relation) }}
  33. {% do persist_docs(target_relation, model) %}
  34. {{ run_hooks(post_hooks, inside_transaction=False) }}
  35. {{ run_hooks(post_hooks, inside_transaction=True) }}
  36. {{ return({'relations': [target_relation]}) }}
  37. {% endmaterialization %}