diff options
| -rw-r--r-- | rust/README.cockroach.md | 24 | ||||
| -rw-r--r-- | rust/migrations/00000000000000_diesel_initial_setup/down.sql | 5 | ||||
| -rw-r--r-- | rust/migrations/00000000000000_diesel_initial_setup/up.sql | 36 | ||||
| -rw-r--r-- | rust/migrations/2018-05-12-001226_init/up.sql | 87 | ||||
| -rw-r--r-- | rust/src/database_schema.rs | 26 | ||||
| -rw-r--r-- | rust/src/lib.rs | 6 | 
6 files changed, 105 insertions, 79 deletions
diff --git a/rust/README.cockroach.md b/rust/README.cockroach.md new file mode 100644 index 00000000..e42d8c4b --- /dev/null +++ b/rust/README.cockroach.md @@ -0,0 +1,24 @@ + +Setup a user and database on a local, single-host node: + +    # using v2.0.5 (2018/08/13) +    cockroach start --insecure --store=fatcat-dev --host=localhost +    cockroach user set maxroach --insecure +    cockroach sql --insecure -e 'CREATE DATABASE fatcat' +    cockroach sql --insecure -e 'GRANT ALL ON DATABASE fatcat TO maxroach' +    cockroach sql --insecure -e 'CREATE DATABASE fatcat_test' +    cockroach sql --insecure -e 'GRANT ALL ON DATABASE fatcat_test TO maxroach' + +Create a .env file: + +    DATABASE_URL=postgres://maxroach@localhost:26257/fatcat +    TEST_DATABASE_URL=postgres://maxroach@localhost:26257/fatcat_test +    RUST_TEST_THREADS=1 + +Create: + +    cockroach sql --insecure -d fatcat < migrations/2018-05-12-001226_init/down.sql +    cockroach sql --insecure -d fatcat < migrations/2018-05-12-001226_init/up.sql + +    cockroach sql --insecure -d fatcat_test < migrations/2018-05-12-001226_init/down.sql +    cockroach sql --insecure -d fatcat_test < migrations/2018-05-12-001226_init/up.sql diff --git a/rust/migrations/00000000000000_diesel_initial_setup/down.sql b/rust/migrations/00000000000000_diesel_initial_setup/down.sql index a9f52609..adfa4902 100644 --- a/rust/migrations/00000000000000_diesel_initial_setup/down.sql +++ b/rust/migrations/00000000000000_diesel_initial_setup/down.sql @@ -2,5 +2,6 @@  -- and other internal bookkeeping. This file is safe to edit, any future  -- changes will be added to existing projects as new migrations. -DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); -DROP FUNCTION IF EXISTS diesel_set_updated_at(); +SELECT 1; +-- DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); +-- DROP FUNCTION IF EXISTS diesel_set_updated_at(); diff --git a/rust/migrations/00000000000000_diesel_initial_setup/up.sql b/rust/migrations/00000000000000_diesel_initial_setup/up.sql index 3400c7c5..f89156f7 100644 --- a/rust/migrations/00000000000000_diesel_initial_setup/up.sql +++ b/rust/migrations/00000000000000_diesel_initial_setup/up.sql @@ -16,22 +16,24 @@  --  -- SELECT diesel_manage_updated_at('users');  -- ``` -CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ -BEGIN -    EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s -                    FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); -END; -$$ LANGUAGE plpgsql; -CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ -BEGIN -    IF ( -        NEW IS DISTINCT FROM OLD AND -        NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at -    ) THEN -        NEW.updated_at := current_timestamp; -    END IF; -    RETURN NEW; -END; -$$ LANGUAGE plpgsql; +--  CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ +--  BEGIN +--      EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s +--                      FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); +--  END; +--  $$ LANGUAGE plpgsql; +--  CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ +--  BEGIN +--      IF ( +--          NEW IS DISTINCT FROM OLD AND +--          NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at +--      ) THEN +--          NEW.updated_at := current_timestamp; +--      END IF; +--      RETURN NEW; +--  END; +--  $$ LANGUAGE plpgsql; + +SELECT 1 diff --git a/rust/migrations/2018-05-12-001226_init/up.sql b/rust/migrations/2018-05-12-001226_init/up.sql index b0733576..6b0e9c6e 100644 --- a/rust/migrations/2018-05-12-001226_init/up.sql +++ b/rust/migrations/2018-05-12-001226_init/up.sql @@ -1,26 +1,28 @@  -- written for Postgres 9.6 with OSSP extension for UUIDs -- ... but actually runs on Postgres 10 in qa/production -CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; +-- CREATE EXTENSION IF NOT EXISTS "uuid-ossp";  -- uuid_generate_v1mc: timestamp ordered, random MAC address --- uuid_generate_v4:   totally random +-- gen_random_uuid:   totally random  -- NB: could use LIKE clause, or "composite types"  CREATE TABLE editor ( -    id                  UUID PRIMARY KEY DEFAULT uuid_generate_v4(), +    id                  UUID PRIMARY KEY DEFAULT gen_random_uuid(),      username            TEXT NOT NULL UNIQUE,      is_admin            BOOLEAN NOT NULL DEFAULT false,      registered          TIMESTAMP WITHOUT TIME ZONE DEFAULT now() NOT NULL,      active_editgroup_id UUID -- REFERENCES( editgroup(id) via ALTER below  ); +CREATE INDEX active_editgroup_idx ON editor(active_editgroup_id); +  CREATE TABLE editgroup ( -    id                  UUID PRIMARY KEY DEFAULT uuid_generate_v4(), +    id                  UUID PRIMARY KEY DEFAULT gen_random_uuid(),      editor_id           UUID REFERENCES editor(id) NOT NULL,      created             TIMESTAMP WITHOUT TIME ZONE DEFAULT now() NOT NULL, -    extra_json          JSON, +    extra_json          JSONB,      description         TEXT  ); @@ -44,8 +46,8 @@ CREATE TABLE abstracts (  -------------------- Creators -----------------------------------------------  CREATE TABLE creator_rev ( -    id                  UUID PRIMARY KEY DEFAULT uuid_generate_v4(), -    extra_json          JSON, +    id                  UUID PRIMARY KEY DEFAULT gen_random_uuid(), +    extra_json          JSONB,      display_name        TEXT NOT NULL,      given_name          TEXT, @@ -59,11 +61,11 @@ CREATE TABLE creator_rev (  -- Could denormalize a "is_live" flag into revision tables, to make indices  -- more efficient -CREATE INDEX creator_rev_orcid_idx ON creator_rev(orcid) WHERE orcid IS NOT NULL; -CREATE INDEX creator_rev_wikidata_idx ON creator_rev(wikidata_qid) WHERE wikidata_qid IS NOT NULL; +CREATE INDEX creator_rev_orcid_idx ON creator_rev(orcid); -- WHERE orcid IS NOT NULL; +CREATE INDEX creator_rev_wikidata_idx ON creator_rev(wikidata_qid); -- WHERE wikidata_qid IS NOT NULL;  CREATE TABLE creator_ident ( -    id                  UUID PRIMARY KEY DEFAULT uuid_generate_v4(), +    id                  UUID PRIMARY KEY DEFAULT gen_random_uuid(),      is_live             BOOL NOT NULL DEFAULT false,      rev_id              UUID REFERENCES creator_rev(id),      redirect_id         UUID REFERENCES creator_ident(id) @@ -79,15 +81,15 @@ CREATE TABLE creator_edit (      rev_id              UUID REFERENCES creator_rev(id),      redirect_id         UUID REFERENCES creator_ident(id),      prev_rev            UUID REFERENCES creator_rev(id), -    extra_json          JSON +    extra_json          JSONB  );  CREATE INDEX creator_edit_idx ON creator_edit(editgroup_id);  -------------------- Containers --------------------------------------------  CREATE TABLE container_rev ( -    id                  UUID PRIMARY KEY DEFAULT uuid_generate_v4(), -    extra_json          JSON, +    id                  UUID PRIMARY KEY DEFAULT gen_random_uuid(), +    extra_json          JSONB,      name                TEXT NOT NULL,      publisher           TEXT, @@ -97,11 +99,11 @@ CREATE TABLE container_rev (      coden               TEXT  ); -CREATE INDEX container_rev_issnl_idx ON container_rev(issnl) WHERE issnl IS NOT NULL; -CREATE INDEX container_rev_wikidata_idx ON container_rev(wikidata_qid) WHERE wikidata_qid IS NOT NULL; +CREATE INDEX container_rev_issnl_idx ON container_rev(issnl); -- WHERE issnl IS NOT NULL; +CREATE INDEX container_rev_wikidata_idx ON container_rev(wikidata_qid); -- WHERE wikidata_qid IS NOT NULL;  CREATE TABLE container_ident ( -    id                  UUID PRIMARY KEY DEFAULT uuid_generate_v4(), +    id                  UUID PRIMARY KEY DEFAULT gen_random_uuid(),      is_live             BOOL NOT NULL DEFAULT false,      rev_id              UUID REFERENCES container_rev(id),      redirect_id         UUID REFERENCES container_ident(id) @@ -117,15 +119,15 @@ CREATE TABLE container_edit (      rev_id              UUID REFERENCES container_rev(id),      redirect_id         UUID REFERENCES container_ident(id),      prev_rev            UUID REFERENCES container_rev(id), -    extra_json          JSON +    extra_json          JSONB  );  CREATE INDEX container_edit_idx ON container_edit(editgroup_id);  -------------------- Files -------------------------------------------------  CREATE TABLE file_rev ( -    id                  UUID PRIMARY KEY DEFAULT uuid_generate_v4(), -    extra_json          JSON, +    id                  UUID PRIMARY KEY DEFAULT gen_random_uuid(), +    extra_json          JSONB,      size                BIGINT,      sha1                CHAR(40), @@ -134,9 +136,9 @@ CREATE TABLE file_rev (      mimetype            TEXT  ); -CREATE INDEX file_rev_sha1_idx ON file_rev(sha1) WHERE sha1 IS NOT NULL; -CREATE INDEX file_rev_md5_idx ON file_rev(md5) WHERE md5 IS NOT NULL; -CREATE INDEX file_rev_sha256_idx ON file_rev(sha256) WHERE sha256 IS NOT NULL; +CREATE INDEX file_rev_sha1_idx ON file_rev(sha1); -- WHERE sha1 IS NOT NULL; +CREATE INDEX file_rev_md5_idx ON file_rev(md5); -- WHERE md5 IS NOT NULL; +CREATE INDEX file_rev_sha256_idx ON file_rev(sha256); -- WHERE sha256 IS NOT NULL;  CREATE TABLE file_rev_url (      id                  BIGSERIAL PRIMARY KEY, @@ -148,7 +150,7 @@ CREATE TABLE file_rev_url (  CREATE INDEX file_rev_url_rev_idx ON file_rev_url(file_rev);  CREATE TABLE file_ident ( -    id                  UUID PRIMARY KEY DEFAULT uuid_generate_v4(), +    id                  UUID PRIMARY KEY DEFAULT gen_random_uuid(),      is_live             BOOL NOT NULL DEFAULT false,      rev_id              UUID REFERENCES file_rev(id),      redirect_id         UUID REFERENCES file_ident(id) @@ -164,15 +166,15 @@ CREATE TABLE file_edit (      rev_id              UUID REFERENCES file_rev(id),      redirect_id         UUID REFERENCES file_ident(id),      prev_rev            UUID REFERENCES file_rev(id), -    extra_json          JSON +    extra_json          JSONB  );  CREATE INDEX file_edit_idx ON file_edit(editgroup_id);  -------------------- Release -----------------------------------------------  CREATE TABLE release_rev ( -    id                  UUID PRIMARY KEY DEFAULT uuid_generate_v4(), -    extra_json          JSON, +    id                  UUID PRIMARY KEY DEFAULT gen_random_uuid(), +    extra_json          JSONB,      work_ident_id       UUID NOT NULL, -- FOREIGN KEY; see ALRTER below      container_ident_id  UUID REFERENCES container_ident(id), @@ -195,13 +197,13 @@ CREATE TABLE release_rev (      -- TODO: identifier table?  ); -CREATE INDEX release_rev_doi_idx ON release_rev(doi) WHERE doi IS NOT NULL; -CREATE INDEX release_rev_pmid_idx ON release_rev(pmid) WHERE pmid IS NOT NULL; -CREATE INDEX release_rev_pmcid_idx ON release_rev(pmcid) WHERE pmcid IS NOT NULL; -CREATE INDEX release_rev_wikidata_idx ON release_rev(wikidata_qid) WHERE wikidata_qid IS NOT NULL; -CREATE INDEX release_rev_isbn13_idx ON release_rev(isbn13) WHERE isbn13 IS NOT NULL; -CREATE INDEX release_rev_core_idx ON release_rev(core_id) WHERE core_id IS NOT NULL; -CREATE INDEX release_rev_work_idx ON release_rev(work_ident_id) WHERE work_ident_id IS NOT NULL; +CREATE INDEX release_rev_doi_idx ON release_rev(doi); -- WHERE doi IS NOT NULL; +CREATE INDEX release_rev_pmid_idx ON release_rev(pmid); -- WHERE pmid IS NOT NULL; +CREATE INDEX release_rev_pmcid_idx ON release_rev(pmcid); -- WHERE pmcid IS NOT NULL; +CREATE INDEX release_rev_wikidata_idx ON release_rev(wikidata_qid); -- WHERE wikidata_qid IS NOT NULL; +CREATE INDEX release_rev_isbn13_idx ON release_rev(isbn13); -- WHERE isbn13 IS NOT NULL; +CREATE INDEX release_rev_core_idx ON release_rev(core_id); -- WHERE core_id IS NOT NULL; +CREATE INDEX release_rev_work_idx ON release_rev(work_ident_id); -- WHERE work_ident_id IS NOT NULL;  CREATE TABLE release_rev_abstract (      id              BIGSERIAL PRIMARY KEY, @@ -211,11 +213,8 @@ CREATE TABLE release_rev_abstract (      lang            TEXT  ); -CREATE INDEX release_rev_abstract_rev_idx ON release_rev_abstract(release_rev); -CREATE INDEX release_rev_abstract_sha1_idx ON release_rev_abstract(abstract_sha1); -  CREATE TABLE release_ident ( -    id                  UUID PRIMARY KEY DEFAULT uuid_generate_v4(), +    id                  UUID PRIMARY KEY DEFAULT gen_random_uuid(),      is_live             BOOL NOT NULL DEFAULT false,      rev_id              UUID REFERENCES release_rev(id),      redirect_id         UUID REFERENCES release_ident(id) @@ -231,19 +230,19 @@ CREATE TABLE release_edit (      rev_id              UUID REFERENCES release_rev(id),      redirect_id         UUID REFERENCES release_ident(id),      prev_rev            UUID REFERENCES release_rev(id), -    extra_json          JSON +    extra_json          JSONB  );  CREATE INDEX release_edit_idx ON release_edit(editgroup_id);  -------------------- Works --------------------------------------------------  CREATE TABLE work_rev ( -    id                  UUID PRIMARY KEY DEFAULT uuid_generate_v4(), -    extra_json          JSON +    id                  UUID PRIMARY KEY DEFAULT gen_random_uuid(), +    extra_json          JSONB  );  CREATE TABLE work_ident ( -    id                  UUID PRIMARY KEY DEFAULT uuid_generate_v4(), +    id                  UUID PRIMARY KEY DEFAULT gen_random_uuid(),      is_live             BOOL NOT NULL DEFAULT false,      rev_id              UUID REFERENCES work_rev(id),      redirect_id         UUID REFERENCES work_ident(id) @@ -259,7 +258,7 @@ CREATE TABLE work_edit (      rev_id              UUID REFERENCES work_rev(id),      redirect_id         UUID REFERENCES work_ident(id),      prev_rev            UUID REFERENCES work_rev(id), -    extra_json          JSON +    extra_json          JSONB  );  CREATE INDEX work_edit_idx ON work_edit(editgroup_id); @@ -277,7 +276,7 @@ CREATE TABLE release_contrib (      raw_name            TEXT,      role                TEXT, -- TODO: enum?      index_val           BIGINT, -    extra_json          JSON +    extra_json          JSONB  );  CREATE INDEX release_contrib_rev_idx ON release_contrib(release_rev); @@ -289,7 +288,7 @@ CREATE TABLE release_ref (      target_release_ident_id UUID REFERENCES release_ident(id), -- or work?      index_val               BIGINT,      key                     TEXT, -    extra_json              JSON, -- title, year, container_title, locator (aka, page), oci_id +    extra_json              JSONB, -- title, year, container_title, locator (aka, page), oci_id      container_title         TEXT,      year                    BIGINT,      title                   TEXT, diff --git a/rust/src/database_schema.rs b/rust/src/database_schema.rs index a6605d81..bc3f6c3a 100644 --- a/rust/src/database_schema.rs +++ b/rust/src/database_schema.rs @@ -22,7 +22,7 @@ table! {          rev_id -> Nullable<Uuid>,          redirect_id -> Nullable<Uuid>,          prev_rev -> Nullable<Uuid>, -        extra_json -> Nullable<Json>, +        extra_json -> Nullable<Jsonb>,      }  } @@ -38,7 +38,7 @@ table! {  table! {      container_rev (id) {          id -> Uuid, -        extra_json -> Nullable<Json>, +        extra_json -> Nullable<Jsonb>,          name -> Text,          publisher -> Nullable<Text>,          issnl -> Nullable<Bpchar>, @@ -57,7 +57,7 @@ table! {          rev_id -> Nullable<Uuid>,          redirect_id -> Nullable<Uuid>,          prev_rev -> Nullable<Uuid>, -        extra_json -> Nullable<Json>, +        extra_json -> Nullable<Jsonb>,      }  } @@ -73,7 +73,7 @@ table! {  table! {      creator_rev (id) {          id -> Uuid, -        extra_json -> Nullable<Json>, +        extra_json -> Nullable<Jsonb>,          display_name -> Text,          given_name -> Nullable<Text>,          surname -> Nullable<Text>, @@ -87,7 +87,7 @@ table! {          id -> Uuid,          editor_id -> Uuid,          created -> Timestamp, -        extra_json -> Nullable<Json>, +        extra_json -> Nullable<Jsonb>,          description -> Nullable<Text>,      }  } @@ -111,7 +111,7 @@ table! {          rev_id -> Nullable<Uuid>,          redirect_id -> Nullable<Uuid>,          prev_rev -> Nullable<Uuid>, -        extra_json -> Nullable<Json>, +        extra_json -> Nullable<Jsonb>,      }  } @@ -134,7 +134,7 @@ table! {  table! {      file_rev (id) {          id -> Uuid, -        extra_json -> Nullable<Json>, +        extra_json -> Nullable<Jsonb>,          size -> Nullable<Int8>,          sha1 -> Nullable<Bpchar>,          sha256 -> Nullable<Bpchar>, @@ -160,7 +160,7 @@ table! {          raw_name -> Nullable<Text>,          role -> Nullable<Text>,          index_val -> Nullable<Int8>, -        extra_json -> Nullable<Json>, +        extra_json -> Nullable<Jsonb>,      }  } @@ -173,7 +173,7 @@ table! {          rev_id -> Nullable<Uuid>,          redirect_id -> Nullable<Uuid>,          prev_rev -> Nullable<Uuid>, -        extra_json -> Nullable<Json>, +        extra_json -> Nullable<Jsonb>,      }  } @@ -193,7 +193,7 @@ table! {          target_release_ident_id -> Nullable<Uuid>,          index_val -> Nullable<Int8>,          key -> Nullable<Text>, -        extra_json -> Nullable<Json>, +        extra_json -> Nullable<Jsonb>,          container_title -> Nullable<Text>,          year -> Nullable<Int8>,          title -> Nullable<Text>, @@ -204,7 +204,7 @@ table! {  table! {      release_rev (id) {          id -> Uuid, -        extra_json -> Nullable<Json>, +        extra_json -> Nullable<Jsonb>,          work_ident_id -> Uuid,          container_ident_id -> Nullable<Uuid>,          title -> Text, @@ -244,7 +244,7 @@ table! {          rev_id -> Nullable<Uuid>,          redirect_id -> Nullable<Uuid>,          prev_rev -> Nullable<Uuid>, -        extra_json -> Nullable<Json>, +        extra_json -> Nullable<Jsonb>,      }  } @@ -260,7 +260,7 @@ table! {  table! {      work_rev (id) {          id -> Uuid, -        extra_json -> Nullable<Json>, +        extra_json -> Nullable<Jsonb>,      }  } diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 38f9b343..2236d602 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -103,9 +103,9 @@ pub fn test_server() -> Result<api_server::Server> {      let conn = server.db_pool.get().expect("db_pool error");      // run migrations; revert latest (dummy data); re-run latest -    diesel_migrations::run_pending_migrations(&conn).unwrap(); -    diesel_migrations::revert_latest_migration(&conn).unwrap(); -    diesel_migrations::run_pending_migrations(&conn).unwrap(); +    //diesel_migrations::run_pending_migrations(&conn).unwrap(); +    //diesel_migrations::revert_latest_migration(&conn).unwrap(); +    //diesel_migrations::run_pending_migrations(&conn).unwrap();      Ok(server)  }  | 
