private void CreateExternalCohortTableReference() { ExternalCohortTable alreadyExisting = DataExportRepository.GetAllObjects <ExternalCohortTable>() .SingleOrDefault(external => external.Name.Equals(ExternalCohortTableNameInCatalogue)); if (alreadyExisting != null) { //remove dependencies cohorts ExtractableCohort toCleanup = DataExportRepository.GetAllObjects <ExtractableCohort>().SingleOrDefault(ec => ec.OriginID == cohortIDInTestData); if (toCleanup != null) { //cleanup any configs that use the cohort foreach ( var configToCleanup in DataExportRepository.GetAllObjects <ExtractionConfiguration>() .Where(config => config.Cohort_ID == toCleanup.ID)) { configToCleanup.DeleteInDatabase(); } toCleanup.DeleteInDatabase(); } alreadyExisting.DeleteInDatabase(); } var newExternal = new ExternalCohortTable(DataExportRepository, "TestExternalCohort", DatabaseType.MicrosoftSQLServer) { Database = CohortDatabaseName, Server = _cohortDatabase.Server.Name, DefinitionTableName = definitionTableName, TableName = cohortTableName, Name = ExternalCohortTableNameInCatalogue, Username = _cohortDatabase.Server.ExplicitUsernameIfAny, Password = _cohortDatabase.Server.ExplicitPasswordIfAny, PrivateIdentifierField = "PrivateID", ReleaseIdentifierField = "ReleaseID", DefinitionTableForeignKeyField = "cohortDefinition_id" }; newExternal.SaveToDatabase(); _externalCohortTable = newExternal; }
public void Create_ExternalCohortTable_Manually() { MemoryDataExportRepository repository = new MemoryDataExportRepository(); var table = new ExternalCohortTable(repository, "My Cohort Database", DatabaseType.MicrosoftSQLServer); table.Database = "mydb"; table.PrivateIdentifierField = "chi"; table.ReleaseIdentifierField = "release"; table.DefinitionTableForeignKeyField = "c_id"; table.TableName = "Cohorts"; table.DefinitionTableName = "InventoryTable"; table.Server = "superfastdatabaseserver\\sqlexpress"; table.SaveToDatabase(); var ex = Assert.Throws <Exception>(() => table.Check(new ThrowImmediatelyCheckNotifier())); Assert.AreEqual("Could not connect to Cohort database called 'My Cohort Database'", ex.Message); }
public ExternalCohortTable CreateDatabase(PrivateIdentifierPrototype privateIdentifierPrototype, ICheckNotifier notifier) { if (!_targetDatabase.Exists()) { notifier.OnCheckPerformed(new CheckEventArgs("Did not find database " + _targetDatabase + " on server so creating it", CheckResult.Success)); _targetDatabase.Create(); } try { var definitionTable = _targetDatabase.CreateTable("CohortDefinition", new[] { new DatabaseColumnRequest("id", new DatabaseTypeRequest(typeof(int))) { AllowNulls = false, IsAutoIncrement = true, IsPrimaryKey = true }, new DatabaseColumnRequest("projectNumber", new DatabaseTypeRequest(typeof(int))) { AllowNulls = false }, new DatabaseColumnRequest("version", new DatabaseTypeRequest(typeof(int))) { AllowNulls = false }, new DatabaseColumnRequest("description", new DatabaseTypeRequest(typeof(string), 3000)) { AllowNulls = false }, new DatabaseColumnRequest("dtCreated", new DatabaseTypeRequest(typeof(DateTime))) { AllowNulls = false, Default = MandatoryScalarFunctions.GetTodaysDate } }); var idColumn = definitionTable.DiscoverColumn("id"); var foreignKey = new DatabaseColumnRequest(_definitionTableForeignKeyField, new DatabaseTypeRequest(typeof(int)), false) { IsPrimaryKey = true }; var cohortTable = _targetDatabase.CreateTable("Cohort", new [] { new DatabaseColumnRequest(privateIdentifierPrototype.RuntimeName, privateIdentifierPrototype.DataType, false) { IsPrimaryKey = true }, new DatabaseColumnRequest(_releaseIdentifierFieldName, new DatabaseTypeRequest(typeof(string), 300)) { AllowNulls = AllowNullReleaseIdentifiers }, foreignKey } , //foreign key between id and cohortDefinition_id new Dictionary <DatabaseColumnRequest, DiscoveredColumn>() { { foreignKey, idColumn } }, true); notifier.OnCheckPerformed(new CheckEventArgs("About to create pointer to the source", CheckResult.Success)); var pointer = new ExternalCohortTable(_dataExportRepository, "TestExternalCohort", _targetDatabase.Server.DatabaseType) { DatabaseType = _targetDatabase.Server.DatabaseType, Server = _targetDatabase.Server.Name, Database = _targetDatabase.GetRuntimeName(), Username = _targetDatabase.Server.ExplicitUsernameIfAny, Password = _targetDatabase.Server.ExplicitPasswordIfAny, Name = _targetDatabase.GetRuntimeName(), TableName = cohortTable.GetRuntimeName(), PrivateIdentifierField = privateIdentifierPrototype.RuntimeName, ReleaseIdentifierField = _releaseIdentifierFieldName, DefinitionTableForeignKeyField = _definitionTableForeignKeyField, DefinitionTableName = definitionTable.GetRuntimeName() }; pointer.SaveToDatabase(); notifier.OnCheckPerformed(new CheckEventArgs("successfully created reference to cohort source in data export manager", CheckResult.Success)); notifier.OnCheckPerformed(new CheckEventArgs("About to run post creation checks", CheckResult.Success)); pointer.Check(notifier); notifier.OnCheckPerformed(new CheckEventArgs("Finished", CheckResult.Success)); return(pointer); } catch (Exception e) { notifier.OnCheckPerformed( new CheckEventArgs("Entire setup failed with exception (double click to find out why)", CheckResult.Fail, e)); return(null); } }
private bool ObjectSaverButton1OnBeforeSave(DatabaseEntity databaseEntity) { SaveDatabaseSettings(); _externalCohortTable.SaveToDatabase(); return(true); }