Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add CurrentUser to SystemMethods #346

Merged
merged 4 commits into from

2 participants

Daniel Lee Tom Marien
Daniel Lee
Collaborator

Partially implemented #308 by adding CurrentUser to SystemMethods and implementing it for SqlServer and Postgres. It doesn't seem to be possible for MySql but should be possible to add for other database types.

This pull request also covers #316 by implementing CurrentUTCDateTime for Postgres. And the last change was adding an implementation of CurrentDateTime for MySql.

daniellee added some commits
Daniel Lee daniellee Add CurrentUTCDateTime SystemMethod to Postgres 99d3e87
Daniel Lee daniellee New CurrentUser SystemMethod and implemented for Postgres
Uses the current_user function in Postgres as the the default value.
d24c500
Daniel Lee daniellee Implemented CurrentDateTime SystemMethod for MySql
Using the CURRENT_TIMESTAMP function as default value.
70e5f83
Daniel Lee daniellee Implemented CurrentUser SystemMethod for SqlServer
Added tests for the other SystemMethods as well.
756cc1f
Tom Marien tommarien was assigned
Tom Marien tommarien merged commit 756cc1f into from
Tom Marien
Collaborator

@daniellee Thanks, m8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 6, 2012
  1. Daniel Lee
  2. Daniel Lee

    New CurrentUser SystemMethod and implemented for Postgres

    daniellee authored
    Uses the current_user function in Postgres as the the default value.
  3. Daniel Lee

    Implemented CurrentDateTime SystemMethod for MySql

    daniellee authored
    Using the CURRENT_TIMESTAMP function as default value.
  4. Daniel Lee

    Implemented CurrentUser SystemMethod for SqlServer

    daniellee authored
    Added tests for the other SystemMethods as well.
This page is out of date. Refresh to see the latest.
6 src/FluentMigrator.Runner/Generators/MySql/MySqlColumn.cs
View
@@ -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();
}
}
4 src/FluentMigrator.Runner/Generators/Postgres/PostgresColumn.cs
View
@@ -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();
2  src/FluentMigrator.Runner/Generators/SqlServer/SqlServerColumn.cs
View
@@ -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  src/FluentMigrator.Tests/FluentMigrator.Tests.csproj
View
@@ -230,6 +230,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" />
32 src/FluentMigrator.Tests/Unit/Generators/MySql/MySqlSystemMethodsTests.cs
View
@@ -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");
+ }
+ }
+}
26 src/FluentMigrator.Tests/Unit/Generators/Postgres/PostgresGeneratorTests.cs
View
@@ -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";
69 src/FluentMigrator.Tests/Unit/Generators/SqlServer2005/SqlServer2005GeneratorTests.cs
View
@@ -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()");
+ }
}
}
3  src/FluentMigrator/SystemMethods.cs
View
@@ -6,6 +6,7 @@ public enum SystemMethods
NewGuid,
NewSequentialId,
CurrentDateTime,
- CurrentUTCDateTime
+ CurrentUTCDateTime,
+ CurrentUser
}
}
Something went wrong with that request. Please try again.