Skip to content

feat: Implement proper schema migration structure (Issue #94)#138

Merged
nuest merged 3 commits into
mainfrom
feature/schema-migration-issue-94
Jul 13, 2026
Merged

feat: Implement proper schema migration structure (Issue #94)#138
nuest merged 3 commits into
mainfrom
feature/schema-migration-issue-94

Conversation

@subhanLabs

@subhanLabs subhanLabs commented Jun 29, 2026

Copy link
Copy Markdown
Member

Closes #94

Implements a proper versioned schema migration structure for the CODECHECK plugin, replacing the previous ad-hoc approach where table creation was split across three disconnected methods called manually from setEnabled().

Approach

This implementation follows the pattern used by the PKP PLN plugin (pkp/pln), which is the only official PKP plugin with a full install + upgrade migration structure. The pattern is compatible with OJS 3.5, confirmed via pkp-lib/stable-3_5_0/classes/plugins/Plugin.php.

Changes

New structure

classes/migration/
├── install/
│   └── CodecheckSchemaMigration.php   ← all 4 tables, fresh install only
└── upgrade/
    └── I94_AddMissingColumns.php      ← defensive column-level changes

How it works

Fresh install: setEnabled() calls getInstallMigration()->up() which creates all 4 tables (codecheck_metadata, codecheck_orcid_tokens, codecheck_issue_labels, codecheck_status) and then runs all upgrade migrations. Since columns already exist in the freshly created table, the upgrade migrations do nothing extra.

Existing install (upgrade): Schema::hasTable() is true so table creation is skipped, existing data is untouched. Upgrade migrations then run and use Schema::hasColumn() guards to add only the columns that are missing from older installs.

OJS Plugin Gallery install: getInstallMigration() hooks into OJS's own Installer::postInstall event automatically via the parent Plugin class, no manual intervention needed.

Adding future columns: Create a new
classes/migration/upgrade/I{issue}_Description.php with a hasColumn guard, then add one line calling it at the bottom of the install migration.
Both fresh installs and existing installs converge to the same schema state.

Key fixes compared to before

Before After
Fresh install 3 methods called manually in setEnabled() Single entry point via getInstallMigration()
Existing install with new columns Silently skipped, column never added I94_AddMissingColumns adds missing columns safely
OJS gallery install Bypassed entirely Hooks into Installer::postInstall automatically
down() Dropped tables — risked data loss Throws DowngradeNotSupportedException
Reset button Called down() from migration Explicit drops in resetSchema(), separate from migration system

Other changes

  • Remove classes/migration/CodecheckSchemaMigration.php (old file)
  • Remove unused CodecheckSchemaMigration import from Manage.php
  • Remove private $migration property from CodecheckPlugin
  • Bump version.xml from 0.0.0.0 to 1.0.0.0

References

- Split migration into install/ and upgrade/ following PLN plugin pattern
- Add getInstallMigration() to hook into OJS Installer::postInstall
- Consolidate all 4 tables into single install migration entry point
- Add I94_AddMissingColumns for defensive column-level upgrades
- Remove drop-based down() — replaced with explicit resetSchema()
- Remove old classes/migration/CodecheckSchemaMigration.php
- Bump version.xml to 1.0.0.0

@nuest nuest left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the first iteration. Can you please update the documentation as well?

What I find especially important: Where is the order of the upgrade scripts configured?

If I understand correctly, that would be in the up() function.

Also make clear in the documentation that "adding a column" is only one thing that could happen, an upgrade script may also process all records (e.g., rename an allowed value), insert content, e.g., for settings, or remove fields/columns/tables that are not used anymore.
Correct?

Comment thread version.xml Outdated
Comment thread classes/migration/upgrade/I94_AddMissingColumns.php Outdated
- Add CodecheckMigration abstract base class with start/end logging
- Subclasses implement runUp() instead of up()
- down() and DowngradeNotSupportedException consolidated into base class
- Document upgrade script ordering and scope in install migration
- Revert version.xml to 0.0.0.0
@subhanLabs

subhanLabs commented Jul 13, 2026

Copy link
Copy Markdown
Member Author

Thanks for the first iteration. Can you please update the documentation as well?

What I find especially important: Where is the order of the upgrade scripts configured?

If I understand correctly, that would be in the up() function.

Also make clear in the documentation that "adding a column" is only one thing that could happen, an upgrade script may also process all records (e.g., rename an allowed value), insert content, e.g., for settings, or remove fields/columns/tables that are not used anymore. Correct?

Added a docblock to the install migration explaining that the order of calls at the bottom of runUp() defines execution order, and that upgrade scripts aren't limited to adding columns, they can rename, migrate data, insert defaults, or remove things too.

@subhanLabs
subhanLabs requested a review from nuest July 13, 2026 12:29
@nuest
nuest merged commit 5340070 into main Jul 13, 2026
3 checks passed
@nuest
nuest deleted the feature/schema-migration-issue-94 branch July 13, 2026 12:49
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.

Add proper schema migration

2 participants