예제 #1
0
		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);
			}
		}
예제 #2
0
        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);
            }
        }