Skip to content

Column ordering #22

@puzzledpolymath

Description

@puzzledpolymath

The issue

When compiling a migration and an entity uses behaviors, such as CreatedAt and UpdatedAt, the RenderModifiers generator adds these to the table schema first ignoring or not knowing the order in which fields are defined. Which means the migration files need to be manually edited to ensure column ordering reflects the entity fields.

Let me know if this something you see as an issue and I can try and work on a solution.

Potential Solution

cycle/schema-migrations-generator/src/GenerateMigrations.php

    public function run(Registry $registry): Registry
    {
        $databases = [];
        foreach ($registry as $e) {
            if ($registry->hasTable($e) && !$e->getOptions()->has(SyncTables::READONLY_SCHEMA)) {
                // An array of column names in the order fields are listed
                $names = array_map(fn(Field $field) => $field->getColumn(), \iterator_to_array($e->getFields()));
                // Store the table before modification and passing to $databases
                $table = $registry->getTableSchema($e);
                // Call newly added method for sorting columns
                $table->sortColumns($names);
                // Set table with preferred column ordering
                $databases[$registry->getDatabase($e)][] = $table;
            }
        }
        ...
    }

cycle/database/src/Schema/AbstractTable.php

    public function sortColumns(array $names): void
    {
        $this->current->sortColumnsByNames($names);
    }

cycle/database/src/Schema/State.php

    public function sortColumnsByNames(array $names): void
    {
        $sorted = [];

        foreach ($names as $name) {
            if (isset($this->columns[$name])) {
                $sorted[$name] = $this->columns[$name];
            }
        }

        foreach ($this->columns as $name => $column) {
            if (! isset($sorted[$name])) {
                $sorted[$name] = $column;
            }
        }

        $this->columns = $sorted;
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions