Skip to content

Commit

Permalink
Merge pull request #1042 from foo-543674/master
Browse files Browse the repository at this point in the history
Fix Oracle dialect create sequence NOCACHE
  • Loading branch information
jzabroski committed Jul 18, 2019
2 parents 9d80faa + 5bf34f9 commit f3aabd0
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 10 deletions.
Expand Up @@ -416,10 +416,19 @@ public override string Generate(CreateSequenceExpression expression)
result.AppendFormat(" START WITH {0}", seq.StartWith);
}

const long MINIMUM_CACHE_VALUE = 2;
if (seq.Cache.HasValue)
{
if (seq.Cache.Value < MINIMUM_CACHE_VALUE)
{
return CompatibilityMode.HandleCompatibilty("Cache size must be greater than 1; if you intended to disable caching, set Cache to null.");
}
result.AppendFormat(" CACHE {0}", seq.Cache);
}
else
{
result.Append(" NO CACHE");
}

if (seq.Cycle)
{
Expand Down
Expand Up @@ -92,10 +92,19 @@ public override string Generate(CreateSequenceExpression expression)
result.AppendFormat(" START WITH {0}", seq.StartWith);
}

const long MINIMUM_CACHE_VALUE = 2;
if (seq.Cache.HasValue)
{
if (seq.Cache.Value < MINIMUM_CACHE_VALUE)
{
return CompatibilityMode.HandleCompatibilty("Cache size must be greater than 1; if you intended to disable caching, set Cache to null.");
}
result.AppendFormat(" CACHE {0}", seq.Cache);
}
else
{
result.Append(" NO CACHE");
}

if (seq.Cycle)
{
Expand Down
Expand Up @@ -124,10 +124,19 @@ public override string Generate(CreateSequenceExpression expression)
result.AppendFormat(" START WITH {0}", seq.StartWith);
}

const long MINIMUM_CACHE_VALUE = 2;
if (seq.Cache.HasValue)
{
if (seq.Cache.Value < MINIMUM_CACHE_VALUE)
{
return CompatibilityMode.HandleCompatibilty("Oracle does not support Cache value equal to 1; if you intended to disable caching, set Cache to null. For information on Oracle limitations, see: https://docs.oracle.com/en/database/oracle/oracle-database/18/sqlrf/CREATE-SEQUENCE.html#GUID-E9C78A8C-615A-4757-B2A8-5E6EFB130571__GUID-7E390BE1-2F6C-4E5A-9D5C-5A2567D636FB");
}
result.AppendFormat(" CACHE {0}", seq.Cache);
}
else
{
result.Append(" NOCACHE");
}

if (seq.Cycle)
{
Expand Down
Expand Up @@ -374,7 +374,50 @@ protected string GetDataList(List<object> data)

public override string Generate(CreateSequenceExpression expression)
{
return string.Format("{0};", base.Generate(expression));
var result = new StringBuilder("CREATE SEQUENCE ");
var seq = expression.Sequence;
result.AppendFormat(Quoter.QuoteSequenceName(seq.Name, seq.SchemaName));

if (seq.Increment.HasValue)
{
result.AppendFormat(" INCREMENT {0}", seq.Increment);
}

if (seq.MinValue.HasValue)
{
result.AppendFormat(" MINVALUE {0}", seq.MinValue);
}

if (seq.MaxValue.HasValue)
{
result.AppendFormat(" MAXVALUE {0}", seq.MaxValue);
}

if (seq.StartWith.HasValue)
{
result.AppendFormat(" START WITH {0}", seq.StartWith);
}

const long MINIMUM_CACHE_VALUE = 2;
if (seq.Cache.HasValue)
{
if (seq.Cache.Value < MINIMUM_CACHE_VALUE)
{
return CompatibilityMode.HandleCompatibilty("Cache size must be greater than 1; if you intended to disable caching, set Cache to null.");
}
result.AppendFormat(" CACHE {0}", seq.Cache);
}
else
{
result.Append(" CACHE 1");
}

if (seq.Cycle)
{
result.Append(" CYCLE");
}

return string.Format("{0};", result.ToString());
}

public override string Generate(DeleteSequenceExpression expression)
Expand Down
Expand Up @@ -80,10 +80,19 @@ public override string Generate(Expressions.CreateSequenceExpression expression)
result.AppendFormat(" START WITH {0}", seq.StartWith);
}

const long MINIMUM_CACHE_VALUE = 2;
if (seq.Cache.HasValue)
{
if (seq.Cache.Value < MINIMUM_CACHE_VALUE)
{
return CompatibilityMode.HandleCompatibilty("Cache size must be greater than 1; if you intended to disable caching, set Cache to null.");
}
result.AppendFormat(" CACHE {0}", seq.Cache);
}
else
{
result.Append(" NO CACHE");
}

