-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
248 additions
and
153 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
DROP FUNCTION IF EXISTS create_user; | ||
CREATE OR REPLACE FUNCTION create_user( | ||
p_username VARCHAR, | ||
p_hashed_password VARCHAR, | ||
p_fullname VARCHAR, | ||
p_email VARCHAR | ||
) | ||
RETURNS TABLE ( | ||
username VARCHAR, | ||
hashed_password VARCHAR, | ||
fullname VARCHAR, | ||
email VARCHAR, | ||
password_changed_at TIMESTAMP WITH TIME ZONE, | ||
created_at TIMESTAMP WITH TIME ZONE | ||
) AS $$ | ||
BEGIN | ||
RETURN QUERY | ||
INSERT INTO users (username, hashed_password, fullname, email) | ||
VALUES (p_username, p_hashed_password, p_fullname, p_email) | ||
RETURNING *; | ||
END; | ||
$$ LANGUAGE plpgsql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
DROP FUNCTION IF EXISTS get_user; | ||
CREATE OR REPLACE FUNCTION get_user( | ||
p_username VARCHAR | ||
) | ||
RETURNS TABLE ( | ||
username VARCHAR, | ||
hashed_password VARCHAR, | ||
fullname VARCHAR, | ||
email VARCHAR, | ||
password_changed_at TIMESTAMP WITH TIME ZONE, | ||
created_at TIMESTAMP WITH TIME ZONE | ||
) AS $$ | ||
BEGIN | ||
RETURN QUERY | ||
SELECT | ||
users.username, | ||
users.hashed_password, | ||
users.fullname, | ||
users.email, | ||
users.password_changed_at, | ||
users.created_at | ||
FROM users | ||
WHERE users.username = p_username | ||
LIMIT 1; | ||
END; | ||
$$ LANGUAGE plpgsql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
DROP FUNCTION IF EXISTS update_user; | ||
CREATE OR REPLACE FUNCTION update_user( | ||
p_username VARCHAR, | ||
p_hashed_password VARCHAR, | ||
p_password_changed_at TIMESTAMP WITH TIME ZONE, | ||
p_fullname VARCHAR, | ||
p_email VARCHAR | ||
) | ||
RETURNS TABLE ( | ||
username VARCHAR, | ||
hashed_password VARCHAR, | ||
fullname VARCHAR, | ||
email VARCHAR, | ||
password_changed_at TIMESTAMP WITH TIME ZONE, | ||
created_at TIMESTAMP WITH TIME ZONE | ||
) AS $$ | ||
BEGIN | ||
RETURN QUERY | ||
UPDATE users | ||
SET | ||
hashed_password = COALESCE(p_hashed_password, users.hashed_password), | ||
password_changed_at = COALESCE(p_password_changed_at, users.password_changed_at), | ||
fullname = COALESCE(p_fullname, users.fullname), | ||
email = COALESCE(p_email, users.email) | ||
WHERE users.username = p_username | ||
RETURNING *; | ||
END; | ||
$$ LANGUAGE plpgsql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package db | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestGetSQLFiles(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
migrationDir string | ||
expectedFiles []string | ||
expectedErrMsgSubstring string | ||
}{ | ||
{ | ||
name: "Valid Directory", | ||
migrationDir: "test_data", | ||
expectedFiles: []string{"test_data/file_1.sql", "test_data/subdir/file_2.sql"}, | ||
}, | ||
{ | ||
name: "Non-Existent Directory", | ||
migrationDir: "nonexistent_directory", | ||
expectedErrMsgSubstring: "no such file or directory", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
files, err := getSQLFiles(tc.migrationDir) | ||
|
||
if tc.expectedErrMsgSubstring != "" { | ||
if err == nil || !strings.Contains(err.Error(), tc.expectedErrMsgSubstring) { | ||
t.Fatalf("Expected error containing '%s', but got '%v'", tc.expectedErrMsgSubstring, err) | ||
} | ||
return | ||
} | ||
|
||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
|
||
if !reflect.DeepEqual(files, tc.expectedFiles) { | ||
t.Fatalf("Expected files %v, but got %v", tc.expectedFiles, files) | ||
} | ||
}) | ||
} | ||
} |
Empty file.
Empty file.
Oops, something went wrong.