Skip to content

Commit

Permalink
add first batch of v2 schema scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
larrybabb committed Nov 19, 2024
1 parent 2b86e7d commit 3b4aadd
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
4 changes: 2 additions & 2 deletions scripts/general/all_schemas-func.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ AS (
FROM INFORMATION_SCHEMA.SCHEMATA iss
WHERE
(
REGEXP_CONTAINS(iss.schema_name, r'^clinvar_\d{4}_\d{2}_\d{2}_v\d+_\d+_\d+$')
REGEXP_CONTAINS(iss.schema_name, r'^clinvar_\d{4}_\d{2}_\d{2}_v1_\d+_\d+$')
OR
REGEXP_CONTAINS(iss.schema_name, r'^clinvar_\d{4}_\d{2}_\d{2}_v\d+_\d+_\d+_beta\d+$')
REGEXP_CONTAINS(iss.schema_name, r'^clinvar_\d{4}_\d{2}_\d{2}_v1_\d+_\d+_beta\d+$')
)
AND
iss.schema_name <> "clinvar_2019_06_01_v0"
Expand Down
23 changes: 23 additions & 0 deletions scripts/general/all_schemas-v2-func.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
CREATE OR REPLACE TABLE FUNCTION `clinvar_ingest.all_schemas_v2`()
AS (
-- the state of al clinvar schemas available at the moment
SELECT
r.schema_name,
r.release_date,
LAG(r.release_date, 1, DATE('0001-01-01')) OVER (ORDER BY r.release_date ASC) AS prev_release_date,
LEAD(r.release_date, 1, DATE('9999-12-31')) OVER (ORDER BY r.release_date ASC) AS next_release_date
FROM (

SELECT
iss.schema_name,
CAST(REGEXP_REPLACE(iss.schema_name, r'clinvar_(\d{4})_(\d{2})_(\d{2}).*', '\\1-\\2-\\3') as DATE) AS release_date
FROM INFORMATION_SCHEMA.SCHEMATA iss
WHERE
(
REGEXP_CONTAINS(iss.schema_name, r'^clinvar_\d{4}_\d{2}_\d{2}_v2_\d+_\d+$')
OR
REGEXP_CONTAINS(iss.schema_name, r'^clinvar_\d{4}_\d{2}_\d{2}_v2_\d+_\d+_(alpha|beta)\d*$')
)
) r
ORDER BY 2
);
15 changes: 15 additions & 0 deletions scripts/general/schema-on-v2-func.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE OR REPLACE TABLE FUNCTION `clinvar_ingest.schema_on_v2`(on_date DATE)
AS (
-- the state of schemas available on a certain date
-- if the date lands on a schema release date then that will be the schema
-- otherwise the schema with the release date just prior to that date will be the schema
SELECT
x.schema_name,
x.release_date,
x.prev_release_date,
x.next_release_date
FROM `clinvar_ingest.all_schemas_v2`() x
WHERE on_date >= x.release_date
ORDER BY 2 DESC
LIMIT 1
);
17 changes: 17 additions & 0 deletions scripts/general/schemas-on-or-after-v2-func.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE OR REPLACE TABLE FUNCTION `clinvar_ingest.schemas_on_or_after_v2`(on_or_after_date DATE)
AS (
-- the state of schemas available on or after a certain date
-- if the date lands on a schema release date then that will be the first schema
-- if the date is prior to the earliest release date then return all schemas
-- otherwise the schema with the release date just prior to that date will be the first schema
SELECT
x.schema_name,
x.release_date,
x.prev_release_date,
x.next_release_date
FROM `clinvar_ingest.all_schemas_v2`() x
WHERE (on_or_after_date > x.prev_release_date AND on_or_after_date < x.next_release_date) OR on_or_after_date <= x.release_date
ORDER BY 2
);

-- select * from `clinvar_ingest.schemas_on_or_after`(DATE('2020-06-01'));
16 changes: 16 additions & 0 deletions scripts/general/schemas-on-or-before-v2-func.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE OR REPLACE TABLE FUNCTION `clinvar_ingest.schemas_on_or_before_v2`(on_or_before_date DATE)
AS (
-- the state of schemas available on or before a certain date
-- if the date lands on a schema release date then that will be the last schema
-- otherwise the schema with the release date just prior to that date will be the last schema
SELECT
x.schema_name,
x.release_date,
x.prev_release_date,
x.next_release_date
FROM `clinvar_ingest.all_schemas_v2`() x
WHERE on_or_before_date >= x.release_date
ORDER BY 2
);

-- select * from `clinvar_ingest.schemas_on_or_before`(DATE('2020-06-01'));

0 comments on commit 3b4aadd

Please sign in to comment.