예제 #1
0
        /// <summary>
        /// Verify that a default exists on a column.
        /// </summary>
        /// <param name="connection">The connection to test.</param>
        /// <param name="table">The name of the table.</param>
        /// <param name="column">The name of the column.</param>
        /// <param name="value">If specified, the expected value of the default.</param>
        /// <returns>True if the default exists as expected.</returns>
        private bool DefaultExists(RecordingDbConnection connection, string table, string column, string value = null)
        {
            return(connection.DoNotLog(() =>
            {
                string definition = connection.ExecuteScalarSql <string>(@"SELECT definition
					FROM sys.default_constraints d
					JOIN sys.objects o ON (d.parent_object_id = o.object_id)
					JOIN sys.columns c ON (d.parent_object_id = c.object_id AND d.parent_column_id = c.column_id)
					WHERE o.name = @Table AND c.name = @Column"                    ,
                                                                         new Dictionary <string, object>()
                {
                    { "Table", table }, { "Column", column }
                });

                if (definition == null)
                {
                    return false;
                }

                if (value == null)
                {
                    return true;
                }

                return definition == value;
            }));
        }
        /// <summary>
        /// Manages the signatures of objects in the schema database
        /// </summary>
        /// <param name="connection">The connection to the database</param>
		/// <param name="schemaGroup">The name of the schema group to modify</param>
        public SchemaRegistry (RecordingDbConnection connection, string schemaGroup)
        {
			SchemaGroup = schemaGroup;
			Connection = connection;

			// make sure we have a table to work with
            EnsureSchemaTable();

			// load in the entries from the database
			Connection.DoNotLog(() =>
			{
				Entries = Connection.QuerySql(
					String.Format(CultureInfo.InvariantCulture, "SELECT * FROM [{0}] WHERE SchemaGroup = @SchemaGroup", SchemaRegistryTableName),
					new Dictionary<string, object>() { { "SchemaGroup", schemaGroup } }).Select(
					(dynamic e) => new SchemaRegistryEntry()
					{
						SchemaGroup = e.SchemaGroup,
						ObjectName = e.ObjectName,
						Signature = e.Signature,
						Type = (SchemaObjectType)Enum.Parse(typeof(SchemaObjectType), e.Type),
						OriginalOrder = e.OriginalOrder
					}).ToList();

				// automatically handle the old format for entries
				// WAS: 'ROLE [foo]'
				// NOW: '[foo]'
				foreach (var entry in Entries.Where(e =>
					e.Type == SchemaObjectType.Schema ||
					e.Type == SchemaObjectType.Login ||
					e.Type == SchemaObjectType.User ||
					e.Type == SchemaObjectType.Role ||
					e.Type == SchemaObjectType.Queue || 
					e.Type == SchemaObjectType.Service))
					entry.ObjectName = _registryUpgradeRegex.Replace(entry.ObjectName, "");

				// automatically reformat names to fully qualified name
				// WAS: '[foo]'
				// NOW: '[dbo].[foo]'
				foreach (var entry in Entries)
					entry.ObjectName = new SchemaObject(entry.Type, entry.ObjectName, null).Name;
			});
		}
예제 #3
0
        /// <summary>
        /// Verify all of the objects in the database and registry.
        /// </summary>
        /// <param name="schema">The schema to verify.</param>
        /// <param name="connection">The connection to use.</param>
        private static void VerifyObjectsAndRegistry(IEnumerable <string> schema, RecordingDbConnection connection)
        {
            connection.DoNotLog(() =>
            {
                // make sure the schema registry was updated
                SchemaRegistry registry = new SchemaRegistry(connection, TestSchemaGroup);

                // make sure all of the objects exist in the database
                foreach (var schemaObject in schema.Select(s => new SchemaObject(s)))
                {
                    // azure doesn't support xml index, so lets comment those out
                    if (schemaObject.Sql.Contains("XML INDEX") && connection.IsAzure())
                    {
                        continue;
                    }

                    Assert.True(schemaObject.Exists(connection), "Object {0} is missing from database", schemaObject.Name);
                    Assert.True(registry.Contains(schemaObject), "Object {0} is missing from registry", schemaObject.Name);
                }
            });
        }
		/// <summary>
		/// Verify that a default exists on a column.
		/// </summary>
		/// <param name="connection">The connection to test.</param>
		/// <param name="table">The name of the table.</param>
		/// <param name="column">The name of the column.</param>
		/// <param name="value">If specified, the expected value of the default.</param>
		/// <returns>True if the default exists as expected.</returns>
		private bool DefaultExists(RecordingDbConnection connection, string table, string column, string value = null)
		{
			return connection.DoNotLog(() =>
			{
				string definition = connection.ExecuteScalarSql<string>(@"SELECT definition
					FROM sys.default_constraints d
					JOIN sys.objects o ON (d.parent_object_id = o.object_id)
					JOIN sys.columns c ON (d.parent_object_id = c.object_id AND d.parent_column_id = c.column_id)
					WHERE o.name = @Table AND c.name = @Column",
					new Dictionary<string, object>() { { "Table", table }, { "Column", column } });

				if (definition == null)
					return false;

				if (value == null)
					return true;

				return definition == value;
			});
		}
		/// <summary>
		/// Verify all of the objects in the database and registry.
		/// </summary>
		/// <param name="schema">The schema to verify.</param>
		/// <param name="connection">The connection to use.</param>
		private static void VerifyObjectsAndRegistry(IEnumerable<string> schema, RecordingDbConnection connection)
		{
			connection.DoNotLog(() =>
			{
				// make sure the schema registry was updated
				SchemaRegistry registry = new SchemaRegistry(connection, TestSchemaGroup);

				// make sure all of the objects exist in the database
				foreach (var schemaObject in schema.Select(s => new SchemaObject(s)))
				{
					// azure doesn't support xml index, so lets comment those out
					if (schemaObject.Sql.Contains("XML INDEX") && connection.IsAzure())
						continue;

					Assert.True(schemaObject.Exists(connection), "Object {0} is missing from database", schemaObject.Name);
					Assert.True(registry.Contains(schemaObject), "Object {0} is missing from registry", schemaObject.Name);
				}
			});
		}
		/// <summary>
		/// Determine if this is a type of object that we can modify.
		/// </summary>
		/// <param name="type">The type of the object.</param>
		/// <returns>True if we know how to drop the object.</returns>
		internal bool CanModify(SchemaInstaller.InstallContext context, RecordingDbConnection connection)
		{
			return connection.DoNotLog(() => _implementation.CanModify(context, connection));
		}