if (seq.Cycle)
{
Expand Down
@@ -1,3 +1,4 @@
using FluentMigrator.Exceptions;
using FluentMigrator.Runner.Generators.DB2;
using FluentMigrator.Runner.Generators.DB2.iSeries;

Expand All @@ -15,7 +16,10 @@ public class Db2SequenceTests : BaseSequenceTests
[SetUp]
public void Setup()
{
Generator = new Db2Generator(new Db2ISeriesQuoter());
Generator = new Db2Generator(new Db2ISeriesQuoter())
{
CompatibilityMode = Runner.CompatibilityMode.STRICT,
};
}

[Test]
Expand All @@ -37,6 +41,28 @@ public override void CanCreateSequenceWithDefaultSchema()
result.ShouldBe("CREATE SEQUENCE Sequence INCREMENT 2 MINVALUE 0 MAXVALUE 100 START WITH 2 CACHE 10 CYCLE");
}

[Test]
public void CanCreateSequenceWithNocache()
{
var expression = GeneratorTestHelper.GetCreateSequenceExpression();
expression.Sequence.Cache = null;

var result = Generator.Generate(expression);
result.ShouldBe("CREATE SEQUENCE Sequence INCREMENT 2 MINVALUE 0 MAXVALUE 100 START WITH 2 NO CACHE CYCLE");
}

[Test]
public void CanNotCreateSequenceWithCacheOne()
{
var expression = GeneratorTestHelper.GetCreateSequenceExpression();
expression.Sequence.Cache = 1;

Should.Throw<DatabaseOperationNotSupportedException>(
() => Generator.Generate(expression),
"Cache size must be greater than 1; if you intended to disable caching, set Cache to null."
);
}

[Test]
public override void CanDropSequenceWithCustomSchema()
{
Expand Down
Expand Up @@ -16,6 +16,7 @@
//
#endregion

using FluentMigrator.Exceptions;
using FluentMigrator.Runner.Generators.Hana;

using NUnit.Framework;
Expand All @@ -33,7 +34,10 @@ public class HanaSequenceTests : BaseSequenceTests
[SetUp]
public void Setup()
{
Generator = new HanaGenerator();
Generator = new HanaGenerator()
{
CompatibilityMode = Runner.CompatibilityMode.STRICT,
};
}

[Test]
Expand All @@ -55,6 +59,28 @@ public override void CanCreateSequenceWithDefaultSchema()
result.ShouldBe("CREATE SEQUENCE \"Sequence\" INCREMENT BY 2 MINVALUE 0 MAXVALUE 100 START WITH 2 CACHE 10 CYCLE;");
}

[Test]
public void CanCreateSequenceWithNocache()
{
var expression = GeneratorTestHelper.GetCreateSequenceExpression();
expression.Sequence.Cache = null;

var result = Generator.Generate(expression);
result.ShouldBe("CREATE SEQUENCE \"Sequence\" INCREMENT BY 2 MINVALUE 0 MAXVALUE 100 START WITH 2 NO CACHE CYCLE;");
}

[Test]
public void CanNotCreateSequenceWithCacheOne()
{
var expression = GeneratorTestHelper.GetCreateSequenceExpression();
expression.Sequence.Cache = 1;

Should.Throw<DatabaseOperationNotSupportedException>(
() => Generator.Generate(expression),
"Cache size must be greater than 1; if you intended to disable caching, set Cache to null."
);
}

[Test]
public override void CanDropSequenceWithCustomSchema()
{
Expand Down
@@ -1,4 +1,5 @@
using FluentMigrator.Runner.Generators.Oracle;
using FluentMigrator.Exceptions;
using FluentMigrator.Runner.Generators.Oracle;
using NUnit.Framework;

using Shouldly;
Expand All @@ -13,7 +14,10 @@ public class OracleSequenceTests : BaseSequenceTests
[SetUp]
public void Setup()
{
Generator = new OracleGenerator();
Generator = new OracleGenerator()
{
CompatibilityMode = Runner.CompatibilityMode.STRICT,
};
}

[Test]
Expand All @@ -35,6 +39,28 @@ public override void CanCreateSequenceWithDefaultSchema()
result.ShouldBe("CREATE SEQUENCE Sequence INCREMENT BY 2 MINVALUE 0 MAXVALUE 100 START WITH 2 CACHE 10 CYCLE");
}

[Test]
public void CanCreateSequenceWithNocacheSchema()
{
var expression = GeneratorTestHelper.GetCreateSequenceExpression();
expression.Sequence.Cache = null;

var result = Generator.Generate(expression);
result.ShouldBe("CREATE SEQUENCE Sequence INCREMENT BY 2 MINVALUE 0 MAXVALUE 100 START WITH 2 NOCACHE CYCLE");
}

