-- schema: {{nspname}}

CREATE schema if not exists {{nspname}};

{{#each rows}}
-- {{{description}}}

CREATE TABLE if not exists {{nspname}}.{{relname}}
(
  {{#each columns}}
  {{column_name}} {{data_type}} {{#unless is_nullable}}NOT NULL{{/unless}}{{#if column_default}}DEFAULT {{column_default}}{{/if}}{{#ifCond @index '!=' (_math ../columns.length '-' 1)}},{{/ifCond}} -- {{{coalesce description '-'}}}
  {{/each}}{{#if constraints.length}},{{/if}}
  {{#each constraints}}
  CONSTRAINT {{constraint_name}} {{#ifCond constraint_type '==' 'c'}} {{check_definition}} {{/ifCond}} {{#ifCond constraint_type '==' 'f'}}FOREIGN KEY ({{foreign_column}}) REFERENCES {{foreign_table}} ({{foreign_column}}) MATCH {{select confmatchtype data="constraint_matchtype"}} ON UPDATE {{select confupdtype data="constraint_action"}} ON DELETE {{select confdeltype data="constraint_action"}}{{^}}{{#ifCond constraint_type '!=' 'c'}}{{select constraint_type data="constraint_type_full"}} ({{column_name}}){{/ifCond}}{{/ifCond}}{{#ifCond @index '!=' (_math ../constraints.length '-' 1)}},{{/ifCond}}
  {{/each}}
);

{{/each}}