<commit_before>
function Initialize-Database
{
    <#
    .SYNOPSIS
    Intializes the database so that it can be migrated by Rivet.
    #>
    param(
    )

    if( -not (Test-Schema -Name $RivetSchemaName) )
    {
        Add-Schema -Name $RivetSchemaName
    }
    
    $oldRivetSchemaName = 'pstep'
    if( (Test-Table -Name $RivetMigrationsTableName -SchemaName $oldRivetSchemaName) )
    {
        Invoke-Query ('alter schema {0} transfer {1}.Migrations' -f $RivetSchemaName,$oldRivetSchemaName)
    }

    if( (Test-Schema -Name $oldRivetSchemaName) )
    {
        Remove-Schema -Name $oldRivetSchemaName
    }
    
    if( (Test-Table -Name $RivetMigrationsTableName -SchemaName $RivetSchemaName) )
    {
        $query = @'
            select 
                ty.name 
            from 
                sys.tables t 
                join sys.schemas s on t.schema_id = s.schema_id
                join sys.columns c on t.object_id = c.object_id 
                join sys.types ty on c.system_type_id = ty.system_type_id AND C.user_type_id = ty.user_type_id 
            where 
                s.name = '{0}' and t.name = '{1}' and c.name = 'AtUtc'
'@ -f $RivetSchemaName,$RivetMigrationsTableName
        $atUtcType = Invoke-Query $query -AsScalar
        if( $atUtcType -eq 'datetime' )
        {
            Write-Host (' {0}.{1}.AtUtc datetime -> datetime2' -f $RivetSchemaName,$RivetMigrationsTableName)
            $query = @'
            alter table {0}.{1} drop constraint AtUtcDefault
            alter table {0}.{1} alter column AtUtc datetime2 not null
            alter table {0}.{1} add constraint AtUtcDefault default (GetUtcDate()) for AtUtc
'@ -f $RivetSchemaName,$RivetMigrationsTableName
            Invoke-Query $query
        }
    }
    else
    {
        Add-Table -Name $RivetMigrationsTableName -SchemaName $RivetSchemaName -Column {
            New-Column 'ID' -BigInt -NotNull
            New-Column 'Name' -VarChar 50 -Unicode -NotNull
            New-Column 'Who' -VarChar 50 -Unicode -NotNull
            New-Column 'ComputerName' -VarChar 50 -Unicode -NotNull
            New-Column 'AtUtc' -Datetime2 -NotNull
        }

        $query = @'
            alter table {0} add constraint MigrationsPK primary key (ID)
            alter table {0} add constraint AtUtcDefault default (GetUtcDate()) for AtUtc
'@ -f $RivetMigrationsTableFullName
        Invoke-Query -Query $query
    }

}<commit_msg>Bug fix: initializing database not wrapped in transaction so database gets stuck in bad state.<commit_after>
function Initialize-Database
{
    <#
    .SYNOPSIS
    Intializes the database so that it can be migrated by Rivet.
    #>
    param(
    )

    $Connection.Transaction = $Connection.BeginTransaction()

    try
    {
        if( -not (Test-Schema -Name $RivetSchemaName) )
        {
            Add-Schema -Name $RivetSchemaName
        }
    
        $oldRivetSchemaName = 'pstep'
        if( (Test-Table -Name $RivetMigrationsTableName -SchemaName $oldRivetSchemaName) )
        {
            Invoke-Query ('alter schema {0} transfer {1}.Migrations' -f $RivetSchemaName,$oldRivetSchemaName)
        }

        if( (Test-Schema -Name $oldRivetSchemaName) )
        {
            Remove-Schema -Name $oldRivetSchemaName
        }
    
        if( (Test-Table -Name $RivetMigrationsTableName -SchemaName $RivetSchemaName) )
        {
            $query = @'
                select 
                    ty.name 
                from 
                    sys.tables t 
                    join sys.schemas s on t.schema_id = s.schema_id
                    join sys.columns c on t.object_id = c.object_id 
                    join sys.types ty on c.system_type_id = ty.system_type_id AND C.user_type_id = ty.user_type_id 
                where 
                    s.name = '{0}' and t.name = '{1}' and c.name = 'AtUtc'
'@ -f $RivetSchemaName,$RivetMigrationsTableName
            $atUtcType = Invoke-Query $query -AsScalar
            if( $atUtcType -eq 'datetime' )
            {
                Write-Host (' {0}.{1}.AtUtc datetime -> datetime2' -f $RivetSchemaName,$RivetMigrationsTableName)
                $query = @'
                alter table {0}.{1} drop constraint AtUtcDefault
                alter table {0}.{1} alter column AtUtc datetime2 not null
                alter table {0}.{1} add constraint AtUtcDefault default (GetUtcDate()) for AtUtc
'@ -f $RivetSchemaName,$RivetMigrationsTableName
                Invoke-Query $query
            }
        }
        else
        {
            Add-Table -Name $RivetMigrationsTableName -SchemaName $RivetSchemaName -Column {
                New-Column 'ID' -BigInt -NotNull
                New-Column 'Name' -VarChar 50 -Unicode -NotNull
                New-Column 'Who' -VarChar 50 -Unicode -NotNull
                New-Column 'ComputerName' -VarChar 50 -Unicode -NotNull
                New-Column 'AtUtc' -Datetime2 -NotNull
            }

            $query = @'
                alter table {0} add constraint MigrationsPK primary key (ID)
                alter table {0} add constraint AtUtcDefault default (GetUtcDate()) for AtUtc
'@ -f $RivetMigrationsTableFullName
            Invoke-Query -Query $query
        }
        $Connection.Transaction.Commit()
    }
    catch
    {
        $Connection.Transaction.Rollback()
            
        Write-RivetError -Message ('Failed to initialize database so it can be migrated.') -Exception $_.Exception -CallStack (Get-PSCallStack)
    }

}
{'fraction_non_alphanumeric': 0.09483072442664725, 'fraction_numerical': 0.008372770294867127, 'mean_word_length': 1.845675815639565, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2335156', 'n_tokens_mistral': 1733, 'n_tokens_neox': 1661, 'n_words': 421}