ActivateStrictMode() public static méthode

public static ActivateStrictMode ( IDatabase db ) : void
db IDatabase
Résultat void
        public void Up(IDatabase db)
        {
            if (!this.IsFeatureSupported(db))
            {
                return;
            }
            MySqlHelper.ActivateStrictMode(db);

            db.CreateTable(Tables[0].Name)
            .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32).AsIdentity()
            .WithNotNullableColumn(Tables[0].Columns[1], DbType.Decimal).OfSize(3)
            .WithNotNullableColumn(Tables[0].Columns[2], DbType.Decimal).OfSize(5, 2);

            db.Execute(GetInsertStatement((decimal)Tables[0].Value(0, 2) + 0.003m)); // the extra precision should be cut off silently
            db.Execute(context =>
            {
                IDbCommand command  = context.CreateCommand();
                command.CommandText = GetInsertStatement(1000m);
                Log.Verbose(LogCategory.Sql, command.CommandText);
                try
                {
                    command.ExecuteNonQuery();
                    Assert.Fail("The previous query should have failed.");
                }
                catch (Exception x)
                {
                    if (!x.IsDbException())
                    {
                        throw;
                    }
                }
            });
        }
        public void Up(IDatabase db)
        {
            MySqlHelper.ActivateStrictMode(db);

            db.CreateTable(Tables[0].Name)
            .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32).AsIdentity()
            .WithNotNullableColumn(Tables[0].Columns[1], DbType.String);

            db.Execute(GetInsertStatement((string)Tables[0].Value(0, 1)));

            // Note: the following statement does *not* the identity constraint. Doing so is very difficult. For example, see: http://stackoverflow.com/questions/702745/sql-server-how-to-drop-identity-from-a-column
            //db.Tables[TableName].Columns[ColumnNames[0]].AlterToNotNullable(DbType.Int32);

            db.Tables[Tables[0].Name].Drop(); // make sure, TRIGGERS and SEQUENCES are dropped as well

            db.CreateTable(Tables[0].Name)
            .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32)
            .WithNotNullableColumn(Tables[0].Columns[1], DbType.String);

            // inserting another row without specifying the Id value should fail as the Identity constraint is removed
            if (db.Context.ProviderMetadata.Platform != Platform.SQLite) // SQLite automatically generates identity columns for PKs
            {
                db.Execute(context =>
                {
                    IDbCommand command  = context.CreateCommand();
                    command.CommandText = GetInsertStatement((string)Tables[0].Value(0, 1));
                    Log.Verbose(LogCategory.Sql, command.CommandText);
                    try
                    {
                        command.ExecuteNonQuery();
                        Assert.Fail("The previous query should have failed.");
                    }
                    catch (Exception x)
                    {
                        if (!x.IsDbException())
                        {
                            throw;
                        }
                    }
                });
            }

            db.Execute(GetInsertStatement((int)Tables[0].Value(0, 0), (string)Tables[0].Value(0, 1)));

            db.Tables[Tables[0].Name].Drop(); // make sure, TRIGGERS and SEQUENCES are dropped as well

            // recreating the table with the identity constraint again might reveal undropped TRIGGERS or SEQUENCES
            db.CreateTable(Tables[0].Name)
            .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32).AsIdentity()
            .WithNotNullableColumn(Tables[0].Columns[1], DbType.String);

            db.Execute(GetInsertStatement((string)Tables[0].Value(0, 1)));
        }
