<commit_before>﻿namespace Skeleton.Migrations.Migrator
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using Dapper.SessionsFactory;

    public class SqlServerMigrator : IMigrator
    {
        private readonly ISessionsFactory _sessionsFactory;
        private readonly IMigration[] _migrations;

        public SqlServerMigrator(ISessionsFactory sessionsFactory, IMigration[] migrations)
        {
            if(sessionsFactory == null)
                throw new ArgumentNullException(nameof(sessionsFactory));
            if (migrations == null)
                throw new ArgumentNullException(nameof(migrations));

            _sessionsFactory = sessionsFactory;
            _migrations = migrations;
        }

        private static void CheckAndCreateVersionTable(ISession session)
        {
            session.Execute(@"
if object_id ('[dbo].[VersionInfo]') is null
	begin
		create table [dbo].[VersionInfo](
			[Version] [bigint] NOT NULL,
			[AppliedOn] [datetime] NOT NULL CONSTRAINT [DF_VersionInfo_AppliedOn] DEFAULT (getutcdate())
		) on [PRIMARY]		

		create unique clustered index [UC_dbo_VersionInfo_Version] ON [dbo].[VersionInfo]
		(
			[Version] asc
		)with (pad_index = off, statistics_norecompute = off, sort_in_tempdb = off, ignore_dup_key = off, drop_existing = off, online = off, allow_row_locks = on, allow_page_locks = on)
	end");
        }

        private static IEnumerable<long> GetAppliedVersions(ISession session)
        {
            return session.Query<long>("select v.[Version] from [dbo].[VersionInfo] v").ToArray();
        }

        private static void AddAppliedVersion(ISession session, long version)
        {
            session.Execute("INSERT INTO [dbo].[VersionInfo] ([Version]) VALUES (@Version)",
                new {Version = version});
        }

        public void Migrate()
        {
            HashSet<long> appliedVersions;
            using (var session = _sessionsFactory.Create(IsolationLevel.Snapshot))
            {
                CheckAndCreateVersionTable(session);
                appliedVersions = new HashSet<long>(GetAppliedVersions(session));

                session.Commit();
            }

            var newMigrations = _migrations.Where(x => appliedVersions.Contains(x.Version) == false).ToArray();<commit_msg>Isolation level was set to Serializable<commit_after>﻿namespace Skeleton.Migrations.Migrator
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using Dapper.SessionsFactory;

    public class SqlServerMigrator : IMigrator
    {
        private readonly ISessionsFactory _sessionsFactory;
        private readonly IMigration[] _migrations;

        public SqlServerMigrator(ISessionsFactory sessionsFactory, IMigration[] migrations)
        {
            if(sessionsFactory == null)
                throw new ArgumentNullException(nameof(sessionsFactory));
            if (migrations == null)
                throw new ArgumentNullException(nameof(migrations));

            _sessionsFactory = sessionsFactory;
            _migrations = migrations;
        }

        private static void CheckAndCreateVersionTable(ISession session)
        {
            session.Execute(@"
if object_id ('[dbo].[VersionInfo]') is null
	begin
		create table [dbo].[VersionInfo](
			[Version] [bigint] NOT NULL,
			[AppliedOn] [datetime] NOT NULL CONSTRAINT [DF_VersionInfo_AppliedOn] DEFAULT (getutcdate())
		) on [PRIMARY]		

		create unique clustered index [UC_dbo_VersionInfo_Version] ON [dbo].[VersionInfo]
		(
			[Version] asc
		)with (pad_index = off, statistics_norecompute = off, sort_in_tempdb = off, ignore_dup_key = off, drop_existing = off, online = off, allow_row_locks = on, allow_page_locks = on)
	end");
        }

        private static IEnumerable<long> GetAppliedVersions(ISession session)
        {
            return session.Query<long>("select v.[Version] from [dbo].[VersionInfo] v").ToArray();
        }

        private static void AddAppliedVersion(ISession session, long version)
        {
            session.Execute("INSERT INTO [dbo].[VersionInfo] ([Version]) VALUES (@Version)",
                new {Version = version});
        }

        public void Migrate()
        {
            HashSet<long> appliedVersions;
            using (var session = _sessionsFactory.Create(IsolationLevel.Serializable))
            {
                CheckAndCreateVersionTable(session);
                appliedVersions = new HashSet<long>(GetAppliedVersions(session));

                session.Commit();
            }

            var newMigrations = _migrations.Where(x => appliedVersions.Contains(x.Version) == false).ToArray();
{'fraction_non_alphanumeric': 0.09378960709759189, 'fraction_numerical': 0.0, 'mean_word_length': 2.7283464566929134, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '978816', 'n_tokens_mistral': 1343, 'n_tokens_neox': 1258, 'n_words': 296}