Created
December 27, 2012 21:23
-
-
Save jamesmanning/4392105 to your computer and use it in GitHub Desktop.
LINQPad script to generate the set of possible 'alter table alter column not null' statements for the nullable columns in a database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var databaseInfo = GetDatabaseInfo(this); | |
// databaseInfo.Dump(); | |
foreach (var table in databaseInfo.Tables) | |
{ | |
var nullableColumns = | |
from c in table.Columns | |
where c.DatabaseType.EndsWith(" NOT NULL") == false | |
select c; | |
foreach (var column in nullableColumns) | |
{ | |
var alterStatement = String.Format("ALTER TABLE {0} ALTER COLUMN {1} {2} NOT NULL", | |
table.TableName, | |
column.ColumnName, | |
column.DatabaseType); | |
alterStatement.Dump(); | |
// this.ExecuteCommand(alterStatement); | |
} | |
} | |
} | |
// Define other methods and classes here | |
public class DatabaseInfo | |
{ | |
public Type DataContextType { get; set; } | |
public string DatabaseName { get; set; } | |
public string DatabaseServer { get; set; } | |
public TableInfo[] Tables { get; set; } | |
} | |
public class TableInfo | |
{ | |
public Type TableType { get; set; } | |
public Type EntityType { get; set; } | |
public string TableName { get; set; } | |
public ColumnInfo[] Columns { get; set; } | |
} | |
public class ColumnInfo | |
{ | |
public string ColumnName { get; set; } | |
public string DatabaseType { get; set; } | |
} | |
public DatabaseInfo GetDatabaseInfo(LINQPad.DataContextBase dataContext) | |
{ | |
return new DatabaseInfo | |
{ | |
DatabaseName = dataContext.Connection.Database, | |
DatabaseServer = dataContext.Connection.DataSource, | |
DataContextType = dataContext.GetType(), | |
Tables = GetTables(dataContext.GetType()), | |
}; | |
} | |
public TableInfo[] GetTables(Type dataContextType) | |
{ | |
var tableInfoQuery = | |
from prop in dataContextType.GetProperties() | |
where prop.PropertyType.IsGenericType | |
where prop.PropertyType.GetGenericTypeDefinition() == typeof(Table<>) | |
let tableType = prop.PropertyType | |
let entityType = tableType.GenericTypeArguments.Single() | |
select new TableInfo | |
{ | |
TableName = GetTableNameFromEntityType(entityType), | |
EntityType = entityType, | |
TableType = tableType, | |
Columns = GetColumnsFromEntityType(entityType), | |
}; | |
return tableInfoQuery.ToArray(); | |
} | |
public string GetTableNameFromEntityType(Type entityType) | |
{ | |
var tableNameQuery = | |
from ca in entityType.CustomAttributes | |
from na in ca.NamedArguments | |
where na.MemberName == "Name" | |
select na.TypedValue.Value.ToString(); | |
return tableNameQuery.Single(); | |
} | |
public ColumnInfo[] GetColumnsFromEntityType(Type entityType) | |
{ | |
var columnInfoQuery = | |
from field in entityType.GetFields() | |
from attribute in field.CustomAttributes | |
from namedArgument in attribute.NamedArguments | |
where namedArgument.MemberName == "DbType" | |
select new ColumnInfo | |
{ | |
ColumnName = field.Name, | |
DatabaseType = namedArgument.TypedValue.Value.ToString(), | |
}; | |
return columnInfoQuery.ToArray(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment