Skip to content

Changed breakpoint column to disallow nulls again #2162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

Bilge
Copy link
Contributor

@Bilge Bilge commented Jan 3, 2023

For reasons that remain unclear to me, rather than having a single source of truth, the breakpoint column of the migration table may be defined in one of two places, (either here or here). Since 0.13, the breakpoint column may permit null or not, depending on which code path it ends up using. In my case, it suddenly stopped declaring NOT NULL after the upgrade because it used the path which was seemingly missed in #1872. This PR fixes the missed code path.

@MasterOdin MasterOdin merged commit 8141ae7 into cakephp:0.x Jan 4, 2023
@MasterOdin
Copy link
Member

MasterOdin commented Jan 4, 2023

Consolidating would be good and just put it all in createSchemaTable where the end result is giving you a created schema table, be it a fresh creation or updating a previous installation.

Logic could probably be something like:

    public function createSchemaTable(): void
    {
        try {
            $createTable = !$this->hasTable($this->getSchemaTableName());
            $options = $createTable ? ['id' => false, 'primary_key' => 'version'] : [];
            $table = new Table($this->getSchemaTableName(), $options, $this);
            $columns = $createTable ? [] : array_map(function ($column) {
                return $column->getName();
            }, $table->getColumns());
            if (!in_array('version', $columns, true)) {
                $table->addColumn('version', 'biginteger', ['null' => false]);
            }
            if (!in_array('migration_name', $columns, true)) {
                $table->addColumn('migration_name', 'string', ['limit' => 100, 'default' => null, 'null' => true]);
            }
            if (!in_array('start_time', $columns, true)) {
                $table->addColumn('start_time', 'timestamp', ['default' => null, 'null' => true]);
            }
            if (!in_array('end_time', $columns, true)) {
                $table->addColumn('end_time', 'timestamp', ['default' => null, 'null' => true]);
            }
            if (!in_array('breakpoint', $columns, true)) {
                $table->addColumn('breakpoint', 'boolean', ['default' => false, 'null' => false]);
            }
            $table->save();
        } catch (Exception $exception) {
            throw new InvalidArgumentException(
                'There was a problem creating the schema table: ' . $exception->getMessage(),
                (int)$exception->getCode(),
                $exception
            );
        }
    }

where $table->save(); should be a no-op in the general case where we do not do any addColumn calls.

This might break a case where someone was calling createSchemaTable manually and wanting the thrown exception when it exists and so the addColumn actions fails, but I've no idea if that's a strong enough edge case to not do that.

Thoughts?

@Bilge Bilge deleted the consistent-breakpoint-column-definition branch January 4, 2023 20:56
@Bilge
Copy link
Contributor Author

Bilge commented Jan 4, 2023

You're the principal maintainer. Why are you asking me? 🙃

@dereuromark
Copy link
Member

You seem to have quite strong opinions from time to time - only fair to consult you once in a while ;)

@Bilge
Copy link
Contributor Author

Bilge commented Jan 4, 2023

Having a strong opinion doesn't make you right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants