Added support for unique constraints to SchemaBuilder.

This commit is contained in:
Daniel Stolt
2015-08-19 12:05:09 +01:00
parent 6b4a497e2d
commit 1c7ed9cb94
5 changed files with 78 additions and 6 deletions

View File

@@ -125,41 +125,54 @@ namespace Orchard.Data.Migration.Interpreters {
return;
}
// drop columns
// Drop columns.
foreach (var dropColumn in command.TableCommands.OfType<DropColumnCommand>()) {
var builder = new StringBuilder();
Visit(builder, dropColumn);
RunPendingStatements();
}
// add columns
// Add columns.
foreach (var addColumn in command.TableCommands.OfType<AddColumnCommand>()) {
var builder = new StringBuilder();
Visit(builder, addColumn);
RunPendingStatements();
}
// alter columns
// Alter columns.
foreach (var alterColumn in command.TableCommands.OfType<AlterColumnCommand>()) {
var builder = new StringBuilder();
Visit(builder, alterColumn);
RunPendingStatements();
}
// add index
// Add index.
foreach (var addIndex in command.TableCommands.OfType<AddIndexCommand>()) {
var builder = new StringBuilder();
Visit(builder, addIndex);
RunPendingStatements();
}
// drop index
// Drop index.
foreach (var dropIndex in command.TableCommands.OfType<DropIndexCommand>()) {
var builder = new StringBuilder();
Visit(builder, dropIndex);
RunPendingStatements();
}
// Add unique constraint.
foreach (var addUniqueConstraint in command.TableCommands.OfType<AddUniqueConstraintCommand>()) {
var builder = new StringBuilder();
Visit(builder, addUniqueConstraint);
RunPendingStatements();
}
// Drop unique constraint.
foreach (var dropUniqueConstraint in command.TableCommands.OfType<DropUniqueConstraintCommand>()) {
var builder = new StringBuilder();
Visit(builder, dropUniqueConstraint);
RunPendingStatements();
}
}
public void Visit(StringBuilder builder, AddColumnCommand command) {
@@ -210,7 +223,6 @@ namespace Orchard.Data.Migration.Interpreters {
_sqlStatements.Add(builder.ToString());
}
public void Visit(StringBuilder builder, AddIndexCommand command) {
if (ExecuteCustomInterpreter(command)) {
return;
@@ -235,6 +247,31 @@ namespace Orchard.Data.Migration.Interpreters {
_sqlStatements.Add(builder.ToString());
}
public void Visit(StringBuilder builder, AddUniqueConstraintCommand command) {
if (ExecuteCustomInterpreter(command)) {
return;
}
builder.AppendFormat("alter table {0} add constraint {1} unique ({2})",
_dialectLazy.Value.QuoteForTableName(PrefixTableName(command.TableName)),
_dialectLazy.Value.QuoteForColumnName(PrefixTableName(command.ConstraintName)),
String.Join(", ", command.ColumnNames));
_sqlStatements.Add(builder.ToString());
}
public void Visit(StringBuilder builder, DropUniqueConstraintCommand command) {
if (ExecuteCustomInterpreter(command)) {
return;
}
builder.AppendFormat("alter table {0} drop constraint {1}",
_dialectLazy.Value.QuoteForTableName(PrefixTableName(command.TableName)),
_dialectLazy.Value.QuoteForColumnName(PrefixTableName(command.ConstraintName)));
_sqlStatements.Add(builder.ToString());
}
public override void Visit(SqlStatementCommand command) {
if (command.Providers.Count != 0 && !command.Providers.Contains(_shellSettings.DataProvider)) {
return;

View File

@@ -0,0 +1,13 @@
namespace Orchard.Data.Migration.Schema {
public class AddUniqueConstraintCommand : TableCommand {
public string ConstraintName { get; set; }
public AddUniqueConstraintCommand(string tableName, string constraintName, params string[] columnNames)
: base(tableName) {
ColumnNames = columnNames;
ConstraintName = constraintName;
}
public string[] ColumnNames { get; private set; }
}
}

View File

@@ -47,5 +47,15 @@ namespace Orchard.Data.Migration.Schema {
var command = new DropIndexCommand(Name, indexName);
TableCommands.Add(command);
}
public void CreateUniqueConstraint(string constraintName, params string[] columnNames) {
var command = new AddUniqueConstraintCommand(Name, constraintName, columnNames);
TableCommands.Add(command);
}
public void DropUniqueConstraint(string constraintName) {
var command = new DropUniqueConstraintCommand(Name, constraintName);
TableCommands.Add(command);
}
}
}

View File

@@ -0,0 +1,10 @@
namespace Orchard.Data.Migration.Schema {
public class DropUniqueConstraintCommand : TableCommand {
public string ConstraintName { get; set; }
public DropUniqueConstraintCommand(string tableName, string constraintName)
: base(tableName) {
ConstraintName = constraintName;
}
}
}

View File

@@ -149,6 +149,8 @@
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="Data\Migration\Schema\AddUniqueConstraintCommand.cs" />
<Compile Include="Data\Migration\Schema\DropUniqueConstraintCommand.cs" />
<Compile Include="Environment\Extensions\Helpers\ExtensionManagerExtensions.cs" />
<Compile Include="Environment\Extensions\Models\LifecycleStatus.cs" />
<Compile Include="Mvc\Updater.cs" />