Exemplo n.º 1
0
 /// <summary>
 /// Removes a DatabaseUpdateStep item to the collection.
 /// </summary>
 /// <param name="item">The item to remove.</param>
 public void Remove(DatabaseUpdateStep item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("item");
     }
     List.Remove(item);
 }
Exemplo n.º 2
0
 /// <summary>
 /// Inserts a DatabaseUpdateStep instance into the collection.
 /// </summary>
 /// <param name="item">The item to add.</param>
 public void Insert(int index, DatabaseUpdateStep item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("item");
     }
     List.Insert(index, item);
 }
Exemplo n.º 3
0
 /// <summary>
 /// Adds a DatabaseUpdateStep instance to the collection.
 /// </summary>
 /// <param name="item">The item to add.</param>
 public int Add(DatabaseUpdateStep item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("item");
     }
     return(List.Add(item));
 }
Exemplo n.º 4
0
 /// <summary>
 /// Discovers if the given item is in the collection.
 /// </summary>
 /// <param name="item">The item to find.</param>
 /// <returns>Returns true if the given item is in the collection.</returns>
 public bool Contains(DatabaseUpdateStep item)
 {
     if (IndexOf(item) == -1)
     {
         return(false);
     }
     else
     {
         return(true);
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// Returns the index of the item in the collection.
 /// </summary>
 /// <param name="item">The item to find.</param>
 /// <returns>The index of the item, or -1 if it is not found.</returns>
 public int IndexOf(DatabaseUpdateStep item)
 {
     return(List.IndexOf(item));
 }
Exemplo n.º 6
0
        /// <summary>
        /// Creates ot updates the database.
        /// </summary>
        /// <param name="create"></param>
        /// <param name="checkOnly"></param>
        // mbr - 28-09-2007 - case 814 - added args.
        private DatabaseUpdateCheckResults CreateUpdateDatabase(IOperationItem operation, bool create, bool checkOnly,
                                                                DatabaseUpdateArgs args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            // operation...
            if (operation == null)
            {
                operation = new OperationItem();
            }

            // set...
            operation.Status = "Loading existing schema...";

            // database...
            SqlSchema databaseSchema = null;

            if (create)
            {
                databaseSchema = new SqlSchema();
            }
            else
            {
                if (args.Trace)
                {
                    this.LogInfo(() => "Loading schema...");
                }

                // mbr - 28-09-2007 - case 814 - defer to a method that can limit the types...
//				databaseSchema = Database.GetSchema();fic
                databaseSchema = this.GetSchema(args);
                if (databaseSchema == null)
                {
                    throw new InvalidOperationException("databaseSchema is null.");
                }
            }

            // Create an entity schema
            SqlSchema entitySchema = new SqlSchema();

            // Load all the entity types from the path
            ArrayList entityTypes = new ArrayList();

            // mbr - 02-10-2007 - for c7 - changed so that we can limit the entity types...
            if (args.LimitEntityTypes.Count == 0)
            {
                // mbr - 04-10-2007 - case 851 - do not do this behaviour (oddly both of these statements are
                // actually identical),
//				entityTypes.AddRange(EntityType.GetAllEntityTypes());
//				this.MergeEntityTypes(entityTypes, EntityType.LoadFromAttributes(AssemblyPath));
//				foreach(Assembly asm in this.Assemblies)
//					this.MergeEntityTypes(entityTypes, EntityType.LoadFromAttributes(asm));

                // load...
                this.MergeEntityTypes(entityTypes, EntityType.GetEntityTypes());
            }
            else
            {
                this.MergeEntityTypes(entityTypes, args.LimitEntityTypes);
            }

            // log...
            if (args.Trace)
            {
                this.LogInfo(() => string.Format("Found '{0}' entity types.", entityTypes.Count));
            }

            // steps...
            DatabaseUpdateStepCollection steps = new DatabaseUpdateStepCollection();

            if (args.AddArrayParameterUdts)
            {
                steps.Add(new AddArrayParameterUdtsUpdateStep());
            }

            SyncSchemaDatabaseUpdateStep syncStep = new SyncSchemaDatabaseUpdateStep();

            steps.Add(syncStep);

            // mbr - 02-03-2006 - we can have update steps that don't have an entity type...
            TypeFinder finder = new TypeFinder(typeof(DatabaseUpdateStep));

            finder.AddAttributeSpecification(typeof(DatabaseUpdateStepAttribute), false);
            Type[] stepTypes = finder.GetTypes();
            if (stepTypes == null)
            {
                throw new InvalidOperationException("stepTypes is null.");
            }

            // mbr - 02-10-2007 - for c7...
            if (Database.ExtensibilityProvider == null)
            {
                throw new InvalidOperationException("Database.ExtensibilityProvider is null.");
            }

            // Walk each entity type and add to entitySchema
            foreach (EntityType entityType in entityTypes)
            {
                // mbr - 14-12-2005 - should we do it?
                bool skip = entityType.Type.IsDefined(typeof(SkipDatabaseUpdateAttribute), false);

                // mbr - 2010-01-29 - changed the log here to allow db update to do named databases...
//				if(!(skip) && entityType.UsesDefaultDatabase)
                bool ok = false;
                if (!(skip))
                {
                    // no named database, and entity has no named database...
                    if (!(args.HasDatabaseName) && entityType.UsesDefaultDatabase)
                    {
                        ok = true;
                    }
                    else if (args.HasDatabaseName && string.Compare(entityType.DatabaseName, args.DatabaseName, true, Cultures.System) == 0)
                    {
                        ok = true;
                    }
                }
                else
                {
                    ok = false;
                }

                // do we do it?
                if (ok)
                {
                    if (args.Trace)
                    {
                        this.LogInfo(() => string.Format("Touching '{0}' ({1})...", entityType.Name, entityType.Type.Assembly.GetName().Name));
                    }

                    // add the base table...
                    SqlTable coreTable = SqlTable.GetTable(entitySchema, entityType);
                    if (coreTable == null)
                    {
                        throw new InvalidOperationException("coreTable is null.");
                    }
                    entitySchema.Tables.Add(coreTable);

                    // type...
                    Type type = entityType.Type;
                    if (type == null)
                    {
                        throw new InvalidOperationException("type is null.");
                    }

                    // reload it - something weird happens with these and they can't be used with the metadata so reload it so that we're
                    // certain we have the right ones...
                    type = Type.GetType(type.AssemblyQualifiedName, true, true);
                    if (type == null)
                    {
                        throw new InvalidOperationException("type is null.");
                    }

                    // mbr - 02-10-2007 - for c7 - add other tables that we need...
                    Database.ExtensibilityProvider.AddSchemaTables(entityType, type, coreTable, entitySchema);

                    // add the custom units...
                    foreach (Type stepType in stepTypes)
                    {
                        // get the attribute...
                        DatabaseUpdateStepAttribute[] attrs = (DatabaseUpdateStepAttribute[])stepType.GetCustomAttributes(typeof(DatabaseUpdateStepAttribute), true);
                        if (attrs == null)
                        {
                            throw new InvalidOperationException("attrs is null.");
                        }

                        // walk...
                        foreach (DatabaseUpdateStepAttribute attr in attrs)
                        {
                            if (attr.EntityType != null && attr.EntityType.IsAssignableFrom(type))
                            {
                                // create...
                                DatabaseUpdateStep step = (DatabaseUpdateStep)Activator.CreateInstance(stepType, new object[] { type });
                                if (step == null)
                                {
                                    throw new InvalidOperationException("step is null.");
                                }

                                // add..
                                steps.Add(step);
                            }
                        }
                    }
                }
                else
                {
                    if (args.Trace)
                    {
                        this.LogInfo(() => string.Format("Skipping '{0}'.", entityType.Name));
                    }
                }
            }

            // mbr - 02-10-2007 - for c7 - don't do custom steps if we're limiting entity types...
            if (args.LimitEntityTypes.Count == 0)
            {
                // do the ones that don't have entity types...
                foreach (Type stepType in stepTypes)
                {
                    // get the attribute...
                    DatabaseUpdateStepAttribute[] attrs = (DatabaseUpdateStepAttribute[])stepType.GetCustomAttributes(typeof(DatabaseUpdateStepAttribute), true);
                    if (attrs == null)
                    {
                        throw new InvalidOperationException("attrs is null.");
                    }

                    // walk...
                    foreach (DatabaseUpdateStepAttribute attr in attrs)
                    {
                        if (attr.EntityType == null)
                        {
                            // create...
                            DatabaseUpdateStep step = (DatabaseUpdateStep)Activator.CreateInstance(stepType);
                            if (step == null)
                            {
                                throw new InvalidOperationException("step is null.");
                            }

                            // add..
                            steps.Add(step);
                        }
                    }
                }
            }

            // get the work units...
            operation.Status = "Creating schema delta...";

            // mbr - 02-10-2007 - for c7 - changed to deferral.
//			syncStep.WorkUnits.AddRange(entitySchema.GetSchemaWorkUnits(databaseSchema, operation));
            syncStep.Initialize(entitySchema, databaseSchema);

            // run...
            if (!(checkOnly))
            {
                if (args.Trace)
                {
                    this.LogInfo(() => string.Format("Applying '{0}' steps...", steps.Count));
                }

                // context...
                DatabaseUpdateContext context = new DatabaseUpdateContext(operation, args.Trace);

                // mbr - 21-12-2005 - run the steps...
                foreach (DatabaseUpdateStep step in steps)
                {
                    try
                    {
                        if (args.Trace)
                        {
                            this.LogInfo(() => string.Format("Applying step: {0}", step));
                        }

                        // set...
                        operation.Status = string.Format("Running step '{0}'...", step);
                        step.Execute(context);
                    }
                    catch (Exception ex)
                    {
                        // log...
                        string message = string.Format("Failed database update when running step '{0}'.", step);
                        if (this.Log.IsErrorEnabled)
                        {
                            this.Log.Error(message, ex);
                        }

                        // throw...
                        throw new InvalidOperationException(message, ex);
                    }
                }
            }
            else
            {
                if (args.Trace)
                {
                    this.LogInfo(() => "Checking only -- not doing work.");
                }
            }

            if (args.Trace)
            {
                this.LogInfo(() => "Database update finished.");
            }

            this.OnUpdated();

            // return...
            return(new DatabaseUpdateCheckResults(steps));
        }