From 97c493131b48a9db6c7c3c50788656abe87f054a Mon Sep 17 00:00:00 2001 From: Jian <52763034+tianj7@users.noreply.github.com> Date: Wed, 2 Oct 2024 13:51:49 -0500 Subject: [PATCH] Add new schema_version endpoint (#116) * add new schema_version endpoint * update sorting --- controllers/version.go | 5 ++ models/source.go | 5 ++ models/version.go | 75 ++++++++++++++++++- server/router.go | 2 + tests/models_tests/models_test.go | 7 ++ tests/setup_local_db/ddl_atlas.sql | 16 ++++ tests/setup_local_db/ddl_results_and_cdm.sql | 11 +++ tests/setup_local_db/test_data_atlas.sql | 7 ++ .../test_data_results_and_cdm.sql | 5 ++ 9 files changed, 132 insertions(+), 1 deletion(-) diff --git a/controllers/version.go b/controllers/version.go index 4ee0bd91..5b5a85ef 100644 --- a/controllers/version.go +++ b/controllers/version.go @@ -15,3 +15,8 @@ func (u VersionController) Retrieve(c *gin.Context) { version := versionModel.GetVersion() c.JSON(http.StatusOK, gin.H{"version": version}) } + +func (u VersionController) RetrieveSchemaVersion(c *gin.Context) { + version := versionModel.GetSchemaVersion() + c.JSON(http.StatusOK, gin.H{"version": version}) +} diff --git a/models/source.go b/models/source.go index 04a71cd8..33c70aca 100644 --- a/models/source.go +++ b/models/source.go @@ -48,6 +48,10 @@ func (h Source) GetSourceSchemaNameBySourceIdAndSourceType(id int, sourceType So return &SourceSchema{SchemaName: "MISC"}, nil } + if sourceType == Dbo { + return &SourceSchema{SchemaName: "DBO"}, nil + } + // otherwise, get the schema name from source_daimon table atlasDb := db.GetAtlasDB() db2 := atlasDb.Db @@ -70,6 +74,7 @@ const ( Results SourceType = 2 Temp SourceType = 5 Misc SourceType = 6 + Dbo SourceType = 7 ) // Get the data source details for given source id and source type. diff --git a/models/version.go b/models/version.go index d70b99e2..b376026c 100644 --- a/models/version.go +++ b/models/version.go @@ -1,12 +1,85 @@ package models -import "github.com/uc-cdis/cohort-middleware/version" +import ( + "time" + + "github.com/uc-cdis/cohort-middleware/db" + "github.com/uc-cdis/cohort-middleware/utils" + "github.com/uc-cdis/cohort-middleware/version" +) type Version struct { GitCommit string GitVersion string } +type DbSchemaVersion struct { + AtlasSchemaVersion string + DataSchemaVersion int +} + +type SchemaVersion struct { + InstalledRank int + Version string + Description string + Type string + Script string + Checksum int + InstalledBy string + InstalledOn time.Time + ExecutionTime int + Success bool +} + +type VersionInfo struct { + Version int + AppliedOn time.Time + Description string +} + func (h Version) GetVersion() *Version { return &Version{GitCommit: version.GitCommit, GitVersion: version.GitVersion} } + +func (h Version) GetSchemaVersion() *DbSchemaVersion { + dbSchemaVersion := &DbSchemaVersion{"error", -1} + + atlasDb := db.GetAtlasDB().Db + var atlasSchemaVersion *SchemaVersion + query := atlasDb.Model(&SchemaVersion{}). + Limit(1). + Select("version"). + Order("installed_rank desc") + + query, cancel := utils.AddTimeoutToQuery(query) + defer cancel() + meta_result := query.Scan(&atlasSchemaVersion) + if meta_result.Error == nil { + dbSchemaVersion.AtlasSchemaVersion = atlasSchemaVersion.Version + } + + var source = new(Source) + sources, _ := source.GetAllSources() + if len(sources) < 1 { + panic("Error: No data source found") + } else if len(sources) > 1 { + panic("More than one data source! Exiting") + } + var dataSourceModel = new(Source) + dboDataSource := dataSourceModel.GetDataSource(sources[0].SourceId, Dbo) + + var versionInfo *VersionInfo + query = dboDataSource.Db.Table(dboDataSource.Schema + ".versioninfo"). + Limit(1). + Select("Version"). + Order("Version Desc") + + query, cancel = utils.AddTimeoutToQuery(query) + defer cancel() + meta_result = query.Scan(&versionInfo) + if meta_result.Error == nil { + dbSchemaVersion.DataSchemaVersion = versionInfo.Version + } + + return dbSchemaVersion +} diff --git a/server/router.go b/server/router.go index 838c06d3..75a633dd 100644 --- a/server/router.go +++ b/server/router.go @@ -62,6 +62,8 @@ func NewRouter() *gin.Engine { // Data Dictionary endpoint authorized.GET("/data-dictionary/Generate", cohortData.GenerateDataDictionary) + // Get Schema Version + authorized.GET("/_schema_version", version.RetrieveSchemaVersion) } return r diff --git a/tests/models_tests/models_test.go b/tests/models_tests/models_test.go index 8fab4e7a..4b4ba101 100644 --- a/tests/models_tests/models_test.go +++ b/tests/models_tests/models_test.go @@ -1014,6 +1014,13 @@ func TestGetVersion(t *testing.T) { } } +func TestGetSchemaVersion(t *testing.T) { + v := versionModel.GetSchemaVersion() + if v.AtlasSchemaVersion != "1.0.1" || v.DataSchemaVersion != 1 { + t.Errorf("Wrong value") + } +} + func TestGetSourceByName(t *testing.T) { allSources, _ := sourceModel.GetAllSources() foundSource, _ := sourceModel.GetSourceByName(allSources[0].SourceName) diff --git a/tests/setup_local_db/ddl_atlas.sql b/tests/setup_local_db/ddl_atlas.sql index 5f7edb31..7e2d908c 100644 --- a/tests/setup_local_db/ddl_atlas.sql +++ b/tests/setup_local_db/ddl_atlas.sql @@ -104,6 +104,22 @@ CREATE TABLE atlas.sec_role_permission ON DELETE NO ACTION ); +CREATE TABLE atlas.schema_version +( + installed_rank integer NOT NULL, + version varchar(50), + description varchar(200) NOT NULL, + type varchar(20) NOT NULL, + script varchar(1000) NOT NULL, + checksum int, + installed_by varchar(100) NOT NULL, + installed_on timestamp(3) NOT NULL, + execution_time int NOT NULL, + success boolean NOT NULL, + CONSTRAINT pk_schema_version PRIMARY KEY (installed_rank) +); + + CREATE VIEW atlas.COHORT_DEFINITION_SEC_ROLE AS select distinct cast(regexp_replace(sec_permission.value, diff --git a/tests/setup_local_db/ddl_results_and_cdm.sql b/tests/setup_local_db/ddl_results_and_cdm.sql index 3472745e..b3cdb193 100644 --- a/tests/setup_local_db/ddl_results_and_cdm.sql +++ b/tests/setup_local_db/ddl_results_and_cdm.sql @@ -124,3 +124,14 @@ CREATE TABLE misc.DATA_DICTIONARY_RESULT value_summary JSON --For sql server use varbinary(max) ); ALTER TABLE misc.DATA_DICTIONARY_RESULT ADD CONSTRAINT xpk_DATA_DICTIONARY_RESULT PRIMARY KEY ( concept_id ) ; + +-- ======================================================== +DROP SCHEMA IF EXISTS dbo CASCADE; +CREATE SCHEMA dbo; +-- ======================================================== + +CREATE TABLE dbo.VersionInfo( + Version bigint NOT NULL, + AppliedOn timestamp NULL, + Description varchar(1024) NULL +) diff --git a/tests/setup_local_db/test_data_atlas.sql b/tests/setup_local_db/test_data_atlas.sql index 5c33e421..1552eef9 100644 --- a/tests/setup_local_db/test_data_atlas.sql +++ b/tests/setup_local_db/test_data_atlas.sql @@ -117,6 +117,13 @@ values (5467, 5000, 1194) -- 5000 dummyGlobalReaderRole has READ ONLY access to cohort 4 ; +INSERT INTO atlas.schema_version + (installed_rank, version, description, type, script, checksum, installed_by, installed_on, execution_time, success) +VALUES + (1,'1.0.0', 'Initial version', 'SCHEMA', 'ohdsi', null, 'qa_testuser', '2022-11-07 23:28:04', 0, true), + (2, '1.0.1', 'Update', 'SQL', 'V1.0.1 Script', 175123456, 'qa_test_user', '2022-11-07 23:28:04', 118, true) +; + insert into atlas.cohort_generation_info (id, person_count, source_id, start_time, status, is_valid, is_canceled) values diff --git a/tests/setup_local_db/test_data_results_and_cdm.sql b/tests/setup_local_db/test_data_results_and_cdm.sql index 95d51582..44f52480 100644 --- a/tests/setup_local_db/test_data_results_and_cdm.sql +++ b/tests/setup_local_db/test_data_results_and_cdm.sql @@ -173,6 +173,11 @@ values (4,18) ; +INSERT INTO dbo.VersionInfo +(Version, AppliedOn, Description) +Values(0, '20240101 09:30:00 AM', 'Initial Version'), +(1, '20240901 09:30:00 AM', 'Table Update'); + WITH cte_counts AS (SELECT observation_concept_id, COUNT(DISTINCT person_id) AS number_of_people_with_variable, COUNT(DISTINCT CASE WHEN value_as_number IS NOT NULL THEN person_id END) AS number_of_people_where_value_is_filled_number,