added better db migration support than what Subsonic provides out of the box.

This commit is contained in:
kay.one 2011-05-23 17:34:57 -07:00
commit ce63f05512
91 changed files with 7218 additions and 48 deletions

View file

@ -0,0 +1,40 @@
#region License
//The contents of this file are subject to the Mozilla Public License
//Version 1.1 (the "License"); you may not use this file except in
//compliance with the License. You may obtain a copy of the License at
//http://www.mozilla.org/MPL/
//Software distributed under the License is distributed on an "AS IS"
//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//License for the specific language governing rights and limitations
//under the License.
#endregion
namespace Migrator.Framework.SchemaBuilder
{
public class AddColumnExpression : ISchemaBuilderExpression
{
private IFluentColumn _column;
private string _toTable;
public AddColumnExpression(string toTable, IFluentColumn column)
{
_column = column;
_toTable = toTable;
}
public void Create(ITransformationProvider provider)
{
provider.AddColumn(_toTable, _column.Name, _column.Type, _column.Size, _column.ColumnProperty, _column.DefaultValue);
if (_column.ForeignKey != null)
{
provider.AddForeignKey(
"FK_" + _toTable + "_" + _column.Name + "_" + _column.ForeignKey.PrimaryTable + "_" +
_column.ForeignKey.PrimaryKey,
_toTable, _column.Name, _column.ForeignKey.PrimaryTable, _column.ForeignKey.PrimaryKey, _column.Constraint);
}
}
}
}

View file

@ -0,0 +1,28 @@
#region License
//The contents of this file are subject to the Mozilla Public License
//Version 1.1 (the "License"); you may not use this file except in
//compliance with the License. You may obtain a copy of the License at
//http://www.mozilla.org/MPL/
//Software distributed under the License is distributed on an "AS IS"
//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//License for the specific language governing rights and limitations
//under the License.
#endregion
namespace Migrator.Framework.SchemaBuilder
{
public class AddTableExpression : ISchemaBuilderExpression
{
private string _newTable;
public AddTableExpression(string newTable)
{
_newTable = newTable;
}
public void Create(ITransformationProvider provider)
{
provider.AddTable(_newTable);
}
}
}

View file

@ -0,0 +1,28 @@
#region License
//The contents of this file are subject to the Mozilla Public License
//Version 1.1 (the "License"); you may not use this file except in
//compliance with the License. You may obtain a copy of the License at
//http://www.mozilla.org/MPL/
//Software distributed under the License is distributed on an "AS IS"
//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//License for the specific language governing rights and limitations
//under the License.
#endregion
namespace Migrator.Framework.SchemaBuilder
{
public class DeleteTableExpression : ISchemaBuilderExpression
{
private string _tableName;
public DeleteTableExpression(string tableName)
{
_tableName = tableName;
}
public void Create(ITransformationProvider provider)
{
provider.RemoveTable(_tableName);
}
}
}

View file

@ -0,0 +1,80 @@
#region License
//The contents of this file are subject to the Mozilla Public License
//Version 1.1 (the "License"); you may not use this file except in
//compliance with the License. You may obtain a copy of the License at
//http://www.mozilla.org/MPL/
//Software distributed under the License is distributed on an "AS IS"
//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//License for the specific language governing rights and limitations
//under the License.
#endregion
using System.Data;
namespace Migrator.Framework.SchemaBuilder
{
public class FluentColumn : IFluentColumn
{
private Column _inner;
private ForeignKeyConstraint _constraint;
private ForeignKey _fk;
public FluentColumn(string columnName)
{
_inner = new Column(columnName);
}
public ColumnProperty ColumnProperty
{
get { return _inner.ColumnProperty; }
set { _inner.ColumnProperty = value; }
}
public string Name
{
get { return _inner.Name; }
set { _inner.Name = value; }
}
public DbType Type
{
get { return _inner.Type; }
set { _inner.Type = value; }
}
public int Size
{
get { return _inner.Size; }
set { _inner.Size = value; }
}
public bool IsIdentity
{
get { return _inner.IsIdentity; }
}
public bool IsPrimaryKey
{
get { return _inner.IsPrimaryKey; }
}
public object DefaultValue
{
get { return _inner.DefaultValue; }
set { _inner.DefaultValue = value; }
}
public ForeignKeyConstraint Constraint
{
get { return _constraint; }
set { _constraint = value; }
}
public ForeignKey ForeignKey
{
get { return _fk; }
set { _fk = value; }
}
}
}

