@@ -453,56 +453,81 @@ Usage:
453
453
454
454
...
455
455
```
456
- #### get_relations_by_prefix
456
+
457
+
458
+ #### get_relations_by_pattern ([ source] ( macros/sql/get_relations_by_pattern.sql ) )
459
+
457
460
Returns a list of [ Relations] ( https://docs.getdbt.com/docs/writing-code-in-dbt/class-reference/#relation )
458
- that match a given prefix, with an optional exclusion pattern. It's particularly
459
- handy paired with ` union_relations ` .
461
+ that match a given schema- or table-name pattern.
462
+
463
+ This macro is particularly handy when paired with ` union_relations ` .
464
+
460
465
** Usage:**
461
466
```
462
- -- Returns a list of relations that match schema.prefix%
463
- {% set relations = dbt_utils.get_relations_by_prefix('my_schema', 'my_prefix') %}
467
+ -- Returns a list of relations that match schema_pattern%.table
468
+ {% set relations = dbt_utils.get_relations_by_pattern('schema_pattern%', 'table_pattern') %}
469
+
470
+ -- Returns a list of relations that match schema_pattern.table_pattern%
471
+ {% set relations = dbt_utils.get_relations_by_pattern('schema_pattern', 'table_pattern%') %}
464
472
465
473
-- Returns a list of relations as above, excluding any that end in `deprecated`
466
- {% set relations = dbt_utils.get_relations_by_prefix('my_schema ', 'my_prefix ', '%deprecated') %}
474
+ {% set relations = dbt_utils.get_relations_by_pattern('schema_pattern ', 'table_pattern% ', '%deprecated') %}
467
475
468
476
-- Example using the union_relations macro
469
- {% set event_relations = dbt_utils.get_relations_by_prefix('events ', 'event_ ') %}
477
+ {% set event_relations = dbt_utils.get_relations_by_pattern('venue% ', 'clicks ') %}
470
478
{{ dbt_utils.union_relations(relations = event_relations) }}
471
479
```
472
480
473
481
** Args:**
474
- * ` schema ` (required): The schema to inspect for relations.
475
- * ` prefix ` (required): The prefix of the table/view (case insensitive)
476
- * ` exclude ` (optional): Exclude any relations that match this pattern.
482
+ * ` schema_pattern ` (required): The schema pattern to inspect for relations.
483
+ * ` table_pattern ` (required): The name of the table/view (case insensitive).
484
+ * ` exclude ` (optional): Exclude any relations that match this table pattern.
477
485
* ` database ` (optional, default = ` target.database ` ): The database to inspect
478
486
for relations.
479
487
480
- #### get_relations_by_pattern
481
- > This was built from the get_relations_by_prefix macro.
488
+ ** Examples:**
489
+ Generate drop statements for all Relations that match a naming pattern:
490
+ ``` sql
491
+ {% set relations_to_drop = dbt_utils .get_relations_by_pattern (
492
+ schema_pattern= ' public' ,
493
+ table_pattern= ' dbt\_ %'
494
+ ) %}
482
495
496
+ {% set sql_to_execute = [] %}
497
+
498
+ {{ log(' Statements to run:' , info= True) }}
499
+
500
+ {% for relation in relations_to_drop %}
501
+ {% set drop_command - %}
502
+ -- drop {{ relation.type }} {{ relation }} cascade;
503
+ {%- endset %}
504
+ {% do log(drop_command, info= True) %}
505
+ {% do sql_to_execute .append (drop_command) %}
506
+ {% endfor %}
507
+ ```
508
+
509
+ #### get_relations_by_prefix ([ source] ( macros/sql/get_relations_by_prefix.sql ) )
510
+ > This macro will soon be deprecated in favor of the more flexible ` get_relations_by_pattern ` macro (above)
483
511
Returns a list of [ Relations] ( https://docs.getdbt.com/docs/writing-code-in-dbt/class-reference/#relation )
484
- that match a given schema or table pattern and table/view name with an optional exclusion pattern. Like its cousin
485
- get_relations_by_prefix, it's particularly handy paired with ` union_relations ` .
512
+ that match a given prefix, with an optional exclusion pattern. It's particularly
513
+ handy paired with ` union_relations ` .
486
514
** Usage:**
487
515
```
488
- -- Returns a list of relations that match schema%.table
489
- {% set relations = dbt_utils.get_relations_by_pattern('schema_pattern%', 'table_pattern') %}
490
-
491
- -- Returns a list of relations that match schema.table%
492
- {% set relations = dbt_utils.get_relations_by_pattern('schema_pattern', 'table_pattern%') %}
516
+ -- Returns a list of relations that match schema.prefix%
517
+ {% set relations = dbt_utils.get_relations_by_prefix('my_schema', 'my_prefix') %}
493
518
494
519
-- Returns a list of relations as above, excluding any that end in `deprecated`
495
- {% set relations = dbt_utils.get_relations_by_pattern('schema_pattern ', 'table_pattern% ', '%deprecated') %}
520
+ {% set relations = dbt_utils.get_relations_by_prefix('my_schema ', 'my_prefix ', '%deprecated') %}
496
521
497
522
-- Example using the union_relations macro
498
- {% set event_relations = dbt_utils.get_relations_by_pattern('venue% ', 'clicks ') %}
523
+ {% set event_relations = dbt_utils.get_relations_by_prefix('events ', 'event_ ') %}
499
524
{{ dbt_utils.union_relations(relations = event_relations) }}
500
525
```
501
526
502
527
** Args:**
503
- * ` schema_pattern ` (required): The schema pattern to inspect for relations.
504
- * ` table_pattern ` (required): The name of the table/view (case insensitive).
505
- * ` exclude ` (optional): Exclude any relations that match this table pattern.
528
+ * ` schema ` (required): The schema to inspect for relations.
529
+ * ` prefix ` (required): The prefix of the table/view (case insensitive)
530
+ * ` exclude ` (optional): Exclude any relations that match this pattern.
506
531
* ` database ` (optional, default = ` target.database ` ): The database to inspect
507
532
for relations.
508
533
0 commit comments