Removing attributes for migrations as a library that does this already exists.

Updating a few DTOs for programmatic creation.
This commit is contained in:
Morten@Thinkpad-X220.ab-nat1.dk
2012-10-17 10:12:30 -02:00
parent 2523781fc1
commit 5bd2059cd7
28 changed files with 112 additions and 286 deletions

View File

@@ -14,20 +14,16 @@ namespace Umbraco.Core.Models.Rdbms
public string AppAlias { get; set; }
[Column("appIcon")]
[DatabaseType(DatabaseTypes.Nvarchar, Length = 255)]
public string AppIcon { get; set; }
[Column("appName")]
[DatabaseType(DatabaseTypes.Nvarchar, Length = 255)]
public string AppName { get; set; }
[Column("appInitWithTreeAlias")]
[NullSetting(NullSetting = NullSettings.Null)]
[DatabaseType(DatabaseTypes.Nvarchar, Length = 255)]
public string AppInitWithTreeAlias { get; set; }
[Column("sortOrder")]
[DatabaseType(DatabaseTypes.TinyInteger)]
[Constraint(Name = "DF_app_sortOrder", Default = "0")]
public byte SortOrder { get; set; }
}

View File

@@ -1,4 +1,5 @@
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.DatabaseAnnotations;
namespace Umbraco.Core.Models.Rdbms
{
@@ -8,15 +9,18 @@ namespace Umbraco.Core.Models.Rdbms
internal class AppTreeDto
{
[Column("treeSilent")]
[Constraint(Default = "0")]
public bool Silent { get; set; }
[Column("treeInitialize")]
[Constraint(Default = "1")]
public bool Initialize { get; set; }
[Column("treeSortOrder")]
public byte SortOrder { get; set; }
[Column("appAlias")]
[PrimaryKeyColumn(AutoIncrement = false, Clustered = true, Name = "PK_umbracoAppTree", OnColumns = "[appAlias], [treeAlias]")]
public string AppAlias { get; set; }
[Column("treeAlias")]
@@ -38,6 +42,7 @@ namespace Umbraco.Core.Models.Rdbms
public string HandlerType { get; set; }
[Column("action")]
[NullSetting(NullSetting = NullSettings.Null)]
public string Action { get; set; }
}
}

View File