View file

@ -0,0 +1,39 @@
#region License
//The contents of this file are subject to the Mozilla Public License
//Version 1.1 (the "License"); you may not use this file except in
//compliance with the License. You may obtain a copy of the License at
//http://www.mozilla.org/MPL/
//Software distributed under the License is distributed on an "AS IS"
//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//License for the specific language governing rights and limitations
//under the License.
#endregion
namespace Migrator.Framework.SchemaBuilder
{
public class ForeignKey
{
private string _primaryTable;
private string _primaryKey;
public ForeignKey(string primaryTable, string primaryKey)
{
_primaryTable = primaryTable;
_primaryKey = primaryKey;
}
public string PrimaryTable
{
get { return _primaryTable; }
set { _primaryTable = value; }
}
public string PrimaryKey
{
get { return _primaryKey; }
set { _primaryKey = value; }
}
}
}

View file

@ -0,0 +1,13 @@
using System.Data;
namespace Migrator.Framework.SchemaBuilder
{
public interface IColumnOptions
{
SchemaBuilder OfType(DbType dbType);
SchemaBuilder WithSize(int size);
IForeignKeyOptions AsForeignKey();
}
}

View file

@ -0,0 +1,24 @@
#region License
//The contents of this file are subject to the Mozilla Public License
//Version 1.1 (the "License"); you may not use this file except in
//compliance with the License. You may obtain a copy of the License at
//http://www.mozilla.org/MPL/
//Software distributed under the License is distributed on an "AS IS"
//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//License for the specific language governing rights and limitations
//under the License.
#endregion
namespace Migrator.Framework.SchemaBuilder
{
public interface IDeleteTableOptions
{
SchemaBuilder WithTable(string name);
SchemaBuilder AddTable(string name);
IDeleteTableOptions DeleteTable(string name);
}
}

View file

@ -0,0 +1,22 @@
#region License
//The contents of this file are subject to the Mozilla Public License
//Version 1.1 (the "License"); you may not use this file except in
//compliance with the License. You may obtain a copy of the License at
//http://www.mozilla.org/MPL/
//Software distributed under the License is distributed on an "AS IS"
//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//License for the specific language governing rights and limitations
//under the License.
#endregion
namespace Migrator.Framework.SchemaBuilder
{
public interface IFluentColumn : IColumn
{
ForeignKeyConstraint Constraint { get; set; }
ForeignKey ForeignKey { get; set; }
}
}

View file

@ -0,0 +1,20 @@
#region License
//The contents of this file are subject to the Mozilla Public License
//Version 1.1 (the "License"); you may not use this file except in
//compliance with the License. You may obtain a copy of the License at
//http://www.mozilla.org/MPL/
//Software distributed under the License is distributed on an "AS IS"
//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//License for the specific language governing rights and limitations
//under the License.
#endregion
namespace Migrator.Framework.SchemaBuilder
{
public interface IForeignKeyOptions
{
SchemaBuilder ReferencedTo(string primaryKeyTable, string primaryKeyColumn);
}
}

View file

@ -0,0 +1,20 @@
#region License
//The contents of this file are subject to the Mozilla Public License
//Version 1.1 (the "License"); you may not use this file except in
//compliance with the License. You may obtain a copy of the License at
//http://www.mozilla.org/MPL/
//Software distributed under the License is distributed on an "AS IS"
//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//License for the specific language governing rights and limitations
//under the License.
#endregion
namespace Migrator.Framework.SchemaBuilder
{
public interface ISchemaBuilderExpression
{
void Create(ITransformationProvider provider);
}
}

View file

@ -0,0 +1,31 @@
#region License
//The contents of this file are subject to the Mozilla Public License
//Version 1.1 (the "License"); you may not use this file except in
//compliance with the License. You may obtain a copy of the License at
//http://www.mozilla.org/MPL/
//Software distributed under the License is distributed on an "AS IS"
//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//License for the specific language governing rights and limitations
//under the License.
#endregion
namespace Migrator.Framework.SchemaBuilder
{
public class RenameTableExpression : ISchemaBuilderExpression
{
private string _oldName;
private string _newName;
public RenameTableExpression(string oldName, string newName)
{
_oldName = oldName;
_newName = newName;
}
public void Create(ITransformationProvider provider)
{
provider.RenameTable(_oldName, _newName);
}
}
}

