diff --git a/migration/migration.go b/migration/migration.go index bdaa2b7a1d..5042a50298 100644 --- a/migration/migration.go +++ b/migration/migration.go @@ -446,6 +446,9 @@ func GetMigrations() Migrations { // Version 102 m = append(m, steps{ExecuteSQLFile("102-add-forward-and-reverse-link-type-descriptions.sql")}) + // Version 103 + m = append(m, steps{ExecuteSQLFile("103-user-email-notnull-notempty.sql")}) + // Version N // // In order to add an upgrade, simply append an array of MigrationFunc to the diff --git a/migration/migration_blackbox_test.go b/migration/migration_blackbox_test.go index 5b7aa66531..fdf862aabf 100644 --- a/migration/migration_blackbox_test.go +++ b/migration/migration_blackbox_test.go @@ -152,6 +152,7 @@ func TestMigrations(t *testing.T) { t.Run("TestMigration100", testDropUserspacedataTable) t.Run("TestMigration101", testTypeGroupHasDescriptionField) t.Run("TestMigration102", testLinkTypeDescriptionFields) + t.Run("TestMigration103", testMigration103NotNullNotEmptyonEmail) // Perform the migration err = migration.Migrate(sqlDB, databaseName) @@ -1181,6 +1182,21 @@ func testMigration99CodebaseCVEScanDefaultFalse(t *testing.T) { require.Nil(t, runSQLscript(sqlDB, "099-codebase-cve-scan-default-false-cleanup.sql")) } +func testMigration103NotNullNotEmptyonEmail(t *testing.T) { + migrateToVersion(t, sqlDB, migrations[:103], 103) + + // setup + require.Nil(t, runSQLscript(sqlDB, "103-user-email-notnull-notempty.sql")) + + // migrate to the current version + migrateToVersion(t, sqlDB, migrations[:104], 104) + + // check we do not have any empty/null emails + rows, err := sqlDB.Query("SELECT email FROM users where email IS NULL or email = '';") + require.Nil(t, err) + require.False(t, rows.Next(), "row found with email = '' or NULL when all should have a valid email") +} + // runSQLscript loads the given filename from the packaged SQL test files and // executes it on the given database. Golang text/template module is used // to handle all the optional arguments passed to the sql test files diff --git a/migration/sql-files/103-user-email-notnull-notempty.sql b/migration/sql-files/103-user-email-notnull-notempty.sql new file mode 100644 index 0000000000..27f657e01c --- /dev/null +++ b/migration/sql-files/103-user-email-notnull-notempty.sql @@ -0,0 +1,6 @@ +-- Set email = id@example.com for users whose email is '' (empty) or ' ' (any +-- length of only white spaces) or NULL in database +UPDATE users SET email=concat(id::TEXT, '@example.com') WHERE COALESCE(trim(email), '') = ''; + +ALTER TABLE users ALTER COLUMN email SET NOT NULL; +ALTER TABLE users ADD CONSTRAINT email_notempty_check CHECK (trim(email) <> ''); \ No newline at end of file diff --git a/migration/sql-test-files/103-user-email-notnull-notempty.sql b/migration/sql-test-files/103-user-email-notnull-notempty.sql new file mode 100644 index 0000000000..0478051a69 --- /dev/null +++ b/migration/sql-test-files/103-user-email-notnull-notempty.sql @@ -0,0 +1,6 @@ +-- Create users with null/empty email value + +INSERT INTO users (full_name, email) VALUES ('Lorem1', ''); +INSERT INTO users (full_name, email) VALUES ('Lorem2', ' '); +INSERT INTO users (full_name, email) VALUES ('Lorem3', ' '); +INSERT INTO users (full_name, email) VALUES ('Lorem4', NULL); \ No newline at end of file