Skip to content

Conversation

allouis
Copy link
Contributor

@allouis allouis commented Jan 28, 2019

closes #10388

This migration finds all tables with nullable columns, it then loops through the tables and their nullable columns, updating each column to a null when its current value is an empty string.

@allouis
Copy link
Contributor Author

allouis commented Jan 28, 2019

I've tested this locally but figured it could use some more eyes.

@gargol What do you think of this?

@naz
Copy link
Contributor

naz commented Jan 28, 2019

Changes make sense. Will also give it a spin on my local instance 👍

});
};

module.exports.up = ({connection}) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

* */
const tablesToUpdate = Object.keys(schema.tables).reduce((tablesToUpdate, tableName) => {
const table = schema.tables[tableName];
const columns = Object.keys(table).filter(columnName => table[columnName].nullable);
Copy link
Contributor

Choose a reason for hiding this comment

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

To make this a bit more explicit, maybe the type fo string or text should be checked here as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking this too - though we only update if the existing value is '' so we shouldn't pick up any fields like that. Maybe it's better to be explicit tho 👍

@naz
Copy link
Contributor

naz commented Jan 28, 2019

@allouis running locally using knex-migrator migrate --v 2.13 --force throws:

[2019-01-28 17:32:55] ERROR

NAME: MigrationScriptError
MESSAGE: Cannot read property 'schema' of undefined

level:normal

[object Object]
Error occurred while executing the following migration: 1-remove-empty-strings.js
MigrationScriptError: Cannot read property 'schema' of undefined

at:

TypeError: Cannot read property 'schema' of undefined
    at /home/gargol/workspace/ghost/Ghost/core/server/data/migrations/versions/2.13/1-remove-empty-strings.js:27:23

how are you testing it on your setup?

@allouis
Copy link
Contributor Author

allouis commented Jan 28, 2019

^ This broke since I pushed the transaction stuff - fixing now

@allouis
Copy link
Contributor Author

allouis commented Jan 28, 2019

@gargol fixed!

I'm testing with knex-migrator migrate --v 2.13 --force and knex-migrator rollback --v 2.12 --force

};

module.exports.down = ({connection}) => {
const replaceNullWithEmptyString = createReplace(connection, null, '');
Copy link
Contributor Author

@allouis allouis Jan 28, 2019

Choose a reason for hiding this comment

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

@kirrg001 I found something interesting, which is that setting the {transaction: true} config on the exports, passed a transacting object to up, but still a connection object to down

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cool 👍

@naz
Copy link
Contributor

naz commented Jan 28, 2019

@allouis migrations work fine here 👍 merge at will 🚀

@allouis allouis merged commit 0edacf3 into TryGhost:master Jan 28, 2019
@allouis allouis deleted the no-more-empty-strings-pls branch January 29, 2019 09:41
kevinansfield added a commit to kevinansfield/Ghost that referenced this pull request Sep 16, 2019
refs TryGhost#10388, original PR TryGhost#10428

- re-introduces the migration which normalizes all empty strings in the database to `NULL`
- this was previously reverted due to being too large of a migration for a minor but a major is a good time to do it
kevinansfield added a commit that referenced this pull request Sep 16, 2019
refs #10388, original PR #10428

- re-introduces the migration which normalizes all empty strings in the database to `NULL`
- this was previously reverted due to being too large of a migration for a minor but a major is a good time to do it
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.

Normalize empty fields to null instead of empty string
3 participants