The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Configuration.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. namespace Teknik.Migrations
  2. {
  3. using Areas.Paste;
  4. using Helpers;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data.Entity;
  8. using System.Data.Entity.Migrations;
  9. using System.Linq;
  10. using Teknik.Configuration;
  11. internal sealed class Configuration : DbMigrationsConfiguration<Models.TeknikEntities>
  12. {
  13. public Configuration()
  14. {
  15. AutomaticMigrationsEnabled = true;
  16. AutomaticMigrationDataLossAllowed = true;
  17. }
  18. protected override void Seed(Models.TeknikEntities context)
  19. {
  20. Config config = Config.Load();
  21. // Pre-populate with the default stuff
  22. // Create system blog
  23. Areas.Profile.Models.User systemUser = new Areas.Profile.Models.User();
  24. systemUser.Username = Constants.SERVERUSER;
  25. systemUser.JoinDate = DateTime.Now;
  26. systemUser.LastSeen = DateTime.Now;
  27. systemUser.UserSettings = new Areas.Profile.Models.UserSettings();
  28. systemUser.BlogSettings = new Areas.Profile.Models.BlogSettings();
  29. systemUser.UploadSettings = new Areas.Profile.Models.UploadSettings();
  30. context.Users.AddOrUpdate(systemUser);
  31. context.SaveChanges();
  32. Areas.Blog.Models.Blog systemBlog = new Areas.Blog.Models.Blog();
  33. systemBlog.UserId = systemUser.UserId;
  34. systemBlog.BlogId = config.BlogConfig.ServerBlogId;
  35. context.Blogs.AddOrUpdate(systemBlog);
  36. context.SaveChanges();
  37. // Create roles and groups
  38. Areas.Profile.Models.Role adminRole = new Areas.Profile.Models.Role();
  39. adminRole.Name = "Admin";
  40. adminRole.Description = "Allows complete access to user specific actions";
  41. context.Roles.AddOrUpdate(adminRole);
  42. Areas.Profile.Models.Role podcastRole = new Areas.Profile.Models.Role();
  43. podcastRole.Name = "Podcast";
  44. podcastRole.Description = "Allows create/edit/delete access to podcasts";
  45. context.Roles.AddOrUpdate(podcastRole);
  46. Areas.Profile.Models.Group adminGroup = new Areas.Profile.Models.Group();
  47. adminGroup.Name = "Administrators";
  48. adminGroup.Description = "System Administrators with full access";
  49. adminGroup.Roles = new List<Areas.Profile.Models.Role>();
  50. adminGroup.Roles.Add(adminRole);
  51. adminGroup.Roles.Add(podcastRole);
  52. context.Groups.AddOrUpdate(adminGroup);
  53. Areas.Profile.Models.Group podcastGroup = new Areas.Profile.Models.Group();
  54. podcastGroup.Name = "Podcast";
  55. podcastGroup.Description = "Podcast team members";
  56. podcastGroup.Roles = new List<Areas.Profile.Models.Role>();
  57. podcastGroup.Roles.Add(podcastRole);
  58. context.Groups.AddOrUpdate(podcastGroup);
  59. Areas.Profile.Models.Group memberGroup = new Areas.Profile.Models.Group();
  60. memberGroup.Name = "Member";
  61. memberGroup.Description = "The default member group with basic permissions";
  62. context.Groups.AddOrUpdate(memberGroup);
  63. context.SaveChanges();
  64. if (config.DatabaseConfig.Migrate && !config.DevEnvironment)
  65. {
  66. // Convert legacy MySQL DB to new MS SQL DB
  67. MysqlDatabase db = new MysqlDatabase(config.DatabaseConfig);
  68. db.MysqlErrorEvent += Db_MysqlErrorEvent;
  69. // Transfer transactions
  70. var transRet = db.Query("SELECT * FROM `transactions`");
  71. foreach (var tran in transRet)
  72. {
  73. switch (tran["trans_type"].ToString())
  74. {
  75. case "One-Time":
  76. Areas.Transparency.Models.OneTime tr = new Areas.Transparency.Models.OneTime();
  77. tr.DateSent = DateTime.Parse(tran["date_posted"].ToString());
  78. tr.Amount = Double.Parse(tran["amount"].ToString());
  79. tr.Currency = tran["currency"].ToString();
  80. tr.Recipient = tran["recipient"].ToString();
  81. tr.Reason = tran["reason"].ToString();
  82. context.Transactions.AddOrUpdate(tr);
  83. break;
  84. case "Bill":
  85. Areas.Transparency.Models.Bill bill = new Areas.Transparency.Models.Bill();
  86. bill.DateSent = DateTime.Parse(tran["date_posted"].ToString());
  87. bill.Amount = Double.Parse(tran["amount"].ToString());
  88. bill.Currency = tran["currency"].ToString();
  89. bill.Recipient = tran["recipient"].ToString();
  90. bill.Reason = tran["reason"].ToString();
  91. context.Transactions.AddOrUpdate(bill);
  92. break;
  93. case "Donation":
  94. Areas.Transparency.Models.Donation don = new Areas.Transparency.Models.Donation();
  95. don.DateSent = DateTime.Parse(tran["date_posted"].ToString());
  96. don.Amount = Double.Parse(tran["amount"].ToString());
  97. don.Currency = tran["currency"].ToString();
  98. don.Sender = tran["sender"].ToString();
  99. don.Reason = tran["reason"].ToString();
  100. context.Transactions.AddOrUpdate(don);
  101. break;
  102. }
  103. }
  104. context.SaveChanges();
  105. // Transfer Users and Blogs/Posts
  106. Dictionary<int, int> userMapping = new Dictionary<int, int>();
  107. Dictionary<int, int> postMapping = new Dictionary<int, int>();
  108. var userRet = db.Query("SELECT * FROM `users`");
  109. foreach (var user in userRet)
  110. {
  111. // Create User
  112. Areas.Profile.Models.User newUser = new Areas.Profile.Models.User();
  113. newUser.UserSettings = new Areas.Profile.Models.UserSettings();
  114. newUser.BlogSettings = new Areas.Profile.Models.BlogSettings();
  115. newUser.UploadSettings = new Areas.Profile.Models.UploadSettings();
  116. newUser.TransferAccount = true;
  117. newUser.Username = user["username"].ToString();
  118. newUser.HashedPassword = user["password"].ToString();
  119. newUser.JoinDate = DateTime.Parse(user["join_date"].ToString());
  120. newUser.LastSeen = DateTime.Parse(user["join_date"].ToString());
  121. newUser.UserSettings.About = user["about"].ToString();
  122. newUser.UserSettings.Website = user["website"].ToString();
  123. newUser.UserSettings.Quote = user["quote"].ToString();
  124. newUser.BlogSettings.Title = user["blog_title"].ToString();
  125. newUser.BlogSettings.Description = user["blog_desc"].ToString();
  126. if (user["site_admin"].ToString() == "1")
  127. {
  128. newUser.Groups.Add(adminGroup);
  129. }
  130. else
  131. {
  132. newUser.Groups.Add(memberGroup);
  133. }
  134. context.Users.AddOrUpdate(newUser);
  135. context.SaveChanges();
  136. int oldUserId = Int32.Parse(user["id"].ToString());
  137. int userId = newUser.UserId;
  138. userMapping.Add(oldUserId, userId);
  139. // Create Blog for user
  140. Areas.Blog.Models.Blog newBlog = new Areas.Blog.Models.Blog();
  141. newBlog.UserId = userId;
  142. context.Blogs.AddOrUpdate(newBlog);
  143. context.SaveChanges();
  144. int blogId = newBlog.BlogId;
  145. // Transfer Blog Posts
  146. var postRet = db.Query("SELECT * FROM `blog` WHERE `author_id` = {0}", new object[] { oldUserId });
  147. if (postRet != null)
  148. {
  149. foreach (var post in postRet)
  150. {
  151. // Create new Blog Post
  152. Areas.Blog.Models.BlogPost newPost = new Areas.Blog.Models.BlogPost();
  153. if (post["user_id"].ToString() == "0")
  154. {
  155. newPost.BlogId = config.BlogConfig.ServerBlogId;
  156. newPost.System = true;
  157. }
  158. else
  159. {
  160. newPost.BlogId = blogId;
  161. }
  162. newPost.DatePosted = DateTime.Parse(post["date_posted"].ToString());
  163. DateTime publishDate = DateTime.Now;
  164. DateTime.TryParse(post["date_published"].ToString(), out publishDate);
  165. if (publishDate < newPost.DatePosted)
  166. {
  167. publishDate = newPost.DatePosted;
  168. }
  169. newPost.DatePublished = publishDate;
  170. newPost.DateEdited = publishDate;
  171. newPost.Published = (post["published"].ToString() == "1");
  172. newPost.Title = post["title"].ToString();
  173. newPost.Article = post["post"].ToString();
  174. context.BlogPosts.AddOrUpdate(newPost);
  175. context.SaveChanges();
  176. postMapping.Add(Int32.Parse(post["id"].ToString()), newPost.BlogPostId);
  177. }
  178. }
  179. }
  180. // Transfer Blog Comments
  181. var commentRet = db.Query("SELECT * FROM `comments` WHERE `service` = 'blog'");
  182. foreach (var comment in commentRet)
  183. {
  184. int postId = Int32.Parse(comment["reply_id"].ToString());
  185. int userId = Int32.Parse(comment["user_id"].ToString());
  186. if (postMapping.ContainsKey(postId) && userMapping.ContainsKey(userId))
  187. {
  188. Areas.Blog.Models.BlogPostComment newComment = new Areas.Blog.Models.BlogPostComment();
  189. newComment.BlogPostId = postMapping[postId];
  190. newComment.UserId = userMapping[userId];
  191. newComment.Article = comment["post"].ToString();
  192. newComment.DatePosted = DateTime.Parse(comment["date_posted"].ToString());
  193. newComment.DateEdited = DateTime.Parse(comment["date_posted"].ToString());
  194. context.BlogComments.AddOrUpdate(newComment);
  195. context.SaveChanges();
  196. }
  197. }
  198. // Transfer Pastes
  199. var pasteRet = db.Query("SELECT * FROM `paste`");
  200. foreach (var paste in pasteRet)
  201. {
  202. // If it's a password protected paste, we just skip it
  203. if (paste["password"] == null)
  204. {
  205. string content = paste["code"].ToString();
  206. string title = paste["title"].ToString();
  207. DateTime posted = DateTime.Parse(paste["posted"].ToString());
  208. int userId = Int32.Parse(paste["user_id"].ToString());
  209. Areas.Paste.Models.Paste newPaste = PasteHelper.CreatePaste(content, title);
  210. newPaste.DatePosted = posted;
  211. newPaste.Url = paste["pid"].ToString();
  212. if (userMapping.ContainsKey(userId) && userId != 0)
  213. {
  214. newPaste.UserId = userMapping[userId];
  215. }
  216. context.Pastes.AddOrUpdate(newPaste);
  217. context.SaveChanges();
  218. }
  219. }
  220. }
  221. }
  222. private void Db_MysqlErrorEvent(object sender, string e)
  223. {
  224. throw new NotImplementedException();
  225. }
  226. }
  227. }