Exemplo n.º 1
0
        public virtual void Builds_RelationalCommand_with_parameters()
        {
            var builder = new RawSqlCommandBuilder(
                new RelationalCommandBuilderFactory(
                    new FakeDiagnosticsLogger <DbLoggerCategory.Database.Command>(),
                    new FallbackRelationalTypeMappingSource(
                        TestServiceFactory.Instance.Create <TypeMappingSourceDependencies>(),
                        TestServiceFactory.Instance.Create <RelationalTypeMappingSourceDependencies>(),
                        TestServiceFactory.Instance.Create <FakeRelationalTypeMapper>())),
                new RelationalSqlGenerationHelper(new RelationalSqlGenerationHelperDependencies()),
                new ParameterNameGeneratorFactory(new ParameterNameGeneratorDependencies()));

            var rawSqlCommand = builder.Build("SQL COMMAND TEXT {0} {1} {2}", new object[] { 1, 2L, "three" });

            Assert.Equal("SQL COMMAND TEXT @p0 @p1 @p2", rawSqlCommand.RelationalCommand.CommandText);
            Assert.Equal(3, rawSqlCommand.RelationalCommand.Parameters.Count);
            Assert.Equal("p0", rawSqlCommand.RelationalCommand.Parameters[0].InvariantName);
            Assert.Equal("p1", rawSqlCommand.RelationalCommand.Parameters[1].InvariantName);
            Assert.Equal("p2", rawSqlCommand.RelationalCommand.Parameters[2].InvariantName);

            Assert.Equal(3, rawSqlCommand.ParameterValues.Count);
            Assert.Equal(1, rawSqlCommand.ParameterValues["p0"]);
            Assert.Equal(2L, rawSqlCommand.ParameterValues["p1"]);
            Assert.Equal("three", rawSqlCommand.ParameterValues["p2"]);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initiates a migration operation to the target migration version.
        /// </summary>
        /// <param name="plugin">The plugin whose migrations should be run.</param>
        /// <param name="targetMigration">The target migration.</param>
        public virtual void Migrate(EntityPlugin plugin, string targetMigration = null)
        {
            Logger.LogInformation(LoggingEvents.MigratingId, LoggingEvents.Migrating, plugin.Identifier, Connection.DbConnection.Database, Connection.DbConnection.DataSource);

            //
            // Verify the history table exists and if not create it.
            //
            if (!PluginHistoryRepository.Exists())
            {
                var command = RawSqlCommandBuilder.Build(PluginHistoryRepository.GetCreateScript());

                var query = new RelationalCommandParameterObject(Connection, null, null, CurrentContext.Context, CommandLogger);

                command.ExecuteNonQuery(query);
            }

            //
            // Get all the command lists to be executed.
            //
            var commandLists = GetMigrationCommandLists(
                plugin,
                PluginHistoryRepository.GetAppliedMigrations(plugin),
                targetMigration);

            //
            // Execute each command list in order.
            //
            foreach (var commandList in commandLists)
            {
                MigrationCommandExecutor.ExecuteNonQuery(commandList(), Connection);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Generates down SQL scripts.
        /// </summary>
        /// <param name="plugin">The plugin.</param>
        /// <param name="migration">The migration.</param>
        /// <param name="previousMigration">The previous migration.</param>
        /// <returns></returns>
        protected virtual IReadOnlyList <MigrationCommand> GenerateDownSql(EntityPlugin plugin, Migration migration, Migration previousMigration)
        {
            var historyScript  = PluginHistoryRepository.GetDeleteScript(plugin, migration.GetType().GetCustomAttribute <MigrationAttribute>().Id);
            var historyCommand = RawSqlCommandBuilder.Build(historyScript);

            return(MigrationsSqlGenerator
                   .Generate(migration.DownOperations, previousMigration?.TargetModel)
                   .Concat(new[] { new MigrationCommand(historyCommand, CurrentContext.Context, CommandLogger) })
                   .ToList());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Generates up SQL scripts.
        /// </summary>
        /// <param name="plugin">The plugin.</param>
        /// <param name="migration">The migration.</param>
        /// <returns></returns>
        protected virtual IReadOnlyList <MigrationCommand> GenerateUpSql(EntityPlugin plugin, Migration migration)
        {
            var migrationId    = migration.GetType().GetCustomAttribute <MigrationAttribute>()?.Id;
            var historyRow     = new HistoryRow(migrationId, ProductInfo.GetVersion());
            var historyScript  = PluginHistoryRepository.GetInsertScript(plugin, historyRow);
            var historyCommand = RawSqlCommandBuilder.Build(historyScript);

            return(MigrationsSqlGenerator
                   .Generate(migration.UpOperations, migration.TargetModel)
                   .Concat(new[] { new MigrationCommand(historyCommand, CurrentContext.Context, CommandLogger) })
                   .ToList());
        }
Exemplo n.º 5
0
        public virtual void Builds_RelationalCommand_without_optional_parameters()
        {
            var builder = new RawSqlCommandBuilder(
                new RelationalCommandBuilderFactory(
                    new FakeDiagnosticsLogger <DbLoggerCategory.Database.Command>(),
                    new FakeRelationalTypeMapper(new RelationalTypeMapperDependencies())),
                new RelationalSqlGenerationHelper(new RelationalSqlGenerationHelperDependencies()),
                new ParameterNameGeneratorFactory(new ParameterNameGeneratorDependencies()));

            var command = builder.Build("SQL COMMAND TEXT");

            Assert.Equal("SQL COMMAND TEXT", command.CommandText);
            Assert.Equal(0, command.Parameters.Count);
        }
Exemplo n.º 6
0
        public virtual void Builds_RelationalCommand_with_parameters()
        {
            var builder = new RawSqlCommandBuilder(
                new RelationalCommandBuilderFactory(
                    new FakeSensitiveDataLogger <RelationalCommandBuilderFactory>(),
                    new DiagnosticListener("Fake"),
                    new FakeRelationalTypeMapper()),
                new RelationalSqlGenerationHelper(),
                new ParameterNameGeneratorFactory());

            var command = builder.Build("SQL COMMAND TEXT {0} {1} {2}", new object[] { 1, 2L, "three" });

            Assert.Equal("SQL COMMAND TEXT @p0 @p1 @p2", command.CommandText);
            Assert.Equal(3, command.Parameters.Count);
        }
Exemplo n.º 7
0
        public virtual void Builds_RelationalCommand_with_empty_parameter_list()
        {
            var builder = new RawSqlCommandBuilder(
                new RelationalCommandBuilderFactory(
                    new FakeSensitiveDataLogger <RelationalCommandBuilderFactory>(),
                    new DiagnosticListener("Fake"),
                    new FakeRelationalTypeMapper()),
                new RelationalSqlGenerationHelper(),
                new ParameterNameGeneratorFactory());

            var command = builder.Build("SQL COMMAND TEXT", new object[0]);

            Assert.Equal("SQL COMMAND TEXT", command.CommandText);
            Assert.Equal(0, command.Parameters.Count);
        }
Exemplo n.º 8
0
        public virtual void Builds_RelationalCommand_with_empty_parameter_list()
        {
            var builder = new RawSqlCommandBuilder(
                new RelationalCommandBuilderFactory(
                    new FakeDiagnosticsLogger <DbLoggerCategory.Database.Command>(),
                    TestServiceFactory.Instance.Create <FakeRelationalTypeMapper>()),
                new RelationalSqlGenerationHelper(new RelationalSqlGenerationHelperDependencies()),
                new ParameterNameGeneratorFactory(new ParameterNameGeneratorDependencies()));

            var rawSqlCommand = builder.Build("SQL COMMAND TEXT", new object[0]);

            Assert.Equal("SQL COMMAND TEXT", rawSqlCommand.RelationalCommand.CommandText);
            Assert.Equal(0, rawSqlCommand.RelationalCommand.Parameters.Count);
            Assert.Equal(0, rawSqlCommand.ParameterValues.Count);
        }
Exemplo n.º 9
0
        public virtual void Builds_RelationalCommand_with_empty_parameter_list()
        {
            var builder = new RawSqlCommandBuilder(
                new RelationalCommandBuilderFactory(
                    new FakeInterceptingLogger <LoggerCategory.Database.Sql>(),
                    new DiagnosticListener("Fake"),
                    new FakeRelationalTypeMapper(new RelationalTypeMapperDependencies())),
                new RelationalSqlGenerationHelper(new RelationalSqlGenerationHelperDependencies()),
                new ParameterNameGeneratorFactory(new ParameterNameGeneratorDependencies()));

            var rawSqlCommand = builder.Build("SQL COMMAND TEXT", new object[0]);

            Assert.Equal("SQL COMMAND TEXT", rawSqlCommand.RelationalCommand.CommandText);
            Assert.Equal(0, rawSqlCommand.RelationalCommand.Parameters.Count);
            Assert.Equal(0, rawSqlCommand.ParameterValues.Count);
        }
Exemplo n.º 10
0
        public virtual void Builds_RelationalCommand_with_parameters()
        {
            var builder = new RawSqlCommandBuilder(
                new RelationalCommandBuilderFactory(
                    new FakeSensitiveDataLogger <RelationalCommandBuilderFactory>(),
                    new DiagnosticListener("Fake"),
                    new FakeRelationalTypeMapper(new RelationalTypeMapperDependencies())),
                new RelationalSqlGenerationHelper(new RelationalSqlGenerationHelperDependencies()),
                new ParameterNameGeneratorFactory(new ParameterNameGeneratorDependencies()));

            var rawSqlCommand = builder.Build("SQL COMMAND TEXT {0} {1} {2}", new object[] { 1, 2L, "three" });

            Assert.Equal("SQL COMMAND TEXT @p0 @p1 @p2", rawSqlCommand.RelationalCommand.CommandText);
            Assert.Equal(3, rawSqlCommand.RelationalCommand.Parameters.Count);
            Assert.Equal("p0", rawSqlCommand.RelationalCommand.Parameters[0].InvariantName);
            Assert.Equal("p1", rawSqlCommand.RelationalCommand.Parameters[1].InvariantName);
            Assert.Equal("p2", rawSqlCommand.RelationalCommand.Parameters[2].InvariantName);

            Assert.Equal(3, rawSqlCommand.ParameterValues.Count);
            Assert.Equal(1, rawSqlCommand.ParameterValues["p0"]);
            Assert.Equal(2L, rawSqlCommand.ParameterValues["p1"]);
            Assert.Equal("three", rawSqlCommand.ParameterValues["p2"]);
        }