[Test]
public void CanNotCreateSequenceWithCacheOneSchema()
{
var expression = GeneratorTestHelper.GetCreateSequenceExpression();
expression.Sequence.Cache = 1;

Should.Throw<DatabaseOperationNotSupportedException>(
() => Generator.Generate(expression),
"Oracle does not support Cache value equal to 1; if you intended to disable caching, set Cache to null. For information on Oracle limitations, see: https://docs.oracle.com/en/database/oracle/oracle-database/18/sqlrf/CREATE-SEQUENCE.html#GUID-E9C78A8C-615A-4757-B2A8-5E6EFB130571__GUID-7E390BE1-2F6C-4E5A-9D5C-5A2567D636FB"
);
}

[Test]
public override void CanDropSequenceWithCustomSchema()
{
Expand All @@ -54,4 +80,4 @@ public override void CanDropSequenceWithDefaultSchema()
result.ShouldBe("DROP SEQUENCE Sequence");
}
}
}
}
@@ -1,3 +1,4 @@
using FluentMigrator.Exceptions;
using FluentMigrator.Runner.Generators.Postgres;
using FluentMigrator.Runner.Processors.Postgres;

Expand All @@ -16,7 +17,10 @@ public class PostgresSequenceTests : BaseSequenceTests
public void Setup()
{
var quoter = new PostgresQuoter(new PostgresOptions());
Generator = new PostgresGenerator(quoter);
Generator = new PostgresGenerator(quoter)
{
CompatibilityMode = Runner.CompatibilityMode.STRICT,
};
}

[Test]
Expand All @@ -38,6 +42,28 @@ public override void CanCreateSequenceWithDefaultSchema()
result.ShouldBe("CREATE SEQUENCE \"Sequence\" INCREMENT 2 MINVALUE 0 MAXVALUE 100 START WITH 2 CACHE 10 CYCLE;");
}

[Test]
public void CanCreateSequenceWithNocache()
{
var expression = GeneratorTestHelper.GetCreateSequenceExpression();
expression.Sequence.Cache = null;

var result = Generator.Generate(expression);
result.ShouldBe("CREATE SEQUENCE \"Sequence\" INCREMENT BY 2 MINVALUE 0 MAXVALUE 100 START WITH 2 CACHE 1 CYCLE;");
}

[Test]
public void CanNotCreateSequenceWithCacheOne()
{
var expression = GeneratorTestHelper.GetCreateSequenceExpression();
expression.Sequence.Cache = 1;

Should.Throw<DatabaseOperationNotSupportedException>(
() => Generator.Generate(expression),
"Cache size must be greater than 1; if you intended to disable caching, set Cache to null."
);
}

[Test]
public override void CanDropSequenceWithCustomSchema()
{
Expand Down
@@ -1,4 +1,5 @@
using FluentMigrator.Runner.Generators.SqlServer;
using FluentMigrator.Exceptions;
using FluentMigrator.Runner.Generators.SqlServer;
using NUnit.Framework;

using Shouldly;
Expand All @@ -13,7 +14,10 @@ public class SqlServer2012SequenceTests : BaseSequenceTests
[SetUp]
public void Setup()
{
Generator = new SqlServer2012Generator();
Generator = new SqlServer2012Generator()
{
CompatibilityMode = Runner.CompatibilityMode.STRICT,
};
}


Expand All @@ -36,6 +40,28 @@ public override void CanCreateSequenceWithDefaultSchema()
result.ShouldBe("CREATE SEQUENCE [dbo].[Sequence] INCREMENT BY 2 MINVALUE 0 MAXVALUE 100 START WITH 2 CACHE 10 CYCLE");
}

[Test]
public void CanCreateSequenceWithNocache()
{
var expression = GeneratorTestHelper.GetCreateSequenceExpression();
expression.Sequence.Cache = null;

var result = Generator.Generate(expression);
result.ShouldBe("CREATE SEQUENCE [dbo].[Sequence] INCREMENT BY 2 MINVALUE 0 MAXVALUE 100 START WITH 2 NO CACHE CYCLE");
}

[Test]
public void CanNotCreateSequenceWithCacheOne()
{
var expression = GeneratorTestHelper.GetCreateSequenceExpression();
expression.Sequence.Cache = 1;

Should.Throw<DatabaseOperationNotSupportedException>(
() => Generator.Generate(expression),
"Cache size must be greater than 1; if you intended to disable caching, set Cache to null."
);
}

[Test]
public override void CanDropSequenceWithCustomSchema()
{
Expand All @@ -55,4 +81,4 @@ public override void CanDropSequenceWithDefaultSchema()
result.ShouldBe("DROP SEQUENCE [dbo].[Sequence]");
}
}
}
}

0 comments on commit f3aabd0

Please sign in to comment.