public IValidationResult Run(MappingSet set)
        {
            IValidationResult result = new ValidationResult(this);

            DatabaseProcessor proc = new DatabaseProcessor();
            var mergeResults = proc.MergeDatabases(_realDatabase, set.Database);

            if (mergeResults.AnyChanges)
            {
                foreach (TableAdditionOperation tableOp in mergeResults.TableOperations.OfType<TableAdditionOperation>())
                {
                    // Additions show things that are in the generated database schema and not in the real one.
                    result.Issues.Add(new ValidationIssue("Table exists in your NHibernate mapping files but not in your database",
                                                          tableOp.Object, ValidationErrorLevel.Warning));
                }

                foreach (ColumnAdditionOperation columnOp in mergeResults.ColumnOperations.OfType<ColumnAdditionOperation>())
                {
                    result.Issues.Add(new ValidationIssue("Column exists in your NHibernate mapping files but not in your database",
                                                          columnOp.Object, ValidationErrorLevel.Warning));
                }
            }

            return result;
        }
        public void The_ResultSet_Has_Nothing_in_It()
        {
            Database db1 = TestDatabaseLoader.TestDatabase();
            Database db2 = TestDatabaseLoader.TestDatabase();

            DatabaseMergeResult result = new DatabaseProcessor().MergeDatabases(db1, db2);

            Assert.That(result.TableOperations, Is.Empty);
            Assert.That(result.ColumnOperations, Is.Empty);
            Assert.That(result.IndexOperations, Is.Empty);
            Assert.That(result.KeyOperations, Is.Empty);
        }
        private void RefreshSchema()
        {
            // Note: this is only ok because this method only runs on the UI thread.
            // Two instances of it will not run at once, so this is not a race condition.
            // The moment it can run from another thread, this assumption is false and the
            // code is incorrect.
            if (RefreshingSchema)
                return;

            databaseLock.WaitOne();

            RefreshingSchema = true;
            IDatabaseLoader loader = CreateDatabaseLoader(form);

            // Test connection first
            if (TestConnection(false) == false)
            {
                databaseLock.Set();
                return;
            }

            Thread thread = new Thread(
                () =>
                {
                    try
                    {
                        IDatabase newDb = loader.LoadDatabase(loader.DatabaseObjectsToFetch, null);
                        new DatabaseProcessor().CreateRelationships(newDb);

                        if (Database == null || Database.IsEmpty)
                        {
                            Database = newDb;
                            DatabaseConnector = loader.DatabaseConnector;
                            NewDatabaseCreated.RaiseEvent(this);
                            return;
                        }

                        var result = new DatabaseProcessor().MergeDatabases(Database, newDb);
                        if (result.AnyChanges)
                        {
                            mainPanel.ShowDatabaseRefreshResults(result, Database, newDb);
                            SchemaRefreshed.RaiseEvent(this);
                        }
                        else
                        {
                            form.SetDatabaseOperationResults(new DatabaseOperationResults("Schema Refresh", true, "No schema changes detected."));
                        }
                    }
                    finally
                    {
                        databaseLock.Set();
                        RefreshingSchema = false;
                    }
                });
            thread.Start();
        }
