Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new schema_version endpoint #116

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions controllers/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
5 changes: 5 additions & 0 deletions models/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
75 changes: 74 additions & 1 deletion models/version.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/models_tests/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions tests/setup_local_db/ddl_atlas.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions tests/setup_local_db/ddl_results_and_cdm.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
7 changes: 7 additions & 0 deletions tests/setup_local_db/test_data_atlas.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/setup_local_db/test_data_results_and_cdm.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading