0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-04-15 03:01:37 -05:00

🐛 Updated base model to store null instead of empty string ()

refs  

This updates the base model to retrieve column information, and explicitly set every property whose column is `nullable` and content is the empty string (`""`) to `null`
This commit is contained in:
Fabien O'Carroll 2019-01-28 16:58:28 +01:00 committed by GitHub
parent 12a265b500
commit 95880dddeb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 11 additions and 44 deletions
core
server/models
test/unit/models/base

View file

@ -201,7 +201,6 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
*/
onValidate: function onValidate(model, columns, options) {
this.setEmptyValuesToNull();
return validation.validateSchema(this.tableName, this, options);
},
@ -374,20 +373,16 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
return attrs;
},
// Sets given values to `null`
getNullableStringProperties() {
const table = schema.tables[this.tableName];
return Object.keys(table).filter(column => table[column].nullable);
},
setEmptyValuesToNull: function setEmptyValuesToNull() {
var self = this,
attr;
if (!this.emptyStringProperties) {
return;
}
attr = this.emptyStringProperties();
_.each(attr, function (value) {
if (self.get(value) === '') {
self.set(value, null);
const nullableStringProps = this.getNullableStringProperties();
return nullableStringProps.forEach((prop) => {
if (this.get(prop) === '') {
this.set(prop, null);
}
});
},

View file

@ -454,13 +454,6 @@ Post = ghostBookshelf.Model.extend({
return sequence(ops);
},
emptyStringProperties: function emptyStringProperties() {
// CASE: the client might send empty image properties with "" instead of setting them to null.
// This can cause GQL to fail. We therefore enforce 'null' for empty image properties.
// See https://github.com/TryGhost/GQL/issues/24
return ['feature_image', 'og_image', 'twitter_image'];
},
created_by: function createdBy() {
return this.belongsTo('User', 'created_by');
},

View file

@ -49,13 +49,6 @@ Tag = ghostBookshelf.Model.extend({
}
},
emptyStringProperties: function emptyStringProperties() {
// CASE: the client might send empty image properties with "" instead of setting them to null.
// This can cause GQL to fail. We therefore enforce 'null' for empty image properties.
// See https://github.com/TryGhost/GQL/issues/24
return ['feature_image'];
},
posts: function posts() {
return this.belongsToMany('Post');
},

View file

@ -230,13 +230,6 @@ User = ghostBookshelf.Model.extend({
return attrs;
},
emptyStringProperties: function emptyStringProperties() {
// CASE: the client might send empty image properties with "" instead of setting them to null.
// This can cause GQL to fail. We therefore enforce 'null' for empty image properties.
// See https://github.com/TryGhost/GQL/issues/24
return ['profile_image', 'cover_image'];
},
format: function format(options) {
if (!_.isEmpty(options.website) &&
!validator.isURL(options.website, {

View file

@ -174,8 +174,8 @@ describe('Models: base', function () {
it('resets given empty value to null', function () {
const base = models.Base.Model.forge({a: '', b: ''});
base.emptyStringProperties = sinon.stub();
base.emptyStringProperties.returns(['a']);
base.getNullableStringProperties = sinon.stub();
base.getNullableStringProperties.returns(['a']);
base.get('a').should.eql('');
base.get('b').should.eql('');
@ -183,13 +183,6 @@ describe('Models: base', function () {
should.not.exist(base.get('a'));
base.get('b').should.eql('');
});
it('does not reset to null if model does\'t provide properties', function () {
const base = models.Base.Model.forge({a: ''});
base.get('a').should.eql('');
base.setEmptyValuesToNull();
base.get('a').should.eql('');
});
});
describe('destroy', function () {