exists.sql 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. {% macro schema_exists(schema) %}
  16. {#
  17. Checks if a specified schema exists in the current database.
  18. ## Arguments
  19. - `schema` (string): The name of the schema to check for existence.
  20. ## Returns
  21. Boolean: `true` if the schema exists, `false` otherwise.
  22. #}
  23. {% set query %}
  24. SELECT *
  25. FROM mz_schemas
  26. JOIN mz_databases ON mz_schemas.database_id = mz_databases.id
  27. WHERE mz_databases.name = current_database()
  28. AND mz_schemas.name = {{ dbt.string_literal(schema) }}
  29. {%- endset -%}
  30. {% set results = run_query(query) %}
  31. {% if execute %}
  32. {% if results|length > 0 %}
  33. {{ return(true) }}
  34. {% else %}
  35. {{ return(false) }}
  36. {% endif %}
  37. {% endif %}
  38. {% endmacro %}
  39. {% macro cluster_exists(cluster) %}
  40. {#
  41. Checks if a specified cluster exists in the Materialize environment.
  42. ## Arguments
  43. - `cluster` (string): The name of the cluster to check for existence.
  44. ## Returns
  45. Boolean: `true` if the cluster exists, `false` otherwise.
  46. #}
  47. {% set query %}
  48. SELECT * FROM mz_clusters
  49. WHERE name = {{ dbt.string_literal(cluster) }}
  50. {%- endset -%}
  51. {% set results = run_query(query) %}
  52. {% if execute %}
  53. {% if results|length > 0 %}
  54. {{ return(true) }}
  55. {% else %}
  56. {{ return(false) }}
  57. {% endif %}
  58. {% endif %}
  59. {% endmacro %}