|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Data.SqlClient; |
| 5 | +using System.Data; |
| 6 | +using Курсовой_проект.Services; |
| 7 | + |
| 8 | +namespace Курсовой_проект.DBMigration |
| 9 | +{ |
| 10 | + class TypeComparerRise : IComparer<Type> |
| 11 | + { |
| 12 | + public int Compare(Type x, Type y) |
| 13 | + { |
| 14 | + return x.Name.CompareTo(y.Name); |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + class TypeComparerWaning : IComparer<Type> |
| 19 | + { |
| 20 | + public int Compare(Type x, Type y) |
| 21 | + { |
| 22 | + return x.Name.CompareTo(y.Name) * (-1); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + class Program |
| 27 | + { |
| 28 | + static void Main(string[] args) |
| 29 | + { |
| 30 | + /*int choise = 0; |
| 31 | +
|
| 32 | + Console.WriteLine("Write 1 (to apply all migrations) or 2 (revert one migration). "); |
| 33 | +
|
| 34 | + do |
| 35 | + { |
| 36 | + try |
| 37 | + { |
| 38 | + Console.Write("Write your choise: "); |
| 39 | + choise = Convert.ToInt32(Console.ReadLine()); |
| 40 | + } |
| 41 | + catch |
| 42 | + { |
| 43 | + Console.WriteLine("Entered incorrect value."); |
| 44 | + } |
| 45 | +
|
| 46 | + } while (choise != 1 && choise != 2); |
| 47 | +
|
| 48 | + CheckOfMigrationsTableExist(); |
| 49 | +
|
| 50 | + if (choise == 1) |
| 51 | + { |
| 52 | + ApplyMigrations(); |
| 53 | + Console.WriteLine("\nSuccess apply migrations"); |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + RevertMigration(); |
| 58 | + Console.WriteLine("\nSuccess revert migration"); |
| 59 | + } |
| 60 | +
|
| 61 | + Console.ReadLine();*/ |
| 62 | + |
| 63 | + using (DBConnection dbConnection = new DBConnection()) |
| 64 | + { |
| 65 | + int i = 0; |
| 66 | + while (i < 1000) { |
| 67 | + try |
| 68 | + { |
| 69 | + SqlCommand command = new SqlCommand("INSERT INTO Products(Mark_Name, Vendor_Code, Model, Description, Price) " + |
| 70 | + "VALUES(@Mark_Name, @Vendor_Code, @Model, @Description, @Price)", dbConnection.myConnection); |
| 71 | + command.Parameters.AddWithValue("@Mark_Name", "BMW"); |
| 72 | + command.Parameters.AddWithValue("@Vendor_Code", new Random().Next(0, 10000).ToString()); |
| 73 | + command.Parameters.AddWithValue("@Model", new Random().Next(0, 10000).ToString()); |
| 74 | + command.Parameters.AddWithValue("@Description", ""); |
| 75 | + command.Parameters.AddWithValue("@Price", new Random().Next(0, 100000)); |
| 76 | + command.ExecuteNonQuery(); |
| 77 | + |
| 78 | + Console.WriteLine("New line added"); |
| 79 | + } |
| 80 | + catch (Exception e) |
| 81 | + { |
| 82 | + Console.WriteLine(e.Message); |
| 83 | + } |
| 84 | + |
| 85 | + i++; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + static public void CheckOfMigrationsTableExist() |
| 92 | + { |
| 93 | + using (DBConnection dbConnection = new DBConnection()) { |
| 94 | + try |
| 95 | + { |
| 96 | + SqlDataReader reader = null; |
| 97 | + SqlCommand command = new SqlCommand("SELECT OBJECT_ID (N'dbo.MigrationHistory', N'U')", dbConnection.myConnection); |
| 98 | + reader = command.ExecuteReader(); |
| 99 | + |
| 100 | + reader.Read(); |
| 101 | + |
| 102 | + if (reader.GetValue(0) == DBNull.Value) |
| 103 | + { |
| 104 | + reader.Close(); |
| 105 | + |
| 106 | + string query = "CREATE TABLE dbo.MigrationHistory (Id int PRIMARY KEY IDENTITY(1,1), ClassNumber bigint NULL, DateApplied datetime NULL);"; |
| 107 | + |
| 108 | + command = new SqlCommand(query, dbConnection.myConnection); |
| 109 | + command.ExecuteNonQuery(); |
| 110 | + } |
| 111 | + } |
| 112 | + catch (Exception e) |
| 113 | + { |
| 114 | + Console.WriteLine(e.Message); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + static public void ApplyMigrations() |
| 120 | + { |
| 121 | + var type = typeof(IMigration); |
| 122 | + var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(s => s.GetTypes()).Where(p => type.IsAssignableFrom(p)).ToList(); |
| 123 | + |
| 124 | + IComparer<Type> comparer = new TypeComparerRise(); |
| 125 | + types.Sort(comparer); |
| 126 | + |
| 127 | + Int64 res; |
| 128 | + foreach (var t in types) |
| 129 | + { |
| 130 | + if (Int64.TryParse(t.Name.Replace("_", ""), out res)) |
| 131 | + { |
| 132 | + var elem = (IMigration)Activator.CreateInstance(t); |
| 133 | + |
| 134 | + Migrator.Apply(Convert.ToInt64(t.Name.Replace("_", "")), elem.ApplyQuery()); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + static public void RevertMigration() |
| 140 | + { |
| 141 | + var type = typeof(IMigration); |
| 142 | + var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(s => s.GetTypes()).Where(p => type.IsAssignableFrom(p)).ToList(); |
| 143 | + |
| 144 | + IComparer<Type> comparer = new TypeComparerWaning(); |
| 145 | + types.Sort(comparer); |
| 146 | + |
| 147 | + Int64 res; |
| 148 | + foreach (var t in types) |
| 149 | + { |
| 150 | + if (Int64.TryParse(t.Name.Replace("_", ""), out res)) |
| 151 | + { |
| 152 | + var elem = (IMigration)Activator.CreateInstance(t); |
| 153 | + |
| 154 | + if (Migrator.Revert(Convert.ToInt64(t.Name.Replace("_", "")), elem.RevertQuery())) |
| 155 | + { |
| 156 | + break; |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments