public LiveVsTemplateComparer(ITableInfo table, ImageTableTemplateCollection templateCollection) { // locate the live table and script it as it stands today var discoveredTable = table.Discover(DataAccessContext.InternalDataProcessing); LiveSql = discoveredTable.ScriptTableCreation(false, false, false); LiveSql = TailorLiveSql(LiveSql, discoveredTable.Database.Server.DatabaseType); // The live table name e.g. CT_StudyTable var liveTableName = discoveredTable.GetRuntimeName(); // Without the prefix e.g. StudyTable var liveTableNameWithoutPrefix = liveTableName.Substring(liveTableName.IndexOf("_", StringComparison.Ordinal) + 1); var template = templateCollection.Tables.FirstOrDefault( c => c.TableName.Equals(liveTableName, StringComparison.CurrentCultureIgnoreCase) || c.TableName.Equals(liveTableNameWithoutPrefix, StringComparison.CurrentCultureIgnoreCase)); if (template == null) { throw new Exception($"Could not find a Template called '{liveTableName}' or '{liveTableNameWithoutPrefix}'. Templates in file were {string.Join(",",templateCollection.Tables.Select(t=>t.TableName))}"); } //script the template var creator = new ImagingTableCreation(discoveredTable.Database.Server.GetQuerySyntaxHelper()); TemplateSql = creator.GetCreateTableSql(discoveredTable.Database, liveTableName, template, discoveredTable.Schema); TemplateSql = TailorTemplateSql(TemplateSql); }
protected AlterTableCommandExecution(IBasicActivateItems activator, ITableInfo tableInfo) : base(activator) { TableInfo = tableInfo; try { Table = TableInfo.Discover(DataAccessContext.InternalDataProcessing); } catch (Exception) { SetImpossible("Could not resolve Server/Table connection details"); return; } if (!Table.Exists()) { SetImpossible("Table does not exist"); return; } if (Table.TableType != TableType.Table) { SetImpossible("Table is a " + Table.TableType); return; } }
/// <summary> /// Creates a mock implementation of <see cref="ILoadMetadata"/> which loads the supplied <paramref name="tableInfo"/> /// </summary> /// <param name="tableInfo"></param> /// <returns></returns> public static ILoadMetadata Mock_LoadMetadataLoadingTable(ITableInfo tableInfo) { var lmd = new Mock <ILoadMetadata>(); var cata = new Mock <ICatalogue>(); lmd.Setup(m => m.GetDistinctLiveDatabaseServer()).Returns(tableInfo.Discover(DataAccessContext.DataLoad).Database.Server); lmd.Setup(m => m.GetAllCatalogues()).Returns(new[] { cata.Object }); cata.Setup(m => m.GetTableInfoList(It.IsAny <bool>())).Returns(new[] { tableInfo }); return(lmd.Object); }
public void FetchData(ICheckNotifier checkNotifier) { try { DiscoveredDatabase database; DiscoveredServer server; try { database = DataAccessPortal.GetInstance().ExpectDatabase(_tableInfo, DataAccessContext.InternalDataProcessing); server = database.Server; } catch (Exception ex) { checkNotifier.OnCheckPerformed(new CheckEventArgs("Could not connect to data access point TableInfo " + _tableInfo, CheckResult.Fail, ex)); return; } if (database.Exists()) { checkNotifier.OnCheckPerformed(new CheckEventArgs("Verified database exists " + database, CheckResult.Success)); } var liveTable = _tableInfo.Discover(DataAccessContext.InternalDataProcessing); var archiveTable = database.ExpectTable(_tableInfo.GetRuntimeName() + "_Archive", liveTable.Schema); if (liveTable.Exists()) { checkNotifier.OnCheckPerformed(new CheckEventArgs("Verified table exists " + _tableInfo, CheckResult.Success)); } if (archiveTable.Exists()) { checkNotifier.OnCheckPerformed(new CheckEventArgs("Verified Archive table exists " + archiveTable, CheckResult.Success)); } else { checkNotifier.OnCheckPerformed(new CheckEventArgs("Did not find an Archive table called " + archiveTable, CheckResult.Fail)); } var allCols = _tableInfo.ColumnInfos.ToArray(); var allArchiveCols = archiveTable.DiscoverColumns().ToArray(); _pks = allCols.Where(c => c.IsPrimaryKey).ToArray(); if (_pks.Any()) { checkNotifier.OnCheckPerformed(new CheckEventArgs("Found the following primary keys:" + string.Join(",", _pks.Select(p => p.GetRuntimeName())), CheckResult.Success)); } else { checkNotifier.OnCheckPerformed(new CheckEventArgs("Table does not have any ColumnInfos marked with IsPrimaryKey (try synchronizing the TableInfo if you are sure you have some", CheckResult.Fail)); } _sharedColumns = allCols.Where( //from all columns take all columns where c => allArchiveCols.Any( //there is a column with the same name in the archive columns (ignoring case) archiveCol => c.GetRuntimeName().Equals(archiveCol.GetRuntimeName(), StringComparison.InvariantCultureIgnoreCase) //but dont care about differences in these columns (e.g. the actual data load run id will obviously be different!) && !SpecialFieldNames.IsHicPrefixed(c) )).ToArray(); checkNotifier.OnCheckPerformed(new CheckEventArgs("Shared columns between the archive and the live table are " + string.Join(",", _sharedColumns.Select(c => c.GetRuntimeName())), CheckResult.Success)); GetInsertData(server, database, checkNotifier); GetUpdatetData(server, database, checkNotifier); } catch (Exception e) { checkNotifier.OnCheckPerformed(new CheckEventArgs("Fatal error trying to fetch data", CheckResult.Fail, e)); } }