@@ -10,19 +10,16 @@ namespace Umbraco.Core.Models.Rdbms
{
[Column("pk")]
[PrimaryKeyColumn]
[DatabaseType(DatabaseTypes.Integer)]
[NullSetting(NullSetting = NullSettings.Null)]
public int PrimaryKey { get; set; }
[Column("nodeId")]
[ForeignKey(typeof(NodeDto))]
[DatabaseType(DatabaseTypes.Integer)]
[NullSetting(NullSetting = NullSettings.Null)]
[Index(IndexTypes.UniqueNonclustered, Name = "IX_cmsContent")]
[Index(IndexTypes.UniqueNonClustered, Name = "IX_cmsContent")]
public int NodeId { get; set; }
[Column("contentType")]
[DatabaseType(DatabaseTypes.Integer)]
[NullSetting(NullSetting = NullSettings.Null)]
public int ContentType { get; set; }

View File

@@ -1,4 +1,5 @@
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.DatabaseAnnotations;
namespace Umbraco.Core.Models.Rdbms
{
@@ -7,6 +8,7 @@ namespace Umbraco.Core.Models.Rdbms
internal class ContentType2ContentTypeDto
{
[Column("parentContentTypeId")]
[PrimaryKeyColumn(AutoIncrement = false, Clustered = true, Name = "PK_cmsContentType2ContentType", OnColumns = "[parentContentTypeId], [childContentTypeId]")]
public int ParentId { get; set; }
[Column("childContentTypeId")]

View File

@@ -1,4 +1,5 @@
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.DatabaseAnnotations;
namespace Umbraco.Core.Models.Rdbms
{
@@ -8,12 +9,16 @@ namespace Umbraco.Core.Models.Rdbms
internal class ContentTypeAllowedContentTypeDto
{
[Column("Id")]
[ForeignKey(typeof(ContentTypeDto), Name = "FK_cmsContentTypeAllowedContentType_cmsContentType")]
[PrimaryKeyColumn(AutoIncrement = false, Clustered = true, Name = "PK_cmsContentTypeAllowedContentType", OnColumns = "[Id], [AllowedId]")]
public int Id { get; set; }
[Column("AllowedId")]
[ForeignKey(typeof(ContentTypeDto), Name = "FK_cmsContentTypeAllowedContentType_cmsContentType1")]
public int AllowedId { get; set; }
[Column("SortOrder")]
[Constraint(Name = "df_cmsContentTypeAllowedContentType_sortOrder", Default = "0")]
public int SortOrder { get; set; }
}
}

View File

@@ -1,4 +1,5 @@
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.DatabaseAnnotations;
namespace Umbraco.Core.Models.Rdbms
{
@@ -8,27 +9,38 @@ namespace Umbraco.Core.Models.Rdbms
internal class ContentTypeDto
{
[Column("pk")]
[PrimaryKeyColumn]
public int PrimaryKey { get; set; }
[Column("nodeId")]
[ForeignKey(typeof(NodeDto))]
[Index(IndexTypes.UniqueNonClustered, Name = "IX_cmsContentType")]
public int NodeId { get; set; }
[Column("alias")]
[NullSetting(NullSetting = NullSettings.Null)]
public string Alias { get; set; }
[Column("icon")]
[Index(IndexTypes.NonClustered)]
[NullSetting(NullSetting = NullSettings.Null)]
public string Icon { get; set; }
[Column("thumbnail")]
[Constraint(Default = "'folder.png'")]
public string Thumbnail { get; set; }
[Column("description")]
[NullSetting(NullSetting = NullSettings.Null)]
[DatabaseType(DatabaseTypes.Nvarchar, Length = 1500)]
public string Description { get; set; }
[Column("isContainer")]
[Constraint(Default = "0")]
public bool IsContainer { get; set; }
[Column("allowAtRoot")]
[Constraint(Default = "0")]
public bool AllowAtRoot { get; set; }
[ResultColumn]

View File

@@ -22,7 +22,7 @@ namespace Umbraco.Core.Models.Rdbms
[Column("parentID")]
[DatabaseType(DatabaseTypes.Integer)]
[ForeignKey(typeof(NodeDto))]
[IndexAttribute(IndexTypes.Nonclustered, Name = "IX_umbracoNodeParentId")]
[IndexAttribute(IndexTypes.NonClustered, Name = "IX_umbracoNodeParentId")]
public int ParentId { get; set; }
[Column("nodeUser")]
@@ -55,7 +55,7 @@ namespace Umbraco.Core.Models.Rdbms
[Column("nodeObjectType")]
[DatabaseType(DatabaseTypes.UniqueIdentifier)]
[NullSetting(NullSetting = NullSettings.Null)]
[IndexAttribute(IndexTypes.Nonclustered, Name = "IX_umbracoNodeObjectType")]
[IndexAttribute(IndexTypes.NonClustered, Name = "IX_umbracoNodeObjectType")]
public Guid? NodeObjectType { get; set; }
[Column("createDate")]

View File

@@ -57,7 +57,7 @@
string syntax = string.Empty;
if (attribute.AutoIncrement)
syntax = "IDENTITY(1, 1)";
syntax = " IDENTITY(1, 1)";
return syntax;
}
@@ -70,8 +70,11 @@
if (DatabaseFactory.Current.DatabaseProvider == DatabaseProviders.SqlServerCE)
clustered = string.Empty;
string syntax = string.Format("ALTER TABLE [{0}] ADD CONSTRAINT [{1}] PRIMARY KEY {2} ([{3}])", tableName,
constraintName, clustered, propertyName);
string syntax = string.IsNullOrEmpty(attribute.OnColumns)
? string.Format("ALTER TABLE [{0}] ADD CONSTRAINT [{1}] PRIMARY KEY {2} ([{3}])",
tableName, constraintName, clustered, propertyName)
: string.Format("ALTER TABLE [{0}] ADD CONSTRAINT [{1}] PRIMARY KEY {2} ({3})",
tableName, constraintName, clustered, attribute.OnColumns);
return syntax;
}
@@ -90,7 +93,9 @@
var primaryKeyAttribute = attribute.Type.FirstAttribute<PrimaryKeyAttribute>();
var referencedTableName = tableNameAttribute.Value;
string constraintName = string.Format("FK_{0}_{1}", tableName, referencedTableName);
string constraintName = string.IsNullOrEmpty(attribute.Name)
? string.Format("FK_{0}_{1}", tableName, referencedTableName)
: attribute.Name;
string syntax =
string.Format(
"ALTER TABLE [{0}] ADD CONSTRAINT [{1}] FOREIGN KEY ([{2}]) REFERENCES [{3}] ([{4}])",
@@ -107,7 +112,7 @@
case IndexTypes.Clustered:
indexType = "CLUSTERED";
break;
case IndexTypes.Nonclustered:
case IndexTypes.NonClustered:
indexType = "NONCLUSTERED";
break;
case IndexTypes.PrimaryXml:
@@ -116,8 +121,8 @@
case IndexTypes.Spartial:
indexType = "SPARTIAL";
break;
case IndexTypes.UniqueNonclustered:
indexType = "UNIQUENONCLUSTERED";
case IndexTypes.UniqueNonClustered:
indexType = "UNIQUE NONCLUSTERED";
break;
}
string name = string.IsNullOrEmpty(attribute.Name) ? string.Format("IX_{0}_{1}", tableName, propertyName) : attribute.Name;

View File

@@ -12,6 +12,6 @@ namespace Umbraco.Core.Persistence.DatabaseAnnotations
public string OnDelete { get; set; }
public string OnUpdate { get; set; }
//Default naming: FK_thisTableName_refTableName
public string Name { get; set; }//Used to override Default naming: FK_thisTableName_refTableName
}
}

View File

@@ -3,8 +3,8 @@
public enum IndexTypes
{
Clustered,
Nonclustered,
UniqueNonclustered,
NonClustered,
UniqueNonClustered,
PrimaryXml,
Spartial
}

View File

@@ -14,5 +14,6 @@ namespace Umbraco.Core.Persistence.DatabaseAnnotations
public bool Clustered { get; set; }//Defaults to true
public bool AutoIncrement { get; set; }//Default to true
public string Name { get; set; }//Overrides the default naming of a PrimaryKey constraint: PK_tableName
public string OnColumns { get; set; }
}
}

