Skip to content

Instantly share code, notes, and snippets.

@Fogapod
Last active August 12, 2020 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fogapod/95bce022040e039843fea0d9ae233a89 to your computer and use it in GitHub Desktop.
Save Fogapod/95bce022040e039843fea0d9ae233a89 to your computer and use it in GitHub Desktop.
START TRANSACTION;
# create user before function because:
# See https://github.com/edgedb/edgedb/issues/1625
START MIGRATION TO {
module default {
type User {
required property created_at -> datetime {
default := datetime_current();
};
required property nickname -> name_type {
constraint exclusive on (str_lower(__subject__));
};
required property email -> email_type {
constraint exclusive;
};
required property email_verified -> bool {
default := false;
};
index on (__subject__.email);
}
scalar type name_type extending str {
constraint min_len_value(4);
constraint max_len_value(12);
constraint regexp(r'[a-zA-Z\d]([a-zA-Z\d]|-(?=[a-zA-Z\d])){3,11}');
}
scalar type email_type extending str {
constraint max_len_value(500);
constraint regexp(r'.+@.+\..+');
}
}
};
POPULATE MIGRATION;
COMMIT MIGRATION;
START MIGRATION TO {
module default {
type User {
required property created_at -> datetime {
default := datetime_current();
};
required property nickname -> name_type {
constraint exclusive on (str_lower(__subject__));
};
required property email -> email_type {
constraint exclusive;
};
required property email_verified -> bool {
default := false;
};
index on (__subject__.email);
}
scalar type name_type extending str {
constraint min_len_value(4);
constraint max_len_value(12);
constraint regexp(r'[a-zA-Z\d]([a-zA-Z\d]|-(?=[a-zA-Z\d])){3,11}');
}
scalar type email_type extending str {
constraint max_len_value(500);
constraint regexp(r'.+@.+\..+');
}
function delete_conflicting_users(
nickname: optional name_type,
email: optional email_type
) -> tuple<int64, int64> {
volatility := 'VOLATILE';
using EdgeQL $$
WITH
MODULE default,
conflicting := (
SELECT User
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 (count(conflicting), count(deleted))
$$
}
}
};
POPULATE MIGRATION;
COMMIT MIGRATION;
COMMIT;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment