Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
230 changes: 115 additions & 115 deletions templates/bake/config/diff.twig
Original file line number Diff line number Diff line change
Expand Up @@ -47,81 +47,81 @@ class {{ name }} extends AbstractMigration
*/
public function up(): void
{
{% for tableName, tableDiff in data %}
{% set hasRemoveFK = tableDiff['constraints']['remove'] is not empty or tableDiff['indexes']['remove'] is
{% for tableName, tableDiff in data %}
{%~ set hasRemoveFK = tableDiff['constraints']['remove'] is not empty or tableDiff['indexes']['remove'] is
not empty %}
{% if hasRemoveFK %}
{%~ if hasRemoveFK %}
$this->table('{{ tableName }}')
{% endif %}
{% if tableDiff['constraints']['remove'] is not empty %}
{% for constraintName, constraintDefinition in tableDiff['constraints']['remove'] %}
{%~ endif %}
{%~ if tableDiff['constraints']['remove'] is not empty %}
{%~ for constraintName, constraintDefinition in tableDiff['constraints']['remove'] %}
->dropForeignKey([], '{{ constraintName }}')
{% endfor %}
{% endif %}
{% if tableDiff['indexes']['remove'] is not empty %}
{% for indexName, indexDefinition in tableDiff['indexes']['remove'] %}
{%~ endfor %}
{%~ endif %}
{%~ if tableDiff['indexes']['remove'] is not empty %}
{%~ for indexName, indexDefinition in tableDiff['indexes']['remove'] %}
->removeIndexByName('{{ indexName }}')
{% endfor %}
{% endif %}
{% if hasRemoveFK %}
{%~ endfor %}
{%~ endif %}
{%~ if hasRemoveFK %}
->update();
{% endif %}
{% if tableDiff['columns']['remove'] is not empty or tableDiff['columns']['changed'] is not empty %}
{%~ endif %}
{%~ if tableDiff['columns']['remove'] is not empty or tableDiff['columns']['changed'] is not empty %}

{{ Migration.tableStatement(tableName, true) | raw }}
{% if tableDiff['columns']['remove'] is not empty %}
{% for columnName, columnDefinition in tableDiff['columns']['remove'] %}
{%~ if tableDiff['columns']['remove'] is not empty %}
{%~ for columnName, columnDefinition in tableDiff['columns']['remove'] %}
->removeColumn('{{ columnName }}')
{% endfor %}
{% endif %}
{% if tableDiff['columns']['changed'] is not empty %}
{% for columnName, columnAttributes in tableDiff['columns']['changed'] %}
{% set type = columnAttributes['type'] %}
{% set columnAttributes = Migration.getColumnOption(columnAttributes) %}
{% set columnAttributes = Migration.stringifyList(columnAttributes, {'indent': 4, 'remove':
{%~ endfor %}
{%~ endif %}
{%~ if tableDiff['columns']['changed'] is not empty %}
{%~ for columnName, columnAttributes in tableDiff['columns']['changed'] %}
{%~ set type = columnAttributes['type'] %}
{%~ set columnAttributes = Migration.getColumnOption(columnAttributes) %}
{%~ set columnAttributes = Migration.stringifyList(columnAttributes, {'indent': 4, 'remove':
['type']}) %}
->changeColumn('{{ columnName }}', '{{ type }}', [{{ columnAttributes | raw }}])
{% endfor %}
{% endif -%}
{% if Migration.wasTableStatementGeneratedFor(tableName) %}
{%~ endfor %}
{%~ endif -%}
{%~ if Migration.wasTableStatementGeneratedFor(tableName) %}
->update();
{% endif %}
{% endif %}
{% endfor %}
{% if tables['add'] is not empty %}
{{ Migration.element('Migrations.create-tables', {'tables': tables['add'], 'autoId': autoId, 'useSchema':
{%~ endif %}
{%~ endif %}
{% endfor %}
{% if tables['add'] is not empty %}
{{~ Migration.element('Migrations.create-tables', {'tables': tables['add'], 'autoId': autoId, 'useSchema':
true}) | raw -}}
{% endif %}
{% for tableName, tableDiff in data %}
{% if tableDiff['columns']['add'] is not empty or tableDiff['indexes']['add'] is not empty %}
{% endif %}
{% for tableName, tableDiff in data %}
{%~ if tableDiff['columns']['add'] is not empty or tableDiff['indexes']['add'] is not empty %}

{{ Migration.tableStatement(tableName, true) | raw }}
{% if tableDiff['columns']['add'] is not empty %}
{{ Migration.element('Migrations.add-columns', {'columns': tableDiff['columns']['add']}) | raw -}}
{% endif -%}
{% if tableDiff['indexes']['add'] is not empty %}
{{ Migration.element('Migrations.add-indexes', {'backend': backend, 'indexes': tableDiff['indexes']['add']}) | raw -}}
{% endif -%}
{% if Migration.wasTableStatementGeneratedFor(tableName) %}
{%~ if tableDiff['columns']['add'] is not empty %}
{{~ Migration.element('Migrations.add-columns', {'columns': tableDiff['columns']['add']}) | raw -}}
{%~ endif -%}
{%~ if tableDiff['indexes']['add'] is not empty %}
{{~ Migration.element('Migrations.add-indexes', {'backend': backend, 'indexes': tableDiff['indexes']['add']}) | raw -}}
{%~ endif -%}
{%~ if Migration.wasTableStatementGeneratedFor(tableName) %}
->update();
{% endif -%}
{% endif -%}
{% endfor -%}
{% for tableName, tableDiff in data -%}
{% if tableDiff['constraints']['add'] is not empty -%}
{%~ endif -%}
{%~ endif -%}
{% endfor -%}
{% for tableName, tableDiff in data -%}
{% if tableDiff['constraints']['add'] is not empty -%}

{{ Migration.element(
'Migrations.add-foreign-keys',
{'constraints': tableDiff['constraints']['add'], 'table': tableName, 'backend': backend}
) | raw -}}
{% endif -%}
{% endfor -%}
{% if tables['remove'] is not empty %}
{% for tableName, table in tables['remove'] %}
{{~ Migration.element(
'Migrations.add-foreign-keys',
{'constraints': tableDiff['constraints']['add'], 'table': tableName, 'backend': backend}
) | raw -}}
{%~ endif -%}
{% endfor -%}
{% if tables['remove'] is not empty %}
{%~ for tableName, table in tables['remove'] %}

$this->table('{{ tableName }}')->drop()->save();
{% endfor %}
{% endif %}
{%~ endfor %}
{% endif %}
}

/**
Expand All @@ -134,87 +134,87 @@ not empty %}
*/
public function down(): void
{
{% set returnedData = Migration.getReturnedData() %}
{% set constraints = [] %}
{% set emptyLine = false %}
{% if returnedData['dropForeignKeys'] is not empty %}
{% for table, columnsList in returnedData['dropForeignKeys'] %}
{% set maxKey = columnsList | length - 1 %}
{% if emptyLine %}
{% set returnedData = Migration.getReturnedData() %}
{% set constraints = [] %}
{% set emptyLine = false %}
{% if returnedData['dropForeignKeys'] is not empty %}
{%~ for table, columnsList in returnedData['dropForeignKeys'] %}
{%~ set maxKey = columnsList | length - 1 %}
{%~ if emptyLine %}

{% else %}
{% set emptyLine = true %}
{% endif %}
{%~ else %}
{%~ set emptyLine = true %}
{%~ endif %}
$this->table('{{ table }}')
{% for key, columns in columnsList %}
{%~ for key, columns in columnsList %}
->dropForeignKey(
{{ columns | raw }}
){{ (key == maxKey) ? '->save();' : '' }}
{% endfor -%}
{% endfor -%}
{% endif -%}
{% if tables['remove'] is not empty -%}
{{ Migration.element('Migrations.create-tables', {
{%~ endfor -%}
{%~ endfor -%}
{% endif -%}
{% if tables['remove'] is not empty -%}
{{~ Migration.element('Migrations.create-tables', {
'tables': tables['remove'],
'autoId': autoId,
'useSchema': true
}) | raw -}}
{% endif -%}
{% for tableName, tableDiff in data -%}
{% do Migration.resetTableStatementGenerationFor(tableName) %}
{% if tableDiff['indexes']['add'] is not empty %}
{% endif -%}
{% for tableName, tableDiff in data -%}
{%~ do Migration.resetTableStatementGenerationFor(tableName) %}
{%~ if tableDiff['indexes']['add'] is not empty %}

$this->table('{{ tableName }}')
{% for indexName, index in tableDiff['indexes']['add'] %}
{%~ for indexName, index in tableDiff['indexes']['add'] %}
->removeIndexByName('{{ indexName }}')
{% endfor %}
{%~ endfor %}
->update();
{% endif %}
{% if (tableDiff['columns']['remove'] is not empty or
{%~ endif %}
{%~ if (tableDiff['columns']['remove'] is not empty or
tableDiff['columns']['changed'] is not empty or
tableDiff['columns']['add'] is not empty or
tableDiff['indexes']['remove'] is not empty) %}

{{ Migration.tableStatement(tableName, true) | raw}}
{% endif %}
{% if tableDiff['columns']['remove'] is not empty -%}
{{ Migration.element('Migrations.add-columns', {'columns': tableDiff['columns']['remove']}) | raw -}}
{% endif -%}
{% if tableDiff['columns']['changed'] is not empty -%}
{% set oldTableDef = dumpSchema[tableName] -%}
{% for columnName, columnAttributes in tableDiff['columns']['changed'] -%}
{% set columnAttributes = oldTableDef.getColumn(columnName) -%}
{% set type = columnAttributes['type'] -%}
{% set columnAttributes = Migration.getColumnOption(columnAttributes) -%}
{% set columnAttributes = Migration.stringifyList(columnAttributes, {'indent': 4, 'remove': ['type']}) %}
{%~ endif %}
{%~ if tableDiff['columns']['remove'] is not empty -%}
{{~ Migration.element('Migrations.add-columns', {'columns': tableDiff['columns']['remove']}) | raw -}}
{%~ endif -%}
{%~ if tableDiff['columns']['changed'] is not empty -%}
{%~ set oldTableDef = dumpSchema[tableName] -%}
{%~ for columnName, columnAttributes in tableDiff['columns']['changed'] -%}
{%~ set columnAttributes = oldTableDef.getColumn(columnName) -%}
{%~ set type = columnAttributes['type'] -%}
{%~ set columnAttributes = Migration.getColumnOption(columnAttributes) -%}
{%~ set columnAttributes = Migration.stringifyList(columnAttributes, {'indent': 4, 'remove': ['type']}) %}
->changeColumn('{{ columnName }}', '{{ type }}', [{{ columnAttributes | raw }}])
{% endfor -%}
{% endif -%}
{% if tableDiff['columns']['add'] is not empty %}
{% for columnName, columnAttributes in tableDiff['columns']['add'] %}
{%~ endfor -%}
{%~ endif -%}
{%~ if tableDiff['columns']['add'] is not empty %}
{%~ for columnName, columnAttributes in tableDiff['columns']['add'] %}
->removeColumn('{{ columnName }}')
{% endfor -%}
{% endif -%}
{% if tableDiff['indexes']['remove'] is not empty -%}
{{ Migration.element('Migrations.add-indexes', {'indexes': tableDiff['indexes']['remove']}) | raw -}}
{% endif -%}
{% if Migration.wasTableStatementGeneratedFor(tableName) %}
{%~ endfor -%}
{%~ endif -%}
{%~ if tableDiff['indexes']['remove'] is not empty -%}
{{~ Migration.element('Migrations.add-indexes', {'indexes': tableDiff['indexes']['remove']}) | raw -}}
{%~ endif -%}
{%~ if Migration.wasTableStatementGeneratedFor(tableName) %}
->update();
{% endif %}
{% endfor %}
{% for tableName, tableDiff in data %}
{% if tableDiff['constraints']['remove'] is not empty %}
{{ Migration.element(
'Migrations.add-foreign-keys',
{'constraints': tableDiff['constraints']['remove'], 'table': tableName}
{%~ endif %}
{% endfor %}
{% for tableName, tableDiff in data %}
{%~ if tableDiff['constraints']['remove'] is not empty %}
{{~ Migration.element(
'Migrations.add-foreign-keys',
{'constraints': tableDiff['constraints']['remove'], 'table': tableName}
) | raw -}}
{% endif %}
{% endfor %}
{% if tables['add'] is not empty %}
{%~ endif %}
{% endfor %}
{% if tables['add'] is not empty %}

{% for tableName, table in tables['add'] %}
{%~ for tableName, table in tables['add'] %}
$this->table('{{ tableName }}')->drop()->save();
{% endfor %}
{% endif %}
{%~ endfor %}
{% endif %}
}
}
28 changes: 14 additions & 14 deletions templates/bake/config/skeleton.twig
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,35 @@ class {{ name }} extends AbstractMigration
{
{% for table in tables %}
$table = $this->table('{{ table }}');
{% if tableMethod != 'drop' %}
{% if columnMethod == 'removeColumn' %}
{% for column, config in columns['fields'] %}
{%~ if tableMethod != 'drop' %}
{%~ if columnMethod == 'removeColumn' %}
{%~ for column, config in columns['fields'] %}
$table->{{ columnMethod }}('{{ column }}');
{% endfor %}
{% for column, config in columns['indexes'] %}
{%~ endfor %}
{%~ for column, config in columns['indexes'] %}
$table->{{ indexMethod }}([{{
Migration.stringifyList(config['columns']) | raw
}}]);
{% endfor %}
{% else %}
{% for column, config in columns['fields'] %}
{%~ endfor %}
{%~ else %}
{%~ for column, config in columns['fields'] %}
$table->{{ columnMethod }}('{{ column }}', '{{ config['columnType'] }}', [{{
Migration.stringifyList(config['options'], {'indent': 3}, wantedOptions) | raw
}}]);
{% endfor %}
{% for column, config in columns['indexes'] %}
{%~ endfor %}
{%~ for column, config in columns['indexes'] %}
$table->{{ indexMethod }}([{{
Migration.stringifyList(config['columns'], {'indent': 3}) | raw }}
], [{{
Migration.stringifyList(config['options'], {'indent': 3}) | raw
}}]);
{% endfor %}
{% if tableMethod == 'create' and columns['primaryKey'] is not empty %}
{%~ endfor %}
{%~ if tableMethod == 'create' and columns['primaryKey'] is not empty %}
$table->addPrimaryKey([{{
Migration.stringifyList(columns['primaryKey'], {'indent': 3}) | raw
}}]);
{% endif %}
{% endif %}
{%~ endif %}
{%~ endif %}
{% endif %}
$table->{{ tableMethod }}(){% if tableMethod == 'drop' %}->save(){% endif %};
{% endfor %}
Expand Down
20 changes: 10 additions & 10 deletions templates/bake/config/snapshot.twig
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,21 @@ class {{ name }} extends AbstractMigration
*/
public function down(): void
{
{% set returnedData = Migration.getReturnedData() %}
{% if returnedData and returnedData['dropForeignKeys'] is not empty %}
{% for table, columnsList in Migration.returnedData['dropForeignKeys'] %}
{% set maxKey = columnsList|length - 1 %}
{%~ set returnedData = Migration.getReturnedData() %}
{%~ if returnedData and returnedData['dropForeignKeys'] is not empty %}
{%~ for table, columnsList in Migration.returnedData['dropForeignKeys'] %}
{%~ set maxKey = columnsList|length - 1 %}
$this->table('{{ table }}')
{% for key, columns in columnsList %}
{%~ for key, columns in columnsList %}
->dropForeignKey(
{{ columns|raw }}
){{ (key == maxKey) ? '->save();' : '' }}
{% endfor %}
{%~ endfor %}

{% endfor %}
{% endif %}
{% for table in tables %}
{%~ endfor %}
{% endif %}
{% for table in tables %}
$this->table('{{ table }}')->drop()->save();
{% endfor %}
{% endfor %}
}
}
12 changes: 6 additions & 6 deletions templates/bake/element/add-columns.twig
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{% for columnName, columnAttributes in columns %}
{% set type = columnAttributes['type'] %}
{% set columnAttributes = Migration.getColumnOption(columnAttributes) %}
{% set columnAttributes = Migration.stringifyList(columnAttributes, {'indent': 4, 'remove': ['type']}) %}
{% if columnAttributes is not empty %}
{%~ set type = columnAttributes['type'] %}
{%~ set columnAttributes = Migration.getColumnOption(columnAttributes) %}
{%~ set columnAttributes = Migration.stringifyList(columnAttributes, {'indent': 4, 'remove': ['type']}) %}
{%~ if columnAttributes is not empty %}
->addColumn('{{ columnName }}', '{{ type }}', [{{ columnAttributes | raw }}])
{% else %}
{%~ else %}
->addColumn('{{ columnName }}', '{{ type }}')
{% endif -%}
{%~ endif -%}
{% endfor -%}
Loading
Loading