strapi / migration-scripts

Collection of Strapi Migration scripts
58 stars 57 forks source link

Apply defaults(if any) if column undefined in mongo + Typo Fix #80

Closed Skwangles closed 1 year ago

Skwangles commented 1 year ago

fixes #79 If undefined, checks for default, if found, adds it - i.e. if mongoDB entry doesn't have a column value that it should have, add its default (if it exists). This problem is really strapi v3's problem, where it does not add DEFAULT properties to tables (specifically PostgreSQL) Also, fixed an obvious typo.

Why this approach

Since Object.keys(entry).forEach( already checks if a property is in the model.attributes (then returns if not), I found it would work just as good the other way, to loop through the model's attributes and check if the entry contains it (trys to apply the default value if doesn't).

My Approach

I have only implemented the case where an attribute.default exists and the property is UNDEFINED (i.e. does not exist, not = null), the logic being that if it is '= null' it could have been set null for a reason, thus should only do this for undefined. Note: When I say 'undefined', I mean it is physically not defined in the object (i.e. 'b' is "undefined" in {a:1})

I have limited testing capacity, so would appreciate some further testing on other people's systems for a wider range of applications.

Xander-SquareKicker commented 1 year ago

Applying defaults to undefined properties could be dangerous, as uncleaned/old datasets could just 'work' without being properly cleaned - i.e. uses 'fields' properties instead of the 'properties' attribute, the properties attribute with this will just be applied empty (default), without the 'fields' entries being switched over manually by a developer, causing unseen issues?

tldr; May not be a a good idea to apply defaults, if a developer might need to resolve it manually