Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

Commit

Permalink
Add NotNull and NotEmpty constraint to Users.email column (#2248)
Browse files Browse the repository at this point in the history
Fixes #2237
  • Loading branch information
jarifibrahim authored Aug 20, 2018
1 parent 09d3745 commit 87fdcec
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions migration/migration_blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions migration/sql-files/103-user-email-notnull-notempty.sql
Original file line number Diff line number Diff line change
@@ -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) <> '');
6 changes: 6 additions & 0 deletions migration/sql-test-files/103-user-email-notnull-notempty.sql
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 87fdcec

Please sign in to comment.