Exemple #3
0
        private static readonly DateTime DefaultDate = new DateTime(2011, 2, 1, 10, 58, 21); // 1st of Feb.

        public void Up(IDatabase db)
        {
            if (!this.IsFeatureSupported(db))
            {
                return;
            }
            MySqlHelper.ActivateStrictMode(db);

            db.CreateTable(Tables[0].Name)
            .WithPrimaryKeyColumn(Tables[0].Columns[0], DbType.Int32).AsIdentity()
            .WithNotNullableColumn(Tables[0].Columns[1], DbType.String).OfSize(100);
            db.Execute(GetInsertStatement(0));
            db.Execute(GetInsertStatement(1));

            // add two new columns one of int and one of string
            db.Tables[Tables[0].Name].AddNotNullableColumn(Tables[0].Columns[2], DbType.String).OfSize(100).HavingTemporaryDefault(DefaultString);
            db.Tables[Tables[0].Name].AddNotNullableColumn(Tables[0].Columns[3], DbType.Int32).HavingTemporaryDefault(DefaultInt);
            db.Tables[Tables[0].Name].AddNotNullableColumn(Tables[0].Columns[4], DbType.DateTime).HavingTemporaryDefault(DefaultDate);

            // ensure that the default values have been droped
            db.Execute(context =>
            {
                IDbCommand command  = context.CreateCommand();
                command.CommandText = GetInsertStatement(1);
                Log.Verbose(LogCategory.Sql, command.CommandText);
                try
                {
                    command.ExecuteNonQuery();
                    Assert.Fail("The previous query should have failed.");
                }
                catch (Exception x)
                {
                    if (!x.IsDbException())
                    {
                        throw;
                    }
                    // a DbException is expected (for the case of a SqlServer35 the SqlCeException is not derived from DbException)
                    if (!(x is DbException) && x.GetType().Name != "SqlCeException")
                    {
                        throw;
                    }
                }
            });
        }
        public void Up(IDatabase db)
        {
            MySqlHelper.ActivateStrictMode(db);

            const string tableName = "Mig8";

            ExpectedTables.Clear();

            // create a table that contains columns for all supported data types
            ICreatedTable table = db.CreateTable(tableName);
            Dictionary <string, DbType> columns = new Dictionary <string, DbType>();
            int i = 1;

            foreach (SupportsAttribute support in IntegrationTestContext.SupportsAttributes
                     .Where(s => !IntegrationTestContext.IsScripting || s.IsScriptable)
                     .OrderByDescending(s => s.MaximumSize))                         // make sure the first column is not a LOB column as Teradata automatically adds an index to the first column and then would crash with: 'Cannot create index on LOB columns.'
            {
                if (support.DbType == DbType.AnsiStringFixedLength ||                // skip fixed length character types as the table would grow too large
                    support.DbType == DbType.StringFixedLength ||                    // skip fixed length character types as the table would grow too large
                    support.DbType == DbType.Int64 ||                                // skip Int64 as the ODBC driver does not support DbParameters for this data type --> note that Int64 is implicitly tested as MigSharp uses this data type for its timestamp column
                    (support.DbType == DbType.Decimal && support.MaximumScale == 0)) // this is test thoroughly in Migration11
                {
                    continue;
                }

                string columnName = "Column" + i++;
                ICreatedTableWithAddedColumn column = table.WithNullableColumn(columnName, support.DbType);
                if (support.MaximumSize > 0)
                {
                    column.OfSize(support.MaximumSize, support.MaximumScale > 0 ? support.MaximumScale : (int?)null);
                }


                if (db.Context.ProviderMetadata.Platform == Platform.MySql)
                {
                    // having two maxlength strings exceeds the maximum table length in mysql (65535) - reduce the size of these columns
                    if (support.DbType == DbType.AnsiString ||
                        support.DbType == DbType.String)
                    {
                        column.OfSize(5000);
                    }
                }

                columns.Add(columnName, support.DbType);
            }

            db.Execute(context =>
            {
                IDbCommand command = context.CreateCommand();

                ExpectedTables[0].Clear();
                var values = new List <object>();
                foreach (var column in columns)
                {
                    DbType dbType;
                    object value = GetTestValue(column, db, out dbType);
                    // MySQL only retains 8 digits for float values
                    if (db.Context.ProviderMetadata.Platform == Platform.MySql)
                    {
                        if (dbType == DbType.Single)
                        {
                            value = (float)Math.Round((float)value, 5);
                        }
                    }
                    command.AddParameter("@" + column.Key, dbType, value);
                    values.Add(value);
                }
                ExpectedTables[0].Add(values.ToArray());

                command.CommandText = string.Format(CultureInfo.InvariantCulture, @"INSERT INTO ""{0}"" ({1}) VALUES ({2})",
                                                    Tables[0].Name,
                                                    string.Join(", ", columns.Keys.Select(c => "\"" + c + "\"").ToArray()),
                                                    string.Join(", ", command.Parameters.Cast <IDbDataParameter>().Select(p => context.ProviderMetadata.GetParameterSpecifier(p)).ToArray()));
                context.CommandExecutor.ExecuteNonQuery(command);
            });

            ExpectedTables.Add(new ExpectedTable(tableName, columns.Keys));

            // create a table for each supported primary key data type
            // (as combining them all into one table would be too much)
            foreach (SupportsAttribute support in IntegrationTestContext.SupportsAttributes
                     .Where(s => (!IntegrationTestContext.IsScripting || s.IsScriptable) && s.CanBeUsedAsPrimaryKey))
            {
                string        pkTableName = Tables[0].Name + "WithPkOf" + support.DbType + support.MaximumScale;
                ICreatedTable pkTable     = db.CreateTable(pkTableName);
                int           maximumSize = support.MaximumSize;
                if (db.Context.ProviderMetadata.Platform == Platform.SqlServer)
                {
                    // SQL Server only allow PKs with a maximum length of 900 bytes
                    if (support.DbType == DbType.AnsiStringFixedLength)
                    {
                        maximumSize = 900; // FEATURE: this information should be part of the SupportAttribute
                    }
                    if (support.DbType == DbType.StringFixedLength)
                    {
                        maximumSize = 450; // FEATURE: this information should be part of the SupportAttribute
                    }
                }

                if (db.Context.ProviderMetadata.Platform == Platform.MySql)
                {
                    // MySQL only allow PKs with a maximum length of 767 bytes
                    if (support.DbType == DbType.AnsiString ||
                        support.DbType == DbType.String)
                    {
                        maximumSize = 767 / 3; // utf8 chars can take up to three bytes per char (see: https://dev.mysql.com/doc/refman/5.0/en/column-count-limit.html)
                    }
                }

                ICreatedTableWithAddedColumn column = pkTable.WithPrimaryKeyColumn("Id", support.DbType);
                if (maximumSize > 0)
                {
                    column.OfSize(maximumSize, support.MaximumScale > 0 ? support.MaximumScale : (int?)null);
                }
                ExpectedTables.Add(new ExpectedTable(pkTableName, "Id"));
            }
        }