Permalink
Browse files

Implemented CurrentUser SystemMethod for SqlServer

Added tests for the other SystemMethods as well.
  • Loading branch information...
1 parent 70e5f83 commit 756cc1f8ee37b92528e66ed3e23d13f23d876304 @daniellee committed Dec 6, 2012
@@ -48,6 +48,8 @@ protected override string FormatSystemMethods(SystemMethods systemMethod)
return "GETDATE()";
case SystemMethods.CurrentUTCDateTime:
return "GETUTCDATE()";
+ case SystemMethods.CurrentUser:
+ return "CURRENT_USER";
}
return null;
@@ -1,4 +1,6 @@
-using FluentMigrator.Expressions;
+using System.Data;
+using FluentMigrator.Expressions;
+using FluentMigrator.Model;
using FluentMigrator.Runner.Generators.SqlServer;
using NUnit.Framework;
using NUnit.Should;
@@ -39,5 +41,70 @@ public void CanGenerateNecessaryStatementsForADeleteDefaultExpression()
generator.Generate(expression).ShouldBe(expected);
}
+
+ [Test]
+ public void CanUseSystemMethodCurrentUserAsADefaultValueForAColumn()
+ {
+ const string tableName = "NewTable";
+
+ var columnDefinition = new ColumnDefinition { Name = "NewColumn", Size = 15, Type = DbType.String, DefaultValue = SystemMethods.CurrentUser };
+
+ var expression = new CreateColumnExpression { Column = columnDefinition, TableName = tableName };
+
+ string sql = generator.Generate(expression);
+ sql.ShouldBe("ALTER TABLE [dbo].[NewTable] ADD [NewColumn] NVARCHAR(15) NOT NULL CONSTRAINT DF__NewColumn DEFAULT CURRENT_USER");
+ }
+
+ [Test]
+ public void CanUseSystemMethodNewGuidAsADefaultValueForAColumn()
+ {
+ const string tableName = "NewTable";
+
+ var columnDefinition = new ColumnDefinition { Name = "NewColumn", Type = DbType.Guid, DefaultValue = SystemMethods.NewGuid };
+
+ var expression = new CreateColumnExpression { Column = columnDefinition, TableName = tableName };
+
+ string sql = generator.Generate(expression);
+ sql.ShouldBe("ALTER TABLE [dbo].[NewTable] ADD [NewColumn] UNIQUEIDENTIFIER NOT NULL CONSTRAINT DF__NewColumn DEFAULT NEWID()");
+ }
+
+ [Test]
+ public void CanUseSystemMethodNewSequentialIdAsADefaultValueForAColumn()
+ {
+ const string tableName = "NewTable";
+
+ var columnDefinition = new ColumnDefinition { Name = "NewColumn", Type = DbType.Guid, DefaultValue = SystemMethods.NewSequentialId };
+
+ var expression = new CreateColumnExpression { Column = columnDefinition, TableName = tableName };
+
+ string sql = generator.Generate(expression);
+ sql.ShouldBe("ALTER TABLE [dbo].[NewTable] ADD [NewColumn] UNIQUEIDENTIFIER NOT NULL CONSTRAINT DF__NewColumn DEFAULT NEWSEQUENTIALID()");
+ }
+
+ [Test]
+ public void CanUseSystemMethodCurrentDateTimeAsADefaultValueForAColumn()
+ {
+ const string tableName = "NewTable";
+
+ var columnDefinition = new ColumnDefinition { Name = "NewColumn", Type = DbType.DateTime, DefaultValue = SystemMethods.CurrentDateTime };
+
+ var expression = new CreateColumnExpression { Column = columnDefinition, TableName = tableName };
+
+ string sql = generator.Generate(expression);
+ sql.ShouldBe("ALTER TABLE [dbo].[NewTable] ADD [NewColumn] DATETIME NOT NULL CONSTRAINT DF__NewColumn DEFAULT GETDATE()");
+ }
+
+ [Test]
+ public void CanUseSystemMethodCurrentUTCDateTimeAsADefaultValueForAColumn()
+ {
+ const string tableName = "NewTable";
+
+ var columnDefinition = new ColumnDefinition { Name = "NewColumn", Type = DbType.DateTime, DefaultValue = SystemMethods.CurrentUTCDateTime };
+
+ var expression = new CreateColumnExpression { Column = columnDefinition, TableName = tableName };
+
+ string sql = generator.Generate(expression);
+ sql.ShouldBe("ALTER TABLE [dbo].[NewTable] ADD [NewColumn] DATETIME NOT NULL CONSTRAINT DF__NewColumn DEFAULT GETUTCDATE()");
+ }
}
}

0 comments on commit 756cc1f

Please sign in to comment.