View file

@ -0,0 +1,169 @@
#region License
//The contents of this file are subject to the Mozilla Public License
//Version 1.1 (the "License"); you may not use this file except in
//compliance with the License. You may obtain a copy of the License at
//http://www.mozilla.org/MPL/
//Software distributed under the License is distributed on an "AS IS"
//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//License for the specific language governing rights and limitations
//under the License.
#endregion
using System;
using System.Collections.Generic;
using System.Data;
namespace Migrator.Framework.SchemaBuilder
{
public class SchemaBuilder : IColumnOptions, IForeignKeyOptions, IDeleteTableOptions
{
private string _currentTable;
private IFluentColumn _currentColumn;
private IList<ISchemaBuilderExpression> _exprs;
public SchemaBuilder()
{
_exprs = new List<ISchemaBuilderExpression>();
}
public IEnumerable<ISchemaBuilderExpression> Expressions
{
get { return _exprs; }
}
/// <summary>
/// Adds a Table to be created to the Schema
/// </summary>
/// <param name="name">Table name to be created</param>
/// <returns>SchemaBuilder for chaining</returns>
public SchemaBuilder AddTable(string name)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException("name");
_exprs.Add(new AddTableExpression(name));
_currentTable = name;
return this;
}
public IDeleteTableOptions DeleteTable(string name)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException("name");
_currentTable = "";
_currentColumn = null;
_exprs.Add(new DeleteTableExpression(name));
return this;
}
/// <summary>
/// Reference an existing table.
/// </summary>
/// <param name="newName">Table to reference</param>
/// <returns>SchemaBuilder for chaining</returns>
public SchemaBuilder RenameTable(string newName)
{
if (string.IsNullOrEmpty(newName))
throw new ArgumentNullException("newName");
_exprs.Add(new RenameTableExpression(_currentTable, newName));
_currentTable = newName;
return this;
}
/// <summary>
/// Reference an existing table.
/// </summary>
/// <param name="name">Table to reference</param>
/// <returns>SchemaBuilder for chaining</returns>
public SchemaBuilder WithTable(string name)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException("name");
_currentTable = name;
return this;
}
/// <summary>
/// Adds a Column to be created
/// </summary>
/// <param name="name">Column name to be added</param>
/// <returns>IColumnOptions to restrict chaining</returns>
public IColumnOptions AddColumn(string name)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException("name");
if (string.IsNullOrEmpty(_currentTable))
throw new ArgumentException("missing referenced table");
IFluentColumn column = new FluentColumn(name);
_currentColumn = column;
_exprs.Add(new AddColumnExpression(_currentTable, column));
return this;
}
public SchemaBuilder OfType(DbType columnType)
{
_currentColumn.Type = columnType;
return this;
}
public SchemaBuilder WithProperty(ColumnProperty columnProperty)
{
_currentColumn.ColumnProperty = columnProperty;
return this;
}
public SchemaBuilder WithSize(int size)
{
if (size == 0)
throw new ArgumentNullException("size", "Size must be greater than zero");
_currentColumn.Size = size;
return this;
}
public SchemaBuilder WithDefaultValue(object defaultValue)
{
if (defaultValue == null)
throw new ArgumentNullException("defaultValue", "DefaultValue cannot be null or empty");
_currentColumn.DefaultValue = defaultValue;
return this;
}
public IForeignKeyOptions AsForeignKey()
{
_currentColumn.ColumnProperty = ColumnProperty.ForeignKey;
return this;
}
public SchemaBuilder ReferencedTo(string primaryKeyTable, string primaryKeyColumn)
{
_currentColumn.Constraint = ForeignKeyConstraint.NoAction;
_currentColumn.ForeignKey = new ForeignKey(primaryKeyTable, primaryKeyColumn);
return this;
}
public SchemaBuilder WithConstraint(ForeignKeyConstraint action)
{
_currentColumn.Constraint = action;
return this;
}
}
}