ci_utils.sql 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 set_cluster_ci_tag(cluster, ci_tag=env_var('CI_TAG', '')) %}
  16. {% if ci_tag != '' %}
  17. {% set ci_comment %}
  18. COMMENT ON CLUSTER {{ adapter.quote(cluster) }} IS {{ dbt.string_literal(ci_tag) }};
  19. {% endset %}
  20. {{ run_query(ci_comment) }}
  21. {% endif %}
  22. {% endmacro %}
  23. {% macro set_schema_ci_tag(schema, ci_tag=env_var('CI_TAG', '')) %}
  24. {% if ci_tag != '' %}
  25. {% set ci_comment %}
  26. COMMENT ON SCHEMA {{ adapter.quote(schema) }} IS {{ dbt.string_literal(ci_tag) }};
  27. {% endset %}
  28. {{ run_query(ci_comment) }}
  29. {% endif %}
  30. {% endmacro %}
  31. {% macro check_cluster_ci_tag(cluster, ci_tag=env_var('CI_TAG', '')) %}
  32. {% if ci_tag == '' %}
  33. {{ return(false) }}
  34. {% endif %}
  35. {% set query %}
  36. SELECT comment = {{ dbt.string_literal(ci_tag) }}, comment
  37. FROM mz_internal.mz_comments
  38. JOIN mz_clusters USING (id)
  39. WHERE mz_clusters.name = {{ dbt.string_literal(cluster) }}
  40. {% endset %}
  41. {% set results = run_query(query) %}
  42. {% if execute %}
  43. {% if results|length > 0 %}
  44. {% if results.rows[0][0] is false %}
  45. {{ exceptions.raise_compiler_error("""
  46. Deployment cluster """ ~ cluster ~ """ already exists and is tagged
  47. for CI deployment """ ~ results.rows[0][1]
  48. )}}
  49. {% else %}
  50. {{ return(true) }}
  51. {% endif %}
  52. {% endif %}
  53. {{ return(false) }}
  54. {% endif %}
  55. {% endmacro %}
  56. {% macro check_schema_ci_tag(schema, ci_tag=env_var('CI_TAG', '')) %}
  57. {% if ci_tag == '' %}
  58. {{ return(false) }}
  59. {% endif %}
  60. {% set query %}
  61. SELECT comment = {{ dbt.string_literal(ci_tag) }}, comment
  62. FROM mz_internal.mz_comments c
  63. JOIN mz_schemas s ON c.id = s.id
  64. JOIN mz_databases d ON s.database_id = d.id
  65. WHERE s.name = {{ dbt.string_literal(schema) }}
  66. AND d.name = current_database()
  67. {% endset %}
  68. {% set results = run_query(query) %}
  69. {% if execute %}
  70. {% if results|length > 0 %}
  71. {% if results.rows[0][0] is false %}
  72. {{ exceptions.raise_compiler_error("""
  73. Deployment schema """ ~ schema ~ """ already exists and is tagged
  74. for CI deployment """ ~ results.rows[0][1]
  75. )}}
  76. {% else %}
  77. {{ return(true) }}
  78. {% endif %}
  79. {% endif %}
  80. {{ return(false) }}
  81. {% endif %}
  82. {% endmacro %}