View File

@@ -1,41 +0,0 @@
using Umbraco.Core.Persistence.DatabaseAnnotations;
namespace Umbraco.Core.Persistence.Migrations
{
/// <summary>
/// Represents an abstract class for descriping changes to a database column
/// Used to Add a column with an optional constraint
/// </summary>
public abstract class AddColumnChange : DropColumnChange
{
public abstract string Constraint { get; }
public abstract string DefaultForConstraint { get; }
public abstract DatabaseTypes DatabaseType { get; }
public virtual int DatabaseTypeLength
{
get { return 0; }
}
public abstract NullSettings NullSetting { get; }
public override Sql ToSql()
{
var sql = new Sql();
string constraint = !string.IsNullOrEmpty(Constraint)
? string.Format("CONSTRAINT [{0}] DEFAULT ({1})", Constraint, DefaultForConstraint)
: string.Format("CONSTRAINT [DF_{0}_{1}] DEFAULT ({2})", TableName, ColumnName,
DefaultForConstraint);
sql.Append(string.Format("ALTER TABLE [{0}] ADD [{1}] {2} {3} {4};",
TableName, ColumnName,
DatabaseType.ToSqlSyntax(DatabaseTypeLength),
NullSetting.ToSqlSyntax(),
constraint));
return sql;
}
}
}

View File

@@ -1,11 +0,0 @@
namespace Umbraco.Core.Persistence.Migrations
{
public abstract class BaseChange : IDatabaseChange
{
public abstract Sql ToSql();
public abstract string TableName { get; }
public abstract string Version { get; }
}
}

View File

@@ -1,9 +0,0 @@
namespace Umbraco.Core.Persistence.Migrations
{
public enum ChangeTypes
{
ADD,
DROP,
RENAME
}
}

View File

@@ -1,10 +0,0 @@
namespace Umbraco.Core.Persistence.Migrations
{
/// <summary>
/// Represents an abstract class for descriping changes to a database constraint
/// Used to Add, Remove or Rename a constraint
/// </summary>
public abstract class ConstraintChange
{
}
}

View File

@@ -1,10 +0,0 @@
namespace Umbraco.Core.Persistence.Migrations
{
/// <summary>
/// Represents an abstract class for descriping changes to a database column's type
/// Used to Add, Remove or Change (increase/decrease length) a column's database
/// </summary>
public abstract class DatabaseTypeChange
{
}
}

View File

@@ -1,29 +0,0 @@
using Umbraco.Core.Persistence.DatabaseAnnotations;
namespace Umbraco.Core.Persistence.Migrations
{
/// <summary>
/// Represents an abstract class for descriping changes to a database column
/// Used to Remove a column
/// </summary>
public abstract class DropColumnChange : BaseChange
{
public abstract string ColumnName { get; }
public virtual ChangeTypes ChangeType
{
get
{
return ChangeTypes.DROP;
}
}
public override Sql ToSql()
{
var sql = new Sql();
sql.Append(string.Format("ALTER TABLE [{0}] DROP COLUMN [{1}];",
TableName, ColumnName));
return sql;
}
}
}

View File

@@ -1,10 +0,0 @@
namespace Umbraco.Core.Persistence.Migrations
{
/// <summary>
/// Represents an abstract class for descriping changes to a database column's foreign key
/// Used to Add, Remove or Rename a column's foreign key
/// </summary>
public abstract class ForeignKeyChange
{
}
}

View File

@@ -1,8 +0,0 @@
namespace Umbraco.Core.Persistence.Migrations
{
public interface IDatabaseChange
{
string Version { get; }
Sql ToSql();
}
}

