public async Task SetVersionAsync(MigratorDatabase database, long version, CancellationToken cancellation = default) { if (await database.ExecuteAsync($"UPDATE {TableName} SET {DatabaseVersionColumnName} = {version}", cancellation: cancellation) == 0) { await database.ExecuteAsync($"INSERT INTO {TableName}(Version) VALUES({version})", cancellation : cancellation); } }
public override async Task ExecuteAsync(MigratorDatabase database, CancellationToken cancellation = default) { if (database == null) { throw new ArgumentNullException(nameof(database)); } foreach (var commandText in CommandSeparator.Split(await _readToEndAsync())) { if (string.IsNullOrWhiteSpace(commandText)) { continue; } await database.ExecuteAsync(commandText, 0, cancellation); } }
private async Task CreateAsync(MigratorDatabase database, CancellationToken cancellation = default) { if (HasSchema) { await CreateSchemaIfNotExistingAsync(database, cancellation); } try { await database.ExecuteAsync($@" CREATE TABLE {TableName} ( Id UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY CLUSTERED, [Version] BIGINT )", cancellation : cancellation); } catch (SqlException e) when(e.Number == ThereIsAlreadyAnObjectNamedXxxInTheDatabase) { } }
private async Task CreateSchemaIfNotExistingAsync(MigratorDatabase database, CancellationToken cancellation = default) { var schemaCount = await database.SingleAsync <int>($@" SELECT Count(schema_name) FROM information_schema.schemata WHERE schema_name = '{SchemaName}'", cancellation : cancellation); if (schemaCount == 1) { return; } try { await database.ExecuteAsync($"CREATE SCHEMA {SchemaName}", cancellation : cancellation); } catch (SqlException e) when(e.Number == ThereIsAlreadyAnObjectNamedXxxInTheDatabase) { } }