diff options
Diffstat (limited to 'Gestor.Application/Migration/Migrator.cs')
| -rw-r--r-- | Gestor.Application/Migration/Migrator.cs | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/Gestor.Application/Migration/Migrator.cs b/Gestor.Application/Migration/Migrator.cs new file mode 100644 index 0000000..eab28a3 --- /dev/null +++ b/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 |