View File

@@ -1,10 +0,0 @@
namespace Umbraco.Core.Persistence.Migrations
{
/// <summary>
/// Represents an abstract class for descriping changes to a database index
/// Used to Add, Remove or Rename an index
/// </summary>
public abstract class IndexChange
{
}
}

View File

@@ -1,10 +0,0 @@
namespace Umbraco.Core.Persistence.Migrations
{
/// <summary>
/// Represents an abstract class for descriping changes to a database table.
/// Used to create or drop tables
/// </summary>
public abstract class TableChange
{
}
}

View File

@@ -38,6 +38,11 @@ namespace Umbraco.Core.Persistence
var last = objProperties.Last();
foreach (var propertyInfo in objProperties)
{
//If current property is a ResultColumn then skip it
var resultColumnAttribute = propertyInfo.FirstAttribute<ResultColumnAttribute>();
if(resultColumnAttribute != null) continue;
//Assumes ExplicitColumn attribute and thus having a ColumnAttribute with the name of the column
var columnAttribute = propertyInfo.FirstAttribute<ColumnAttribute>();
if(columnAttribute == null) continue;
@@ -52,6 +57,31 @@ namespace Umbraco.Core.Persistence
else
{
//Convetion - Use property's Type to set/find db type
//TODO Improve :)
if(propertyInfo.PropertyType == typeof(string))
{
sb.AppendFormat(" [nvarchar] (255)");
}
else if (propertyInfo.PropertyType == typeof(int))
{
sb.AppendFormat(" [int]");
}
else if (propertyInfo.PropertyType == typeof(bool))
{
sb.AppendFormat(" [bit]");
}
else if (propertyInfo.PropertyType == typeof(byte))
{
sb.AppendFormat(" [tinyint]");
}
else if (propertyInfo.PropertyType == typeof(DateTime))
{
sb.AppendFormat(" [datetime]");
}
else if (propertyInfo.PropertyType == typeof(Guid))
{
sb.AppendFormat(" [uniqueidentifier]");
}
}
var nullSettingAttribute = propertyInfo.FirstAttribute<NullSettingAttribute>();
@@ -68,7 +98,7 @@ namespace Umbraco.Core.Persistence
var primaryKeyColumnAttribute = propertyInfo.FirstAttribute<PrimaryKeyColumnAttribute>();
if(primaryKeyColumnAttribute != null)
{
sb.AppendFormat(" {0}", primaryKeyColumnAttribute.ToSqlSyntax());
sb.Append(primaryKeyColumnAttribute.ToSqlSyntax());
//Add to list of primary key constraints
primaryKeyConstraints.Add(new Sql(primaryKeyColumnAttribute.ToSqlSyntax(tableName, columnAttribute.Name)));
}

View File

@@ -105,7 +105,6 @@
<Compile Include="Persistence\Caching\NullCacheProvider.cs" />
<Compile Include="Persistence\Caching\RuntimeCacheProvider.cs" />
<Compile Include="Persistence\DatabaseAnnotations\AttributeExtensions.cs" />
<Compile Include="Persistence\Migrations\ChangeTypes.cs" />
<Compile Include="Persistence\DatabaseAnnotations\ConstraintAttribute.cs" />
<Compile Include="Persistence\DatabaseAnnotations\DatabaseTypeAttribute.cs" />
<Compile Include="Persistence\DatabaseAnnotations\DatabaseTypes.cs" />
@@ -132,16 +131,7 @@
<Compile Include="Persistence\Factories\RelationFactory.cs" />
<Compile Include="Persistence\Factories\RelationTypeFactory.cs" />
<Compile Include="Persistence\Mappers\ModelDtoMapper.cs" />
<Compile Include="Persistence\Migrations\AddColumnChange.cs" />
<Compile Include="Persistence\Migrations\BaseChange.cs" />
<Compile Include="Persistence\Migrations\ConstraintChange.cs" />
<Compile Include="Persistence\Migrations\DatabaseTypeChange.cs" />
<Compile Include="Persistence\Migrations\DropColumnChange.cs" />
<Compile Include="Persistence\Migrations\ForeignKeyChange.cs" />
<Compile Include="Persistence\Migrations\IDatabaseChange.cs" />
<Compile Include="Persistence\Migrations\IndexChange.cs" />
<Compile Include="Persistence\Migrations\Initial\DatabaseCreation.cs" />
<Compile Include="Persistence\Migrations\TableChange.cs" />
<Compile Include="Persistence\PetaPocoExtensions.cs" />
<Compile Include="Persistence\Querying\ExpressionHelper.cs" />
<Compile Include="Persistence\Querying\IQuery.cs" />