code-corps / code-corps-api

Elixir/Phoenix API for Code Corps.
https://www.codecorps.org
MIT License
235 stars 86 forks source link

Consolidate normalization of github user type field #1345

Closed begedin closed 6 years ago

begedin commented 6 years ago

Problem

The github user payload has a type field which can be "User", "Bot" or "Organization".

With "User" and "Bot", we normalize these values during conversion into "user" and "bot".

However, "Organization" remains capitalized and is not changed when inserting/updating into our database. Any case where where the type is being saved as "Organization" should be saved as "organization" (lowercased).

Subtasks

from u in User, where [type: "Organization"]
|> Repo.update_all(set: [type: "organization"])
joshsmith commented 6 years ago

We should also make the database normalize this to lower.

begedin commented 6 years ago

Only way I can think of to achieve this would be via a trigger. Off the top of my head:

CREATE OR REPLACE FUNCTION lowercase_type() RETURNS trigger AS $lowercase_type$
    BEGIN        
        NEW.foo = LOWER(NEW.foo);
        RETURN NEW;
    END;
$lowercase_type$;

CREATE TRIGGER lowercase_type_trigger BEFORE INSERT OR UPDATE ON users
    FOR EACH ROW EXECUTE PROCEDURE lowercase_type();

This should be a separate migration in addition to the one above.

The up migration should add the function and the trigger, in that order. The down migration should remove the trigger, then the function.

I'm not sure there's a programmatic way to add a trigger/function with ecto, other than using execute/1.

joshsmith commented 6 years ago

I think maybe that I had meant that we can index the type as LOWER() rather than actually coerce the date itself to be lower. My mistake here.