Skip to content

Commit c5d52f4

Browse files
committed
Fixed column properties mapper up to apply reserved word rules as part of migration.
Added some hacks to deal with existing migrations where we have length 256 strings, when it should be 255 (will address this properly with a MySql51Dialect that allows larger varchar(n) values, at which point they can actually be length 256 or greater.
1 parent bc66f4c commit c5d52f4

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

src/Migrator.Providers/ColumnPropertiesMapper.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,28 @@ namespace Migrator.Providers
1010
/// </summary>
1111
public class ColumnPropertiesMapper
1212
{
13+
protected Dialect dialect;
14+
15+
/// <summary>The SQL type</summary>
16+
protected string type;
17+
18+
/// <summary>The name of the column</summary>
19+
protected string name;
20+
1321
/// <summary>
1422
/// the type of the column
1523
/// </summary>
1624
protected string columnSql;
1725

1826
/// <summary>
19-
/// Sql if this column has a default value
27+
/// Sql if This column is Indexed
2028
/// </summary>
21-
protected object defaultVal;
22-
23-
protected Dialect dialect;
29+
protected bool indexed = false;
2430

2531
/// <summary>
26-
/// Sql if This column is Indexed
32+
/// Sql if this column has a default value
2733
/// </summary>
28-
protected bool indexed;
29-
30-
/// <summary>The name of the column</summary>
31-
protected string name;
32-
33-
/// <summary>The SQL type</summary>
34-
protected string type;
34+
protected object defaultVal;
3535

3636
public ColumnPropertiesMapper(Dialect dialect, string type)
3737
{
@@ -79,18 +79,18 @@ public void MapColumnProperties(Column column)
7979
Name = column.Name;
8080
indexed = PropertySelected(column.ColumnProperty, ColumnProperty.Indexed);
8181

82-
var vals = new List<string>();
83-
vals.Add(dialect.ColumnNameNeedsQuote ? QuotedName : Name);
82+
List<string> vals = new List<string>();
83+
vals.Add(dialect.ColumnNameNeedsQuote || dialect.IsReservedWord(Name) ? QuotedName : Name);
8484

8585
vals.Add(type);
8686

87-
if (! dialect.IdentityNeedsType)
87+
if (!dialect.IdentityNeedsType)
8888
AddValueIfSelected(column, ColumnProperty.Identity, vals);
8989

9090
if (dialect.IsUnsignedCompatible(column.Type))
9191
AddValueIfSelected(column, ColumnProperty.Unsigned, vals);
9292

93-
if (! PropertySelected(column.ColumnProperty, ColumnProperty.PrimaryKey) || dialect.NeedsNotNullForIdentity)
93+
if (!PropertySelected(column.ColumnProperty, ColumnProperty.PrimaryKey) || dialect.NeedsNotNullForIdentity)
9494
AddValueIfSelected(column, ColumnProperty.NotNull, vals);
9595

9696
AddValueIfSelected(column, ColumnProperty.PrimaryKey, vals);
@@ -107,7 +107,7 @@ public void MapColumnProperties(Column column)
107107
columnSql = String.Join(" ", vals.ToArray());
108108
}
109109

110-
void AddValueIfSelected(Column column, ColumnProperty property, ICollection<string> vals)
110+
private void AddValueIfSelected(Column column, ColumnProperty property, ICollection<string> vals)
111111
{
112112
if (PropertySelected(column.ColumnProperty, property))
113113
vals.Add(dialect.SqlForProperty(property));
@@ -118,4 +118,4 @@ public static bool PropertySelected(ColumnProperty source, ColumnProperty compar
118118
return (source & comparison) == comparison;
119119
}
120120
}
121-
}
121+
}

src/Migrator.Providers/Impl/Mysql/MysqlDialect.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ public class MysqlDialect : Dialect
88
{
99
public MysqlDialect()
1010
{
11+
// TODO: As per http://dev.mysql.com/doc/refman/5.0/en/char.html 5.0.3 and above
12+
// can handle varchar(n) up to a length OF 65,535 - so the limit of 255 should no longer apply.
13+
1114
RegisterColumnType(DbType.AnsiStringFixedLength, "CHAR(255)");
1215
RegisterColumnType(DbType.AnsiStringFixedLength, 255, "CHAR($l)");
1316
RegisterColumnType(DbType.AnsiStringFixedLength, 65535, "TEXT");
1417
RegisterColumnType(DbType.AnsiStringFixedLength, 16777215, "MEDIUMTEXT");
1518
RegisterColumnType(DbType.AnsiString, "VARCHAR(255)");
1619
RegisterColumnType(DbType.AnsiString, 255, "VARCHAR($l)");
20+
RegisterColumnType(DbType.AnsiString, 256, "VARCHAR(255)");
1721
RegisterColumnType(DbType.AnsiString, 65535, "TEXT");
1822
RegisterColumnType(DbType.AnsiString, 16777215, "MEDIUMTEXT");
1923
RegisterColumnType(DbType.Binary, "LONGBLOB");
@@ -39,8 +43,10 @@ public MysqlDialect()
3943
RegisterColumnType(DbType.StringFixedLength, 16777215, "MEDIUMTEXT");
4044
RegisterColumnType(DbType.String, "VARCHAR(255)");
4145
RegisterColumnType(DbType.String, 255, "VARCHAR($l)");
46+
RegisterColumnType(DbType.String, 256, "VARCHAR(255)");
4247
RegisterColumnType(DbType.String, 65535, "TEXT");
4348
RegisterColumnType(DbType.String, 16777215, "MEDIUMTEXT");
49+
RegisterColumnType(DbType.String, 1073741823, "LONGTEXT");
4450
RegisterColumnType(DbType.Time, "TIME");
4551

4652
RegisterProperty(ColumnProperty.Unsigned, "UNSIGNED");

0 commit comments

Comments
 (0)