Skip to content

Commit

Permalink
Merge pull request #346 of daniellee/systemmethods
Browse files Browse the repository at this point in the history
# 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
tommarien committed Dec 14, 2012
2 parents 92fc95d + 756cc1f commit af884cc
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/FluentMigrator.Runner/Generators/MySql/MySqlColumn.cs
Expand Up @@ -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();
}
}
Expand Down
Expand Up @@ -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();
Expand Down
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/FluentMigrator.Tests/FluentMigrator.Tests.csproj
Expand Up @@ -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" />
Expand Down
@@ -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");
}
}
}
Expand Up @@ -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";
Expand Down
@@ -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;
Expand Down Expand Up @@ -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()");
}
}
}
3 changes: 2 additions & 1 deletion src/FluentMigrator/SystemMethods.cs
Expand Up @@ -6,6 +6,7 @@ public enum SystemMethods
NewGuid,
NewSequentialId,
CurrentDateTime,
CurrentUTCDateTime
CurrentUTCDateTime,
CurrentUser
}
}

0 comments on commit af884cc

Please sign in to comment.