Ejemplo n.º 1
0
        public System.IO.Stream InitService()
        {
            Trace.TraceInformation("Processing InitService request");

            try
            {
                Common.Logon.CheckRootCredential(scope, credential);
            }
            catch (UnauthorizedAccessException e)
            {
                WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Unauthorized;
                WebOperationContext.Current.OutgoingResponse.StatusDescription = e.Message;
                return null;
            }
            Common.Solution solution = CreateSolution();

            if (CheckIfDbExists(solution))
            {
                return MakeErrorAnswer("License service is already initialized");
            }
            else
            {
                try
                {
                    DatabaseFactory dbf = new DatabaseFactory(solution.DatabaseServer);

                    Assembly a = Assembly.GetExecutingAssembly();
                    Trace.TraceInformation("loaded assembly {0} from {1}", a.FullName, a.CodeBase);

                    //create database
                    String script = "";
                    if (solution.IsAsured)
                        script = new StreamReader(a.GetManifestResourceStream("LicenseService.Database.createDatabaseAzure.sql")).ReadToEnd();
                    else
                        script = new StreamReader(a.GetManifestResourceStream("LicenseService.Database.createDatabase.sql")).ReadToEnd();
                    script = script.Replace("%databaseName%", solution.DatabaseName);
                    Trace.TraceInformation("preparing to run script {0}", script);
                    dbf.RunScript(script);
                    Trace.TraceInformation("script1 succeeded");

                    SqlConnection.ClearAllPools(); // dirty fix for next connection not knowing about created database

                    //run script
                    dbf = new DatabaseFactory(solution.ConnectionString);
                    Trace.TraceInformation("got new dbf");
                    script = new StreamReader(a.GetManifestResourceStream("LicenseService.Database.database.sql")).ReadToEnd();
                    Trace.TraceInformation("preparing to run script {0}", script);
                    dbf.RunScript(script);
                    Trace.TraceInformation("script2 succeeded");

                    return MakeTextAnswer("ok");
                }
                catch (Exception e)
                {
                    return MakeExceptionAnswer(e);
                }
            }
        }
 private void TryUpdateDatabase()
 {
     Assembly a = Assembly.GetExecutingAssembly();
     //run script
     var dbf = new DatabaseFactory(_solution.ConnectionString);
     String script = new StreamReader(a.GetManifestResourceStream("PushService.Database.database.sql")).ReadToEnd();
     dbf.RunScript(script);
 }
Ejemplo n.º 3
0
        private void BuildFilters(Config config, Solution solution)
        {
            //remove existing filters
            try
            {
                using (SqlConnection conn = new SqlConnection(solution.ConnectionString))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("[admin].RemoveFilters", conn);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlCommandBuilder.DeriveParameters(cmd);
                    cmd.ExecuteNonQuery();
                }
            }
            catch
            {
            }

            //create new ones
            String solutionFolder = solution.SolutionFolder;

            if (deleteTempFiles)
            {
                solutionFolder = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString());
                Directory.CreateDirectory(solutionFolder);
                Directory.CreateDirectory(System.IO.Path.Combine(solutionFolder, @"Database"));
                Directory.CreateDirectory(System.IO.Path.Combine(solutionFolder, @"configuration"));
                Directory.CreateDirectory(System.IO.Path.Combine(solutionFolder, @"sqlite"));
            }

            solution.Log("Start updating solution");

            Dictionary <String, String> tempFiles = GetTempFiles(solutionFolder);

            DatabaseFactory.DatabaseFactory dbf = new DatabaseFactory.DatabaseFactory(solution.ConnectionString);

            //filters
            String filtersScript;

            if (solution.IsAsured)
            {
                filtersScript = new FiltersAzure(config).TransformText();
            }
            else
            {
                filtersScript = new Filters(config).TransformText();
            }
            System.IO.File.WriteAllText(tempFiles["filters"], filtersScript);
            dbf.RunScript(filtersScript);

            solution.Log("Filters ok");

            //sync patch
            String syncPatchScript = new SyncPatch2(config).TransformText();

            System.IO.File.WriteAllText(tempFiles["syncpatch"], syncPatchScript);
            dbf.RunScript(syncPatchScript);

            solution.Log("Sync patch ok");

            solution.Log("Solution has been successfully updated");
        }
Ejemplo n.º 4
0
        private void BuildAll(Config config, Solution solution)
        {
            String solutionFolder = solution.SolutionFolder;

            CreateSolutionFolders(solutionFolder);

            if (deleteTempFiles)
            {
                solutionFolder = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString());
                Directory.CreateDirectory(solutionFolder);
                Directory.CreateDirectory(System.IO.Path.Combine(solutionFolder, @"Database"));
                Directory.CreateDirectory(System.IO.Path.Combine(solutionFolder, @"configuration"));
                Directory.CreateDirectory(System.IO.Path.Combine(solutionFolder, @"sqlite"));
            }

            solution.Log("Start building solution");

            Dictionary <String, String> tempFiles = GetTempFiles(solutionFolder);

            DatabaseFactory.DatabaseFactory dbf = null;

            //init database
            dbf = new DatabaseFactory.DatabaseFactory(solution.DatabaseServer);

            String dropDatabaseScript;

            if (solution.IsAsured)
            {
                dropDatabaseScript = new DropDatabaseAzure(config).TransformText();
            }
            else
            {
                dropDatabaseScript = new DropDatabase(config).TransformText();
            }
            System.IO.File.WriteAllText(tempFiles["dropdatabase"], dropDatabaseScript);
            try
            {
                dbf.RunScript(dropDatabaseScript);
            }
            catch (Exception)
            {
            }

            String createDatabaseScript;

            if (solution.IsAsured)
            {
                createDatabaseScript = new CreateDatabaseAzure(config).TransformText();
            }
            else
            {
                createDatabaseScript = new CreateDatabase(config).TransformText();
            }

            System.IO.File.WriteAllText(tempFiles["createdatabase"], createDatabaseScript);
            dbf.RunScript(createDatabaseScript);

            solution.Log("DB scripts ok");

            //create database
            dbf = new DatabaseFactory.DatabaseFactory(solution.ConnectionString);
            String dbScript = new Database(config).TransformText();

            System.IO.File.WriteAllText(tempFiles["database"], dbScript);
            dbf.RunScript(dbScript);

            solution.Log("Database ok");

            System.Data.SqlClient.SqlConnection.ClearAllPools();

            //sort tables
            SyncOrder.Initialize(solution.ConnectionString);
            SyncOrder.Sort(config.Entities);

            //provisioning
            String syncConfig = tempFiles["syncconfig"];

            System.IO.File.WriteAllText(syncConfig, new SyncConfig(config, solution.ConnectionString).TransformText());
            dbf.Provision(new String[] { "/mode:provision", String.Format("/scopeconfig:{0}", syncConfig) });

            solution.Log("Provision ok");

            //filters
            String filtersScript;

            if (solution.IsAsured)
            {
                filtersScript = new FiltersAzure(config).TransformText();
            }
            else
            {
                filtersScript = new Filters(config).TransformText();
            }
            System.IO.File.WriteAllText(tempFiles["filters"], filtersScript);
            dbf.RunScript(filtersScript);

            solution.Log("Filters ok");

            //sync patch
            String syncPatchScript = new SyncPatch2(config).TransformText();

            System.IO.File.WriteAllText(tempFiles["syncpatch"], syncPatchScript);
            dbf.RunScript(syncPatchScript);

            solution.Log("Sync patch ok");

            //admin
            dbScript = new DatabaseAdmin(config).TransformText();
            System.IO.File.WriteAllText(tempFiles["admin"], dbScript);
            dbf.RunScript(dbScript);

            solution.Log("Admin ok");

            //clustered primary keys patch
            config.BuildClusteredPrimaryKeys(solution.ConnectionString);
            String keysPatchScript = new KeysPatch(config).TransformText();

            System.IO.File.WriteAllText(tempFiles["keyspatch"], keysPatchScript);
            dbf.RunScript(keysPatchScript);

            solution.Log("Clustered keys patch ok");

            if (solution.IsAsured)
            {
                //clustered primary keys patch
                config.BuildNonClusteredPrimaryKeys(solution.ConnectionString);
                String azureKeysPatchScript = new AzureKeysPatch(config).TransformText();
                System.IO.File.WriteAllText(tempFiles["azurekeyspatch"], azureKeysPatchScript);
                dbf.RunScript(azureKeysPatchScript);

                solution.Log("Non clustered keys patch ok");
                solution.Log("Default NEWID values on tabular sections ok");
            }
            else
            {
                String nonAzureKeysPatchScript = new NonAzureKeysPatch(config).TransformText();
                System.IO.File.WriteAllText(tempFiles["azurekeyspatch"], nonAzureKeysPatchScript);
                dbf.RunScript(nonAzureKeysPatchScript);
                solution.Log("Default NEWSEQUENTIALID values on tabular sections ok");
            }

            //initial load
            String dataLoadScript = new DataLoad(config).TransformText();

            System.IO.File.WriteAllText(tempFiles["dataload"], dataLoadScript);
            dbf.RunScript(dataLoadScript);

            solution.Log("initial data load ok");

            //sqlite
            String sqlieScript = new SQLiteDatabase(config).TransformText();

            System.IO.File.WriteAllText(tempFiles["sqlite"], sqlieScript);
            if (!System.IO.Directory.Exists(System.IO.Path.Combine(solution.SolutionFolder, "sqlite")))
            {
                System.IO.Directory.CreateDirectory(System.IO.Path.Combine(solution.SolutionFolder, "sqlite"));
            }
            String sqlitedb = System.IO.Path.Combine(solution.SolutionFolder, @"sqlite\sqlite.db");

            if (System.IO.File.Exists(sqlitedb))
            {
                System.IO.File.Delete(sqlitedb);
            }

            //SQLiteDatabaseFactory dbflite = new SQLiteDatabaseFactory(sqlitedb);
            //dbflite.CreateDatabase();
            //dbflite.RunScript(sqlieScript);
            //dbflite.InsertMetadata(solution.ConfigurationFile);

            solution.Log("sqlite ok");

            System.IO.File.WriteAllText(System.IO.Path.Combine(solution.SolutionFolder, @"Code\Server.cs"), new Server(config).TransformText());

            System.IO.File.WriteAllText(System.IO.Path.Combine(solution.SolutionFolder, @"Code\Client.cs"), new Client(config).TransformText());

            System.IO.File.WriteAllText(System.IO.Path.Combine(solution.SolutionFolder, @"Code\ClientMetadata.cs"), new ClientMetadata(config).TransformText());

            System.IO.File.WriteAllText(System.IO.Path.Combine(solution.SolutionFolder, @"Code\ClientConstants.cs"), new ClientConstants(config).TransformText());

            System.IO.File.WriteAllText(System.IO.Path.Combine(solution.SolutionFolder, @"Code\Fake.cs"), new Fake().TransformText());

            System.IO.File.WriteAllText(System.IO.Path.Combine(solution.SolutionFolder, @"Code\DbFake.cs"), new DbFake().TransformText());

            solution.Log("Code generation ok");

            BuildClientDll(solution.SolutionFolder);
            solution.Log("Client dll ok");

            BuildServerDll(solution.SolutionFolder);
            solution.Log("Server dll ok");

            BuildResources(config, solution);
            solution.Log("Resources ok");

            if (deleteTempFiles)
            {
                foreach (var entry in tempFiles)
                {
                    System.IO.File.Delete(entry.Value);
                }
            }

            solution.Log("Solution has been successfully built");
        }
