Permalink
Browse files

Merge pull request #346 of daniellee/systemmethods

# By Daniel Lee
# Via Daniel Lee
* pr/n346_daniellee:
  Implemented CurrentUser SystemMethod for SqlServer
  Implemented CurrentDateTime SystemMethod for MySql
  New CurrentUser SystemMethod and implemented for Postgres
  Add CurrentUTCDateTime SystemMethod to Postgres
  • Loading branch information...
2 parents 92fc95d + 756cc1f commit af884cc4c1638757381e1ab0542f303bbb39c629 @tommarien tommarien committed Dec 14, 2012
@@ -18,6 +18,12 @@ protected override string FormatIdentity(ColumnDefinition column)
protected override string FormatSystemMethods(SystemMethods systemMethod)
{
+ switch (systemMethod)
+ {
+ case SystemMethods.CurrentDateTime:
+ return "CURRENT_TIMESTAMP";
+ }
+
throw new NotImplementedException();
}
}
@@ -44,6 +44,10 @@ protected override string FormatSystemMethods(SystemMethods systemMethod)
return "uuid_generate_v4()";
case SystemMethods.CurrentDateTime:
return "now()";
+ case SystemMethods.CurrentUTCDateTime:
+ return "(now() at time zone 'UTC')";
+ case SystemMethods.CurrentUser:
+ return "current_user";
}
throw new NotImplementedException();
@@ -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;
@@ -233,6 +233,7 @@
<Compile Include="Unit\Expressions\DeleteDefaultConstraintExpressionTests.cs" />
<Compile Include="Unit\Generators\GenericGenerator\GenericGeneratorTests.cs" />
<Compile Include="Unit\Generators\Firebird\FirebirdGeneratorTests.cs" />
+ <Compile Include="Unit\Generators\MySql\MySqlSystemMethodsTests.cs" />
<Compile Include="Unit\Generators\Oracle\OracleCreateSequenceTests.cs" />
<Compile Include="Unit\Generators\Postgres\PostgresDataTests.cs" />
<Compile Include="Unit\Generators\SqlServer2000\SqlServer2000GeneratorTests.cs" />
@@ -0,0 +1,32 @@
+using System.Data;
+using FluentMigrator.Expressions;
+using FluentMigrator.Model;
+using FluentMigrator.Runner.Generators.MySql;
+using NUnit.Framework;
+using NUnit.Should;
+
+namespace FluentMigrator.Tests.Unit.Generators.MySql
+{
+ [TestFixture]
+ public class MySqlSystemMethodsTests
+ {
+ private MySqlGenerator generator;
+
+ [SetUp]
+ public void SetUp()
+ {
+ generator = new MySqlGenerator();
+ }
+
+ [Test]
+ public void CanUseSystemMethodCurrentDateTimeAsADefaultValueForAColumn()
+ {
+ var columnDefinition = new ColumnDefinition { Name = "NewColumn", Size = 15, Type = null, CustomType = "TIMESTAMP", DefaultValue = SystemMethods.CurrentDateTime };
+ var expression = new CreateColumnExpression {Column = columnDefinition, TableName = "NewTable"};
+
+ string sql = generator.Generate(expression);
+
+ sql.ShouldBe("ALTER TABLE `NewTable` ADD COLUMN `NewColumn` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP");
+ }
+ }
+}
@@ -606,6 +606,32 @@ private DeleteTableExpression GetDeleteTableExpression(string tableName)
return new DeleteTableExpression { TableName = tableName };
}
+ [Test]
+ public void CanUseSystemMethodCurrentUTCDateTimeAsADefaultValueForAColumn()
+ {
+ const string tableName = "NewTable";
+
+ var columnDefinition = new ColumnDefinition {Name = "NewColumn", Size = 5, Type = DbType.String, DefaultValue = SystemMethods.CurrentUTCDateTime};
+
+ var expression = new CreateColumnExpression {Column = columnDefinition, TableName = tableName};
+
+ string sql = generator.Generate(expression);
+ sql.ShouldBe("ALTER TABLE \"public\".\"NewTable\" ADD \"NewColumn\" varchar(5) NOT NULL DEFAULT (now() at time zone 'UTC')");
+ }
+
+ [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 \"public\".\"NewTable\" ADD \"NewColumn\" varchar(15) NOT NULL DEFAULT current_user");
+ }
+
private CreateTableExpression GetCreateTableExpression(string tableName)
{
string columnName1 = "ColumnName1";
@@ -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()");
+ }
}
}
@@ -6,6 +6,7 @@ public enum SystemMethods
NewGuid,
NewSequentialId,
CurrentDateTime,
- CurrentUTCDateTime
+ CurrentUTCDateTime,
+ CurrentUser
}
}

0 comments on commit af884cc

Please sign in to comment.