/// <summary> /// Cria a tabela de versão do banco de dados, caso não exista. /// </summary> private void CreateVersionTable() { var info = new InformationSchemaColumn { DBName = DataBaseName }; var version = new AppVersion { DBName = DataBaseName }; if (!info.GetByKey(version.TableName, AppVersion.FieldsName.ID)) { version.Create(); } //Verifica se existe a coluna AssemlyName //Se não existir cria a coluna cria if (!info.GetByKey(version.TableName, AppVersion.FieldsName.AssemblyName)) { AlterTable(version, new List <TableAdapterField> { version.Collumns[AppVersion.FieldsName.AssemblyName] }); } }
/// <summary> /// Atualiza a versão do banco de dasdos /// </summary> /// <param name="assemblyVersion">Versão do Assembly</param> /// <param name="dbAppVersion">Objeto Table de Versão do Banco</param> /// <param name="dbVersion">Versão do Banco de dados</param> private void UpdateDbVersion(Version assemblyVersion, AppVersion dbAppVersion, Version dbVersion) { Connection.Instance.ConnectionParameter.Database = DataBaseName; Connection.Instance.Connect(); if (OnBeforeUpdate != null) { OnBeforeUpdate(this, new DBEventArgs { OldVersion = dbVersion, NewVersion = assemblyVersion }); } LoadObjects(); CreateTableLogIfExists(); //Verifica se alterou alguma cosia na tabela var infosSchema = new InformationSchemaColumn { DBName = DataBaseName }.GetAll(); //Pegar todas as tabelas que existem no banco e não existem nos objetos var tableNoExist = (from table in Tables where !infosSchema.Exists(myInfo => table.TableName == myInfo.TableName) select table).ToList(); CreateTables(tableNoExist); //Para todos as tabelas que não existem verifcar se as colunas das tabelas existem ou não var tableExists = (from table in Tables where infosSchema.Exists(info => String.Equals(table.TableName, info.TableName, StringComparison.CurrentCultureIgnoreCase)) select table).ToList(); foreach (var table in tableExists) { var fields = (from column in table.Collumns where !infosSchema.Exists( infoSchema => String.Equals(table.TableName, infoSchema.TableName, StringComparison.CurrentCultureIgnoreCase) && String.Equals(infoSchema.ColumName, column.Name, StringComparison.CurrentCultureIgnoreCase)) select column).ToList(); if (fields.IsEmpty()) { continue; } AlterTable(table, fields); } foreach (var item in new Stack <StoredProcedure>(StoredProcedures)) { item.Drop(); } CreateProcedures(StoredProcedures); AddVersionToDb(assemblyVersion, dbAppVersion); UpdateInitialValues(); if (OnAfterUpdate != null) { OnAfterUpdate(this, new DBEventArgs { OldVersion = dbVersion, NewVersion = assemblyVersion }); } }