Rebus.AdoNet/AdoNetSagaPersister.cs
/// <summary>
/// Creates the necessary saga storage tables if they haven't already been created. If a table already exists
/// with a name that matches one of the desired table names, no action is performed (i.e. it is assumed that
/// the tables already exist).
/// </summary>
Creates the necessary saga storage tables if they haven't already been created. If a table already exists
with a name that matches one of the desired table names, no action is performed .
Creates the necessary saga storage tables if they haven ' t already been created . If a table already exists with a name that matches one of the desired table names no action is performed .
public AdoNetSagaPersisterFluentConfigurer EnsureTablesAreCreated()
		{
			using (var scope = manager.GetScope(autonomous: true))
			{
				var dialect = scope.Dialect;
				var connection = scope.Connection;
				var tableNames = scope.GetTableNames();
				var sagaTableIsAlreadyCreated = tableNames.Contains(sagaTableName, StringComparer.InvariantCultureIgnoreCase);
				var sagaIndexTableIsAlreadyCreated = tableNames.Contains(sagaIndexTableName, StringComparer.OrdinalIgnoreCase);
				if (sagaTableIsAlreadyCreated && sagaIndexTableIsAlreadyCreated)
				{
					return this;
				}
				if (sagaTableIsAlreadyCreated || sagaIndexTableIsAlreadyCreated)
				{
					throw new ApplicationException(string.Format("Table '{0}' do not exist - you have to create it manually",
						sagaIndexTableIsAlreadyCreated ? sagaTableName : sagaIndexTableName));
				}
				log.Info("Tables '{0}' and '{1}' do not exist - they will be created now", sagaTableName, sagaIndexTableName);
				using (var command = connection.CreateCommand())
				{
					command.CommandText = scope.Dialect.FormatCreateTable(
						new AdoNetTable()
						{
							Name = sagaTableName,
							Columns = new []
							{
								new AdoNetColumn() { Name = SAGA_ID_COLUMN, DbType = DbType.Guid },
								new AdoNetColumn() { Name = SAGA_TYPE_COLUMN, DbType = DbType.StringFixedLength, Length = 80 },
								new AdoNetColumn() { Name = SAGA_REVISION_COLUMN, DbType = DbType.Int32 },
								new AdoNetColumn() { Name = SAGA_DATA_COLUMN, DbType = DbType.String, Length = 1073741823}
							},
							PrimaryKey = new[] { SAGA_ID_COLUMN }
						}
					);
					command.ExecuteNonQuery();
				}
				using (var command = connection.CreateCommand())
				{
					command.CommandText = scope.Dialect.FormatCreateTable(
						new AdoNetTable()
						{
							Name = sagaIndexTableName,
							Columns = new []
							{
								new AdoNetColumn() { Name = SAGAINDEX_TYPE_COLUMN, DbType = DbType.StringFixedLength, Length = 80 },
								new AdoNetColumn() { Name = SAGAINDEX_KEY_COLUMN, DbType = DbType.StringFixedLength, Length = 200  },
								new AdoNetColumn() { Name = SAGAINDEX_VALUE_COLUMN, DbType = DbType.StringFixedLength, Length = 200 },
								new AdoNetColumn() { Name = SAGAINDEX_ID_COLUMN, DbType = DbType.Guid }
							},
							PrimaryKey = new[] { SAGAINDEX_ID_COLUMN, SAGAINDEX_KEY_COLUMN  },
							Indexes = new []
							{
								new AdoNetIndex() { Name = "ix_sagaindexes_id", Columns = new[] { SAGAINDEX_ID_COLUMN } }
							}
						}
					);
					command.ExecuteNonQuery();
				}
				scope.Complete();
			}
			return this;
		}
public AdoNetSagaPersisterFluentConfigurer EnsureTablesAreCreated ( ) { using ( var scope = manager . GetScope ( autonomous : true ) ) { var dialect = scope . Dialect ; var connection = scope . Connection ; var tableNames = scope . GetTableNames ( ) ; var sagaTableIsAlreadyCreated = tableNames . Contains ( sagaTableName , StringComparer . InvariantCultureIgnoreCase ) ; var sagaIndexTableIsAlreadyCreated = tableNames . Contains ( sagaIndexTableName , StringComparer . OrdinalIgnoreCase ) ; if ( sagaTableIsAlreadyCreated && sagaIndexTableIsAlreadyCreated ) { return this ; } if ( sagaTableIsAlreadyCreated || sagaIndexTableIsAlreadyCreated ) { throw new ApplicationException ( string . Format ( " " , sagaIndexTableIsAlreadyCreated ? sagaTableName : sagaIndexTableName ) ) ; } log . Info ( " " , sagaTableName , sagaIndexTableName ) ; using ( var command = connection . CreateCommand ( ) ) { command . CommandText = scope . Dialect . FormatCreateTable ( new AdoNetTable ( ) { Name = sagaTableName , Columns = new [ ] { new AdoNetColumn ( ) { Name = SAGA_ID_COLUMN , DbType = DbType . Guid } , new AdoNetColumn ( ) { Name = SAGA_TYPE_COLUMN , DbType = DbType . StringFixedLength , Length = 80 } , new AdoNetColumn ( ) { Name = SAGA_REVISION_COLUMN , DbType = DbType . Int32 } , new AdoNetColumn ( ) { Name = SAGA_DATA_COLUMN , DbType = DbType . String , Length = 1073741823 } } , PrimaryKey = new [ ] { SAGA_ID_COLUMN } } ) ; command . ExecuteNonQuery ( ) ; } using ( var command = connection . CreateCommand ( ) ) { command . CommandText = scope . Dialect . FormatCreateTable ( new AdoNetTable ( ) { Name = sagaIndexTableName , Columns = new [ ] { new AdoNetColumn ( ) { Name = SAGAINDEX_TYPE_COLUMN , DbType = DbType . StringFixedLength , Length = 80 } , new AdoNetColumn ( ) { Name = SAGAINDEX_KEY_COLUMN , DbType = DbType . StringFixedLength , Length = 200 } , new AdoNetColumn ( ) { Name = SAGAINDEX_VALUE_COLUMN , DbType = DbType . StringFixedLength , Length = 200 } , new AdoNetColumn ( ) { Name = SAGAINDEX_ID_COLUMN , DbType = DbType . Guid } } , PrimaryKey = new [ ] { SAGAINDEX_ID_COLUMN , SAGAINDEX_KEY_COLUMN } , Indexes = new [ ] { new AdoNetIndex ( ) { Name = " " , Columns = new [ ] { SAGAINDEX_ID_COLUMN } } } } ) ; command . ExecuteNonQuery ( ) ; } scope . Complete ( ) ; } return this ; }
Creates the necessary saga storage tables if they haven't already been created.
Creates the necessary saga storage tables if they haven ' t already been created .
// bail out if there's already a table in the database with one of the names // if saga index is created, then saga table is not created and vice versa