Пример #1
0
 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);
     }
 }
Пример #2
0
        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);
            }
        }
Пример #3
0
        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)
            {
            }
        }
Пример #4
0
        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)
            {
            }
        }