summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Application/Migration
diff options
context:
space:
mode:
authorLucas Faria Mendes <lucas.fariamo08@gmail.com>2026-03-30 13:38:18 +0000
committerLucas Faria Mendes <lucas.fariamo08@gmail.com>2026-03-30 13:38:18 +0000
commit1f4e14b2e973ee7de337fd4866d9a5ceff5cb6d1 (patch)
treee1c3b20ea08f0cf71122a1e73f0d395f8fd83874 /Codemerx/Gestor.Application/Migration
parent674ca83ba9243a9e95a7568c797668dab6aee26a (diff)
downloadgestor-1f4e14b2e973ee7de337fd4866d9a5ceff5cb6d1.tar.gz
gestor-1f4e14b2e973ee7de337fd4866d9a5ceff5cb6d1.zip
chore: location
Diffstat (limited to 'Codemerx/Gestor.Application/Migration')
-rw-r--r--Codemerx/Gestor.Application/Migration/Migrator.cs189
1 files changed, 189 insertions, 0 deletions
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<bool> 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<bool> ExecuteFiles(IEnumerable<string> filesToExecute)
+ {
+ bool flag;
+ IEnumerable<string> 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<bool> ExecuteScript(string scriptText)
+ {
+ bool flag1 = await Task.Run<bool>(() => {
+ bool flag;
+ IEnumerable<string> 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<string[]> GetAllFiles()
+ {
+ string str = string.Concat(this._executingAssembly.GetName().Name, ".Migration.Files");
+ long num1 = await Migrator.LastUpdate();
+ string[] array = this._executingAssembly.GetManifestResourceNames().Where<string>((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<string>();
+ return array;
+ }
+
+ private static async Task<long> LastUpdate()
+ {
+ long num1 = await Task.Run<long>(() => {
+ long num;
+ using (UnitOfWork commited = Instancia.Commited)
+ {
+ num = commited.get_AtualizacaoRepository().FindLastUpdate();
+ }
+ return num;
+ });
+ return num1;
+ }
+
+ private static async Task<bool> SaveUpdate(long fileId)
+ {
+ bool flag1 = await Task.Run<bool>(() => {
+ 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