Esempio n. 1
0
        public override void Process(PerformDBOperationExpression expression)
        {
            //RegisterExpression<PerformDBOperationExpression>(expression);

            Announcer.Say("Performing DB Operation");

            if (Options.PreviewOnly)
            {
                return;
            }

            if (Connection.State != ConnectionState.Open)
            {
                Connection.Open();
            }

            if (expression.Operation != null)
            {
                expression.Operation(Connection, Transaction);

                if (FBOptions.TransactionModel == FirebirdTransactionModel.AutoCommit)
                {
                    CommitRetaining();
                }
            }
        }
        public void CallingProcessWithPerformDbOperationExpressionWhenInPreviewOnlyModeWillNotMakeDbChanges()
        {
            bool tableExists;

            try
            {
                var expression =
                    new PerformDBOperationExpression
                {
                    Operation = (con, trans) =>
                    {
                        var command = con.CreateCommand();
                        command.CommandText = "CREATE TABLE ProcessTestTable (test int NULL) ";
                        command.Transaction = trans;

                        command.ExecuteNonQuery();
                    }
                };

                Processor.Process(expression);

                tableExists = Processor.TableExists("", "ProcessTestTable");
            }
            finally
            {
                Processor.RollbackTransaction();
            }

            tableExists.ShouldBeFalse();

            var fmOutput = Output.ToString();

            Assert.That(fmOutput, Does.Contain("/* Performing DB Operation */"));
        }
Esempio n. 3
0
        public override void ExecuteWith(IMigrationProcessor processor)
        {
            var exp = new PerformDBOperationExpression()
            {
                Operation = (connection, transaction) =>
                {
                    var helper = new ReferenceListDbHelper(connection, transaction);

                    var refListId = helper.GetReferenceListId(Namespace, Name);
                    if (refListId == null)
                    {
                        throw new Exception($"Reference list '{Namespace}.{Name}' not found");
                    }

                    if (DeleteAll)
                    {
                        // delete all if filter is not specified
                        helper.DeleteReferenceListItems(Namespace, Name);
                    }
                    else
                    if (ItemValue.HasValue)
                    {
                        helper.DeleteReferenceListItem(Namespace, Name, ItemValue.Value);
                    }
                }
            };

            processor.Process(exp);
        }
        private void CreateSequenceForIdentity(string tableName, string columnName)
        {
            CheckTable(tableName);
            LockTable(tableName);
            string sequenceName = GetSequenceName(tableName, columnName);

            if (!SequenceExists(string.Empty, sequenceName))
            {
                CreateSequenceExpression sequence = new CreateSequenceExpression()
                {
                    Sequence = new SequenceDefinition()
                    {
                        Name = sequenceName
                    }
                };
                Process(sequence);
            }
            string triggerName  = GetIdentityTriggerName(tableName, columnName);
            string quotedColumn = _quoter.Quote(columnName);
            string trigger      = string.Format("as begin if (NEW.{0} is NULL) then NEW.{1} = GEN_ID({2}, 1); end", quotedColumn, quotedColumn, _quoter.QuoteSequenceName(sequenceName, string.Empty));

            PerformDBOperationExpression createTrigger = CreateTriggerExpression(tableName, triggerName, true, TriggerEvent.Insert, trigger);

            Process(createTrigger);
        }
        public override void ExecuteWith(IMigrationProcessor processor)
        {
            var exp = new PerformDBOperationExpression()
            {
                Operation = (connection, transaction) =>
                {
                    var helper = new ReferenceListDbHelper(connection, transaction);
                    var id     = helper.GetReferenceListId(Namespace, Name);
                    if (id == null)
                    {
                        return;
                    }

                    if (Description.IsSet)
                    {
                        helper.UpdateReferenceListDescription(id, Description.Value);
                    }
                    if (NoSelectionValue.IsSet)
                    {
                        helper.UpdateReferenceListNoSelectionValue(id, NoSelectionValue.Value);
                    }
                }
            };

            processor.Process(exp);
        }
Esempio n. 6
0
        public PerformDBOperationExpression CreateTriggerExpression(string tableName, string triggerName, bool onBefore, TriggerEvent onEvent, string triggerBody)
        {
            tableName   = truncator.Truncate(tableName);
            triggerName = truncator.Truncate(triggerName);
            CheckTable(tableName);
            LockTable(tableName);
            PerformDBOperationExpression createTrigger = new PerformDBOperationExpression();

            createTrigger.Operation = (connection, transaction) =>
            {
                string triggerSql = String.Format(@"CREATE TRIGGER {0} FOR {1} ACTIVE {2} {3} POSITION 0 
                    {4}
                    ", quoter.Quote(triggerName), quoter.Quote(tableName),
                                                  onBefore ? "before" : "after",
                                                  onEvent.ToString().ToLower(),
                                                  triggerBody
                                                  );
                Announcer.Sql(triggerSql);
                using (var cmd = Factory.CreateCommand(triggerSql, connection, transaction))
                {
                    cmd.CommandTimeout = Options.Timeout;
                    cmd.ExecuteNonQuery();
                }
            };
            return(createTrigger);
        }
Esempio n. 7
0
        private void CreateSequenceForIdentity(string tableName, string columnName)
        {
            CheckTable(tableName);
            LockTable(tableName);
            string sequenceName = GetSequenceName(tableName, columnName);

            if (!SequenceExists(String.Empty, sequenceName))
            {
                CreateSequenceExpression sequence = new CreateSequenceExpression()
                {
                    Sequence = new SequenceDefinition()
                    {
                        Name = sequenceName
                    }
                };
                Process(sequence);
            }
            string triggerName  = GetIdentityTriggerName(tableName, columnName);
            string quotedColumn = quoter.Quote(columnName);
            string trigger      = String.Format("as begin if (NEW.{0} is NULL) then NEW.{1} = GEN_ID({2}, 1); end", quotedColumn, quotedColumn, quoter.QuoteSequenceName(sequenceName));

            PerformDBOperationExpression    createTrigger = CreateTriggerExpression(tableName, triggerName, true, TriggerEvent.Insert, trigger);
            PerformDBOperationExpression    deleteTrigger = DeleteTriggerExpression(tableName, triggerName);
            FirebirdProcessedExpressionBase fbExpression  = new FirebirdProcessedExpression(createTrigger, typeof(PerformDBOperationExpression), this);

            if (this.FBOptions.UndoEnabled)
            {
                fbExpression.AddUndoExpression(deleteTrigger);
            }
            RegisterExpression(fbExpression);
            Process(createTrigger);
        }
        private void DeleteSequenceForIdentity(string tableName, string columnName)
        {
            CheckTable(tableName);
            LockTable(tableName);

            string sequenceName;

            try{
                sequenceName = GetSequenceName(tableName, columnName);
            }
            catch (ArgumentException)
            {
                return;
            }

            DeleteSequenceExpression deleteSequence = null;

            if (SequenceExists(string.Empty, sequenceName))
            {
                deleteSequence = new DeleteSequenceExpression()
                {
                    SchemaName = string.Empty, SequenceName = sequenceName
                };
            }
            string triggerName = GetIdentityTriggerName(tableName, columnName);
            PerformDBOperationExpression deleteTrigger = DeleteTriggerExpression(tableName, triggerName);

            Process(deleteTrigger);

            if (deleteSequence != null)
            {
                Process(deleteSequence);
            }
        }
        public override void ExecuteWith(IMigrationProcessor processor)
        {
            var exp = new PerformDBOperationExpression()
            {
                Operation = (connection, transaction) =>
                {
                    var helper = new NotificationDbHelper(connection, transaction);

                    if (TemplateId.HasValue)
                    {
                        helper.DeleteNotificationTemplate(TemplateId.Value);
                    }
                    else if (DeleteAll)
                    {
                        var templateId = helper.GetNotificationId(Namespace, Name);
                        if (templateId == null)
                        {
                            throw new Exception($"Reference list '{Namespace}.{Name}' not found");
                        }

                        // delete all if filter is not specified
                        helper.DeleteNotificationTemplates(Namespace, Name);
                    }
                    else
                    {
                        throw new NotSupportedException();
                    }
                }
            };

            processor.Process(exp);
        }
        public void CanCreateAndDeleteTableUsingThePerformDBOperationExpressions()
        {
            var expression = new PerformDBOperationExpression
            {
                Operation = (connection, transaction) =>
                {
                    // I know I could be using the expressions to create and delete this table,
                    // but really I just want to test whether I can execute some commands against the connection.

                    var command = connection.CreateCommand();
                    command.Transaction = transaction;
                    command.CommandText = "CREATE TABLE dbo.TestTable(TestTableID int NULL)";

                    command.ExecuteNonQuery();

                    var command2 = connection.CreateCommand();
                    command2.Transaction = transaction;
                    command2.CommandText = "DROP TABLE dbo.TestTable";

                    command2.ExecuteNonQuery();
                }
            };

            ExecuteWithSqlServer2008(processor => processor.Process(expression), true);
            ExecuteWithSqlServer2012(processor => processor.Process(expression), true);
        }
Esempio n. 11
0
        /// <inheritdoc />
        public void WithConnection(Action <IDbConnection, IDbTransaction> operation)
        {
            var expression = new PerformDBOperationExpression {
                Operation = operation
            };

            _context.Expressions.Add(expression);
        }
        public override void Process(PerformDBOperationExpression expression)
        {
            EnsureConnectionIsOpen();

            if (expression.Operation != null)
            {
                expression.Operation(Connection, Transaction);
            }
        }
Esempio n. 13
0
        public void ErrorIsReturnedWhenOperationIsNull()
        {
            var expression = new PerformDBOperationExpression()
            {
                Operation = null
            };
            var errors = ValidationHelper.CollectErrors(expression);

            errors.ShouldContain(ErrorMessages.OperationCannotBeNull);
        }
Esempio n. 14
0
 public override void Process(PerformDBOperationExpression expression)
 {
     Logger.LogSay("Performing DB Operation");
     if (Options.PreviewOnly)
     {
         return;
     }
     EnsureConnectionIsOpen();
     expression.Operation?.Invoke(Connection, Transaction);
 }
        public override void ExecuteWith(IMigrationProcessor processor)
        {
            var exp = new PerformDBOperationExpression()
            {
                Operation = (connection, transaction) => {
                    var helper    = new NotificationDbHelper(connection, transaction);
                    var refListId = helper.InsertNotification(Namespace, Name, Description);
                }
            };

            processor.Process(exp);
        }
        public override void Process(PerformDBOperationExpression expression)
        {
            if (Connection.State != ConnectionState.Open)
            {
                Connection.Open();
            }

            if (expression.Operation != null)
            {
                expression.Operation(Connection, Transaction);
            }
        }
        public void CallingProcessWithPerformDbOperationExpressionWhenInPreviewOnlyModeWillNotMakeDbChanges()
        {
            var output = new StringWriter();

            var connection = new NpgsqlConnection(IntegrationTestOptions.Postgres.ConnectionString);

            var processor = new PostgresProcessor(
                connection,
                new PostgresGenerator(),
                new TextWriterAnnouncer(output),
                new ProcessorOptions {
                PreviewOnly = true
            },
                new PostgresDbFactory());

            bool tableExists;

            try
            {
                var expression =
                    new PerformDBOperationExpression
                {
                    Operation = (con, trans) =>
                    {
                        var command = con.CreateCommand();
                        command.CommandText = "CREATE TABLE processtesttable (test int NULL) ";
                        command.Transaction = trans;

                        command.ExecuteNonQuery();
                    }
                };

                processor.BeginTransaction();
                processor.Process(expression);

                var com = connection.CreateCommand();
                com.CommandText = "";

                tableExists = processor.TableExists("public", "processtesttable");
            }
            finally
            {
                processor.RollbackTransaction();
            }

            tableExists.ShouldBeFalse();
            output.ToString().ShouldBe(
                @"/* Beginning Transaction */
/* Performing DB Operation */
/* Rolling back transaction */
");
        }
Esempio n. 18
0
        public void CallingProcessWithPerformDbOperationExpressionWhenInPreviewOnlyModeWillNotMakeDbChanges()
        {
            var output = new StringWriter();

            var sp = CreateProcessorServices(
                services => services
                .AddSingleton <ILoggerProvider>(new SqlScriptFluentMigratorLoggerProvider(output))
                .ConfigureRunner(r => r.AsGlobalPreview()));

            using (sp)
            {
                using (var scope = sp.CreateScope())
                {
                    var processor = scope.ServiceProvider.GetRequiredService <IMigrationProcessor>();

                    bool tableExists;

                    try
                    {
                        var expression =
                            new PerformDBOperationExpression
                        {
                            Operation = (con, trans) =>
                            {
                                var command = con.CreateCommand();
                                command.CommandText = "CREATE TABLE processtesttable (test int NULL) ";
                                command.Transaction = trans;

                                command.ExecuteNonQuery();
                            }
                        };

                        processor.BeginTransaction();
                        processor.Process(expression);

                        tableExists = processor.TableExists("public", "processtesttable");
                    }
                    finally
                    {
                        processor.RollbackTransaction();
                    }

                    tableExists.ShouldBeFalse();
                    output.ToString().ShouldBe(
                        @"/* Beginning Transaction */
/* Performing DB Operation */
/* Rolling back transaction */
");
                }
            }
        }
Esempio n. 19
0
        public override void ExecuteWith(IMigrationProcessor processor)
        {
            var exp = new PerformDBOperationExpression()
            {
                Operation = (connection, transaction) =>
                {
                    var helper = new NotificationDbHelper(connection, transaction);

                    var notificationId = helper.GetNotificationId(Namespace, Name);
                    if (notificationId == null)
                    {
                        throw new Exception($"Notification '{Namespace}.{Name}' not found");
                    }

                    if (!Template.Id.IsSet)
                    {
                        Template.Id.Set(Guid.NewGuid());
                    }
                    var templateId = Template.Id.Value;

                    helper.InsertNotificationTemplate(notificationId.Value, Template);

                    if (Template.Name.IsSet)
                    {
                        helper.UpdateTemplateName(templateId, Template.Name.Value);
                    }
                    if (Template.Subject.IsSet)
                    {
                        helper.UpdateTemplateSubject(templateId, Template.Subject.Value);
                    }
                    if (Template.Body.IsSet)
                    {
                        helper.UpdateTemplateBody(templateId, Template.Body.Value);
                    }
                    if (Template.BodyFormat.IsSet)
                    {
                        helper.UpdateTemplateBodyFormat(templateId, Template.BodyFormat.Value);
                    }
                    if (Template.SendType.IsSet)
                    {
                        helper.UpdateTemplateSendType(templateId, Template.SendType.Value);
                    }
                    if (Template.IsEnabled.IsSet)
                    {
                        helper.UpdateTemplateIsEnabled(templateId, Template.IsEnabled.Value);
                    }
                }
            };

            processor.Process(exp);
        }
Esempio n. 20
0
        public override void ExecuteWith(IMigrationProcessor processor)
        {
            var exp = new PerformDBOperationExpression()
            {
                Operation = (connection, transaction) =>
                {
                    var helper = new NotificationDbHelper(connection, transaction);
                    helper.DeleteNotificationTemplates(Namespace, Name);
                    helper.DeleteNotification(Namespace, Name);
                }
            };

            processor.Process(exp);
        }
        public override void ExecuteWith(IMigrationProcessor processor)
        {
            var exp = new PerformDBOperationExpression()
            {
                Operation = (connection, transaction) =>
                {
                    var helper = new ReferenceListDbHelper(connection, transaction);
                    helper.DeleteReferenceListItems(Namespace, Name);
                    helper.DeleteReferenceList(Namespace, Name);
                }
            };

            processor.Process(exp);
        }
        public override void Process(PerformDBOperationExpression expression)
        {
            Logger.LogSay("Performing DB Operation");

            if (Options.PreviewOnly)
            {
                return;
            }

            EnsureConnectionIsOpen();

            // MySqlConnector requires the transaction to be set explicitly on the command, and it cannot be nested.
            expression.Operation?.Invoke(Connection, Transaction);
        }
Esempio n. 23
0
        private void DeleteSequenceForIdentity(string tableName, string columnName)
        {
            CheckTable(tableName);
            LockTable(tableName);

            string sequenceName;

            try{
                sequenceName = GetSequenceName(tableName, columnName);
            }
            catch (ArgumentException)
            {
                return;
            }

            DeleteSequenceExpression deleteSequence = null;

            if (SequenceExists(String.Empty, sequenceName))
            {
                deleteSequence = new DeleteSequenceExpression()
                {
                    SchemaName = String.Empty, SequenceName = sequenceName
                };
            }
            string triggerName = GetIdentityTriggerName(tableName, columnName);
            PerformDBOperationExpression    deleteTrigger = DeleteTriggerExpression(tableName, triggerName);
            FirebirdProcessedExpressionBase fbExpression  = new FirebirdProcessedExpression <PerformDBOperationExpression>(deleteTrigger, this);
            FirebirdSchemaProvider          schema        = new FirebirdSchemaProvider(this);
            List <TriggerInfo> triggers = schema.GetTableSchema(tableName).Triggers;

            foreach (TriggerInfo trigger in triggers)
            {
                if (trigger.Name.ToUpper() == triggerName.ToUpper())
                {
                    PerformDBOperationExpression createTrigger = CreateTriggerExpression(tableName, trigger);
                    if (this.FBOptions.UndoEnabled)
                    {
                        fbExpression.AddUndoExpression(createTrigger);
                    }
                    break;
                }
            }
            RegisterExpression(fbExpression);
            Process(deleteTrigger);

            if (deleteSequence != null)
            {
                Process(deleteSequence);
            }
        }
Esempio n. 24
0
        public void CallingProcessWithPerformDBOperationExpressionWhenInPreviewOnlyModeWillNotMakeDbChanges()
        {
            var output = new StringWriter();

            var connection = new SqlConnection(IntegrationTestOptions.SqlServer2008.ConnectionString);

            var processor = new SqlServerProcessor(
                connection,
                new SqlServer2008Generator(),
                new TextWriterAnnouncer(output),
                new ProcessorOptions {
                PreviewOnly = true
            },
                new SqlServerDbFactory());

            bool tableExists;

            try
            {
                var expression =
                    new PerformDBOperationExpression
                {
                    Operation = (con, trans) =>
                    {
                        var command = con.CreateCommand();
                        command.CommandText = "CREATE TABLE ProcessTestTable (test int NULL) ";
                        command.Transaction = trans;

                        command.ExecuteNonQuery();
                    }
                };

                processor.Process(expression);

                tableExists = processor.TableExists("", "ProcessTestTable");
            }
            finally
            {
                processor.RollbackTransaction();
            }

            tableExists.ShouldBeFalse();

            string fmOutput = output.ToString();

            Assert.That(fmOutput, Is.StringContaining("/* Beginning Transaction */"));
            Assert.That(fmOutput, Is.StringContaining("/* Performing DB Operation */"));
            Assert.That(fmOutput, Is.StringContaining("/* Rolling back transaction */"));
        }
Esempio n. 25
0
        public override void Process(PerformDBOperationExpression expression)
        {
            Announcer.Say("Performing DB Operation");

            if (Options.PreviewOnly)
            {
                return;
            }

            EnsureConnectionIsOpen();

            if (expression.Operation != null)
            {
                expression.Operation(Connection, null);
            }
        }
Esempio n. 26
0
        public override void ExecuteWith(IMigrationProcessor processor)
        {
            var exp = new PerformDBOperationExpression()
            {
                Operation = (connection, transaction) => {
                    var helper    = new ReferenceListDbHelper(connection, transaction);
                    var refListId = helper.InsertReferenceList(Namespace, Name, Description);

                    if (NoSelectionValue.IsSet)
                    {
                        helper.UpdateReferenceListNoSelectionValue(refListId, NoSelectionValue.Value);
                    }
                }
            };

            processor.Process(exp);
        }
Esempio n. 27
0
        public void CanCreateAndDeleteTableUsingThePerformDBOperationExpressions()
        {
            if (!IntegrationTestOptions.SqlServer2008.IsEnabled &&
                !IntegrationTestOptions.SqlServer2012.IsEnabled &&
                !IntegrationTestOptions.SqlServer2014.IsEnabled)
            {
                Assert.Ignore("No processor found for the given action.");
            }

            var expression = new PerformDBOperationExpression
            {
                Operation = (connection, transaction) =>
                {
                    // I know I could be using the expressions to create and delete this table,
                    // but really I just want to test whether I can execute some commands against the connection.

                    var command = connection.CreateCommand();
                    command.Transaction = transaction;
                    command.CommandText = "CREATE TABLE dbo.TestTable(TestTableID int NULL)";

                    command.ExecuteNonQuery();

                    var command2 = connection.CreateCommand();
                    command2.Transaction = transaction;
                    command2.CommandText = "DROP TABLE dbo.TestTable";

                    command2.ExecuteNonQuery();
                }
            };

            if (IntegrationTestOptions.SqlServer2008.IsEnabled)
            {
                ExecuteWithSqlServer2008(processor => processor.Process(expression), true, IntegrationTestOptions.SqlServer2008);
            }

            if (IntegrationTestOptions.SqlServer2012.IsEnabled)
            {
                ExecuteWithSqlServer2012(processor => processor.Process(expression), true, IntegrationTestOptions.SqlServer2012);
            }

            if (IntegrationTestOptions.SqlServer2014.IsEnabled)
            {
                ExecuteWithSqlServer2014(processor => processor.Process(expression), true, IntegrationTestOptions.SqlServer2014);
            }
        }
Esempio n. 28
0
        public override void ExecuteWith(IMigrationProcessor processor)
        {
            var exp = new PerformDBOperationExpression()
            {
                Operation = (connection, transaction) =>
                {
                    var helper = new ReferenceListDbHelper(connection, transaction);

                    var refListId = helper.GetReferenceListId(Namespace, Name);
                    if (refListId == null)
                    {
                        throw new Exception($"Reference list '{Namespace}.{Name}' not found");
                    }
                    helper.InsertReferenceListItem(refListId.Value, Item);
                }
            };

            processor.Process(exp);
        }
        public PerformDBOperationExpression DeleteTriggerExpression(string tableName, string triggerName)
        {
            tableName   = Truncator.Truncate(tableName);
            triggerName = Truncator.Truncate(triggerName);
            CheckTable(tableName);
            LockTable(tableName);
            PerformDBOperationExpression deleteTrigger = new PerformDBOperationExpression();

            deleteTrigger.Operation = (connection, transaction) =>
            {
                string triggerSql = string.Format("DROP TRIGGER {0}", _quoter.Quote(triggerName));
                Logger.LogSql(triggerSql);
                using (var cmd = CreateCommand(triggerSql, connection, transaction))
                {
                    cmd.ExecuteNonQuery();
                }
            };
            return(deleteTrigger);
        }
        public override void Process(PerformDBOperationExpression expression)
        {
            Announcer.Say("Performing DB Operation");

            if (Options.PreviewOnly)
            {
                return;
            }

            if (Connection.State != ConnectionState.Open)
            {
                Connection.Open();
            }

            if (expression.Operation != null)
            {
                expression.Operation(Connection, Transaction);
            }
        }