From 1f4e14b2e973ee7de337fd4866d9a5ceff5cb6d1 Mon Sep 17 00:00:00 2001 From: Lucas Faria Mendes Date: Mon, 30 Mar 2026 10:38:18 -0300 Subject: chore: location --- Codemerx/Gestor.Application/Migration/Migrator.cs | 189 ++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 Codemerx/Gestor.Application/Migration/Migrator.cs (limited to 'Codemerx/Gestor.Application/Migration') diff --git a/Codemerx/Gestor.Application/Migration/Migrator.cs b/Codemerx/Gestor.Application/Migration/Migrator.cs new file mode 100644 index 0000000..eab28a3 --- /dev/null +++ b/Codemerx/Gestor.Application/Migration/Migrator.cs @@ -0,0 +1,189 @@ +using Gestor.Application.Helpers; +using Gestor.Infrastructure.Repository.Interface; +using Gestor.Infrastructure.UnitOfWork.Generic; +using Gestor.Infrastructure.UnitOfWork.Logic; +using Gestor.Model.Helper; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Data.SqlClient; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace Gestor.Application.Migration +{ + public class Migrator + { + private readonly Assembly _executingAssembly; + + private static string _connection + { + get; + set; + } + + public Migrator() + { + this._executingAssembly = Assembly.GetExecutingAssembly(); + } + + public async Task Execute() + { + bool flag; + try + { + string[] allFiles = await this.GetAllFiles(); + flag = await this.ExecuteFiles(allFiles); + return flag; + } + catch (Exception exception) + { + } + flag = false; + return flag; + } + + private async Task ExecuteFiles(IEnumerable filesToExecute) + { + bool flag; + IEnumerable strs = filesToExecute; + foreach (string str in + from x in strs + orderby x + select x) + { + using (Stream manifestResourceStream = this._executingAssembly.GetManifestResourceStream(str)) + { + if (manifestResourceStream != null) + { + Encoding encoding = Encoding.GetEncoding("ISO-8859-1"); + Encoding uTF8 = Encoding.UTF8; + using (StreamReader streamReader = new StreamReader(manifestResourceStream)) + { + byte[] bytes = uTF8.GetBytes(streamReader.ReadToEnd()); + byte[] numArray = Encoding.Convert(uTF8, encoding, bytes); + if (await Migrator.ExecuteScript(encoding.GetString(numArray))) + { + string str1 = str; + char[] chrArray = new char[] { '\u005F' }; + if (!await Migrator.SaveUpdate((long)ValidationHelper.ToInt(ValidationHelper.Clear(ValidationHelper.Index(str1.Split(chrArray), 1))))) + { + flag = false; + return flag; + } + } + else + { + flag = false; + return flag; + } + } + streamReader = null; + } + else + { + continue; + } + } + manifestResourceStream = null; + } + flag = true; + return flag; + } + + private static async Task ExecuteScript(string scriptText) + { + bool flag1 = await Task.Run(() => { + bool flag; + IEnumerable strs = Regex.Split(scriptText, "^\\s*GO\\s*$", RegexOptions.IgnoreCase | RegexOptions.Multiline); + try + { + if (string.IsNullOrEmpty(Migrator._connection)) + { + Migrator._connection = Connection.GetConnection(true); + } + using (SqlConnection sqlConnection = new SqlConnection(Migrator._connection)) + { + sqlConnection.Open(); + using (SqlCommand sqlCommand = sqlConnection.CreateCommand()) + { + foreach (string str in strs) + { + if (str.Trim() == "") + { + continue; + } + sqlCommand.CommandText = str; + sqlCommand.ExecuteNonQuery(); + } + } + } + flag = true; + } + catch (Exception exception) + { + flag = false; + } + return flag; + }); + return flag1; + } + + private async Task GetAllFiles() + { + string str = string.Concat(this._executingAssembly.GetName().Name, ".Migration.Files"); + long num1 = await Migrator.LastUpdate(); + string[] array = this._executingAssembly.GetManifestResourceNames().Where((string r) => { + long num = (long)0; + if (r.StartsWith(str) && r.EndsWith(".SQL")) + { + num = (long)ValidationHelper.ToInt(ValidationHelper.Clear(ValidationHelper.Index(r.Split(new char[] { '\u005F' }), 1))); + } + return num > num1; + }).ToArray(); + return array; + } + + private static async Task LastUpdate() + { + long num1 = await Task.Run(() => { + long num; + using (UnitOfWork commited = Instancia.Commited) + { + num = commited.get_AtualizacaoRepository().FindLastUpdate(); + } + return num; + }); + return num1; + } + + private static async Task SaveUpdate(long fileId) + { + bool flag1 = await Task.Run(() => { + bool flag; + using (UnitOfWork commited = Instancia.Commited) + { + try + { + commited.get_AtualizacaoRepository().Save(fileId); + commited.Commit(); + return true; + } + catch (Exception exception) + { + commited.Rollback(); + flag = false; + } + } + return flag; + }); + return flag1; + } + } +} \ No newline at end of file -- cgit v1.2.3