diff options
Diffstat (limited to 'rust')
-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/down.sql | 2 | ||||
-rw-r--r-- | rust/migrations/2018-05-12-001226_init/up.sql | 63 | ||||
-rw-r--r-- | rust/src/api_entity_crud.rs | 20 | ||||
-rw-r--r-- | rust/src/database_schema.rs | 26 | ||||
-rw-r--r-- | rust/src/lib.rs | 6 | ||||
-rw-r--r-- | rust/tests/test_api_server.rs | 2 |
9 files changed, 107 insertions, 77 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/down.sql b/rust/migrations/2018-05-12-001226_init/down.sql index 3f796db6..7667d122 100644 --- a/rust/migrations/2018-05-12-001226_init/down.sql +++ b/rust/migrations/2018-05-12-001226_init/down.sql @@ -31,3 +31,5 @@ DROP TABLE IF EXISTS abstracts CASCADE; DROP TABLE IF EXISTS editor CASCADE; DROP TABLE IF EXISTS editgroup CASCADE; DROP TABLE IF EXISTS changelog CASCADE; + +DROP SEQUENCE IF EXISTS changelog_seq CASCADE; diff --git a/rust/migrations/2018-05-12-001226_init/up.sql b/rust/migrations/2018-05-12-001226_init/up.sql index c21e5b5e..bb62ba75 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 ); @@ -28,8 +30,10 @@ ALTER TABLE editor ADD CONSTRAINT editor_editgroupid_fkey FOREIGN KEY (active_editgroup_id) REFERENCES editgroup(id); +CREATE SEQUENCE changelog_seq START 1 INCREMENT 1; + CREATE TABLE changelog ( - id BIGSERIAL PRIMARY KEY, + id BIGINT PRIMARY KEY DEFAULT nextval('changelog_seq'), editgroup_id UUID REFERENCES editgroup(id) NOT NULL, timestamp TIMESTAMP WITHOUT TIME ZONE DEFAULT now() NOT NULL ); @@ -44,8 +48,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, @@ -63,7 +67,7 @@ CREATE INDEX creator_rev_orcid_idx ON creator_rev(orcid); CREATE INDEX creator_rev_wikidata_idx ON creator_rev(wikidata_qid); 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 +83,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, @@ -101,7 +105,7 @@ CREATE INDEX container_rev_issnl_idx ON container_rev(issnl); CREATE INDEX container_rev_wikidata_idx ON container_rev(wikidata_qid); 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 +121,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), @@ -148,7 +152,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 +168,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), @@ -211,11 +215,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 +232,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 +260,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 +278,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 +290,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/api_entity_crud.rs b/rust/src/api_entity_crud.rs index dd0961d5..2d5ea93d 100644 --- a/rust/src/api_entity_crud.rs +++ b/rust/src/api_entity_crud.rs @@ -391,8 +391,8 @@ impl EntityCrud for ContainerEntity { generic_db_update!(container_ident, container_edit); generic_db_delete!(container_ident, container_edit); generic_db_get_history!(container_edit); - generic_db_accept_edits_batch!("container"); - //generic_db_accept_edits_each!(container_ident, container_edit); + //generic_db_accept_edits_batch!("container"); + generic_db_accept_edits_each!(container_ident, container_edit); generic_db_insert_rev!(); fn db_from_row( @@ -472,8 +472,8 @@ impl EntityCrud for CreatorEntity { generic_db_update!(creator_ident, creator_edit); generic_db_delete!(creator_ident, creator_edit); generic_db_get_history!(creator_edit); - generic_db_accept_edits_batch!("creator"); - //generic_db_accept_edits_each!(creator_ident, creator_edit); + //generic_db_accept_edits_batch!("creator"); + generic_db_accept_edits_each!(creator_ident, creator_edit); generic_db_insert_rev!(); fn db_from_row( @@ -550,8 +550,8 @@ impl EntityCrud for FileEntity { generic_db_update!(file_ident, file_edit); generic_db_delete!(file_ident, file_edit); generic_db_get_history!(file_edit); - generic_db_accept_edits_batch!("file"); - //generic_db_accept_edits_each!(file_ident, file_edit); + //generic_db_accept_edits_batch!("file"); + generic_db_accept_edits_each!(file_ident, file_edit); generic_db_insert_rev!(); fn db_from_row( @@ -688,8 +688,8 @@ impl EntityCrud for ReleaseEntity { generic_db_update!(release_ident, release_edit); generic_db_delete!(release_ident, release_edit); generic_db_get_history!(release_edit); - generic_db_accept_edits_batch!("release"); - //generic_db_accept_edits_each!(release_ident, release_edit); + //generic_db_accept_edits_batch!("release"); + generic_db_accept_edits_each!(release_ident, release_edit); generic_db_insert_rev!(); fn db_create(&self, conn: &DbConn, edit_context: &EditContext) -> Result<Self::EditRow> { @@ -1062,8 +1062,8 @@ impl EntityCrud for WorkEntity { generic_db_update!(work_ident, work_edit); generic_db_delete!(work_ident, work_edit); generic_db_get_history!(work_edit); - generic_db_accept_edits_batch!("work"); - //generic_db_accept_edits_each!(work_ident, work_edit); + //generic_db_accept_edits_batch!("work"); + generic_db_accept_edits_each!(work_ident, work_edit); generic_db_insert_rev!(); fn db_from_row( 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 57cc535c..b4b9e3c7 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) } diff --git a/rust/tests/test_api_server.rs b/rust/tests/test_api_server.rs index 54639228..d10251f0 100644 --- a/rust/tests/test_api_server.rs +++ b/rust/tests/test_api_server.rs @@ -950,7 +950,7 @@ fn test_contribs() { "http://localhost:9411/v0/release", headers.clone(), r#"{"title": "some paper", - "doi": "10.1234/iiiiiii", + "doi": "10.1234/kkkkkkk", "contribs": [{ "index": 1, "raw_name": "textual description of contributor (aka, name)", |