summaryrefslogtreecommitdiff
path: root/Decompiler/Gestor.Application.Migration
diff options
context:
space:
mode:
authorLucas Faria Mendes <lucas.fariamo08@gmail.com>2026-03-30 15:29:41 +0000
committerLucas Faria Mendes <lucas.fariamo08@gmail.com>2026-03-30 15:29:41 +0000
commit225aa1499e37faf9d38257caabbadc68d78b427e (patch)
tree102bb7a40c58595348ae9d3c7076201759fe0720 /Decompiler/Gestor.Application.Migration
parent1f4e14b2e973ee7de337fd4866d9a5ceff5cb6d1 (diff)
downloadgestor-225aa1499e37faf9d38257caabbadc68d78b427e.tar.gz
gestor-225aa1499e37faf9d38257caabbadc68d78b427e.zip
decompiler.com
Diffstat (limited to 'Decompiler/Gestor.Application.Migration')
-rw-r--r--Decompiler/Gestor.Application.Migration/Migrator.cs170
1 files changed, 170 insertions, 0 deletions
diff --git a/Decompiler/Gestor.Application.Migration/Migrator.cs b/Decompiler/Gestor.Application.Migration/Migrator.cs
new file mode 100644
index 0000000..1d7984f
--- /dev/null
+++ b/Decompiler/Gestor.Application.Migration/Migrator.cs
@@ -0,0 +1,170 @@
+using System;
+using System.Collections.Generic;
+using System.Data.Common;
+using System.Data.SqlClient;
+using System.IO;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Threading.Tasks;
+using Gestor.Application.Helpers;
+using Gestor.Infrastructure.UnitOfWork.Generic;
+using Gestor.Infrastructure.UnitOfWork.Logic;
+using Gestor.Model.Helper;
+
+namespace Gestor.Application.Migration;
+
+public class Migrator
+{
+ private readonly Assembly _executingAssembly;
+
+ private static string _connection { get; set; }
+
+ public Migrator()
+ {
+ _executingAssembly = Assembly.GetExecutingAssembly();
+ }
+
+ public async Task<bool> Execute()
+ {
+ _ = 1;
+ try
+ {
+ return await ExecuteFiles(await GetAllFiles());
+ }
+ catch (Exception)
+ {
+ }
+ return false;
+ }
+
+ private async Task<string[]> GetAllFiles()
+ {
+ string folderName = _executingAssembly.GetName().Name + ".Migration.Files";
+ long lastUpdate = await LastUpdate();
+ return _executingAssembly.GetManifestResourceNames().Where(delegate(string r)
+ {
+ long num = 0L;
+ if (r.StartsWith(folderName) && r.EndsWith(".SQL"))
+ {
+ num = ValidationHelper.ToInt(ValidationHelper.Clear(ValidationHelper.Index(r.Split(new char[1] { '_' }), 1)));
+ }
+ return num > lastUpdate;
+ }).ToArray();
+ }
+
+ private async Task<bool> ExecuteFiles(IEnumerable<string> filesToExecute)
+ {
+ foreach (string fileName in filesToExecute.OrderBy((string x) => x))
+ {
+ using Stream resourceStream = _executingAssembly.GetManifestResourceStream(fileName);
+ if (resourceStream == null)
+ {
+ continue;
+ }
+ Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
+ Encoding uTF = Encoding.UTF8;
+ using StreamReader streamReader = new StreamReader(resourceStream);
+ string s = streamReader.ReadToEnd();
+ byte[] bytes = uTF.GetBytes(s);
+ byte[] bytes2 = Encoding.Convert(uTF, encoding, bytes);
+ s = encoding.GetString(bytes2);
+ if (!(await ExecuteScript(s)))
+ {
+ return false;
+ }
+ if (!(await SaveUpdate(ValidationHelper.ToInt(ValidationHelper.Clear(ValidationHelper.Index(fileName.Split(new char[1] { '_' }), 1))))))
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private static async Task<bool> ExecuteScript(string scriptText)
+ {
+ return await Task.Run(delegate
+ {
+ //IL_002e: Unknown result type (might be due to invalid IL or missing references)
+ //IL_0034: Expected O, but got Unknown
+ IEnumerable<string> enumerable = Regex.Split(scriptText, "^\\s*GO\\s*$", RegexOptions.IgnoreCase | RegexOptions.Multiline);
+ try
+ {
+ if (string.IsNullOrEmpty(_connection))
+ {
+ _connection = Connection.GetConnection();
+ }
+ SqlConnection val = new SqlConnection(_connection);
+ try
+ {
+ ((DbConnection)(object)val).Open();
+ SqlCommand val2 = val.CreateCommand();
+ try
+ {
+ foreach (string item in enumerable)
+ {
+ if (!(item.Trim() == ""))
+ {
+ ((DbCommand)(object)val2).CommandText = item;
+ ((DbCommand)(object)val2).ExecuteNonQuery();
+ }
+ }
+ }
+ finally
+ {
+ ((IDisposable)val2)?.Dispose();
+ }
+ }
+ finally
+ {
+ ((IDisposable)val)?.Dispose();
+ }
+ return true;
+ }
+ catch (Exception)
+ {
+ return false;
+ }
+ });
+ }
+
+ private static async Task<bool> SaveUpdate(long fileId)
+ {
+ return await Task.Run(delegate
+ {
+ UnitOfWork commited = Instancia.Commited;
+ try
+ {
+ commited.AtualizacaoRepository.Save(fileId);
+ ((GenericUnitOfWork)commited).Commit();
+ }
+ catch (Exception)
+ {
+ ((GenericUnitOfWork)commited).Rollback();
+ return false;
+ }
+ finally
+ {
+ ((IDisposable)commited)?.Dispose();
+ }
+ return true;
+ });
+ }
+
+ private static async Task<long> LastUpdate()
+ {
+ return await Task.Run(delegate
+ {
+ UnitOfWork commited = Instancia.Commited;
+ try
+ {
+ return commited.AtualizacaoRepository.FindLastUpdate();
+ }
+ finally
+ {
+ ((IDisposable)commited)?.Dispose();
+ }
+ });
+ }
+}