change table name dynamically using a macro generate_alias_name in DBT - dbt

I have table name as rw_ghi_abc.
I want to remove rw_ from the table name and for this I am using macro generate_alias_name as :
{% macro generate_alias_name(re,custom_alias_name=none, node=none) -%}
{%- if custom_alias_name is none -%}
{{ re.search('g\w+',node.name) }}
{%- else -%}
{{ custom_alias_name | trim }}
{%- endif -%}
{%- endmacro %}
I am using re.search so that I can skip rw_ from the name, after dbt run it gives an error as:
Database Error in model rw_ghi_abc(models/RAW/abc/rw_ghi_abc.sql)
001003 (42000): SQL compilation error:
syntax error line 1 at position 77 unexpected '='.
syntax error line 1 at position 102 unexpected '='.
syntax error line 1 at position 113 unexpected '='.
syntax error line 1 at position 131 unexpected ''RAW''.
syntax error line 1 at position 140 unexpected ''abc''.
syntax error line 1 at position 152 unexpected ''rw_ghi_abc''.
syntax error line 1 at position 184 unexpected '='.
syntax error line 1 at position 234 unexpected '='.
syntax error line 1 at position 631 unexpected '='.
syntax error line 1 at position 672 unexpected '='.
syntax error line 1 at position 698 unexpected '='.
syntax error line 1 at position 771 unexpected '='.
syntax error line 1 at position 831 unexpected '='.
syntax error line 1 at position 884 unexpected '='.
syntax error line 1 at position 920 unexpected '='.
syntax error line 1 at position 953 unexpected '='.
syntax error line 1 at position 984 unexpected '='.
syntax error line 1 at position 1,017 unexpected '='.
syntax error line 1 at position 1,085 unexpected ','.
compiled Code at target/run/data/models/RAW/abc/rw_ghi_abc.sql
Can someone help me with this.

If the model name always starts with 'rw_', you can use something like this:
{% macro generate_alias_name(custom_alias_name=none, node=none) -%}
{%- if custom_alias_name is none -%}
{%- if node.name.startswith('rw_') -%}
{{ node.name[node.name.find('rw_') + 3 : node.name|length] }}
{%- else -%}
{{ node.name }}
{%- endif -%}
{%- else -%}
{{ custom_alias_name | trim }}
{%- endif -%}
{%- endmacro %}

Related

Shopify / Liquid - split and delete after split

I would like to split my product titles, e.g.:
"big hat - red"
"small coat - blue"
"medium socks - green"
so that it only outputs the text before the "-". In other words, I want some kind of dynamic split (or a 'split and delete the remainder' operator/function) that outputs
"big hat"
"small coat"
"medium socks"
Using the below doesn't remove the text after the hyphen, and I can't simply hard-code the ending part to be removed, because the text after the hypen varies with each product.
{{ product.title | split:"-" }}
How would I achieve this?
Use split filter and then first filter to get first array element.
{% assign source_string = "before text - after text"%}
{{ source_string | split: "-" | first }}
For your code, it will be
{{ product.title | split:"-" | first }}

How do I fix 'unparsable' sqlfluff lint error

I am receiving the error L: 3 | P: 1 | PRS | Found unparsable section:.
This is when I am calling the date_spine macro provide by dbt_utils. Has anyone come across this before and what expected value to set in the definition of the macro in sqlfluff?
See as follows for defining macro in sqlfluff file in dbt:
date_spine = {% macro date_spine(datepart, start_date, end_date) %}'HERE'{% endmacro %}
You don't need to set date_spine = portion, just define the macro as:
{% macro date_spine(datepart, start_date, end_date) %}'HERE'{% endmacro %}

printing unique variants for all products on top of a collection page

I am trying to create a filter on top of a collection page with all the available variants. But, I need them to be formatted nicely so I'm using the following code:
{% for product in collections.all.products limit: limit %}
<li>
{% assign sizes = product.variants | map: 'option1' | uniq %}
{% assign colors = product.variants | map: 'option2' | uniq %}
{% assign combined_variants = sizes | concat: colors | uniq %}
{% for v-item in combined_variants %}
{{ v-item }} <br/>
{% endfor %}
</li>
{% endfor %}
{{ combined_variants | json }}
The problem is that I need it to iterate through all the products but only print unique values across all of them. How can I achieve this?
You would be better off creating custom collections for each option. Put all Size X in collection Size X. Put all Yellow Budgies in collection Yellow Budgies. Then you can craft a navigation scheme that takes customers to the products that fit their selection. All things Size X, or all things Yellow Budgie.
Building out a different filter could also be the JSON approach using the Mega Menus like Bacon etc. They deliver a payload to your them that can help customers drill to what they want. You have no easy way of doing this yourself with the code you are presenting.

Display remaining days left and then "Closed" when it reaches 0 using Liquid

I currenty have data in my CMS which outputs this on the front end:
2017-03-16T00:00:00
What I like this to do is check the above date and see how many days is left to that day and display it as:
7 days left
(assuming today is the 9th March 2017)
And then when the date reaches 0, it needs to display the text: "Closed"
Currently I have:
{% assign todaysdate = {{todaysdate]}} %}
{% assign todaysdatenew = todaysdate | convert: "date" %}
{% assign formula = {{globals.site.dateNow}} | minus: todaysdatenew %}
{% if {{globals.site.dateNow}} > todaysdatenew %}
Closed
{% else %}
{{formula | date:"%d' days'"}}
{% endif %}
However for some reason it is displaying the result as 6 days left, instead of 7 days left. How do I add 1 extra day?
If it is always one day then you can change just hardcode add the extra day by editing the line below:
{{formula | date:"%d' days'"}}
To:
{% assign formulaResult = {{formula | date:"%d' days'"}} -%}
{{formulaResult | date_add: 1, "day"}}

Possible variable types in buildout cfg files

Is it possible to define a buildout variable of type dictionary?
I am trying to substitute a variable with a dictionary but buildout considers it a string.
E.g.
in buildout.cfg:
[MYPROG]
progr_args =
a : 1
b : 2
d :
d1: 1
d2: 2
in template:
my_params:
{% for key, val in parts.MYPROG.progr_args.items() %}\
${key}: ${val}\
{% end %}
Not with Buildout itself, no. Buildout configuration values are always strings; even the mr.scripty recipe, which lets you use Python code as part of your buildout configuration, stores the results of the Python code a strings.
Ever worse, initial whitespace from continuation lines is stripped, so your entry:
progr_args =
a : 1
b : 2
d :
d1: 1
d2: 2
is stored as \na : 1\nb : 2\nd :\nd1: 1\nd2: 2, having lost all indentation context.
You'll have to use Genshi itself to parse out your values. I suggest you use a separate section:
[MYPROG]
prog_params = section_name
[section_name]
a = 1
b = 2
and in your template:
my_params:
{% for key, val in parts[parts.MYPROG.progr_args].items() %}\
${key}: ${val}\
{% end %}