Skip to content

Commit

Permalink
Fixes in Oracle and MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
jogibear9988 committed Jul 25, 2014
1 parent 2ed1c2b commit 4118178
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ INNER JOIN information_schema.TABLE_CONSTRAINTS i
if (tuple.Item3 == "FOREIGN KEY")
RemoveForeignKey(tuple.Item1, tuple.Item2);
else if (tuple.Item3 == "PRIMARY KEY")
ExecuteNonQuery(String.Format("ALTER TABLE {0} DROP PRIMARY KEY", table));
{
try
{
ExecuteNonQuery(String.Format("ALTER TABLE {0} DROP PRIMARY KEY", table));
}
catch (Exception)
{ }
}
else if (tuple.Item3 == "UNIQUE")
RemoveIndex(tuple.Item1, tuple.Item2);
}
Expand Down
72 changes: 59 additions & 13 deletions src/Migrator.Providers/Impl/Oracle/OracleTransformationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,53 @@ public override void AddColumn(string table, string sqlColumn)
table = QuoteTableNameIfRequired(table);
sqlColumn = QuoteColumnNameIfRequired(sqlColumn);
ExecuteNonQuery(String.Format("ALTER TABLE {0} ADD {1}", table, sqlColumn));
}
}

public override string[] GetConstraints(string table)
{
var constraints = new List<string>();
//using (
// IDataReader reader =
// ExecuteQuery(
// String.Format("SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE LOWER(TABLE_NAME) = LOWER('{0}')", table)))
//{
// while (reader.Read())
// {
// constraints.Add(reader.GetString(0));
// }
//}

using (
IDataReader reader =
ExecuteQuery(
String.Format("SELECT constraint_name FROM user_constraints WHERE lower(table_name) = '{0}'", table.ToLower())))
{
while (reader.Read())
{
constraints.Add(reader.GetString(0));
}
}

return constraints.ToArray();
}

protected override string GetPrimaryKeyConstraintName(string table)
{
var constraints = new List<string>();

using (
IDataReader reader =
ExecuteQuery(
String.Format("SELECT constraint_name FROM user_constraints WHERE lower(table_name) = '{0}' and constraint_type = 'P'", table.ToLower())))
{
while (reader.Read())
{
constraints.Add(reader.GetString(0));
}
}

return constraints.FirstOrDefault();
}

public override bool ConstraintExists(string table, string name)
{
Expand Down Expand Up @@ -207,7 +253,7 @@ public override bool TableExists(string table)
string sql = string.Format("SELECT COUNT(table_name) FROM user_tables WHERE lower(table_name) = '{0}'", table.ToLower());

if (_defaultSchema != null)
sql = string.Format("SELECT COUNT(table_name) FROM all_tables WHERE lower(owner) = '{0}' and lower(table_name) = '{1}'", _defaultSchema.ToLower(), table.ToLower());
sql = string.Format("SELECT COUNT(table_name) FROM user_tables WHERE lower(owner) = '{0}' and lower(table_name) = '{1}'", _defaultSchema.ToLower(), table.ToLower());

Logger.Log(sql);
object count = ExecuteScalar(sql);
Expand Down Expand Up @@ -472,19 +518,19 @@ private string SchemaInfoTableName
}
}

protected override string GetPrimaryKeyConstraintName(string table)
{
var sql = "select constraint_name " +
"from user_indexes join user_constraints on user_indexes.index_name = user_constraints.constraint_name " +
"where lower(user_indexes.table_name) = lower('{0}') and constraint_type = 'P'";
//protected override string GetPrimaryKeyConstraintName(string table)
//{
// var sql = "select constraint_name " +
// "from user_indexes join user_constraints on user_indexes.index_name = user_constraints.constraint_name " +
// "where lower(user_indexes.table_name) = lower('{0}') and constraint_type = 'P'";

sql = string.Format(sql, table);
// sql = string.Format(sql, table);

using (IDataReader reader = ExecuteQuery(sql))
{
return reader.Read() ? reader.GetString(0) : null;
}
}
// using (IDataReader reader = ExecuteQuery(sql))
// {
// return reader.Read() ? reader.GetString(0) : null;
// }
//}

public override Index[] GetIndexes(string table)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Migrator.Providers/TransformationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1539,7 +1539,7 @@ protected string QuoteConstraintNameIfRequired(string name)

protected virtual string GetPrimaryKeyConstraintName(string table)
{
throw new NotImplementedException();
return null;
}

public virtual void RemovePrimaryKey(string table)
Expand Down

0 comments on commit 4118178

Please sign in to comment.