public GenericResponse IsInstalled()
        {
            var response = new GenericResponse {
                Success = false, Message = ""
            };

            string datadirectory    = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
            string connectionString = Config.GetConnectionString("DefaultConnection");

            connectionString = connectionString.Replace("|DataDirectory|", datadirectory);

            SqlConnection connection = new SqlConnection(connectionString);

            try
            {
                using (connection)
                {
                    connection.Open();
                }
                response.Success = true;
            }
            catch
            {
                // database does not exist
                response.Message = "Database Does Not Exist";
            }

            if (response.Success)
            {
                var dbUpgradeConfig = DeployChanges.To.SqlDatabase(connectionString)
                                      .WithScript(new DbUp.Engine.SqlScript("Master.sql", ""));
                var dbUpgrade = dbUpgradeConfig.Build();
                response.Success = !dbUpgrade.IsUpgradeRequired();
                if (!response.Success)
                {
                    response.Message = "Master Installation Scripts Have Not Been Executed";
                }
                else
                {
                    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies()
                                            .Where(item => item.FullName.Contains(".Module.")).ToArray();

                    // get tenants
                    using (var db = new InstallationContext(connectionString))
                    {
                        foreach (Tenant tenant in db.Tenant.ToList())
                        {
                            // upgrade framework
                            dbUpgradeConfig = DeployChanges.To.SqlDatabase(tenant.DBConnectionString)
                                              .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly());
                            dbUpgrade = dbUpgradeConfig.Build();
                            if (dbUpgrade.IsUpgradeRequired())
                            {
                                var result = dbUpgrade.PerformUpgrade();
                                if (!result.Successful)
                                {
                                    // TODO: log result.Error.Message;
                                }
                            }
                            // iterate through Oqtane module assemblies and execute any database scripts
                            foreach (Assembly assembly in assemblies)
                            {
                                InstallModule(assembly, tenant.DBConnectionString);
                            }
                        }
                    }
                }
            }

            return(response);
        }
Example #2
0
        public Installation IsInstalled()
        {
            var installation = new Installation {
                Success = false, Message = ""
            };

            string datadirectory    = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
            string connectionString = _config.GetConnectionString("DefaultConnection");

            connectionString = connectionString.Replace("|DataDirectory|", datadirectory);

            if (!string.IsNullOrEmpty(connectionString))
            {
                SqlConnection connection = new SqlConnection(connectionString);
                try
                {
                    using (connection)
                    {
                        connection.Open();
                    }
                    installation.Success = true;
                }
                catch
                {
                    // database does not exist
                    installation.Message = "Database Does Not Exist";
                }
            }
            else
            {
                installation.Message = "Connection String Has Not Been Specified In Oqtane.Server\\appsettings.json";
            }

            if (installation.Success)
            {
                var dbUpgradeConfig = DeployChanges.To.SqlDatabase(connectionString)
                                      .WithScript(new DbUp.Engine.SqlScript("Master.sql", ""));
                var dbUpgrade = dbUpgradeConfig.Build();
                installation.Success = !dbUpgrade.IsUpgradeRequired();
                if (!installation.Success)
                {
                    installation.Message = "Master Installation Scripts Have Not Been Executed";
                }
                else
                {
                    using (var db = new InstallationContext(connectionString))
                    {
                        ApplicationVersion version = db.ApplicationVersion.ToList().LastOrDefault();
                        if (version == null || version.Version != Constants.Version)
                        {
                            version           = new ApplicationVersion();
                            version.Version   = Constants.Version;
                            version.CreatedOn = DateTime.UtcNow;
                            db.ApplicationVersion.Add(version);
                            db.SaveChanges();
                        }
                    }

                    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies()
                                            .Where(item => item.FullName.Contains(".Module.")).ToArray();

                    // get tenants
                    using (var db = new InstallationContext(connectionString))
                    {
                        foreach (Tenant tenant in db.Tenant.ToList())
                        {
                            connectionString = tenant.DBConnectionString;
                            connectionString = connectionString.Replace("|DataDirectory|", datadirectory);

                            // upgrade framework
                            dbUpgradeConfig = DeployChanges.To.SqlDatabase(connectionString)
                                              .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly());
                            dbUpgrade = dbUpgradeConfig.Build();
                            if (dbUpgrade.IsUpgradeRequired())
                            {
                                var result = dbUpgrade.PerformUpgrade();
                                if (!result.Successful)
                                {
                                    // TODO: log result.Error.Message - problem is logger is not available here
                                }
                            }
                            // iterate through Oqtane module assemblies and execute any database scripts
                            foreach (Assembly assembly in assemblies)
                            {
                                InstallModule(assembly, connectionString);
                            }
                        }
                    }
                }
            }

            return(installation);
        }