Created
August 5, 2020 16:23
-
-
Save Fogapod/0ad0763be7e90e6ab46cb49325e54eab to your computer and use it in GitHub Desktop.
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
START TRANSACTION; | |
START MIGRATION TO { | |
module default { | |
type User { | |
required property nickname -> str { | |
constraint exclusive on (str_lower(__subject__)); | |
}; | |
required property email -> str { | |
constraint exclusive; | |
}; | |
required property email_verified -> bool { | |
default := false; | |
}; | |
required property password -> bytes; | |
required property created_at -> datetime { | |
default := datetime_current(); | |
readonly := true; | |
}; | |
index on (__subject__.email); | |
} | |
function create_user(nickname: str, email: str, password: bytes) -> optional User | |
{ | |
volatility := 'VOLATILE'; | |
using EdgeQL $$ | |
WITH | |
MODULE default, | |
conflicting := ( | |
SELECT User { | |
created_at, | |
} | |
FILTER .nickname = nickname OR .email = email | |
LIMIT 2 | |
), | |
deleted := ( | |
DELETE | |
User | |
FILTER | |
User IN conflicting | |
AND NOT .email_verified | |
AND (datetime_of_transaction() - .created_at) > to_duration(hours := 24 * 5) | |
) | |
SELECT ( | |
FOR _ IN {( | |
SELECT true | |
FILTER count(conflicting) = count(deleted) | |
)} | |
UNION ( | |
INSERT User { | |
nickname := nickname, | |
email := email, | |
password := password, | |
} | |
) | |
) | |
$$ | |
} | |
}; | |
}; | |
POPULATE MIGRATION; | |
COMMIT MIGRATION; | |
COMMIT; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment