Example #1
0
        public void TestDbLocationModifierForImport()
        {
            // Given database name, and path to save to
            string dbName      = TestContext.TestName;
            string dataFolder  = GetTestDir();
            string filePrefix  = "mydb";
            string bacpacPath  = Path.Combine(dataFolder, dbName + ".bacpac");
            string mdfFilePath = Path.Combine(dataFolder, filePrefix + "_Primary.mdf");
            string ldfFilePath = Path.Combine(dataFolder, filePrefix + "_Primary.ldf");

            // Delete any existing artifacts from a previous run
            TestUtils.DropDbAndDeleteFiles(dbName, mdfFilePath, ldfFilePath);

            SqlTestDB importedDb = null;

            try
            {
                // Create a DB and export
                SqlTestDB db = _trash.Add(TestUtils.CreateTestDatabase(TestUtils.DefaultInstanceInfo, "MyOriginalDb"));
                db.Execute(CreateOneTable);
                db.ExportBacpac(bacpacPath);

                // When deploying using the location modifying contributor
                DacImportOptions options = new DacImportOptions();
                options.ImportContributors = DbLocationModifier.ContributorId;

                options.ImportContributorArguments =
                    Utils.BuildContributorArguments(new Dictionary <string, string>()
                {
                    { DbLocationModifier.DbSaveDataLocationArg, dataFolder },
                    { DbLocationModifier.DbSaveLogDataLocationArg, dataFolder },
                    { DbLocationModifier.DbFilePrefixArg, filePrefix },
                });

                importedDb = SqlTestDB.CreateFromBacpac(TestUtils.DefaultInstanceInfo, bacpacPath, options, true);

                // Then expect the database to be saved under that path
                AssertDeploySucceeded(importedDb.BuildConnectionString(), importedDb.DatabaseName);
                Assert.IsTrue(File.Exists(mdfFilePath));
                Assert.IsTrue(File.Exists(ldfFilePath));

                // Note: for a real application, after creating the DB on the server they may want to
                // detach it and reattach using the database path. We are not doing this since it's
                // not relevant to this test
            }
            finally
            {
                if (importedDb != null)
                {
                    importedDb.Dispose();
                }
                TestUtils.DeleteIfExists(bacpacPath);
                TestUtils.DeleteIfExists(mdfFilePath);
                TestUtils.DeleteIfExists(ldfFilePath);
            }
        }
Example #2
0
        /// <summary>
        /// Deploys test scripts to a database and creates a model directly against this DB.
        /// Since this is a RuleTest we load the model as script backed to ensure that we have file names,
        /// source code positions, and that programmability objects (stored procedures, views) have a full SQLDOM
        /// syntax tree instead of just a snippet.
        /// </summary>
        private TSqlModel CreateDatabaseModel()
        {
            ArgumentValidation.CheckForEmptyString(DatabaseName, "DatabaseName");
            SqlTestDB db = TestUtils.CreateTestDatabase(TestUtils.DefaultInstanceInfo, DatabaseName);

            _trash.Add(db);

            TestUtils.ExecuteNonQuery(db, TestScripts.Select(t => t.Item1).SelectMany(s => TestUtils.GetBatches(s)).ToList());

            TSqlModel model = TSqlModel.LoadFromDatabase(db.BuildConnectionString(), new ModelExtractOptions {
                LoadAsScriptBackedModel = true
            });

            AssertModelValid(model);
            return(model);
        }
Example #3
0
        public static void Run()
        {
            Console.WriteLine("Publishing scripts to DB");
            using (SqlTestDB db = TestUtils.CreateTestDatabase(TestUtils.DefaultInstanceInfo, "MyQueryExample"))
            {
                // set up the database with your scripts
                TestUtils.ExecuteNonQuery(db, SampleScripts);

                ValidateQuery(BadQuery, db, false);

                // BUG: Normally AddOrUpdateObjects will replace the contents of a file so we could just update the contents of our "Query File"
                // and re-run validation. This simulates live validation of a query in SSMS/SSDT.
                // Somehow when loading model directly from Database this isn't working. We will resolve this issue in a future DacFx update.
                // For now we create a new model for each query which is inefficient but relatively effective.
                ValidateQuery(GoodQuery, db, true);
            }
        }
Example #4
0
        public static void ValidateQuery(string query, SqlTestDB db, bool expectQueryToPass)
        {
            Console.WriteLine("Testing query '{0}'", query);
            Console.WriteLine("Loading model from DB");
            using (TSqlModel model = TSqlModel.LoadFromDatabase(db.BuildConnectionString()))
            {
                // Currently we just give a message but no source information. Will work around this for now by ensuring the model is fully valid before proceeding
                bool modelInitiallyValid = PrintModelState(model, true);
                if (!modelInitiallyValid)
                {
                    Console.WriteLine("Quitting due to invalid model");
                    return;
                }
                AssertModelHasNProcedures(model, 0);

                string myFileName = "QueryFile.sql";

                // validate bad query fails validation
                model.AddOrUpdateObjects(CreateQueryAsProc(query), myFileName, null);
                PrintModelState(model, expectQueryToPass);

                AssertModelHasNProcedures(model, 1);
            }
        }