Beispiel #1
0
 /// <summary>
 /// Run a test and clean up the databases when complete.
 /// </summary>
 /// <param name="connectionString">The connection string for the database.</param>
 /// <param name="action">The test to run.</param>
 protected static void TestWithDrop(string connectionString, Action action)
 {
     try
     {
         action();
     }
     finally
     {
         SchemaInstaller.DropDatabase(connectionString);
     }
 }
Beispiel #2
0
        public void TestDropDatabase([ValueSource("ConnectionStrings")] string connectionString)
        {
            TestWithDrop(connectionString, () =>
            {
                // create the database if it doesn't exist
                if (!SchemaInstaller.DatabaseExists(connectionString))
                {
                    SchemaInstaller.CreateDatabase(connectionString);
                }

                // drop the database
                Assert.True(SchemaInstaller.DropDatabase(connectionString));

                // make sure the database doesn't exist
                Assert.False(SchemaInstaller.DatabaseExists(connectionString));

                // drop the database again, it should return false
                Assert.False(SchemaInstaller.DropDatabase(connectionString));
            });
        }
Beispiel #3
0
        public void TestCreateDatabaseWithPath([ValueSource("ConnectionStrings")] string connectionString)
        {
            TestWithDrop(connectionString, () =>
            {
                // drop the database if it already exists
                if (SchemaInstaller.DatabaseExists(connectionString))
                {
                    SchemaInstaller.DropDatabase(connectionString);
                }

                // create the database
                Assert.True(SchemaInstaller.CreateDatabase(connectionString, ConfigurationManager.AppSettings["filepath"] ?? Environment.GetEnvironmentVariable("TEMP")));

                // make sure the database exises
                Assert.True(SchemaInstaller.DatabaseExists(connectionString));

                // create the database again, it should return false
                Assert.False(SchemaInstaller.CreateDatabase(connectionString));
            });
        }
        static void Main(string[] args)
        {
            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i].ToUpperInvariant())
                {
                case "-SERVER":
                    Server = args[++i];
                    break;

                case "-DATABASE":
                    Database = args[++i];
                    break;

                case "-SKIPTO":
                    Skip = Int32.Parse(args[++i]) - 1;
                    break;

                case "-TAKE":
                    Take = Int32.Parse(args[++i]);
                    break;

                case "-CLEAN":
                    Clean = true;
                    break;

                case "-FILTER":
                    TypeFilter = (SchemaObjectType)Enum.Parse(typeof(SchemaObjectType), args[++i]);
                    break;

                case "-SMO":
                    SMOTest = true;
                    break;

                case "-SCRIPT":
                    ScriptOnly = true;
                    break;

                default:
                    if (Filename == null)
                    {
                        Filename = args[i];
                    }
                    break;
                }
            }

            // set up the connection string
            ConnectionString.InitialCatalog     = Database;
            ConnectionString.DataSource         = Server;
            ConnectionString.IntegratedSecurity = true;

            // drop the database if starting clean
            if (Clean)
            {
                SchemaInstaller.DropDatabase(ConnectionString.ConnectionString);
            }

            // make sure we are always working with an empty database
            if (!CreateDatabase())
            {
                return;
            }

            // load the schema
            SchemaObjectCollection schema = LoadSchema();

            try
            {
                // install the schema as is
                using (SqlConnection connection = new SqlConnection(ConnectionString.ConnectionString))
                {
                    connection.Open();

                    // install it the first time
                    SchemaInstaller installer = new SchemaInstaller(connection);
                    installer.Install("Test", schema);
                    schema.Verify(connection);

                    // script the result through SMO
                    Console.WriteLine("Scripting database");
                    List <string> originalScript = null;
                    if (SMOTest)
                    {
                        originalScript = ScriptDatabaseWithSMO().ToList();
                        originalScript.Sort();
                    }

                    //  run test cases that modify each of the elements
                    for (int i = Skip; i < schema.Count; i++)
                    {
                        if (Take-- <= 0)
                        {
                            return;
                        }

                        // if a type filter is defined, then filter by that type
                        if (TypeFilter.HasValue && schema[i].SchemaObjectType != TypeFilter.Value)
                        {
                            continue;
                        }

                        // if the type can't be modified, then don't test it
                        if (!schema[i].CanModify(connection))
                        {
                            Console.WriteLine();
                            Console.WriteLine("Not testing modification of {0} {1}", schema[i].SchemaObjectType, schema[i].SqlName.FullName);
                            continue;
                        }

                        // make sure all of the objects are there
                        try
                        {
                            Console.Write('\r');
                            Console.Write(new String(' ', Console.WindowWidth - 1));
                            Console.Write('\r');
                            Console.Write("Testing modifications {0}/{1}", (i + 1), schema.Count);

                            // modify the schema and re-install it
                            schema[i] = new SchemaObject(schema[i].Sql + " -- MODIFIED");

                            if (ScriptOnly)
                            {
                                Console.WriteLine(installer.ScriptChanges("Test", schema));
                            }
                            else
                            {
                                installer.Install("Test", schema);
                            }

                            // make sure all of the objects are there
                            if (SMOTest)
                            {
                                // script the whole database
                                var updatedScript = ScriptDatabaseWithSMO().ToList();
                                updatedScript.Sort();
                                MatchScripts(originalScript, updatedScript);
                            }
                            else
                            {
                                // just verify the dependencies
                                schema.Verify(connection);
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine();
                            Console.WriteLine("ERROR While modifying:");
                            Console.WriteLine(schema[i].Name);
                            Console.WriteLine(e.ToString());

                            throw;
                        }
                    }

                    Console.WriteLine();
                }
            }
            finally
            {
                Console.WriteLine("Dropping database");
                SchemaInstaller.DropDatabase(ConnectionString.ConnectionString);
            }
        }