{% set createData = Migration.getCreateTablesElementData(tables) %}
{% for table, schema in tables %}
    {%~ set tableArgForMethods = useSchema ? schema : table %}
    {%~ set tableArgForArray = useSchema ? table : schema %}
    {%~ set foreignKeys = [] %}
    {%~ set primaryKeysColumns = Migration.primaryKeysColumnsList(tableArgForMethods) %}
    {%~ set primaryKeys = Migration.primaryKeys(tableArgForMethods) %}
    {%~ set specialPk = primaryKeys and (primaryKeys|length > 1 or primaryKeys[0]['name'] != 'id' or primaryKeys[0]['info']['columnType'] != 'integer') and autoId %}
    {%~ if loop.index > 1 %}

    {%~ endif -%}
    {%~ if specialPk %}
        $this->table('{{ tableArgForArray }}', ['id' => false, 'primary_key' => ['{{ Migration.extract(primaryKeys)|join("', '")|raw }}']])
    {%~ elseif not primaryKeys and autoId %}
        $this->table('{{ tableArgForArray }}', ['id' => false])
    {%~ else %}
        $this->table('{{ tableArgForArray }}')
    {%~ endif %}
    {%~ if specialPk or not autoId %}
        {%~ for primaryKey in primaryKeys %}
            {%~ set columnOptions = Migration.getColumnOption(primaryKey['info']['options']) %}
            ->addColumn('{{ primaryKey['name'] }}', '{{ primaryKey['info']['columnType'] }}', [{{ Migration.stringifyList(columnOptions, {'indent': 4}) | raw }}])
        {%~ endfor %}
        {%~ if not autoId and primaryKeys %}
            ->addPrimaryKey(['{{ Migration.extract(primaryKeys)
            | join("', '") | raw }}'])
        {%~ endif %}
    {%~ endif %}
    {%~ for column, config in Migration.columns(tableArgForMethods) %}
        {%~ set columnOptions = Migration.getColumnOption(config['options']) %}
        {%~ if config['columnType'] == 'boolean' and columnOptions['default'] is defined and (Migration.value(columnOptions['default'])) is not same as('null') %}
            {%~ set default = columnOptions['default'] ? true : false %}
            {%~ set columnOptions = columnOptions|merge({'default': default}) %}
        {%~ endif %}
            ->addColumn('{{ column }}', '{{ config['columnType'] }}', [{{
            Migration.stringifyList(columnOptions, {'indent': 4}) | raw
            }}])
    {%~ endfor %}
    {%~ if createData.tables[table].constraints is not empty %}
        {%~ for name, constraint in createData.tables[table].constraints %}
            {%~ if constraint['type'] == 'unique' %}
                {{~ element('Migrations.add-indexes', {
                    indexes: {(name): constraint},
                    backend: backend,
                }) -}}
            {%~ endif %}
        {%~ endfor %}
    {%~ endif %}
    {{- element('Migrations.add-indexes', {
            indexes: createData.tables[table].indexes,
            backend: backend,
    }) }}            ->create();
{% endfor -%}
{% if createData.constraints %}
    {%~ for table, tableConstraints in createData.constraints %}
        {{- element('Migrations.add-foreign-keys', {
                constraints: tableConstraints,
                table: table,
                backend: backend,
            })
        -}}
    {%~ endfor %}
{% endif -%}
