private void RenameColumn(Table table, Column column, string renameFrom, Queue<SchemaItem> callbackQueue) { log("Renaming Column: " + renameFrom + " -> " + column.Name); ExecuteNonQuery("EXEC sp_rename @p1, @p2, 'COLUMN';", table.Name + "." + renameFrom, column.Name); }
private void UpgradeTable(Table table, Queue<SchemaItem> callbackQueue) { List<string> indexNames = existingIndexes.GetValueOrDefault(table.Name.Trim()); List<string> columnNames = existingColumns.GetValueOrDefault(table.Name.Trim()); foreach (var column in table.Columns) { if (Exists(columnNames, column.Name)) { try { // we can alter columns if they aren't in any bool alterable = column.DefaultValue == null && (column.IsIdentity == null || !bool.Parse(column.IsIdentity)); if (alterable && table.Indexes != null) foreach (var index in table.Indexes) if (alterable && Exists(indexNames, index.Name)) { alterable = index.Include != "*" && !Exists(SplitColumnList(index.Columns).ToList(), column.Name) && !Exists(SplitColumnList(index.Include).ToList(), column.Name); } if (alterable) ExecuteNonQuery("ALTER TABLE [" + table.Name + "] ALTER COLUMN [" + column.Name + "] " + GetTypeDescriptor(column) + (column.Nullable ? " NULL" : " NOT NULL")); } catch (Exception ex) { // Most of the time this means nothing or has to be fixed manually log(string.Format("Warning: alter column {0}.{1} failed: {2}", table.Name, column.Name, ex.GetBaseException().Message)); } } else { string renameFrom = null; if (column.OldNames != null) renameFrom = column.OldNames.Where(oldname => Exists(columnNames, oldname)).FirstOrDefault(); if (renameFrom != null) RenameColumn(table, column, renameFrom, callbackQueue); else CreateColumn(table, column, callbackQueue); } if (!string.IsNullOrEmpty(column.References)) CreateForeignKey(table, column); } if (table.Indexes != null) { foreach (var index in table.Indexes) { var exists = Exists(indexNames, index.Name); if (exists && index.Include != "*") { // currently we don't upgrade exising indexes } else { CreateIndex(table, index, exists, callbackQueue); } } } }
private void CreateTable(Table table, Queue<SchemaItem> callbackQueue) { log("Creating Table: " + table.Name); RunCallback(table, CallbackAction.Before); callbackQueue.Enqueue(table); ExecuteNonQuery("CREATE TABLE [" + table.Name + "] ( [__JUST_CREATED] INT NULL )"); UpgradeTable(table, callbackQueue); ExecuteNonQuery("ALTER TABLE [" + table.Name + "] DROP COLUMN [__JUST_CREATED]"); }
private int GetVersionInfo() { object oVersion = null; try { oVersion = ExecuteScalar("SELECT VersionId FROM [Version] WHERE SchemaName = @p1;", schema.Name); } catch { Table version = new Table(); version.Name = "Version"; version.Columns = new Column[] { new Column("VersionId", "int", null), new Column("SchemaName", "varchar", "250"), }; version.Indexes = new Index[] { new Index("PK_Version", IndexType.PrimaryKey, "SchemaName"), }; CreateTable(version, new Queue<SchemaItem>()); } if (oVersion == null || oVersion is DBNull) return -1; else return Convert.ToInt32(oVersion); }
private void CreateIndexPrimaryKey(Table table, Index index) { log("Creating Primary Key: " + table.Name + " (" + index.Columns + ")"); ExecuteNonQuery(string.Format("ALTER TABLE [{0}] ADD CONSTRAINT [{1}] PRIMARY KEY ({2});", table.Name, index.Name, EscapeColumnList(false, index.Columns))); }
private void CreateIndex(Table table, Index index, bool update, Queue<SchemaItem> callbackQueue) { RunCallback(index, CallbackAction.Before); callbackQueue.Enqueue(index); switch (index.Type) { case IndexType.PrimaryKey: CreateIndexPrimaryKey(table, index); break; case IndexType.Index: CreateIndexRegular(table.Name, table.Columns, update, index); break; case IndexType.Unique: CreateIndexUnique(table.Name, index); break; default: break; } }
private void CreateForeignKey(Table table, Column column) { string keyname = string.Format("FK_{0}_{1}_{2}", table.Name, column.Name, column.References); List<string> fkeys = existingForeignKeys.GetValueOrDefault(table.Name.Trim()); if (Exists(fkeys, keyname)) return; log("Creating Foreign Key: " + keyname); existingForeignKeys.GetValue(table.Name, () => new List<string>()).Add(keyname); finalUpdates.Add(string.Format("ALTER TABLE [{0}] ADD CONSTRAINT [FK_{0}_{1}_{2}] FOREIGN KEY ([{1}]) REFERENCES [{2}];", table.Name, column.Name, column.References)); }
private void CreateColumn(Table table, Column column, Queue<SchemaItem> callbackQueue) { log("Creating Column: " + column.Name); RunCallback(column, CallbackAction.Before); callbackQueue.Enqueue(column); string typeDesc = GetColumnDescriptor(column); string query = string.Format("ALTER TABLE [{0}] ADD [{1}] {2}", table.Name, column.Name, typeDesc); ExecuteNonQuery(query); }
internal static string CreateTableWithoutCallbacksOrIndexesOrReferencesString(Table table) { return string.Format("CREATE TABLE [{0}] ({1})", table.Name, (from c in table.Columns select c.Name + " " + GetColumnDescriptor(c)).Join(", ")); }