Ejemplo n.º 5
0
        private void BuildFilters(Config config, Solution solution)
        {
            //remove existing filters
            try
            {
                using (SqlConnection conn = new SqlConnection(solution.ConnectionString))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("[admin].RemoveFilters", conn);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlCommandBuilder.DeriveParameters(cmd);
                    cmd.ExecuteNonQuery();
                }
            }
            catch
            {
            }

            //create new ones
            String solutionFolder = solution.SolutionFolder;

            if (deleteTempFiles)
            {
                solutionFolder = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString());
                Directory.CreateDirectory(solutionFolder);
                Directory.CreateDirectory(System.IO.Path.Combine(solutionFolder, @"Database"));
                Directory.CreateDirectory(System.IO.Path.Combine(solutionFolder, @"configuration"));
                Directory.CreateDirectory(System.IO.Path.Combine(solutionFolder, @"sqlite"));
            }

            solution.Log("Start updating solution");

            Dictionary<String, String> tempFiles = GetTempFiles(solutionFolder);

            DatabaseFactory.DatabaseFactory dbf = new DatabaseFactory.DatabaseFactory(solution.ConnectionString);

            //filters
            String filtersScript;
            if (solution.IsAsured)
                filtersScript = new FiltersAzure(config).TransformText();
            else
                filtersScript = new Filters(config).TransformText();
            System.IO.File.WriteAllText(tempFiles["filters"], filtersScript);
            dbf.RunScript(filtersScript);

            solution.Log("Filters ok");

            //sync patch
            String syncPatchScript = new SyncPatch2(config).TransformText();
            System.IO.File.WriteAllText(tempFiles["syncpatch"], syncPatchScript);
            dbf.RunScript(syncPatchScript);

            solution.Log("Sync patch ok");

            solution.Log("Solution has been successfully updated");
        }
