private DbTableChangeGroup BuildNewTableChangeGroup(DbTableInfo newTable) { var tableChangeGroup = new DbTableChangeGroup(null, newTable); _upgradeInfo.TableChanges.Add(tableChangeGroup); tableChangeGroup.Changes.Add(new DbObjectChange(null, newTable)); // add all keys in new table foreach (var key in newTable.Keys) { _changedKeys.Add(key); if (!key.KeyType.IsSet(KeyType.ForeignKey)) { tableChangeGroup.AddChange(null, key); } } // Stored procs // About referencing old proc in cmd.Peer: the table is new, so there should be normally no old table and no old stored procs; // However, there might be a situation when old stored proc is left after we delete table. In this case we need to drop old stored proc // before we create new one. Stored procs are matched by name, so newCmd would have old proc in Peer field. if (_compareStoredProcs && newTable.CrudCommands.Count > 0) { foreach (var cmd in newTable.CrudCommands) { tableChangeGroup.AddChange(cmd.Peer, cmd); } } return(tableChangeGroup); }
private DbTableChangeGroup BuildNewTableChangeGroup(DbTableInfo newTable) { var tableChangeGroup = new DbTableChangeGroup(null, newTable); _upgradeInfo.TableChanges.Add(tableChangeGroup); tableChangeGroup.Changes.Add(new DbObjectChange(null, newTable)); // add all keys in new table foreach (var key in newTable.Keys) { _changedKeys.Add(key); if (!key.KeyType.IsSet(KeyType.ForeignKey)) { tableChangeGroup.AddChange(null, key); } } return(tableChangeGroup); }
}//method //Analyzes changes in columns and keys, but not ref constraints private DbTableChangeGroup AnalyzeTableChanges(DbTableInfo oldTable) { var tableChangeGrp = new DbTableChangeGroup(oldTable, oldTable.Peer); if (oldTable.Peer == null) { //Table deleted ----------------------------------------------------------------------------------- if (_dropUnknown) { tableChangeGrp.Changes.Add(new DbObjectChange(oldTable, null, DbObjectChangeType.Drop)); //delete keys - all except primary key - it is deleted automatically with the table; foreign keys are deleted with constraints foreach (var oldKey in oldTable.Keys) { _changedKeys.Add(oldKey); if (!oldKey.KeyType.IsSet(KeyType.PrimaryKey | KeyType.ForeignKey)) { tableChangeGrp.AddChange(oldKey, null); } } } return(tableChangeGrp); } //Table/View modified? ----------------------------------------------------------------------------------- var newTable = oldTable.Peer; if (_compareTables) { //Check table rename string descr; if (!NamesMatch(oldTable.TableName, oldTable.Peer.TableName)) { descr = string.Format("Renamed {0} to {1}", oldTable.TableName, oldTable.Peer.TableName); var tableRename = new DbObjectChange(oldTable, oldTable.Peer, DbObjectChangeType.Rename, descr); tableChangeGrp.Changes.Add(tableRename); } // Internals of the table or view switch (oldTable.Kind) { case EntityKind.View: if (_compareViews && !ViewsMatch(oldTable, oldTable.Peer)) { tableChangeGrp.AddChange(oldTable, null, DbObjectChangeType.Drop); tableChangeGrp.AddChange(null, oldTable.Peer, DbObjectChangeType.Add); } break; case EntityKind.Table: foreach (var oldCol in oldTable.Columns) { if (oldCol.Peer == null) { oldCol.Flags |= DbColumnFlags.IsChanging; tableChangeGrp.AddChange(oldCol, null); } else { //Check column rename if (!NamesMatch(oldCol.ColumnName, oldCol.Peer.ColumnName)) { tableChangeGrp.AddChange(oldCol, oldCol.Peer, DbObjectChangeType.Rename); } if (!_comparer.ColumnsMatch(oldCol, oldCol.Peer, out descr)) { oldCol.Flags |= DbColumnFlags.IsChanging; tableChangeGrp.AddChange(oldCol, oldCol.Peer, notes: descr); } } } //foreach col //new columns foreach (var newCol in newTable.Columns) { if (newCol.Peer == null) //if not null - it is already taken care of { tableChangeGrp.AddChange(null, newCol); } } break; } //switch oldTable.Kind //Check table columns } // if compareTables //Detect all changed keys, indexes; skip Foreign keys - they're not real keys // We do not modify keys, only drop/create them if anything mismatches; // key.Peer is set only if the keys did not change and non of the columns changed foreach (var oldKey in oldTable.Keys) { bool changed = oldKey.Peer == null || !_comparer.KeysMatch(oldKey, oldKey.Peer); if (!changed) { continue; } _changedKeys.Add(oldKey); if (oldKey.KeyType.IsSet(KeyType.ForeignKey) || IsPureIndex(oldKey) && !_compareIndexes) { continue; } tableChangeGrp.AddChange(oldKey, oldKey.Peer); } foreach (var key in newTable.Keys) { if (key.Peer != null) { continue; //if Peer != null, it is already included in previous loop } //ignore primary key on Views - this is artificial attribute, used on CLR side only if (newTable.Kind == EntityKind.View && key.KeyType.IsSet(KeyType.PrimaryKey)) { continue; } if (key.KeyType.IsSet(KeyType.ForeignKey) || IsPureIndex(key) && !_compareIndexes) { continue; } tableChangeGrp.AddChange(null, key); } return(tableChangeGrp); }
//Analyzes changes in columns and keys, but not ref constraints private DbTableChangeGroup AnalyzeTableChanges(DbTableInfo oldTable) { var tableChangeGrp = new DbTableChangeGroup(oldTable, oldTable.Peer); if(oldTable.Peer == null) { //Table deleted ----------------------------------------------------------------------------------- if(_dropUnknown) { tableChangeGrp.Changes.Add(new DbObjectChange(oldTable, null, DbObjectChangeType.Drop)); //delete keys - all except primary key - it is deleted automatically with the table; foreign keys are deleted with constraints foreach(var oldKey in oldTable.Keys) { _changedKeys.Add(oldKey); if(!oldKey.KeyType.IsSet(KeyType.PrimaryKey | KeyType.ForeignKey)) tableChangeGrp.AddChange(oldKey, null); } foreach(var cmd in oldTable.CrudCommands) tableChangeGrp.AddChange(cmd, null); } return tableChangeGrp; } //Table/View modified? ----------------------------------------------------------------------------------- var newTable = oldTable.Peer; if(_compareTables) { //Check table rename string descr; if(!NamesMatch(oldTable.TableName, oldTable.Peer.TableName)) { descr = string.Format("Renamed {0} to {1}", oldTable.TableName, oldTable.Peer.TableName); var tableRename = new DbObjectChange(oldTable, oldTable.Peer, DbObjectChangeType.Rename, descr); tableChangeGrp.Changes.Add(tableRename); } // Internals of the table or view switch(oldTable.Kind) { case EntityKind.View: if (!ViewsMatch(oldTable, oldTable.Peer)) { tableChangeGrp.AddChange(oldTable, null, DbObjectChangeType.Drop); tableChangeGrp.AddChange(null, oldTable.Peer, DbObjectChangeType.Add); } break; case EntityKind.Table: foreach(var oldCol in oldTable.Columns) { if(oldCol.Peer == null) { _changedColumns.Add(oldCol); tableChangeGrp.AddChange(oldCol, null); } else { //Check column rename if(!NamesMatch(oldCol.ColumnName, oldCol.Peer.ColumnName)) tableChangeGrp.AddChange(oldCol, oldCol.Peer, DbObjectChangeType.Rename); if(!ColumnsMatch(oldCol, oldCol.Peer, out descr)) { _changedColumns.Add(oldCol); tableChangeGrp.AddChange(oldCol, oldCol.Peer, notes: descr); } } } //foreach col //new columns foreach (var newCol in newTable.Columns) if (newCol.Peer == null) //if not null - it is already taken care of tableChangeGrp.AddChange(null, newCol); break; }//switch oldTable.Kind //Check table columns }// if compareTables //Detect all changed keys, indexes; skip Foreign keys - they're not real keys // We do not modify keys, only drop/create them if anything mismatches; // key.Peer is set only if the keys did not change and non of the columns changed foreach (var key in oldTable.Keys) { bool changed = key.Peer == null || KeyChanged(key); if (!changed) continue; _changedKeys.Add(key); if (key.KeyType.IsSet(KeyType.ForeignKey) || IsPureIndex(key) && !_compareIndexes) continue; tableChangeGrp.AddChange(key, key.Peer); } foreach (var key in newTable.Keys) { if (key.Peer != null) continue; //if Peer != null, it is already included in previous loop if (key.KeyType.IsSet(KeyType.ForeignKey) || IsPureIndex(key) && !_compareIndexes) continue; tableChangeGrp.AddChange(null, key); } // CRUD stored procs if(_compareStoredProcs) { var oldUseProcs = _oldModel.Config.Options.IsSet(DbOptions.UseStoredProcs); var newUseProcs = _newModel.Config.Options.IsSet(DbOptions.UseStoredProcs); if(oldUseProcs || newUseProcs) { var forceRebuildProcs = tableChangeGrp.Changes.Count > 0; foreach(var cmd in newTable.CrudCommands) { if (cmd.CommandType != System.Data.CommandType.StoredProcedure) continue; if(cmd.Peer != null && StoredProceduresMatch(cmd.Peer, cmd)) continue; tableChangeGrp.AddChange(cmd.Peer, cmd); } }//if useProcs // End table modified case ------------------------------------------------------------------------ }//stored procs return tableChangeGrp; }
private DbTableChangeGroup BuildNewTableChangeGroup(DbTableInfo newTable) { var tableChangeGroup = new DbTableChangeGroup(null, newTable); _upgradeInfo.TableChanges.Add(tableChangeGroup); tableChangeGroup.Changes.Add(new DbObjectChange(null, newTable)); // add all keys in new table foreach(var key in newTable.Keys) { _changedKeys.Add(key); if(!key.KeyType.IsSet(KeyType.ForeignKey)) tableChangeGroup.AddChange(null, key); } // Stored procs // About referencing old proc in cmd.Peer: the table is new, so there should be normally no old table and no old stored procs; // However, there might be a situation when old stored proc is left after we delete table. In this case we need to drop old stored proc // before we create new one. Stored procs are matched by name, so newCmd would have old proc in Peer field. if(_compareStoredProcs && newTable.CrudCommands.Count > 0) foreach(var cmd in newTable.CrudCommands) tableChangeGroup.AddChange(cmd.Peer, cmd); return tableChangeGroup; }
}//method //Analyzes changes in columns and keys, but not ref constraints private DbTableChangeGroup AnalyzeTableChanges(DbTableInfo oldTable) { var tableChangeGrp = new DbTableChangeGroup(oldTable, oldTable.Peer); if (oldTable.Peer == null) { //Table deleted ----------------------------------------------------------------------------------- if (_dropUnknown) { tableChangeGrp.Changes.Add(new DbObjectChange(oldTable, null, DbObjectChangeType.Drop)); //delete keys - all except primary key - it is deleted automatically with the table; foreign keys are deleted with constraints foreach (var oldKey in oldTable.Keys) { _changedKeys.Add(oldKey); if (!oldKey.KeyType.IsSet(KeyType.PrimaryKey | KeyType.ForeignKey)) { tableChangeGrp.AddChange(oldKey, null); } } foreach (var cmd in oldTable.CrudCommands) { tableChangeGrp.AddChange(cmd, null); } } return(tableChangeGrp); } //Table/View modified? ----------------------------------------------------------------------------------- var newTable = oldTable.Peer; if (_compareTables) { //Check table rename string descr; if (!NamesMatch(oldTable.TableName, oldTable.Peer.TableName)) { descr = string.Format("Renamed {0} to {1}", oldTable.TableName, oldTable.Peer.TableName); var tableRename = new DbObjectChange(oldTable, oldTable.Peer, DbObjectChangeType.Rename, descr); tableChangeGrp.Changes.Add(tableRename); } // Internals of the table or view switch (oldTable.Kind) { case EntityKind.View: if (!ViewsMatch(oldTable, oldTable.Peer)) { tableChangeGrp.AddChange(oldTable, null, DbObjectChangeType.Drop); tableChangeGrp.AddChange(null, oldTable.Peer, DbObjectChangeType.Add); } break; case EntityKind.Table: foreach (var oldCol in oldTable.Columns) { if (oldCol.Peer == null) { _changedColumns.Add(oldCol); tableChangeGrp.AddChange(oldCol, null); } else { //Check column rename if (!NamesMatch(oldCol.ColumnName, oldCol.Peer.ColumnName)) { tableChangeGrp.AddChange(oldCol, oldCol.Peer, DbObjectChangeType.Rename); } if (!ColumnsMatch(oldCol, oldCol.Peer, out descr)) { _changedColumns.Add(oldCol); tableChangeGrp.AddChange(oldCol, oldCol.Peer, notes: descr); } } } //foreach col //new columns foreach (var newCol in newTable.Columns) { if (newCol.Peer == null) //if not null - it is already taken care of { tableChangeGrp.AddChange(null, newCol); } } break; } //switch oldTable.Kind //Check table columns } // if compareTables //Detect all changed keys, indexes; skip Foreign keys - they're not real keys // We do not modify keys, only drop/create them if anything mismatches; // key.Peer is set only if the keys did not change and non of the columns changed foreach (var key in oldTable.Keys) { bool changed = key.Peer == null || KeyChanged(key); if (!changed) { continue; } _changedKeys.Add(key); if (key.KeyType.IsSet(KeyType.ForeignKey) || IsPureIndex(key) && !_compareIndexes) { continue; } tableChangeGrp.AddChange(key, key.Peer); } foreach (var key in newTable.Keys) { if (key.Peer != null) { continue; //if Peer != null, it is already included in previous loop } if (key.KeyType.IsSet(KeyType.ForeignKey) || IsPureIndex(key) && !_compareIndexes) { continue; } tableChangeGrp.AddChange(null, key); } // CRUD stored procs if (_compareStoredProcs) { var oldUseProcs = _oldModel.Config.Options.IsSet(DbOptions.UseStoredProcs); var newUseProcs = _newModel.Config.Options.IsSet(DbOptions.UseStoredProcs); if (oldUseProcs || newUseProcs) { var forceRebuildProcs = tableChangeGrp.Changes.Count > 0; foreach (var cmd in newTable.CrudCommands) { if (cmd.CommandType != System.Data.CommandType.StoredProcedure) { continue; } if (cmd.Peer != null && StoredProceduresMatch(cmd.Peer, cmd)) { continue; } tableChangeGrp.AddChange(cmd.Peer, cmd); } } //if useProcs // End table modified case ------------------------------------------------------------------------ } //stored procs return(tableChangeGrp); }