Skip to content

Fix migration bugs for MySQL and MongoDB - #1629

Open
stuk88 wants to merge 1 commit into
balderdashy:masterfrom
stuk88:fix/migration-bugs-mysql-mongodb
Open

Fix migration bugs for MySQL and MongoDB#1629
stuk88 wants to merge 1 commit into
balderdashy:masterfrom
stuk88:fix/migration-bugs-mysql-mongodb

Conversation

@stuk88

@stuk88 stuk88 commented Apr 16, 2026

Copy link
Copy Markdown

Summary

  • process-all-records.js: Fix nonAttrKeys > 0 to nonAttrKeys.length > 0 — array-to-number comparison was always false, silently suppressing schema mismatch warnings for schema:true models
  • has-schema-check.js: Replace dead connection/connections property references (pre-0.13 naming) with _datastore — schemaless adapters like sails-mongo could never have their schema: false default consulted, causing incorrect schema enforcement
  • validate-datastore-connectivity.js: Add null guard for adapterDSEntry before accessing .driver — prevents TypeError crash during ORM initialization for adapters that don't expose a .datastores property
  • MetaModel.js: Expose _datastore reference so has-schema-check can access the full datastore config and adapter defaults

Test plan

  • 7 new integration tests covering all fixes (test/unit/migration-fixes.js)
  • Full test suite passes (123 passing, 0 failing)
  • Verify with sails-mongo adapter: models without explicit schema: false should now correctly inherit the adapter default
  • Verify with sails-mysql adapter: schema mismatch warnings now appear when database has extra columns not in the model

Fix three bugs in Waterline core that affect migration behavior:

- process-all-records.js: Fix array-to-number comparison (nonAttrKeys > 0
  should be nonAttrKeys.length > 0) that silently suppressed schema mismatch
  warnings for schema:true models

- has-schema-check.js: Update obsolete connection/connections property
  references to use datastore/_datastore, fixing the fallback that checks
  the adapter's default schema setting. Previously, schemaless adapters
  like sails-mongo could never have their schema:false default consulted.

- validate-datastore-connectivity.js: Add null guard for adapterDSEntry
  before accessing .driver, preventing TypeError crash during ORM
  initialization for adapters that don't expose a .datastores property

- MetaModel.js: Expose _datastore reference so has-schema-check can
  access the datastore config and adapter defaults
Copilot AI review requested due to automatic review settings April 16, 2026 12:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes several migration/initialization edge cases affecting schema enforcement and connectivity validation across adapters (notably MySQL- and MongoDB-like adapters), and adds regression tests to prevent recurrence.

Changes:

  • Fix extraneous-record-key detection for schema:true models by using nonAttrKeys.length > 0.
  • Update schemafulness detection to consult the full datastore entry (_datastore) and expose that reference from MetaModel.
  • Prevent ORM initialization crashes by guarding against missing adapter datastore entries / drivers during connectivity validation.
  • Add new unit/integration tests covering the reported regressions.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
lib/waterline/utils/query/process-all-records.js Corrects extraneous key detection so schema mismatch warnings aren’t silently suppressed.
lib/waterline/utils/system/has-schema-check.js Switches schema default lookup to use _datastore (datastore config + adapter defaults).
lib/waterline/utils/system/validate-datastore-connectivity.js Adds null-safety around adapter datastore entries to avoid TypeError during init.
lib/waterline/MetaModel.js Exposes _datastore on models so schema check can access datastore config/adapter defaults.
test/unit/migration-fixes.js Adds regression tests for schema enforcement + connectivity + alter-strategy behaviors.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


waterline.initialize({
adapters: { schemaless: adapterDef },
datastores: { default: { adapter: 'schemaless', schema: false } }

Copilot AI Apr 16, 2026

Copy link

Choose a reason for hiding this comment

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

This test claims to verify fallback to the adapter's schema:false default, but the datastore config is explicitly setting schema: false, so hasSchema will be false even if the adapter default logic is broken. To actually cover the regression, remove schema: false from the datastore config (and keep only adapterDef.schema = false).

Suggested change
datastores: { default: { adapter: 'schemaless', schema: false } }
datastores: { default: { adapter: 'schemaless' } }

Copilot uses AI. Check for mistakes.
it('should not drop the collection when adapter lacks describe()', function(done) {
var dropCalled = false;
var defineCalled = false;
var runAutoMigrations = require('../../node_modules/waterline-utils').autoMigrations;

Copilot AI Apr 16, 2026

Copy link

Choose a reason for hiding this comment

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

Avoid requiring a dependency via a relative ../../node_modules/... path; it's brittle with different package managers / install layouts. Since waterline-utils is a declared dependency, require it as require('waterline-utils') instead.

Suggested change
var runAutoMigrations = require('../../node_modules/waterline-utils').autoMigrations;
var runAutoMigrations = require('waterline-utils').autoMigrations;

Copilot uses AI. Check for mistakes.
Comment on lines +232 to +233
var runAutoMigrations = require('../../node_modules/waterline-utils').autoMigrations;

Copilot AI Apr 16, 2026

Copy link

Choose a reason for hiding this comment

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

Avoid requiring a dependency via a relative ../../node_modules/... path; it's brittle with different package managers / install layouts. Since waterline-utils is a declared dependency, require it as require('waterline-utils') instead.

Copilot uses AI. Check for mistakes.
it('should fall back to drop+reinsert when new columns are needed', function(done) {
var dropCalledForModel = false;
var createEachCalled = false;
var runAutoMigrations = require('../../node_modules/waterline-utils').autoMigrations;

Copilot AI Apr 16, 2026

Copy link

Choose a reason for hiding this comment

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

Avoid requiring a dependency via a relative ../../node_modules/... path; it's brittle with different package managers / install layouts. Since waterline-utils is a declared dependency, require it as require('waterline-utils') instead.

Suggested change
var runAutoMigrations = require('../../node_modules/waterline-utils').autoMigrations;
var runAutoMigrations = require('waterline-utils').autoMigrations;

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants