public void AlterTable_MigrationRequiresAutomaticDelete_AndProcessorHasUndoDisabled_ShouldNotThrow() { using (var tempDb = new TemporaryDatabase(IntegrationTestOptions.Firebird, _firebirdLibraryProber)) { using (var serviceProvider = CreateServiceProvider(tempDb.ConnectionString, "FluentMigrator.Tests.Integration.Migrations")) { var runner = serviceProvider.GetRequiredService <IMigrationRunner>(); var processor = serviceProvider.GetRequiredService <FirebirdProcessor>(); runner.Up(new MigrationWhichCreatesTwoRelatedTables()); processor.CommitTransaction(); FbConnection.ClearPool((FbConnection)processor.Connection); } //---------------Assert Precondition---------------- Assert.IsTrue(ForeignKeyExists(tempDb.ConnectionString, MigrationWhichCreatesTwoRelatedTables.ForeignKeyName), "Foreign key does not exist after first migration"); using (var serviceProvider = CreateServiceProvider(tempDb.ConnectionString, "FluentMigrator.Tests.Integration.Migrations")) { var runner = serviceProvider.GetRequiredService <IMigrationRunner>(); var processor = serviceProvider.GetRequiredService <FirebirdProcessor>(); runner.Up(new MigrationWhichAltersTableWithFK()); processor.CommitTransaction(); } Assert.IsTrue(ForeignKeyExists(tempDb.ConnectionString, MigrationWhichCreatesTwoRelatedTables.ForeignKeyName), "Foreign key does not exist after second migration"); } }
private bool ForeignKeyExists(string connectionString, string withName) { using (var connection = new FbConnection(connectionString)) { connection.Open(); var keyQuery = String.Format(@" SELECT rc.RDB$CONSTRAINT_NAME AS constraint_name, i.RDB$RELATION_NAME AS table_name, s.RDB$FIELD_NAME AS field_name, i.RDB$DESCRIPTION AS description, rc.RDB$DEFERRABLE AS is_deferrable, rc.RDB$INITIALLY_DEFERRED AS is_deferred, refc.RDB$UPDATE_RULE AS on_update, refc.RDB$DELETE_RULE AS on_delete, refc.RDB$MATCH_OPTION AS match_type, i2.RDB$RELATION_NAME AS references_table, s2.RDB$FIELD_NAME AS references_field, (s.RDB$FIELD_POSITION + 1) AS field_position FROM RDB$INDEX_SEGMENTS s LEFT JOIN RDB$INDICES i ON i.RDB$INDEX_NAME = s.RDB$INDEX_NAME LEFT JOIN RDB$RELATION_CONSTRAINTS rc ON rc.RDB$INDEX_NAME = s.RDB$INDEX_NAME LEFT JOIN RDB$REF_CONSTRAINTS refc ON rc.RDB$CONSTRAINT_NAME = refc.RDB$CONSTRAINT_NAME LEFT JOIN RDB$RELATION_CONSTRAINTS rc2 ON rc2.RDB$CONSTRAINT_NAME = refc.RDB$CONST_NAME_UQ LEFT JOIN RDB$INDICES i2 ON i2.RDB$INDEX_NAME = rc2.RDB$INDEX_NAME LEFT JOIN RDB$INDEX_SEGMENTS s2 ON i2.RDB$INDEX_NAME = s2.RDB$INDEX_NAME WHERE rc.RDB$CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY s.RDB$FIELD_POSITION", MigrationWhichCreatesTwoRelatedTables.ForeignKeyName); var cmd = connection.CreateCommand(); cmd.CommandText = keyQuery; var result = false; using (var rdr = cmd.ExecuteReader()) { while (rdr.Read()) { try { var constraintName = rdr["CONSTRAINT_NAME"]; if (constraintName == null) { continue; } if (constraintName is DBNull) { continue; } if (constraintName.ToString().Trim() == withName) { result = true; break; } } catch { } } } connection.Close(); FbConnection.ClearPool(connection); return(result); } }
public void AlterTable_MigrationRequiresAutomaticDelete_AndProcessorHasUndoDisabled_ShouldNotThrow() { var tempResources = WriteOutFirebirdEmbeddedLibrariesToCurrentWorkingDirectory(); var tempFile = Path.GetTempFileName(); using (var deleter = new AutoDeleter(tempFile)) { File.Delete(tempFile); deleter.Add(tempResources); var connectionString = GetConnectionStringToTempDatabaseAt(tempFile); var runnerContext = new RunnerContext(new TextWriterAnnouncer(System.Console.Out)) { Namespace = "FluentMigrator.Tests.Integration.Migrations" }; try { using (var connection = new FbConnection(connectionString)) { FirebirdProcessor processor; var announcer = new TextWriterAnnouncer(System.Console.Out); announcer.ShowSql = true; var options = FirebirdOptions.AutoCommitBehaviour(); options.TruncateLongNames = false; processor = new FirebirdProcessor(connection, new FirebirdGenerator(options), announcer, new ProcessorOptions(), new FirebirdDbFactory(), options); processor.FBOptions.UndoEnabled = false; var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), runnerContext, processor); runner.Up(new MigrationWhichCreatesTwoRelatedTables()); processor.CommitTransaction(); FbConnection.ClearPool(connection); } //---------------Assert Precondition---------------- Assert.IsTrue(ForeignKeyExists(connectionString, MigrationWhichCreatesTwoRelatedTables.ForeignKeyName), "Foreign key does not exist after first migration"); using (var connection = new FbConnection(connectionString)) { FirebirdProcessor processor; var announcer = new TextWriterAnnouncer(System.Console.Out); announcer.ShowSql = true; var options = FirebirdOptions.AutoCommitBehaviour(); processor = new FirebirdProcessor(connection, new FirebirdGenerator(options), announcer, new ProcessorOptions(), new FirebirdDbFactory(), options); processor.FBOptions.UndoEnabled = false; var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), runnerContext, processor); runner.Up(new MigrationWhichAltersTableWithFK()); processor.CommitTransaction(); } Assert.IsTrue(ForeignKeyExists(connectionString, MigrationWhichCreatesTwoRelatedTables.ForeignKeyName), "Foreign key does not exist after second migration"); } catch (Exception ex) { try { File.Copy(tempFile, "C:\\tmp\\fm_tests.fdb", true); } catch { } throw ex; } } }
public void FbConeccionPrincipal() { _Conect = new FbConnection(); // _Conect.ConnectionString = "User=SYSDBA;password=masterkey;DataSource=localhost;Database=BDCrmPtics;Charset=NONE;Dialect=3;Max Pool Size=1024;"; _Conect.ConnectionString = "User=SYSDBA;password=masterkey;DataSource=localhost;pooling=false;port=3050;Database=181.129.170.198:BDCrmPtics;Charset=NONE;Dialect=3;Max Pool Size=1024;"; FbConnection.ClearPool(_Conect); FbConnection.ClearAllPools(); }
public virtual void Generate(FbDropDatabaseOperation operation, IModel model, MigrationCommandListBuilder builder) { if (Options.HasFlag(MigrationsSqlGenerationOptions.Script)) { throw new NotSupportedException("Dropping database from script is not supported."); } FbConnection.ClearPool(operation.ConnectionString); FbConnection.DropDatabase(operation.ConnectionString); }
public override void Dispose() { if (DataContext is DataConnection dc && dc.Connection is FbConnection fbc) { FbConnection.ClearPool(fbc); } DataContext.Close(); FirebirdTools.ClearAllPools(); base.Dispose(); }
public void TearDown() { if (!_processor.WasCommitted) { _processor.CommitTransaction(); } _connection.Close(); FbConnection.ClearPool(_connection); FbConnection.DropDatabase(IntegrationTestOptions.Firebird.ConnectionString); }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { var connectionBuilder = new FbConnectionStringBuilder("database=localhost:EFCore.fdb;user=sysdba;password=masterkey"); FbConnection.ClearPool(new FbConnection(connectionBuilder.ToString())); connectionBuilder.Pooling = true; connectionBuilder.MaxPoolSize = 200; optionsBuilder.UseFirebird(connectionBuilder.ToString()); }
public void ObsoleteAlterTable_MigrationRequiresAutomaticDelete_AndProcessorHasUndoDisabled_ShouldNotThrow() { using (var tempDb = new TemporaryDatabase(IntegrationTestOptions.Firebird, _firebirdLibraryProber)) { var connectionString = tempDb.ConnectionString; var runnerContext = new RunnerContext(new TextWriterAnnouncer(TestContext.Out)) { Namespace = "FluentMigrator.Tests.Integration.Migrations" }; using (var connection = new FbConnection(connectionString)) { var announcer = new TextWriterAnnouncer(TestContext.Out) { ShowSql = true }; var options = FirebirdOptions.AutoCommitBehaviour(); options.TruncateLongNames = false; var processor = new FirebirdProcessor(connection, new FirebirdGenerator(options), announcer, new ProcessorOptions(), new FirebirdDbFactory(), options); var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), runnerContext, processor); runner.Up(new MigrationWhichCreatesTwoRelatedTables()); processor.CommitTransaction(); FbConnection.ClearPool(connection); } //---------------Assert Precondition---------------- Assert.IsTrue(ForeignKeyExists(connectionString, MigrationWhichCreatesTwoRelatedTables.ForeignKeyName), "Foreign key does not exist after first migration"); using (var connection = new FbConnection(connectionString)) { var announcer = new TextWriterAnnouncer(TestContext.Out) { ShowSql = true }; var options = FirebirdOptions.AutoCommitBehaviour(); var processor = new FirebirdProcessor(connection, new FirebirdGenerator(options), announcer, new ProcessorOptions(), new FirebirdDbFactory(), options); var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), runnerContext, processor); runner.Up(new MigrationWhichAltersTableWithFK()); processor.CommitTransaction(); } Assert.IsTrue(ForeignKeyExists(connectionString, MigrationWhichCreatesTwoRelatedTables.ForeignKeyName), "Foreign key does not exist after second migration"); } }
public virtual void Generate(FbDropDatabaseOperation operation, IModel model, MigrationCommandListBuilder builder) { FbConnection.ClearPool(operation.ConnectionString); FbConnection.DropDatabase(operation.ConnectionString); }
private async Task ExecuteImpl(bool autoCommit, AsyncWrappingCommonArgs async) { if ((_statements?.Count ?? 0) == 0) { throw new InvalidOperationException("There are no commands for execution."); } _shouldClose = false; foreach (var statement in Statements) { if (!(statement.StatementType == SqlStatementType.Connect || statement.StatementType == SqlStatementType.CreateDatabase || statement.StatementType == SqlStatementType.Disconnect || statement.StatementType == SqlStatementType.DropDatabase || statement.StatementType == SqlStatementType.SetAutoDDL || statement.StatementType == SqlStatementType.SetDatabase || statement.StatementType == SqlStatementType.SetNames || statement.StatementType == SqlStatementType.SetSQLDialect)) { await ProvideCommand(async).ConfigureAwait(false); _sqlCommand.CommandText = statement.Text; if (_sqlTransaction == null && !(statement.StatementType == SqlStatementType.Commit || statement.StatementType == SqlStatementType.Rollback)) { _sqlTransaction = await _sqlConnection.BeginTransactionImpl(FbTransaction.DefaultIsolationLevel, null, async).ConfigureAwait(false); } _sqlCommand.Transaction = _sqlTransaction; } try { switch (statement.StatementType) { case SqlStatementType.AlterCharacterSet: case SqlStatementType.AlterDatabase: case SqlStatementType.AlterDomain: case SqlStatementType.AlterException: case SqlStatementType.AlterFunction: case SqlStatementType.AlterIndex: case SqlStatementType.AlterPackage: case SqlStatementType.AlterProcedure: case SqlStatementType.AlterRole: case SqlStatementType.AlterSequence: case SqlStatementType.AlterTable: case SqlStatementType.AlterTrigger: case SqlStatementType.AlterView: case SqlStatementType.CommentOn: case SqlStatementType.CreateCollation: case SqlStatementType.CreateDomain: case SqlStatementType.CreateException: case SqlStatementType.CreateFunction: case SqlStatementType.CreateGenerator: case SqlStatementType.CreateIndex: case SqlStatementType.CreatePackage: case SqlStatementType.CreatePackageBody: case SqlStatementType.CreateProcedure: case SqlStatementType.CreateRole: case SqlStatementType.CreateSequence: case SqlStatementType.CreateShadow: case SqlStatementType.CreateTable: case SqlStatementType.CreateTrigger: case SqlStatementType.CreateView: case SqlStatementType.DeclareCursor: case SqlStatementType.DeclareExternalFunction: case SqlStatementType.DeclareFilter: case SqlStatementType.DeclareStatement: case SqlStatementType.DeclareTable: case SqlStatementType.Delete: case SqlStatementType.DropCollation: case SqlStatementType.DropDomain: case SqlStatementType.DropException: case SqlStatementType.DropExternalFunction: case SqlStatementType.DropFunction: case SqlStatementType.DropFilter: case SqlStatementType.DropGenerator: case SqlStatementType.DropIndex: case SqlStatementType.DropPackage: case SqlStatementType.DropPackageBody: case SqlStatementType.DropProcedure: case SqlStatementType.DropSequence: case SqlStatementType.DropRole: case SqlStatementType.DropShadow: case SqlStatementType.DropTable: case SqlStatementType.DropTrigger: case SqlStatementType.DropView: case SqlStatementType.EventInit: case SqlStatementType.EventWait: case SqlStatementType.Execute: case SqlStatementType.ExecuteImmediate: case SqlStatementType.ExecuteProcedure: case SqlStatementType.Grant: case SqlStatementType.Insert: case SqlStatementType.InsertCursor: case SqlStatementType.Merge: case SqlStatementType.Open: case SqlStatementType.Prepare: case SqlStatementType.Revoke: case SqlStatementType.RecreateFunction: case SqlStatementType.RecreatePackage: case SqlStatementType.RecreatePackageBody: case SqlStatementType.RecreateProcedure: case SqlStatementType.RecreateTable: case SqlStatementType.RecreateTrigger: case SqlStatementType.RecreateView: case SqlStatementType.SetGenerator: case SqlStatementType.Update: case SqlStatementType.Whenever: OnCommandExecuting(_sqlCommand, statement.StatementType); var rowsAffected = await ExecuteCommand(autoCommit, async).ConfigureAwait(false); _requiresNewConnection = false; OnCommandExecuted(null, statement.Text, statement.StatementType, rowsAffected); break; case SqlStatementType.ExecuteBlock: case SqlStatementType.Select: (await ProvideCommand(async).ConfigureAwait(false)).CommandText = statement.Text; OnCommandExecuting(_sqlCommand, statement.StatementType); var dataReader = await _sqlCommand.ExecuteReaderImpl(CommandBehavior.Default, async).ConfigureAwait(false); try { _requiresNewConnection = false; OnCommandExecuted(dataReader, statement.Text, statement.StatementType, -1); } finally { #if NET48 || NETSTANDARD2_0 dataReader.Dispose(); #else await async.AsyncSyncCallNoCancellation(dataReader.DisposeAsync, dataReader.Dispose).ConfigureAwait(false); #endif } break; case SqlStatementType.Commit: OnCommandExecuting(null, statement.StatementType); await CommitTransaction(async).ConfigureAwait(false); OnCommandExecuted(null, statement.Text, statement.StatementType, -1); break; case SqlStatementType.Rollback: OnCommandExecuting(null, statement.StatementType); await RollbackTransaction(async).ConfigureAwait(false); OnCommandExecuted(null, statement.Text, statement.StatementType, -1); break; case SqlStatementType.CreateDatabase: OnCommandExecuting(null, statement.StatementType); await CreateDatabase(statement.CleanText, async).ConfigureAwait(false); _requiresNewConnection = false; OnCommandExecuted(null, statement.Text, statement.StatementType, -1); break; case SqlStatementType.DropDatabase: OnCommandExecuting(null, statement.StatementType); await async.AsyncSyncCall(FbConnection.DropDatabaseAsync, FbConnection.DropDatabase, _connectionString.ToString()).ConfigureAwait(false); _requiresNewConnection = true; OnCommandExecuted(null, statement.Text, statement.StatementType, -1); break; case SqlStatementType.Connect: OnCommandExecuting(null, statement.StatementType); await ConnectToDatabase(statement.CleanText, async).ConfigureAwait(false); _requiresNewConnection = false; OnCommandExecuted(null, statement.Text, statement.StatementType, -1); break; case SqlStatementType.Disconnect: OnCommandExecuting(null, statement.StatementType); await _sqlConnection.CloseImpl(async).ConfigureAwait(false); FbConnection.ClearPool(_sqlConnection); _requiresNewConnection = false; OnCommandExecuted(null, statement.Text, statement.StatementType, -1); break; case SqlStatementType.SetAutoDDL: OnCommandExecuting(null, statement.StatementType); SetAutoDdl(statement.CleanText, ref autoCommit); _requiresNewConnection = false; OnCommandExecuted(null, statement.Text, statement.StatementType, -1); break; case SqlStatementType.SetNames: OnCommandExecuting(null, statement.StatementType); SetNames(statement.CleanText); _requiresNewConnection = true; OnCommandExecuted(null, statement.Text, statement.StatementType, -1); break; case SqlStatementType.SetSQLDialect: OnCommandExecuting(null, statement.StatementType); SetSqlDialect(statement.CleanText); _requiresNewConnection = true; OnCommandExecuted(null, statement.Text, statement.StatementType, -1); break; case SqlStatementType.Fetch: case SqlStatementType.Describe: break; case SqlStatementType.SetDatabase: case SqlStatementType.SetStatistics: case SqlStatementType.SetTransaction: case SqlStatementType.ShowSQLDialect: throw new NotImplementedException(); } } catch (Exception ex) { await DisposeCommand(async).ConfigureAwait(false); await RollbackTransaction(async).ConfigureAwait(false); await CloseConnection(async).ConfigureAwait(false); throw new FbException(string.Format("An exception was thrown when executing command: {1}.{0}Batch execution aborted.{0}The returned message was: {2}.", Environment.NewLine, statement.Text, ex.Message), ex); } } await DisposeCommand(async).ConfigureAwait(false); await CommitTransaction(async).ConfigureAwait(false); await CloseConnection(async).ConfigureAwait(false); }
/// <summary> /// Starts the ordered execution of the SQL statements that are in <see cref="SqlStatements"/> collection. /// </summary> /// <param name="autoCommit">Specifies if the transaction should be committed after a DDL command execution</param> public void Execute(bool autoCommit = true) { if ((_statements?.Count ?? 0) == 0) { throw new InvalidOperationException("There are no commands for execution."); } _shouldClose = false; foreach (var statement in Statements) { if (!(statement.StatementType == SqlStatementType.Connect || statement.StatementType == SqlStatementType.CreateDatabase || statement.StatementType == SqlStatementType.Disconnect || statement.StatementType == SqlStatementType.DropDatabase || statement.StatementType == SqlStatementType.SetAutoDDL || statement.StatementType == SqlStatementType.SetDatabase || statement.StatementType == SqlStatementType.SetNames || statement.StatementType == SqlStatementType.SetSQLDialect)) { ProvideCommand(); _sqlCommand.CommandText = statement.Text; if (_sqlTransaction == null && !(statement.StatementType == SqlStatementType.Commit || statement.StatementType == SqlStatementType.Rollback)) { _sqlTransaction = _sqlConnection.BeginTransaction(); } _sqlCommand.Transaction = _sqlTransaction; } try { switch (statement.StatementType) { case SqlStatementType.AlterCharacterSet: case SqlStatementType.AlterDatabase: case SqlStatementType.AlterDomain: case SqlStatementType.AlterException: case SqlStatementType.AlterFunction: case SqlStatementType.AlterIndex: case SqlStatementType.AlterPackage: case SqlStatementType.AlterProcedure: case SqlStatementType.AlterRole: case SqlStatementType.AlterSequence: case SqlStatementType.AlterTable: case SqlStatementType.AlterTrigger: case SqlStatementType.AlterView: case SqlStatementType.CommentOn: case SqlStatementType.CreateCollation: case SqlStatementType.CreateDomain: case SqlStatementType.CreateException: case SqlStatementType.CreateFunction: case SqlStatementType.CreateGenerator: case SqlStatementType.CreateIndex: case SqlStatementType.CreatePackage: case SqlStatementType.CreatePackageBody: case SqlStatementType.CreateProcedure: case SqlStatementType.CreateRole: case SqlStatementType.CreateSequence: case SqlStatementType.CreateShadow: case SqlStatementType.CreateTable: case SqlStatementType.CreateTrigger: case SqlStatementType.CreateView: case SqlStatementType.DeclareCursor: case SqlStatementType.DeclareExternalFunction: case SqlStatementType.DeclareFilter: case SqlStatementType.DeclareStatement: case SqlStatementType.DeclareTable: case SqlStatementType.Delete: case SqlStatementType.DropCollation: case SqlStatementType.DropDomain: case SqlStatementType.DropException: case SqlStatementType.DropExternalFunction: case SqlStatementType.DropFunction: case SqlStatementType.DropFilter: case SqlStatementType.DropGenerator: case SqlStatementType.DropIndex: case SqlStatementType.DropPackage: case SqlStatementType.DropPackageBody: case SqlStatementType.DropProcedure: case SqlStatementType.DropSequence: case SqlStatementType.DropRole: case SqlStatementType.DropShadow: case SqlStatementType.DropTable: case SqlStatementType.DropTrigger: case SqlStatementType.DropView: case SqlStatementType.EventInit: case SqlStatementType.EventWait: case SqlStatementType.Execute: case SqlStatementType.ExecuteImmediate: case SqlStatementType.ExecuteProcedure: case SqlStatementType.Grant: case SqlStatementType.Insert: case SqlStatementType.InsertCursor: case SqlStatementType.Merge: case SqlStatementType.Open: case SqlStatementType.Prepare: case SqlStatementType.Revoke: case SqlStatementType.RecreateFunction: case SqlStatementType.RecreatePackage: case SqlStatementType.RecreatePackageBody: case SqlStatementType.RecreateProcedure: case SqlStatementType.RecreateTable: case SqlStatementType.RecreateTrigger: case SqlStatementType.RecreateView: case SqlStatementType.SetGenerator: case SqlStatementType.Update: case SqlStatementType.Whenever: OnCommandExecuting(_sqlCommand, statement.StatementType); var rowsAffected = ExecuteCommand(autoCommit); _requiresNewConnection = false; OnCommandExecuted(null, statement.Text, statement.StatementType, rowsAffected); break; case SqlStatementType.ExecuteBlock: case SqlStatementType.Select: #warning Who's disposing this? ProvideCommand().CommandText = statement.Text; OnCommandExecuting(_sqlCommand, statement.StatementType); using (var dataReader = _sqlCommand.ExecuteReader()) { _requiresNewConnection = false; OnCommandExecuted(dataReader, statement.Text, statement.StatementType, -1); } break; case SqlStatementType.Commit: OnCommandExecuting(null, statement.StatementType); CommitTransaction(); OnCommandExecuted(null, statement.Text, statement.StatementType, -1); break; case SqlStatementType.Rollback: OnCommandExecuting(null, statement.StatementType); RollbackTransaction(); OnCommandExecuted(null, statement.Text, statement.StatementType, -1); break; case SqlStatementType.CreateDatabase: OnCommandExecuting(null, statement.StatementType); CreateDatabase(statement.CleanText); _requiresNewConnection = false; OnCommandExecuted(null, statement.Text, statement.StatementType, -1); break; case SqlStatementType.DropDatabase: OnCommandExecuting(null, statement.StatementType); FbConnection.DropDatabase(_connectionString.ToString()); _requiresNewConnection = true; OnCommandExecuted(null, statement.Text, statement.StatementType, -1); break; case SqlStatementType.Connect: OnCommandExecuting(null, statement.StatementType); ConnectToDatabase(statement.CleanText); _requiresNewConnection = false; OnCommandExecuted(null, statement.Text, statement.StatementType, -1); break; case SqlStatementType.Disconnect: OnCommandExecuting(null, statement.StatementType); _sqlConnection.Close(); FbConnection.ClearPool(_sqlConnection); _requiresNewConnection = false; OnCommandExecuted(null, statement.Text, statement.StatementType, -1); break; case SqlStatementType.SetAutoDDL: OnCommandExecuting(null, statement.StatementType); SetAutoDdl(statement.CleanText, ref autoCommit); _requiresNewConnection = false; OnCommandExecuted(null, statement.Text, statement.StatementType, -1); break; case SqlStatementType.SetNames: OnCommandExecuting(null, statement.StatementType); SetNames(statement.CleanText); _requiresNewConnection = true; OnCommandExecuted(null, statement.Text, statement.StatementType, -1); break; case SqlStatementType.SetSQLDialect: OnCommandExecuting(null, statement.StatementType); SetSqlDialect(statement.CleanText); _requiresNewConnection = true; OnCommandExecuted(null, statement.Text, statement.StatementType, -1); break; case SqlStatementType.Fetch: case SqlStatementType.Describe: break; case SqlStatementType.SetDatabase: case SqlStatementType.SetStatistics: case SqlStatementType.SetTransaction: case SqlStatementType.ShowSQLDialect: throw new NotImplementedException(); } } catch (Exception ex) { RollbackTransaction(); CloseConnection(); throw new FbException(string.Format("An exception was thrown when executing command: {1}.{0}Batch execution aborted.{0}The returned message was: {2}.", Environment.NewLine, statement.Text, ex.Message), ex); } } CommitTransaction(); CloseConnection(); }
public void AlterTable_MigrationRequiresAutomaticDelete_AndProcessorHasUndoDisabled_ShouldNotThrow() { // this test was originally created to investigate an issue with foreign key names but in the process, // I found that FirebirdProcessor.CreateSequenceForIdentity doesn't respect FBOptions.UndoEnabled. Since // Undo isn't implemented for Firebird, a migration runner always has to turn it off, but even with it off, // the migrations below will fail unless CreateSequenceForIdentity is appropriately altered var tempResources = WriteOutFirebirdEmbeddedLibrariesToCurrentWorkingDirectory(); var tempFile = Path.GetTempFileName(); using (var deleter = new AutoDeleter(tempFile)) { File.Delete(tempFile); deleter.Add(tempResources); var connectionString = GetConnectionStringToTempDatabaseAt(tempFile); var runnerContext = new RunnerContext(new TextWriterAnnouncer(System.Console.Out)) { Namespace = "FluentMigrator.Tests.Integration.Migrations" }; try { using (var connection = new FbConnection(connectionString)) { FirebirdProcessor processor; var announcer = new TextWriterAnnouncer(System.Console.Out); announcer.ShowSql = true; var options = FirebirdOptions.AutoCommitBehaviour(); options.TruncateLongNames = false; processor = new FirebirdProcessor(connection, new FirebirdGenerator(options), announcer, new ProcessorOptions(), new FirebirdDbFactory(), options); processor.FBOptions.UndoEnabled = false; var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), runnerContext, processor); runner.Up(new MigrationWhichCreatesTwoRelatedTables()); processor.CommitTransaction(); FbConnection.ClearPool(connection); } //---------------Assert Precondition---------------- Assert.IsTrue(ForeignKeyExists(connectionString, MigrationWhichCreatesTwoRelatedTables.ForeignKeyName), "Foreign key does not exist after first migration"); using (var connection = new FbConnection(connectionString)) { FirebirdProcessor processor; var announcer = new TextWriterAnnouncer(System.Console.Out); announcer.ShowSql = true; var options = FirebirdOptions.AutoCommitBehaviour(); processor = new FirebirdProcessor(connection, new FirebirdGenerator(options), announcer, new ProcessorOptions(), new FirebirdDbFactory(), options); processor.FBOptions.UndoEnabled = false; var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), runnerContext, processor); runner.Up(new MigrationWhichAltersTableWithFK()); processor.CommitTransaction(); } Assert.IsTrue(ForeignKeyExists(connectionString, MigrationWhichCreatesTwoRelatedTables.ForeignKeyName), "Foreign key does not exist after second migration"); } catch (Exception ex) { try { File.Copy(tempFile, "C:\\tmp\\fm_tests.fdb", true); } catch { } throw ex; } } }
public override Task DeleteAsync(CancellationToken cancellationToken = default) { FbConnection.ClearPool((FbConnection)_connection.DbConnection); return(FbConnection.DropDatabaseAsync(_connection.ConnectionString, cancellationToken)); }
public override void Delete() { FbConnection.ClearPool((FbConnection)_connection.DbConnection); FbConnection.DropDatabase(_connection.ConnectionString); }
/// <summary> /// Starts the ordered execution of the SQL statements that are in <see cref="SqlStatements"/> collection. /// </summary> /// <param name="autoCommit">Specifies if the transaction should be committed after a DDL command execution</param> public void Execute(bool autoCommit) { if (this.SqlStatements == null || this.SqlStatements.Count == 0) { throw new InvalidOperationException("There are no commands for execution."); } foreach (string sqlStatement in this.SqlStatements) { if (string.IsNullOrEmpty(sqlStatement)) { continue; } // initializate outputs to default int rowsAffected = -1; FbDataReader dataReader = null; SqlStatementType statementType = FbBatchExecution.GetStatementType(sqlStatement); if (!(statementType == SqlStatementType.Connect || statementType == SqlStatementType.CreateDatabase || statementType == SqlStatementType.Disconnect || statementType == SqlStatementType.DropDatabase || statementType == SqlStatementType.SetDatabase || statementType == SqlStatementType.SetNames || statementType == SqlStatementType.SetSQLDialect)) { // Update command configuration this.ProvideCommand(); this.sqlCommand.CommandText = sqlStatement; // Check how transactions are going to be handled if (statementType == SqlStatementType.Insert || statementType == SqlStatementType.Update || statementType == SqlStatementType.Delete) { // DML commands should be inside a transaction if (this.sqlTransaction == null) { this.sqlTransaction = this.sqlConnection.BeginTransaction(); } this.sqlCommand.Transaction = this.sqlTransaction; } else if (this.sqlTransaction != null && !(statementType == SqlStatementType.Commit || statementType == SqlStatementType.Rollback)) { // Non DML Statements should be executed using // implicit transaction support this.sqlTransaction.Commit(); this.sqlTransaction = null; } } try { switch (statementType) { case SqlStatementType.AlterDatabase: case SqlStatementType.AlterDomain: case SqlStatementType.AlterException: case SqlStatementType.AlterIndex: case SqlStatementType.AlterProcedure: case SqlStatementType.AlterTable: case SqlStatementType.AlterTrigger: case SqlStatementType.AlterView: // raise the event this.OnCommandExecuting(this.sqlCommand); rowsAffected = this.ExecuteCommand(this.sqlCommand, autoCommit); this.requiresNewConnection = false; // raise the event this.OnCommandExecuted(sqlStatement, null, rowsAffected); break; case SqlStatementType.Commit: // raise the event this.OnCommandExecuting(null); this.sqlTransaction.Commit(); this.sqlTransaction = null; // raise the event this.OnCommandExecuted(sqlStatement, null, -1); break; case SqlStatementType.Connect: // raise the event this.OnCommandExecuting(null); this.ConnectToDatabase(sqlStatement); this.requiresNewConnection = false; // raise the event this.OnCommandExecuted(sqlStatement, null, -1); break; case SqlStatementType.CreateDatabase: // raise the event this.OnCommandExecuting(null); this.CreateDatabase(sqlStatement); this.requiresNewConnection = false; // raise the event this.OnCommandExecuted(sqlStatement, null, -1); break; case SqlStatementType.CommentOn: case SqlStatementType.CreateDomain: case SqlStatementType.CreateException: case SqlStatementType.CreateGenerator: case SqlStatementType.CreateIndex: case SqlStatementType.CreateProcedure: case SqlStatementType.CreateRole: case SqlStatementType.CreateSequence: case SqlStatementType.CreateShadow: case SqlStatementType.CreateTable: case SqlStatementType.CreateTrigger: case SqlStatementType.CreateView: case SqlStatementType.DeclareCursor: case SqlStatementType.DeclareExternalFunction: case SqlStatementType.DeclareFilter: case SqlStatementType.DeclareStatement: case SqlStatementType.DeclareTable: case SqlStatementType.Delete: // raise the event this.OnCommandExecuting(this.sqlCommand); rowsAffected = this.ExecuteCommand(this.sqlCommand, autoCommit); this.requiresNewConnection = false; // raise the event this.OnCommandExecuted(sqlStatement, null, rowsAffected); break; case SqlStatementType.Describe: break; case SqlStatementType.Disconnect: // raise the event this.OnCommandExecuting(null); this.sqlConnection.Close(); FbConnection.ClearPool(this.sqlConnection); this.requiresNewConnection = false; // raise the event this.OnCommandExecuted(sqlStatement, null, -1); break; case SqlStatementType.DropDatabase: // raise the event this.OnCommandExecuting(null); FbConnection.DropDatabase(this.connectionString.ToString()); //this.sqlConnection = null; this.requiresNewConnection = true; // raise the event this.OnCommandExecuted(sqlStatement, null, -1); break; case SqlStatementType.DropDomain: case SqlStatementType.DropException: case SqlStatementType.DropExternalFunction: case SqlStatementType.DropFilter: case SqlStatementType.DropGenerator: case SqlStatementType.DropIndex: case SqlStatementType.DropProcedure: case SqlStatementType.DropSequence: case SqlStatementType.DropRole: case SqlStatementType.DropShadow: case SqlStatementType.DropTable: case SqlStatementType.DropTrigger: case SqlStatementType.DropView: case SqlStatementType.EventInit: case SqlStatementType.EventWait: case SqlStatementType.Execute: case SqlStatementType.ExecuteImmediate: case SqlStatementType.ExecuteProcedure: this.ProvideCommand().CommandText = sqlStatement; // raise the event this.OnCommandExecuting(this.sqlCommand); rowsAffected = this.ExecuteCommand(this.sqlCommand, autoCommit); this.requiresNewConnection = false; // raise the event this.OnCommandExecuted(sqlStatement, null, rowsAffected); break; case SqlStatementType.Fetch: break; case SqlStatementType.Grant: case SqlStatementType.Insert: case SqlStatementType.InsertCursor: case SqlStatementType.Open: case SqlStatementType.Prepare: case SqlStatementType.Revoke: // raise the event this.OnCommandExecuting(this.sqlCommand); rowsAffected = this.ExecuteCommand(this.sqlCommand, autoCommit); this.requiresNewConnection = false; // raise the event this.OnCommandExecuted(sqlStatement, null, rowsAffected); break; case SqlStatementType.RecreateProcedure: case SqlStatementType.RecreateTable: case SqlStatementType.RecreateTrigger: case SqlStatementType.RecreateView: // raise the event this.OnCommandExecuting(this.sqlCommand); rowsAffected = this.ExecuteCommand(this.sqlCommand, autoCommit); this.requiresNewConnection = false; // raise the event this.OnCommandExecuted(sqlStatement, null, rowsAffected); break; case SqlStatementType.Rollback: // raise the event this.OnCommandExecuting(null); this.sqlTransaction.Rollback(); this.sqlTransaction = null; // raise the event this.OnCommandExecuted(sqlStatement, null, -1); break; case SqlStatementType.Select: this.ProvideCommand().CommandText = sqlStatement; // raise the event this.OnCommandExecuting(this.sqlCommand); dataReader = this.sqlCommand.ExecuteReader(); this.requiresNewConnection = false; // raise the event this.OnCommandExecuted(sqlStatement, dataReader, -1); if (!dataReader.IsClosed) { dataReader.Close(); } break; case SqlStatementType.SetGenerator: case SqlStatementType.AlterSequence: // raise the event this.OnCommandExecuting(this.sqlCommand); rowsAffected = this.ExecuteCommand(this.sqlCommand, autoCommit); this.requiresNewConnection = false; // raise the event this.OnCommandExecuted(sqlStatement, null, rowsAffected); break; case SqlStatementType.SetDatabase: case SqlStatementType.SetNames: case SqlStatementType.SetSQLDialect: case SqlStatementType.SetStatistics: case SqlStatementType.SetTransaction: case SqlStatementType.ShowSQLDialect: throw new NotImplementedException(); case SqlStatementType.Update: case SqlStatementType.Whenever: // raise the event this.OnCommandExecuting(this.sqlCommand); rowsAffected = this.ExecuteCommand(this.sqlCommand, autoCommit); this.requiresNewConnection = false; // raise the event this.OnCommandExecuted(sqlStatement, null, rowsAffected); break; } } catch (Exception ex) { if (this.sqlTransaction != null) { this.sqlTransaction.Rollback(); this.sqlTransaction = null; } throw new FbException(string.Format(CultureInfo.CurrentUICulture, "An exception was thrown when executing command: {0}{2}Batch execution aborted{2}The returned message was: {1}", sqlStatement, ex.Message, Environment.NewLine)); } } if (this.sqlTransaction != null) { // commit root transaction this.sqlTransaction.Commit(); this.sqlTransaction = null; } this.sqlConnection.Close(); }
// Clear connection pool for the database connection since after the 'create database' call, a previously // invalid connection may now be valid. private void ClearPool() => FbConnection.ClearPool(_connection.DbConnection as FbConnection);
/// <summary> /// Starts the ordered execution of the SQL statements that are in <see cref="SqlStatements"/> collection. /// </summary> /// <param name="autoCommit">Specifies if the transaction should be committed after a DDL command execution</param> public void Execute(bool autoCommit = true) { if (this.SqlStatements == null || this.SqlStatements.Count == 0) { throw new InvalidOperationException("There are no commands for execution."); } foreach (string sqlStatement in this.SqlStatements) { if (string.IsNullOrEmpty(sqlStatement)) { continue; } // initializate outputs to default int rowsAffected = -1; FbDataReader dataReader = null; SqlStatementType statementType = FbBatchExecution.GetStatementType(sqlStatement); if (!(statementType == SqlStatementType.Connect || statementType == SqlStatementType.CreateDatabase || statementType == SqlStatementType.Disconnect || statementType == SqlStatementType.DropDatabase || statementType == SqlStatementType.SetAutoDDL || statementType == SqlStatementType.SetDatabase || statementType == SqlStatementType.SetNames || statementType == SqlStatementType.SetSQLDialect)) { this.ProvideCommand(); this.sqlCommand.CommandText = sqlStatement; if (this.sqlTransaction == null && !(statementType == SqlStatementType.Commit || statementType == SqlStatementType.Rollback)) { this.sqlTransaction = this.sqlConnection.BeginTransaction(); } this.sqlCommand.Transaction = this.sqlTransaction; } try { switch (statementType) { case SqlStatementType.AlterDatabase: case SqlStatementType.AlterDomain: case SqlStatementType.AlterException: case SqlStatementType.AlterIndex: case SqlStatementType.AlterProcedure: case SqlStatementType.AlterRole: case SqlStatementType.AlterTable: case SqlStatementType.AlterTrigger: case SqlStatementType.AlterView: this.OnCommandExecuting(this.sqlCommand); rowsAffected = this.ExecuteCommand(autoCommit); this.requiresNewConnection = false; this.OnCommandExecuted(sqlStatement, null, rowsAffected); break; case SqlStatementType.Commit: this.OnCommandExecuting(null); this.CommitTransaction(); this.OnCommandExecuted(sqlStatement, null, -1); break; case SqlStatementType.Connect: this.OnCommandExecuting(null); this.ConnectToDatabase(sqlStatement); this.requiresNewConnection = false; this.OnCommandExecuted(sqlStatement, null, -1); break; case SqlStatementType.CreateDatabase: this.OnCommandExecuting(null); this.CreateDatabase(sqlStatement); this.requiresNewConnection = false; this.OnCommandExecuted(sqlStatement, null, -1); break; case SqlStatementType.CommentOn: case SqlStatementType.CreateCollation: case SqlStatementType.CreateDomain: case SqlStatementType.CreateException: case SqlStatementType.CreateGenerator: case SqlStatementType.CreateIndex: case SqlStatementType.CreateProcedure: case SqlStatementType.CreateRole: case SqlStatementType.CreateSequence: case SqlStatementType.CreateShadow: case SqlStatementType.CreateTable: case SqlStatementType.CreateTrigger: case SqlStatementType.CreateView: case SqlStatementType.DeclareCursor: case SqlStatementType.DeclareExternalFunction: case SqlStatementType.DeclareFilter: case SqlStatementType.DeclareStatement: case SqlStatementType.DeclareTable: case SqlStatementType.Delete: this.OnCommandExecuting(this.sqlCommand); rowsAffected = this.ExecuteCommand(autoCommit); this.requiresNewConnection = false; this.OnCommandExecuted(sqlStatement, null, rowsAffected); break; case SqlStatementType.Describe: break; case SqlStatementType.Disconnect: this.OnCommandExecuting(null); this.sqlConnection.Close(); FbConnection.ClearPool(this.sqlConnection); this.requiresNewConnection = false; this.OnCommandExecuted(sqlStatement, null, -1); break; case SqlStatementType.DropDatabase: this.OnCommandExecuting(null); FbConnection.DropDatabase(this.connectionString.ToString()); this.requiresNewConnection = true; this.OnCommandExecuted(sqlStatement, null, -1); break; case SqlStatementType.DropCollation: case SqlStatementType.DropDomain: case SqlStatementType.DropException: case SqlStatementType.DropExternalFunction: case SqlStatementType.DropFilter: case SqlStatementType.DropGenerator: case SqlStatementType.DropIndex: case SqlStatementType.DropProcedure: case SqlStatementType.DropSequence: case SqlStatementType.DropRole: case SqlStatementType.DropShadow: case SqlStatementType.DropTable: case SqlStatementType.DropTrigger: case SqlStatementType.DropView: case SqlStatementType.EventInit: case SqlStatementType.EventWait: case SqlStatementType.Execute: case SqlStatementType.ExecuteImmediate: case SqlStatementType.ExecuteProcedure: this.ProvideCommand().CommandText = sqlStatement; this.OnCommandExecuting(this.sqlCommand); rowsAffected = this.ExecuteCommand(autoCommit); this.requiresNewConnection = false; this.OnCommandExecuted(sqlStatement, null, rowsAffected); break; case SqlStatementType.ExecuteBlock: this.ProvideCommand().CommandText = sqlStatement; this.OnCommandExecuting(this.sqlCommand); dataReader = this.sqlCommand.ExecuteReader(); this.requiresNewConnection = false; this.OnCommandExecuted(sqlStatement, dataReader, -1); if (!dataReader.IsClosed) { dataReader.Close(); } break; case SqlStatementType.Fetch: break; case SqlStatementType.Grant: case SqlStatementType.Insert: case SqlStatementType.InsertCursor: case SqlStatementType.Open: case SqlStatementType.Prepare: case SqlStatementType.Revoke: this.OnCommandExecuting(this.sqlCommand); rowsAffected = this.ExecuteCommand(autoCommit); this.requiresNewConnection = false; this.OnCommandExecuted(sqlStatement, null, rowsAffected); break; case SqlStatementType.RecreateProcedure: case SqlStatementType.RecreateTable: case SqlStatementType.RecreateTrigger: case SqlStatementType.RecreateView: this.OnCommandExecuting(this.sqlCommand); rowsAffected = this.ExecuteCommand(autoCommit); this.requiresNewConnection = false; this.OnCommandExecuted(sqlStatement, null, rowsAffected); break; case SqlStatementType.Rollback: this.OnCommandExecuting(null); this.RollbackTransaction(); this.OnCommandExecuted(sqlStatement, null, -1); break; case SqlStatementType.Select: this.ProvideCommand().CommandText = sqlStatement; this.OnCommandExecuting(this.sqlCommand); dataReader = this.sqlCommand.ExecuteReader(); this.requiresNewConnection = false; this.OnCommandExecuted(sqlStatement, dataReader, -1); if (!dataReader.IsClosed) { dataReader.Close(); } break; case SqlStatementType.SetAutoDDL: this.OnCommandExecuting(null); this.SetAutoDdl(sqlStatement, ref autoCommit); this.requiresNewConnection = false; this.OnCommandExecuted(sqlStatement, null, -1); break; case SqlStatementType.SetGenerator: case SqlStatementType.AlterSequence: this.OnCommandExecuting(this.sqlCommand); rowsAffected = this.ExecuteCommand(autoCommit); this.requiresNewConnection = false; this.OnCommandExecuted(sqlStatement, null, rowsAffected); break; case SqlStatementType.SetNames: this.OnCommandExecuting(null); this.SetNames(sqlStatement); this.requiresNewConnection = true; this.OnCommandExecuted(sqlStatement, null, -1); break; case SqlStatementType.SetSQLDialect: this.OnCommandExecuting(null); this.SetSqlDialect(sqlStatement); this.requiresNewConnection = true; this.OnCommandExecuted(sqlStatement, null, -1); break; case SqlStatementType.SetDatabase: case SqlStatementType.SetStatistics: case SqlStatementType.SetTransaction: case SqlStatementType.ShowSQLDialect: throw new NotImplementedException(); case SqlStatementType.Update: case SqlStatementType.Whenever: this.OnCommandExecuting(this.sqlCommand); rowsAffected = this.ExecuteCommand(autoCommit); this.requiresNewConnection = false; this.OnCommandExecuted(sqlStatement, null, rowsAffected); break; } } catch (Exception ex) { this.RollbackTransaction(); throw new FbException(string.Format("An exception was thrown when executing command: {1}.{0}Batch execution aborted.{0}The returned message was: {2}.", Environment.NewLine, sqlStatement, ex.Message)); } } this.CommitTransaction(); this.sqlConnection.Close(); }