aquametalabs / aquameta

Web development platform built entirely in PostgreSQL
GNU General Public License v3.0
1.1k stars 52 forks source link

meta: inserting a meta.column is losing the id's default value when it's a uuid_generate_v4(), among other things #211

Open erichanson opened 4 years ago

erichanson commented 4 years ago

Checking out schema entities from meta generates a table like this:

aquameta=# \d endpoint.user
                                                  Table "endpoint.user"
     Column      |            Type             | Collation | Nullable |                      Default                      
-----------------+-----------------------------+-----------+----------+---------------------------------------------------
 id              | uuid                        |           | not null | 
 name            | text                        |           | not null | ''::text
 active          | boolean                     |           | not null | false
 email           | text                        |           | not null | 
 role_id         | meta.role_id                |           | not null | ((public.uuid_generate_v4())::text)::meta.role_id
 created_at      | timestamp without time zone |           | not null | (now())::timestamp without time zone
 activation_code | uuid                        |           | not null | public.uuid_generate_v4()
Indexes:
    "user_pkey" PRIMARY KEY, btree (id)
Check constraints:
    "38159_38366_1_not_null" CHECK (id IS NOT NULL)
    "38159_38366_2_not_null" CHECK (role_id IS NOT NULL)
    "38159_38366_3_not_null" CHECK (email IS NOT NULL)
    "38159_38366_4_not_null" CHECK (name IS NOT NULL)
    "38159_38366_5_not_null" CHECK (active IS NOT NULL)
    "38159_38366_6_not_null" CHECK (activation_code IS NOT NULL)
    "38159_38366_7_not_null" CHECK (created_at IS NOT NULL)
Referenced by:
    TABLE "session" CONSTRAINT "session_user_id_fkey" FOREIGN KEY (user_id) REFERENCES "user"(id) ON DELETE CASCADE

Notice how the id column has no default value. It did before it was committed as a meta entity to version control, and then checked back out. Somehow meta is losing the default value for primary keys, foremost, and looks like there are some other anomalies as well. The original schema before being checked into and out of VCS looked like this:

                                               Table "endpoint.user"
     Column      |            Type             | Collation | Nullable |                  Default                   
-----------------+-----------------------------+-----------+----------+--------------------------------------------
 id              | uuid                        |           | not null | uuid_generate_v4()
 role_id         | meta.role_id                |           | not null | ((uuid_generate_v4())::text)::meta.role_id
 email           | text                        |           | not null | 
 name            | text                        |           | not null | ''::text
 active          | boolean                     |           | not null | false
 activation_code | uuid                        |           | not null | uuid_generate_v4()
 created_at      | timestamp without time zone |           | not null | now()
Indexes:
    "user_pkey" PRIMARY KEY, btree (id)
    "user_email_key" UNIQUE CONSTRAINT, btree (email)
Referenced by:
    TABLE "endpoint.session" CONSTRAINT "session_user_id_fkey" FOREIGN KEY (user_id) REFERENCES endpoint."user"(id) ON DELETE CASCADE
Policies (row security disabled):
    POLICY "user:endpoint/user" FOR SELECT
      TO user
      USING (true)
Triggers:
    endpoint_user_delete_trigger BEFORE DELETE ON endpoint."user" FOR EACH ROW EXECUTE PROCEDURE endpoint.user_delete()
    endpoint_user_insert_trigger BEFORE INSERT ON endpoint."user" FOR EACH ROW EXECUTE PROCEDURE endpoint.user_insert()
    endpoint_user_update_trigger BEFORE UPDATE ON endpoint."user" FOR EACH ROW EXECUTE PROCEDURE endpoint.user_update()

Can be reproduced by running the extension-to-bundle.sql script, which takes all the PostgreSQL extensions, checks them into version control, drops the extension, and then tries to check them back out from the bundle, which, crazily, is mostly working other than this.

Track down the anomalies and eliminate them.