public static List <DbUpgradeScript> CompareDatabaseSchemas(DbFirstConfig config, Type modelType) { var entApp = Activator.CreateInstance(modelType) as EntityApp; entApp.EntityClassProvider = new DummyEntityClassProvider(); entApp.Init(); // important - do not use DbOptions.AutoIndexForeignKeys - which is recommended for MS SQL, but is not helpful here. // This will create a bunch of extra indexes on FKs in entities schema and result in extra differences with original schema. // We ignore stored procs var dbOptions = config.Driver.GetDefaultOptions() & ~DbOptions.AutoIndexForeignKeys; var dbSettings = new DbSettings(config.Driver, dbOptions, config.ConnectionString, upgradeMode: DbUpgradeMode.Always, upgradeOptions: DbUpgradeOptions.UpdateTables | DbUpgradeOptions.UpdateIndexes ); //dbSettings.SetSchemas(config.Schemas); var log = new ActivationLog(null); var dbModelBuilder = new DbModelBuilder(entApp.Model, dbSettings.ModelConfig, log); var dbModel = dbModelBuilder.Build(); var db = new Database(dbModel, dbSettings); var ds = new DataSource("main", db); var upgradeMgr = new DbUpgradeManager(db, log); var upgradeInfo = upgradeMgr.BuildUpgradeInfo(); return(upgradeInfo.AllScripts); }
protected virtual void ConnectToDatabase(DbSettings dbSettings) { try { switch (this.Status) { case EntityAppStatus.Created: this.Init(); break; case EntityAppStatus.Shutdown: return; } ActivationLog.WriteMessage(" Connecting to data source {0}.", dbSettings.DataSourceName); dbSettings.CheckConnectivity(rethrow: true); var dbModel = GetCreateDbModel(dbSettings); var db = new Database(dbModel, dbSettings); var ds = new DataSource(dbSettings.DataSourceName, db); this.DataAccess.RegisterDataSource(ds); this.DataSourceEvents.OnDataSourceChange(new DataSourceEventArgs(db, dbSettings.DataSourceName, DataSourceEventType.Connecting)); CheckUpgradeDatabase(db); LogService.Flush(); this.Status = EntityAppStatus.Connected; this.DataSourceEvents.OnDataSourceChange(new DataSourceEventArgs(db, dbSettings.DataSourceName, DataSourceEventType.Connected)); ActivationLog.WriteMessage("Connected to {0}.", dbSettings.DataSourceName); } finally { LogService.Flush(); } }
public EntityApp Build(DbFirstConfig config) { _config = config; _app = new EntityApp(); var log = new ActivationLog(_app.ActivationLogPath); _dbSettings = new DbSettings(_config.Driver, config.Driver.GetDefaultOptions(), _config.ConnectionString); // create loader and setup filter var modelLoader = _config.Driver.CreateDbModelLoader(_dbSettings, log); modelLoader.SetSchemasSubset(_config.Schemas); //actually load model _dbModel = modelLoader.LoadModel(); Util.Check(_dbModel.Tables.Count() > 0, "No tables found in the database. Code generation aborted."); // Prepare type generator _tempNamespace = "_dummy_" + _callCount++; // artificial unique namespace for dummy interface types // Construct model setup and model GenerateModulesAndAreas(); _entityModel = new EntityModel(_app); // EntityModelBuilder.SetModel(_app, _entityModel); //generate entities and members GenerateEntities(); SetupPrimaryKeys(); GenerateReferenceMembers(); CreateIndexes(); SetupKeyMembers(); return(_app); }
protected virtual void InitApp() { Status = EntityAppStatus.Initializing; SetupLogFileWriters(); ActivationLog.WriteMessage("Initializing EntityApp {0}.====================================", this.AppName); this.AppEvents.OnInitializing(EntityAppInitStep.Initializing); //Check dependencies foreach (var mod in this.Modules) { var depList = mod.GetDependencies(); foreach (var dep in depList) { var ok = Modules.Any(m => dep.IsTypeOrSubType(m)); if (!ok) { ActivationLog.LogError($"Module {mod.Name} requires dependent module {dep} which is not included in the app."); } } } CheckActivationErrors(); //Build model var builder = new EntityModelBuilder(this); builder.BuildModel(); CheckActivationErrors(); //Notify modules that entity app is constructed foreach (var module in this.Modules) { module.Init(); } //init services; note that service might be registered more than once, under different interface var servList = this.GetAllServices().Distinct().ToList(); for (int i = 0; i < servList.Count; i++) { var service = servList[i]; var iServiceInit = service as IEntityServiceBase; if (iServiceInit != null) { iServiceInit.Init(this); } } //complete initialization this.AppEvents.OnInitializing(EntityAppInitStep.Initialized); foreach (var module in this.Modules) { module.AppInitComplete(); } builder.CheckErrors(); Status = EntityAppStatus.Initialized; ActivationLog.WriteMessage("App {0} initialized.", this.AppName); }
public void LogActiviation(string licenseKey, ActivationResults activationResult, string ipAddress) { ActivationLog log = new ActivationLog(); log.LicenseKey = _hashingProvider.ComputeHash(licenseKey, "SHA256"); log.ActivationResult = activationResult; log.IpAddress = ipAddress; log.Timestamp = DateTime.Now; _activationLogRepository.SaveActivationLog(log); }
public void negative_test() { var log = new ActivationLog(); var file = Guid.NewGuid().ToString() + ".txt"; var requirement = new FileExists(file); requirement.Check(log); log.Success.ShouldBeFalse(); log.FullTraceText().ShouldContain("File '{0}' does not exist!".ToFormat(file)); }
public bool GenerateScripts(XmlDocument xmlConfig) { const string header = @"-- DDL Scripts generated by VITA DB Tool. -- Generated on: {0} -- Target database: {1} -- Executed by user {2} on machine {3}. "; _config = new DbUpdateConfig(xmlConfig); Util.Check(File.Exists(_config.AssemblyPath), "Assembly file '{0}' not found.", _config.AssemblyPath); var asm = Assembly.LoadFrom(_config.AssemblyPath); var appType = asm.GetType(_config.AppClassName); Util.Check(appType != null, "Type {0} not found in target assembly."); // Using NonPublic flag to allow internal constructor; // EntityApp must have a parameterless constructor, but it may be made internal, to hide from regular code var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; var appObj = Activator.CreateInstance(appType, flags, null, null, null); Util.Check(appObj != null, "Failed to create instance of class {0}.", _config.AppClassName); var entApp = appObj as EntityApp; Util.Check(entApp != null, "The target instance of class {0} is not an EntityApp instance.", _config.AppClassName); entApp.Init(); var dbSettings = new DbSettings(_config.Driver, _config.DbOptions, _config.ConnectionString, upgradeMode: DbUpgradeMode.Always, upgradeOptions: _config.ModelUpdateOptions); /* * var schemas = entApp.Areas.Select(a => a.Name).ToList(); * dbSettings.SetSchemas(schemas); */ var log = new ActivationLog(null); var dbModelBuilder = new DbModelBuilder(entApp.Model, dbSettings.ModelConfig, log); var dbModel = dbModelBuilder.Build(); var db = new Database(dbModel, dbSettings); var updateMgr = new DbUpgradeManager(db, log); var upgrades = updateMgr.BuildUpgradeInfo(); var ddlSep = dbModel.Driver.SqlDialect.DDLSeparator; var ddl = string.Join(ddlSep, upgrades.AllScripts.Select(scr => scr.Sql)); if (string.IsNullOrEmpty(ddl)) { ddl = "-- (No changes detected)"; } var text = string.Format(header, DateTime.Now.ToString("s"), _config.ConnectionString, "(unknown)", Environment.MachineName) + ddl; File.WriteAllText(_config.OutputPath, text); _feedback.WriteLine(" Generated {0} scripts.", upgrades.AllScripts.Count); _feedback.WriteLine(" DDL scripts are saved to '{0}'", _config.OutputPath); return(true); }
public void positive_test() { new FileSystem().WriteStringToFile("file.txt", "anything"); var log = new ActivationLog(); var requirement = new FileExists("file.txt"); requirement.Check(log); log.Success.ShouldBeTrue(); log.FullTraceText().ShouldContain("File 'file.txt' exists"); }
public void positive_test() { new FileSystem().CreateDirectory("foo"); var log = new ActivationLog(); var requirement = new FolderExists("foo"); requirement.Check(log); log.Success.ShouldBeTrue(); log.FullTraceText().ShouldContain("Folder 'foo' exists"); }
public void SaveActivationLog(Model.ActivationLog activationLog) { using (ScutexServiceEntities db1 = new ScutexServiceEntities()) { ActivationLog al = new ActivationLog(); al.LicenseKey = activationLog.LicenseKey; al.ActivationResult = (int)activationLog.ActivationResult; al.IPAddress = activationLog.IpAddress; al.Timestamp = activationLog.Timestamp; db1.AddToActivationLogs(al); db1.SaveChanges(); } }
} //method public void CheckUpgradeDatabase(Database db) { //Invoke upgrade // Update db model if (db.Settings.UpgradeMode == DbUpgradeMode.Never) { ActivationLog.WriteMessage("Data source upgrade mode set to Never, skipping db upgrade."); return; } // upgrade var updateMgr = new DbUpgradeManager(db, ActivationLog); var upgradeInfo = updateMgr.UpgradeDatabase(); //it might throw exception // _events.OnDataSourceStatusChanging(new DataSourceEventArgs(dataSource, DataSourceEventType.Connected)); }
public void negative_test_with_settings() { var log = new ActivationLog(); var file = Guid.NewGuid().ToString() + ".txt"; var settings = new FileSettings { File = file }; var requirement = new FileExists<FileSettings>(x => x.File, settings); requirement.Check(log); log.Success.ShouldBeFalse(); log.FullTraceText().ShouldContain("File '{0}' defined by FileSettings.File does not exist!".ToFormat(file)); }
public void positive_test_with_generic() { new FileSystem().CreateDirectory("foo"); var settings = new FileSettings { Folder = "foo" }; var log = new ActivationLog(); var requirement = new FolderExists<FileSettings>(x => x.Folder, settings); requirement.Check(log); log.Success.ShouldBeTrue(); log.FullTraceText().ShouldContain("Folder 'foo' defined by FileSettings.Folder exists"); }
public void positive_test_with_generic() { new FileSystem().WriteStringToFile("file.txt", "anything"); var settings = new FileSettings { File = "file.txt" }; var log = new ActivationLog(); var requirement = new FileExists <FileSettings>(x => x.File, settings); requirement.Check(log); log.Success.ShouldBeTrue(); log.FullTraceText().ShouldContain("File 'file.txt' defined by FileSettings.File exists"); }
public void positive_test_with_generic() { new FileSystem().CreateDirectory("foo"); var settings = new FileSettings { Folder = "foo" }; var log = new ActivationLog(); var requirement = new FolderExists <FileSettings>(x => x.Folder, settings); requirement.Check(log); log.Success.ShouldBeTrue(); log.FullTraceText().ShouldContain("Folder 'foo' defined by FileSettings.Folder exists"); }
public void positive_test_with_generic() { new FileSystem().WriteStringToFile("file.txt", "anything"); var settings = new FileSettings { File = "file.txt" }; var log = new ActivationLog(); var requirement = new FileExists<FileSettings>(x => x.File, settings); requirement.Check(log); log.Success.ShouldBeTrue(); log.FullTraceText().ShouldContain("File 'file.txt' defined by FileSettings.File exists"); }
public void negative_test_with_settings() { var log = new ActivationLog(); var file = Guid.NewGuid().ToString() + ".txt"; var settings = new FileSettings { File = file }; var requirement = new FileExists <FileSettings>(x => x.File, settings); requirement.Check(log); log.Success.ShouldBeFalse(); log.FullTraceText().ShouldContain("File '{0}' defined by FileSettings.File does not exist!".ToFormat(file)); }
public static string LastFatalError; // error log sets this if it fails to persist error protected virtual void InitApp() { Util.Check(this.EntityClassProvider != null, "EntityApp.{0} may not be null. Use {1} method from Vita.Entities.Emit assembly to create provider instance.", nameof(EntityClassProvider), "Vita.Entities.Emit.EntityClassEmitter.CreateProvider()"); RegisterService <IEntityClassProvider>(EntityClassProvider); Status = EntityAppStatus.Initializing; CreateLogFileWriters(); ActivationLog.Info("Initializing EntityApp {0}.====================================", this.AppName); this.AppEvents.OnInitializing(EntityAppInitStep.Initializing); //Check dependencies foreach (var mod in this.Modules) { var depList = mod.GetDependencies(); foreach (var dep in depList) { var ok = Modules.Any(m => dep.IsTypeOrSubType(m)); if (!ok) { ActivationLog.Error("Module {0} requires dependent module {1} which is not included in the app.", mod.GetType(), dep); } } } ActivationLog.CheckErrors(); // Init linked apps foreach (var linkedApp in LinkedApps) { if (linkedApp.Status == EntityAppStatus.Created) { linkedApp.Init(); } } //Build model var builder = new EntityModelBuilder(this); builder.BuildModel(); ActivationLog.CheckErrors(); //Notify modules that entity app is constructed foreach (var module in this.Modules) { module.Init(); } //init services var servList = this.GetAllServices(); for (int i = 0; i < servList.Count; i++) { var service = servList[i]; var iServiceInit = service as IEntityServiceBase; if (iServiceInit != null) { iServiceInit.Init(this); } } //complete initialization this.AppEvents.OnInitializing(EntityAppInitStep.Initialized); foreach (var module in this.Modules) { module.AppInitComplete(); } builder.CheckErrors(); Status = EntityAppStatus.Initialized; ActivationLog.Info("App {0} initialized.", this.AppName); }
private void CheckActivationErrors() { ActivationLog.CheckErrors("Application initialization failed."); }