Skip to content

Commit

Permalink
#124 with unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjw committed Aug 27, 2021
1 parent 36ccd21 commit 944f175
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public Indexes(int? commandTimeout, string owner, string tableName) : base(comma
COLUMN_NAME,
COLUMN_POSITION,
DESCEND, --normally ASC
DECODE(UNIQUENESS,'UNIQUE',1,0) IsUnique
DECODE(UNIQUENESS,'UNIQUE',1,0) IsUnique,
ix.INDEX_TYPE
FROM ALL_IND_COLUMNS cols
INNER JOIN ALL_INDEXES ix
ON ix.OWNER = cols.INDEX_OWNER AND ix.INDEX_NAME = cols.INDEX_NAME
Expand Down Expand Up @@ -58,6 +59,7 @@ protected override void Mapper(IDataRecord record)
TableName = tableName,
Name = name,
IsUnique = record.GetBoolean("IsUnique"),
IndexType = record.GetString("INDEX_TYPE")
};
Result.Add(index);
}
Expand Down
2 changes: 1 addition & 1 deletion DatabaseSchemaReader/SqlGen/SqlServer/ConstraintWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public override string WritePrimaryKey(DatabaseConstraint constraint)
//the pk index will have IndexType of PRIMARY NONCLUSTERED
var pkIndex = Table.Indexes.Find(x => x.IndexType?.IndexOf("PRIMARY", StringComparison.OrdinalIgnoreCase) != -1);
if (pkIndex != null &&
pkIndex.IndexType.IndexOf("NONCLUSTERED", StringComparison.OrdinalIgnoreCase) != -1)
pkIndex.IndexType?.IndexOf("NONCLUSTERED", StringComparison.OrdinalIgnoreCase) > -1)
{
nonClustered = "NONCLUSTERED";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Data;
using System.Linq;

namespace DatabaseSchemaReaderTest.SqlGen.Migrations.UnitTests
{
Expand Down Expand Up @@ -115,5 +116,28 @@ public void TestConstraintEscaping()
sql = migration.AddTable(newTable);
Assert.IsTrue(sql.Contains("ALTER TABLE \"Chapter\" ADD CONSTRAINT \"Chapter_Pk\" PRIMARY KEY (\"Id\");"), "Escaping for names");
}


[TestMethod]
public void TestPrimaryKeyNoIndexType()
{
//#119 escaping was not cascaded down from table generator to constraint generator

//arrange
var schemaTemp = new DatabaseSchema(null, SqlType.Oracle);
var newTable = schemaTemp.AddTable("Chapter");
var idColumn = newTable.AddColumn("Id", DbType.Int64);
idColumn.AddPrimaryKey("Chapter_Pk").AddIndex("PK_Index");
//deliberately set indextype to null (from non SqlServer db)
newTable.Indexes.Find(x => x.Name == "PK_Index").IndexType = null;

var migration = new DdlGeneratorFactory(SqlType.SqlServer).MigrationGenerator();
migration.EscapeNames = false;

//act
var sql = migration.AddTable(newTable);

Assert.IsTrue(sql.Contains("ALTER TABLE Chapter ADD CONSTRAINT Chapter_Pk PRIMARY KEY (Id)"), "No escaping for names");
}
}
}

0 comments on commit 944f175

Please sign in to comment.