public void GetInsertOperation_deletes_row()
 {
     var hp = CreateSqliteHistoryRepo();
     var typename = typeof(TestContext).FullName;
     var expected = new SqlOperation
     {
         Sql = $"INSERT INTO \"__migrationHistory\" (\"MigrationId\", \"ContextKey\", \"ProductVersion\") VALUES ('m5', '{typename}', '7');"
     };
     var historyRow = new HistoryRow("m5", "7");
     var actual = hp.GetInsertOperation(historyRow) as SqlOperation;
     Assert.Equal(expected.IsDestructiveChange, actual.IsDestructiveChange);
     Assert.Equal(expected.Sql, actual.Sql);
     Assert.Equal(expected.SuppressTransaction, actual.SuppressTransaction);
 }
        public void GetAppliedMigrations_finds_migrations()
        {
            var testConnection = SqliteTestConnection.CreateScratch();
            testConnection.Open();
            using (var command = testConnection.DbConnection.CreateCommand())
            {
                command.CommandText = CreateSqliteHistoryRepo().Create(true);
                command.ExecuteNonQuery();
            }
            using (var command = testConnection.DbConnection.CreateCommand())
            {
                command.CommandText = "INSERT INTO __migrationHistory VALUES ('different_context','SomeFakeContext','1');";
                command.ExecuteNonQuery();
            }

            var row = new HistoryRow("Mig1", "7");
            using (var command = testConnection.DbConnection.CreateCommand())
            {
                var operation = CreateSqliteHistoryRepo()
                    .GetInsertOperation(row) as SqlOperation;
                command.CommandText = operation?.Sql;
                command.ExecuteNonQuery();
            }

            var hp = new SqliteHistoryRepository(testConnection, new TestContext(), new SqliteUpdateSqlGenerator());

            Assert.Collection(hp.GetAppliedMigrations(), p =>
                {
                    Assert.Equal(row.MigrationId, p.MigrationId);
                    Assert.Equal(row.ProductVersion, p.ProductVersion);
                });
            testConnection.Close();
        }
        public virtual IReadOnlyList<IHistoryRow> GetAppliedMigrations()
        {
            var migrations = new List<IHistoryRow>();

            if (!Exists())
            {
                return migrations;
            }

            using (var connection = _connection.DbConnection)
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                }
                var command = connection.CreateCommand();
                command.CommandText = $"SELECT MigrationId, ProductVersion FROM {_sql.DelimitIdentifier(MigrationTableName)} " +
                                      $"WHERE ContextKey = '{_sql.EscapeLiteral(_contextKey)}' ORDER BY MigrationId;";

                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var row = new HistoryRow(reader.GetString(0), reader.GetString(1));
                        migrations.Add(row);
                    }
                }
            }

            return migrations.AsReadOnly();
        }