Ejemplo n.º 6
0
        private void BuildAll(Config config, Solution solution)
        {
            String solutionFolder = solution.SolutionFolder;
            CreateSolutionFolders(solutionFolder);

            if (deleteTempFiles)
            {
                solutionFolder = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString());
                Directory.CreateDirectory(solutionFolder);
                Directory.CreateDirectory(System.IO.Path.Combine(solutionFolder, @"Database"));
                Directory.CreateDirectory(System.IO.Path.Combine(solutionFolder, @"configuration"));
                Directory.CreateDirectory(System.IO.Path.Combine(solutionFolder, @"sqlite"));
            }

            solution.Log("Start building solution");

            Dictionary<String, String> tempFiles = GetTempFiles(solutionFolder);

            DatabaseFactory.DatabaseFactory dbf = null;

            //init database
            dbf = new DatabaseFactory.DatabaseFactory(solution.DatabaseServer);

            String dropDatabaseScript;
            if (solution.IsAsured)
                dropDatabaseScript = new DropDatabaseAzure(config).TransformText();
            else
                dropDatabaseScript = new DropDatabase(config).TransformText();
            System.IO.File.WriteAllText(tempFiles["dropdatabase"], dropDatabaseScript);
            try
            {
                dbf.RunScript(dropDatabaseScript);
            }
            catch (Exception)
            {
            }

            String createDatabaseScript;
            if(solution.IsAsured)
                createDatabaseScript = new CreateDatabaseAzure(config).TransformText();
            else
                createDatabaseScript = new CreateDatabase(config).TransformText();

            System.IO.File.WriteAllText(tempFiles["createdatabase"], createDatabaseScript);
            dbf.RunScript(createDatabaseScript);

            solution.Log("DB scripts ok");

            //create database
            dbf = new DatabaseFactory.DatabaseFactory(solution.ConnectionString);
            String dbScript = new Database(config).TransformText();
            System.IO.File.WriteAllText(tempFiles["database"], dbScript);
            dbf.RunScript(dbScript);

            solution.Log("Database ok");

            System.Data.SqlClient.SqlConnection.ClearAllPools();

            //sort tables
            SyncOrder.Initialize(solution.ConnectionString);
            SyncOrder.Sort(config.Entities);
            
            //provisioning
            String syncConfig = tempFiles["syncconfig"];
            System.IO.File.WriteAllText(syncConfig, new SyncConfig(config, solution.ConnectionString).TransformText());
            dbf.Provision(new String[] { "/mode:provision", String.Format("/scopeconfig:{0}", syncConfig) });

            solution.Log("Provision ok");

            //filters
            String filtersScript;
            if (solution.IsAsured)
                filtersScript = new FiltersAzure(config).TransformText();
            else
                filtersScript = new Filters(config).TransformText();
            System.IO.File.WriteAllText(tempFiles["filters"], filtersScript);
            dbf.RunScript(filtersScript);

            solution.Log("Filters ok");

            //sync patch
            String syncPatchScript = new SyncPatch2(config).TransformText();
            System.IO.File.WriteAllText(tempFiles["syncpatch"], syncPatchScript);
            dbf.RunScript(syncPatchScript);

            solution.Log("Sync patch ok");

            //admin
            dbScript = new DatabaseAdmin(config).TransformText();
            System.IO.File.WriteAllText(tempFiles["admin"], dbScript);
            dbf.RunScript(dbScript);

            solution.Log("Admin ok");

            //clustered primary keys patch
            config.BuildClusteredPrimaryKeys(solution.ConnectionString);
            String keysPatchScript = new KeysPatch(config).TransformText();
            System.IO.File.WriteAllText(tempFiles["keyspatch"], keysPatchScript);
            dbf.RunScript(keysPatchScript);

            solution.Log("Clustered keys patch ok");

            if (solution.IsAsured)
            {
                //clustered primary keys patch
                config.BuildNonClusteredPrimaryKeys(solution.ConnectionString);
                String azureKeysPatchScript = new AzureKeysPatch(config).TransformText();
                System.IO.File.WriteAllText(tempFiles["azurekeyspatch"], azureKeysPatchScript);
                dbf.RunScript(azureKeysPatchScript);

                solution.Log("Non clustered keys patch ok");
                solution.Log("Default NEWID values on tabular sections ok");
            }
            else
            {
                String nonAzureKeysPatchScript = new NonAzureKeysPatch(config).TransformText();
                System.IO.File.WriteAllText(tempFiles["azurekeyspatch"], nonAzureKeysPatchScript);
                dbf.RunScript(nonAzureKeysPatchScript);
                solution.Log("Default NEWSEQUENTIALID values on tabular sections ok");
            }
    
            //initial load
            String dataLoadScript = new DataLoad(config).TransformText();
            System.IO.File.WriteAllText(tempFiles["dataload"], dataLoadScript);
            dbf.RunScript(dataLoadScript);
            
            solution.Log("initial data load ok");

            //sqlite
            String sqlieScript = new SQLiteDatabase(config).TransformText();
            System.IO.File.WriteAllText(tempFiles["sqlite"], sqlieScript);
            if(!System.IO.Directory.Exists(System.IO.Path.Combine(solution.SolutionFolder,"sqlite")))
                System.IO.Directory.CreateDirectory(System.IO.Path.Combine(solution.SolutionFolder,"sqlite"));
            String sqlitedb = System.IO.Path.Combine(solution.SolutionFolder, @"sqlite\sqlite.db");
            if (System.IO.File.Exists(sqlitedb))
                System.IO.File.Delete(sqlitedb);

            //SQLiteDatabaseFactory dbflite = new SQLiteDatabaseFactory(sqlitedb);
            //dbflite.CreateDatabase();
            //dbflite.RunScript(sqlieScript);
            //dbflite.InsertMetadata(solution.ConfigurationFile);

            solution.Log("sqlite ok");

            System.IO.File.WriteAllText(System.IO.Path.Combine(solution.SolutionFolder, @"Code\Server.cs"), new Server(config).TransformText());

            System.IO.File.WriteAllText(System.IO.Path.Combine(solution.SolutionFolder, @"Code\Client.cs"), new Client(config).TransformText());

            System.IO.File.WriteAllText(System.IO.Path.Combine(solution.SolutionFolder, @"Code\ClientMetadata.cs"), new ClientMetadata(config).TransformText());

            System.IO.File.WriteAllText(System.IO.Path.Combine(solution.SolutionFolder, @"Code\ClientConstants.cs"), new ClientConstants(config).TransformText());

            System.IO.File.WriteAllText(System.IO.Path.Combine(solution.SolutionFolder, @"Code\Fake.cs"), new Fake().TransformText());

            System.IO.File.WriteAllText(System.IO.Path.Combine(solution.SolutionFolder, @"Code\DbFake.cs"), new DbFake().TransformText());

            solution.Log("Code generation ok");

            BuildClientDll(solution.SolutionFolder);
            solution.Log("Client dll ok");

            BuildServerDll(solution.SolutionFolder);
            solution.Log("Server dll ok");

            BuildResources(config, solution);
            solution.Log("Resources ok");

            if (deleteTempFiles)
            {
                foreach (var entry in tempFiles)
                    System.IO.File.Delete(entry.Value);
            }

            solution.Log("Solution has been successfully built");
        }