-- Auto-generated by Griot scaffold
-- Contract: {{ contract_id }}
-- Target: databricks
-- Generated: {{ generated_at }}
{% for schema in schemas %}

-- Schema: {{ schema.name }}
CREATE TABLE IF NOT EXISTS {{ schema.table_name }} (
{% for field in schema.fields %}
    {{ field.name }}  {{ field.sql_type }}
    {%- if not field.nullable %}  NOT NULL{% endif %}
    {%- if not loop.last %},{% endif %}

{% endfor %}
{% if schema.primary_keys %}
    , PRIMARY KEY ({{ schema.primary_keys | join(', ') }})
{% endif %}
{% for fk in schema.foreign_keys %}
    , FOREIGN KEY ({{ fk.column }}) REFERENCES {{ fk.ref_table }}({{ fk.ref_column }}) NOT ENFORCED
{% endfor %}
) USING DELTA
{%- if schema.partition_keys %}

PARTITIONED BY ({{ schema.partition_keys | join(', ') }})
{%- endif %};
{% if schema.field_comments %}

-- Column comments for {{ schema.table_name }}
{% for field in schema.fields %}
{% if field.description %}
ALTER TABLE {{ schema.table_name }} ALTER COLUMN {{ field.name }} COMMENT '{{ field.description | replace("'", "''") }}';
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
