Skip to content

Commit 3139ca5

Browse files
committed
Course project
0 parents  commit 3139ca5

File tree

143 files changed

+16202
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

143 files changed

+16202
-0
lines changed

DBMigration/App.config

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<configuration>
2+
<runtime>
3+
<gcServer enabled="true"/>
4+
</runtime>
5+
<connectionStrings>
6+
<add name="MyConnectionString"
7+
connectionString="Data Source = localhost;
8+
Trusted_Connection = yes;
9+
Database = Electronic;
10+
connection timeout = 3;"
11+
providerName="System.Data.SqlClient"/>
12+
</connectionStrings>
13+
</configuration>

DBMigration/IMigration.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Курсовой_проект.DBMigration
2+
{
3+
interface IMigration
4+
{
5+
string ApplyQuery();
6+
string RevertQuery();
7+
}
8+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace Курсовой_проект.DBMigration.Migrations
2+
{
3+
class _201803251730 : IMigration
4+
{
5+
public string ApplyQuery()
6+
{
7+
//your query for database
8+
string query = "CREATE TABLE Users (User_Id int NOT NULL Primary Key IDENTITY, Username varchar(15) NULL UNIQUE, Phone varchar(15) NULL UNIQUE, Email varchar(254) NULL UNIQUE, "+
9+
"First_Name varchar(15) NULL, Last_Name varchar(15) NULL, Patronymic varchar(15) NULL, Password varchar(50) NULL, Register_Date datetime NULL, User_Permissions varchar(10) NULL CHECK(User_Permissions IN('User', 'Admin', 'Manager')));";
10+
11+
return query;
12+
}
13+
public string RevertQuery()
14+
{
15+
//your revert query for database
16+
string query = "DROP TABLE Users;";
17+
18+
return query;
19+
}
20+
}
21+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Курсовой_проект.DBMigration.Migrations
2+
{
3+
class _201803251816 : IMigration
4+
{
5+
public string ApplyQuery()
6+
{
7+
//your query for database
8+
string query = "CREATE TABLE Marks (Mark_Id int NOT NULL Primary Key IDENTITY, Mark_Name varchar(15) NULL UNIQUE, Description varchar(MAX) NULL);";
9+
10+
return query;
11+
}
12+
public string RevertQuery()
13+
{
14+
//your revert query for database
15+
string query = "DROP TABLE Marks;";
16+
17+
return query;
18+
}
19+
}
20+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace Курсовой_проект.DBMigration.Migrations
2+
{
3+
class _201805252336 : IMigration
4+
{
5+
public string ApplyQuery()
6+
{
7+
//your query for database
8+
string query = "CREATE TABLE Activation (Activation_Id int NOT NULL Primary Key IDENTITY, User_Id int NOT NULL, Activation_String varchar(32) NOT NULL, " +
9+
"Activation bit NOT NULL DEFAULT 0, Activation_Date datetime NULL, " +
10+
"CONSTRAINT UQ_Unique UNIQUE (User_Id, Activation_String));";
11+
12+
return query;
13+
}
14+
public string RevertQuery()
15+
{
16+
//your revert query for database
17+
string query = "DROP TABLE Activation;";
18+
19+
return query;
20+
}
21+
}
22+
}

DBMigration/Migrations/Example.cs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//////////////////////////////////////////////////////////////////
2+
// Example class of migration //
3+
//////////////////////////////////////////////////////////////////
4+
// The class name must match the following format: 197001011230 //
5+
// //
6+
// Where: 1970 01 01 12 30 //
7+
// year month day hour minute //
8+
// //
9+
// The class must implement the interface IMigration //
10+
//////////////////////////////////////////////////////////////////
11+
12+
namespace Курсовой_проект.DBMigration.Migrations
13+
{
14+
class Example : IMigration
15+
{
16+
public string ApplyQuery()
17+
{
18+
//your query for database
19+
string query = "ALTER TABLE dbo.Tweets ADD name VARCHAR(20) NULL;";
20+
21+
//You may formate your query as you want, but this query should be valid.
22+
//For example, next line may add as (All lines must have revert line):
23+
//
24+
// query += "ALTER TABLE dbo.Tweets ADD secondName VARCHAR(100) NULL;";
25+
//
26+
27+
return query;
28+
}
29+
public string RevertQuery()
30+
{
31+
//your revert query for database
32+
string query = "ALTER TABLE dbo.Tweets DROP COLUMN name;";
33+
34+
//query += "ALTER TABLE dbo.Tweets DROP COLUMN secondName;";
35+
36+
return query;
37+
}
38+
}
39+
}

DBMigration/Migrator.cs

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System;
2+
using System.Data.SqlClient;
3+
using System.Data;
4+
using Курсовой_проект.Services;
5+
6+
namespace Курсовой_проект.DBMigration
7+
{
8+
static class Migrator
9+
{
10+
//применить изменение
11+
static public bool Apply(Int64 ClassNumber, string query) {
12+
13+
bool result = false;
14+
15+
using (DBConnection dbConnection = new DBConnection()) {
16+
try
17+
{
18+
SqlDataReader reader = null;
19+
SqlCommand command = new SqlCommand("SELECT * FROM dbo.MigrationHistory WHERE ClassNumber = " + ClassNumber, dbConnection.myConnection);
20+
reader = command.ExecuteReader();
21+
if (!reader.Read())
22+
{
23+
reader.Close();
24+
if (query != String.Empty)
25+
{
26+
command = new SqlCommand(query, dbConnection.myConnection);
27+
command.ExecuteNonQuery();
28+
29+
command = new SqlCommand("INSERT INTO dbo.MigrationHistory (ClassNumber, DateApplied) VALUES (@classNumber, @date)", dbConnection.myConnection);
30+
SqlParameter date = command.Parameters.Add("@date", SqlDbType.DateTime);
31+
date.Value = DateTime.Now;
32+
SqlParameter classNumber = command.Parameters.Add("@classNumber", SqlDbType.BigInt);
33+
classNumber.Value = ClassNumber;
34+
command.ExecuteNonQuery();
35+
36+
result = true;
37+
38+
Console.WriteLine(ClassNumber + ": Successfully applied.");
39+
}
40+
else {
41+
Console.WriteLine(ClassNumber + " error: Empty query.");
42+
}
43+
}
44+
}
45+
catch (Exception e) {
46+
Console.WriteLine(ClassNumber + " error: " + e.Message);
47+
}
48+
}
49+
50+
return result;
51+
}
52+
//отменить изменение
53+
static public bool Revert(Int64 ClassNumber, string query) {
54+
55+
bool result = false;
56+
57+
using (DBConnection dbConnection = new DBConnection())
58+
{
59+
try
60+
{
61+
SqlDataReader reader = null;
62+
SqlCommand command = new SqlCommand("SELECT * FROM dbo.MigrationHistory WHERE ClassNumber = " + ClassNumber, dbConnection.myConnection);
63+
reader = command.ExecuteReader();
64+
if (reader.Read())
65+
{
66+
reader.Close();
67+
if (query != String.Empty)
68+
{
69+
command = new SqlCommand(query, dbConnection.myConnection);
70+
command.ExecuteNonQuery();
71+
72+
command = new SqlCommand("DELETE FROM dbo.MigrationHistory WHERE ClassNumber =" + ClassNumber, dbConnection.myConnection);
73+
command.ExecuteNonQuery();
74+
75+
result = true;
76+
77+
Console.WriteLine(ClassNumber + ": Successfully revert.");
78+
}
79+
else
80+
{
81+
Console.WriteLine(ClassNumber + " error: Empty query.");
82+
}
83+
}
84+
}
85+
catch (Exception e)
86+
{
87+
Console.WriteLine(ClassNumber + " error: " + e.Message);
88+
}
89+
}
90+
91+
return result;
92+
}
93+
}
94+
}

DBMigration/Program.cs

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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

Comments
 (0)