Beispiel #4
0
        private void RefreshSchema()
        {
            // Note: this is only ok because this method only runs on the UI thread.
            // Two instances of it will not run at once, so this is not a race condition.
            // The moment it can run from another thread, this assumption is false and the
            // code is incorrect.
            if (RefreshingSchema)
                return;

            Cursor = Cursors.WaitCursor;
            databaseLock.WaitOne();
            RefreshingSchema = true;
            IDatabaseLoader loader = CreateDatabaseLoader();

            // Test connection first
            if (TestConnection(false) == false)
            {
                databaseLock.Set();
                RefreshingSchema = false;
                Cursor = Cursors.Default;
                return;
            }
            labelNoChanges.Visible = false;
            labelNoChangesExport.Visible = false;

            Thread thread = new Thread(
                () =>
                {
                    try
                    {
                        List<string> schemasToLimit = Database.Tables.Union(Database.Views).Select(t => t.Schema).Distinct().ToList();
                        IDatabase newDb = loader.LoadDatabase(loader.DatabaseObjectsToFetch, schemasToLimit);
                        new DatabaseProcessor().CreateRelationships(newDb);

                        if (Database.Name == "New Database" && Database.Tables.Count == 0)
                        {
                            var mappingSet = new EntityModel.Controller.MappingLayer.MappingProcessor(new EntityModel.Controller.MappingLayer.OneToOneEntityProcessor())
                                .CreateOneToOneMapping(newDb, Database.MappingSet.TablePrefixes, Database.MappingSet.ColumnPrefixes, Database.MappingSet.TableSuffixes, Database.MappingSet.ColumnSuffixes);

                            MappingSet = mappingSet;
                            _Database = newDb;

                            if (NewDatabaseCreated != null)
                                NewDatabaseCreated(mappingSet);
                        }
                        else
                        {
                            var result = new DatabaseProcessor().MergeDatabases(Database, newDb);

                            if (result.AnyChanges)
                            {
                                databaseChanges1.Fill(result, Database, newDb);
                                modelChanges1.Fill(result, Database, newDb);
                                SetControlEnabled(buttonImport, true);
                            }
                            else
                                ShowNoChangesLabel();
                        }
                    }
                    finally
                    {
                        databaseLock.Set();
                        RefreshingSchema = false;
                        SetCursor(Cursors.Default);
                    }
                });
            thread.Start();
        }
        public override void RunCustomNewProjectLogic(ArchAngel.Interfaces.ProviderInfo theProviderInfo, IUserInteractor userInteractor)
        {
            //if (theProviderInfo is ArchAngel.Providers.EntityModel.ProviderInfo)
            //{
            //    ArchAngel.Providers.EntityModel.ProviderInfo providerInfo = (ArchAngel.Providers.EntityModel.ProviderInfo)theProviderInfo;
            //    providerInfo.Clear();
            //    return;
            //}

            try
            {
                log.Debug("Loading project...");
                userInteractor.UpdateWaitScreen("Loading project...");

                NHibernateHelper.ProviderInfo providerInfo = (NHibernateHelper.ProviderInfo)theProviderInfo;
                providerInfo.Clear();

                log.Debug("Loading database...");
                Database database = DatabaseLoader.LoadDatabase(DatabaseLoader.DatabaseObjectsToFetch, null);
                DatabaseProcessor dbProcessor = new DatabaseProcessor();
                dbProcessor.LogErrors = true;
                log.Debug("Creating relationships...");
                dbProcessor.CreateRelationships(database);

                if (dbProcessor.Errors.Count > 0)
                {
                    log.Debug("Database errors exist..." + dbProcessor.Errors.Count.ToString());
                    UI.FormErrors form = new UI.FormErrors("<b><font color='Red'>Note:</font></b> Database problems exist. Please <b>fix</b> these problems (or <b>omit the tables</b> in question) before trying again.", dbProcessor.Errors);
                    form.ShowDialog();
                    return;
                }
                log.Debug("Creating 1 to 1 mappings...");
                var mappingSet = new MappingProcessor(new OneToOneEntityProcessor())
                    .CreateOneToOneMapping(database, this.TablePrefixes, this.ColumnPrefixes, this.TableSuffixes, this.ColumnSuffixes);

                foreach (var entity in mappingSet.EntitySet.Entities)
                {
                    ArchAngel.Interfaces.ProjectOptions.ModelScripts.Scripts.ExistingPropertyNames = new List<string>();

                    foreach (Property prop in entity.Properties)
                    {
                        IColumn mappedCol = prop.MappedColumn();

                        ArchAngel.Interfaces.Scripting.NHibernate.Model.IColumn scriptCol = new Interfaces.Scripting.NHibernate.Model.IColumn()
                        {
                            IsNullable = mappedCol.IsNullable,
                            //IsText =
                            Length = mappedCol.Size,
                            Name = mappedCol.Name,
                            ScriptObject = mappedCol,
                            Type = mappedCol.OriginalDataType
                        };
                        prop.Name = ArchAngel.Interfaces.ProjectOptions.ModelScripts.Scripts.GetPropertyName(scriptCol);
                    }
                }
                providerInfo.EntityProviderInfo.MappingSet = mappingSet;
                /////////////////////////////////
                providerInfo.EntityProviderInfo.Engine.AddModule(new NHibernateProjectLoaderModule(database));
                // Then run the validation rules
                log.Debug("Validating model...");
                userInteractor.UpdateWaitScreen("Validating model...");
                //var rulesEngine = new ValidationRulesEngine(mappingSet);
                //rulesEngine.AddModule(new NHibernateProjectLoaderModule(database));
                log.Debug("Running validation rules...");
                providerInfo.EntityProviderInfo.RunValidationRules();//rulesEngine);
            }
            //catch (Exception ex)
            //{
            //    MessageBox.
            //}
            finally
            {
                log.Debug("Removing wait screen...");
                userInteractor.RemoveWaitScreen();
            }
        }