Skip to content

Commit

Permalink
Test null and missing table, fix missing table info
Browse files Browse the repository at this point in the history
  • Loading branch information
1602 committed Jul 22, 2016
1 parent a01eb7d commit a22b146
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/schema-operations.js
Expand Up @@ -51,7 +51,7 @@ module.exports = function(informationSchema, db, settings) {
return {
fields,
indexes,
sql: sql[0]['Create Table'],
sql: sql[0] && sql[0]['Create Table'],
tableMissing
};
});
Expand Down Expand Up @@ -270,7 +270,7 @@ module.exports = function(informationSchema, db, settings) {
if (oldSettings.Null === 'NO') { // Did not allow null and now does.
if (newSettings.allowNull === true) return true;
if (newSettings.null === true) return true;
if (newSettings.null === undefined && newSettings.allowNull === undefined) return true;
if (typeof newSettings.null === 'undefined' && typeof newSettings.allowNull === 'undefined') return true;
}

if (oldSettings.Type.toUpperCase() !== dataType(newSettings).toUpperCase()) return true;
Expand Down
84 changes: 84 additions & 0 deletions test/migration.test.js
Expand Up @@ -659,6 +659,90 @@ describe('migrations', function() {
return db.autoupdate();
});
});

describe('null', () => {

it('should reflect null/not null setting (allowNull)', () => {
db.define('Model', { a: {
type: String,
allowNull: true
} });
const props = db.models['Model'].properties;
return db.automigrate()
.then(() => {
props.a.allowNull = false;
return db.adapter.getAlterTableSQL('Model');
})
.then(sql => {
expect(sql[0]).toBe('CHANGE COLUMN `a` `a` VARCHAR(255) NOT NULL');
return db.autoupdate();
})
.then(() => {
props.a.allowNull = true;
return db.adapter.getAlterTableSQL('Model');
})
.then(sql => {
expect(sql[0]).toBe('CHANGE COLUMN `a` `a` VARCHAR(255) NULL');
});
});

it('should reflect null/not null setting (null)', () => {
db.define('Model', { a: {
type: String,
null: true
} });
const props = db.models['Model'].properties;
return db.automigrate()
.then(() => {
props.a.null = false;
return db.adapter.getAlterTableSQL('Model');
})
.then(sql => {
expect(sql[0]).toBe('CHANGE COLUMN `a` `a` VARCHAR(255) NOT NULL');
return db.autoupdate();
})
.then(() => {
props.a.null = true;
return db.adapter.getAlterTableSQL('Model');
})
.then(sql => {
expect(sql[0]).toBe('CHANGE COLUMN `a` `a` VARCHAR(255) NULL');
});
});

it('defaults to allow null', () => {
db.define('Model', { a: {
type: String,
allowNull: false
} });
const props = db.models['Model'].properties;
return db.automigrate()
.then(() => {
delete props.a.allowNull;
return db.adapter.getAlterTableSQL('Model');
})
.then(sql => {
expect(sql[0]).toBe('CHANGE COLUMN `a` `a` VARCHAR(255) NULL');
});
});

});

});

context('not existing table', () => {

it('should report "tableMissing"', () => {
db.define('MissingTableName', { a: {
type: String,
allowNull: false
} });
return db.adapter.getTableInfo('MissingTableName')
.then(info => {
expect(info.tableMissing).toBe(true);
});
});

});

});
Expand Down

0 comments on commit a22b146

Please sign in to comment.