-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathcreate-tables.twig
More file actions
64 lines (63 loc) · 3.16 KB
/
Copy pathcreate-tables.twig
File metadata and controls
64 lines (63 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
{% 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 %}
{%~ set tableOptions = Migration.tableOptions(tableArgForMethods) %}
{%~ if specialPk %}
{%~ set tableOptions = {'id': false, 'primary_key': Migration.extract(primaryKeys)}|merge(tableOptions) %}
{%~ elseif not primaryKeys and autoId %}
{%~ set tableOptions = {'id': false}|merge(tableOptions) %}
{%~ endif %}
{%~ if loop.index > 1 %}
{%~ endif -%}
{%~ if tableOptions is not empty %}
$this->table('{{ tableArgForArray }}', [{{ Migration.stringifyTableOptions(tableOptions)|raw }}])
{%~ 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},
}) -}}
{%~ endif %}
{%~ endfor %}
{%~ endif %}
{{- element('Migrations.add-indexes', {
indexes: createData.tables[table].indexes,
}) }} ->create();
{% endfor -%}
{% if createData.constraints %}
{%~ for table, tableConstraints in createData.constraints %}
{{- element('Migrations.add-foreign-keys', {
constraints: tableConstraints,
table: table,
})
-}}
{%~ endfor %}
{% endif -%}