From 281b15f63c64555cfcf671ada6fda682cd78432d Mon Sep 17 00:00:00 2001 From: PMA Date: Mon, 12 Feb 2024 17:00:59 +0100 Subject: [PATCH] up sampled_indivuals_taxa and replacing taxons with taxa --- .../2024-01-24-233039_create_samples_table/up.sql | 2 +- .../2024-01-24-233853_create_taxons_table/down.sql | 2 +- .../2024-01-24-233853_create_taxons_table/up.sql | 2 +- .../down.sql | 1 + .../2024-02-12-155504_create_sample_taxa_table/up.sql | 11 +++++++++++ .../down.sql | 1 + .../up.sql | 11 +++++++++++ web/backend/src/models.rs | 2 +- web/backend/src/models/schema.rs | 2 +- web/backend/src/models/taxons.rs | 4 ++-- 10 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 web/backend/migrations/2024-02-12-155504_create_sample_taxa_table/down.sql create mode 100644 web/backend/migrations/2024-02-12-155504_create_sample_taxa_table/up.sql create mode 100644 web/backend/migrations/2024-02-12-155755_create_sampled_individual_taxa_table/down.sql create mode 100644 web/backend/migrations/2024-02-12-155755_create_sampled_individual_taxa_table/up.sql diff --git a/web/backend/migrations/2024-01-24-233039_create_samples_table/up.sql b/web/backend/migrations/2024-01-24-233039_create_samples_table/up.sql index 3c6b75a6..22ee2dba 100644 --- a/web/backend/migrations/2024-01-24-233039_create_samples_table/up.sql +++ b/web/backend/migrations/2024-01-24-233039_create_samples_table/up.sql @@ -1,7 +1,7 @@ -- UP MIGRATION CREATE TABLE samples ( id SERIAL PRIMARY KEY, - taxon_id INTEGER NOT NULL REFERENCES taxons(id) ON DELETE CASCADE, + taxon_id INTEGER NOT NULL REFERENCES taxa(id) ON DELETE CASCADE, sample_type_id INTEGER NOT NULL REFERENCES sample_types(id) ON DELETE CASCADE, derived_from INTEGER REFERENCES samples(id) ON DELETE SET NULL, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, diff --git a/web/backend/migrations/2024-01-24-233853_create_taxons_table/down.sql b/web/backend/migrations/2024-01-24-233853_create_taxons_table/down.sql index 9cda43ae..85f7d1cc 100644 --- a/web/backend/migrations/2024-01-24-233853_create_taxons_table/down.sql +++ b/web/backend/migrations/2024-01-24-233853_create_taxons_table/down.sql @@ -1,2 +1,2 @@ -- This file should undo anything in `up.sql` -DROP TABLE IF EXISTS taxons; +DROP TABLE IF EXISTS taxa; diff --git a/web/backend/migrations/2024-01-24-233853_create_taxons_table/up.sql b/web/backend/migrations/2024-01-24-233853_create_taxons_table/up.sql index a14ff13c..653272d3 100644 --- a/web/backend/migrations/2024-01-24-233853_create_taxons_table/up.sql +++ b/web/backend/migrations/2024-01-24-233853_create_taxons_table/up.sql @@ -1,5 +1,5 @@ -- Your SQL goes here -CREATE TABLE taxons ( +CREATE TABLE taxa ( id SERIAL PRIMARY KEY, name VARCHAR(80) NOT NULL, description VARCHAR(255) NOT NULL, diff --git a/web/backend/migrations/2024-02-12-155504_create_sample_taxa_table/down.sql b/web/backend/migrations/2024-02-12-155504_create_sample_taxa_table/down.sql new file mode 100644 index 00000000..d9a93fe9 --- /dev/null +++ b/web/backend/migrations/2024-02-12-155504_create_sample_taxa_table/down.sql @@ -0,0 +1 @@ +-- This file should undo anything in `up.sql` diff --git a/web/backend/migrations/2024-02-12-155504_create_sample_taxa_table/up.sql b/web/backend/migrations/2024-02-12-155504_create_sample_taxa_table/up.sql new file mode 100644 index 00000000..da211103 --- /dev/null +++ b/web/backend/migrations/2024-02-12-155504_create_sample_taxa_table/up.sql @@ -0,0 +1,11 @@ +-- Your SQL goes here +-- A migration to create the sample_taxa table. +-- This is a N to M relationship between samples and taxa. +-- A sample can be associated to no or more taxa, and a taxon can be found in multiple samples. +-- +CREATE TABLE sample_taxa ( + sample_id INTEGER NOT NULL REFERENCES samples(id) ON DELETE CASCADE, + taxon_id INTEGER NOT NULL REFERENCES taxa(id) ON DELETE CASCADE, + PRIMARY KEY (sample_id, taxon_id) +); + diff --git a/web/backend/migrations/2024-02-12-155755_create_sampled_individual_taxa_table/down.sql b/web/backend/migrations/2024-02-12-155755_create_sampled_individual_taxa_table/down.sql new file mode 100644 index 00000000..d9a93fe9 --- /dev/null +++ b/web/backend/migrations/2024-02-12-155755_create_sampled_individual_taxa_table/down.sql @@ -0,0 +1 @@ +-- This file should undo anything in `up.sql` diff --git a/web/backend/migrations/2024-02-12-155755_create_sampled_individual_taxa_table/up.sql b/web/backend/migrations/2024-02-12-155755_create_sampled_individual_taxa_table/up.sql new file mode 100644 index 00000000..d50a9d43 --- /dev/null +++ b/web/backend/migrations/2024-02-12-155755_create_sampled_individual_taxa_table/up.sql @@ -0,0 +1,11 @@ +-- Your SQL goes here +-- A migration to create the sampled_individual_taxa table. +-- This is a N to M relationship between sampled_individuals and taxa. +-- An individual can be associated to no or more taxa, and a taxon can be found in multiple individuals. +-- +CREATE TABLE sampled_individual_taxa ( + sampled_individual_id INTEGER NOT NULL REFERENCES sampled_individuals(id) ON DELETE CASCADE, + taxon_id INTEGER NOT NULL REFERENCES taxa(id) ON DELETE CASCADE, + PRIMARY KEY (sampled_individual_id, taxon_id) +); +``` diff --git a/web/backend/src/models.rs b/web/backend/src/models.rs index 69cc0f4b..db5112ff 100644 --- a/web/backend/src/models.rs +++ b/web/backend/src/models.rs @@ -6,7 +6,7 @@ mod spectra; mod spectra_collection; mod task_type; mod tasks; -mod taxons; +mod taxa; mod users; // models.rs diff --git a/web/backend/src/models/schema.rs b/web/backend/src/models/schema.rs index 87df2393..91ed67ff 100644 --- a/web/backend/src/models/schema.rs +++ b/web/backend/src/models/schema.rs @@ -29,7 +29,7 @@ diesel::table! { } diesel::table! { - taxons (id) { + taxa (id) { id -> Int4, name -> Varchar, description -> Text, diff --git a/web/backend/src/models/taxons.rs b/web/backend/src/models/taxons.rs index 96a0a328..d909cd0e 100644 --- a/web/backend/src/models/taxons.rs +++ b/web/backend/src/models/taxons.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; // Define the Taxon model #[derive(Debug, Serialize, Deserialize, Queryable, Identifiable)] -#[diesel(table_name = taxons)] +#[diesel(table_name = taxa)] pub struct Taxon { pub id: i32, pub name: String, @@ -16,7 +16,7 @@ pub struct Taxon { // Define the Taxon model for insert #[derive(Debug, Insertable)] -#[diesel(table_name = taxons)] +#[diesel(table_name = taxa)] pub struct NewTaxon<'a> { pub name: &'a str, pub description: &'a str,