Browse Source

Fixed legacy migration

tags/2.0.3
Teknikode 3 years ago
parent
commit
0215585cee

+ 4
- 4
Teknik/Areas/Blog/Controllers/BlogController.cs View File

@@ -31,7 +31,7 @@ namespace Teknik.Areas.Blog.Controllers
bool isAuth = User.IsInRole("Admin");
var foundPosts = db.BlogPosts.Include("Blog").Include("Blog.User").Where(p => ((p.System || isAuth) && p.Published));
model = new BlogViewModel();
model.BlogId = Constants.SERVERBLOGID;
model.BlogId = Config.BlogConfig.ServerBlogId;

User user = (User.IsInRole("Admin")) ? db.Users.Where(u => u.Username == User.Identity.Name).First() : null;
model.UserId = (user != null) ? user.UserId : 0;
@@ -44,7 +44,7 @@ namespace Teknik.Areas.Blog.Controllers
}
else // A user specific blog
{
Models.Blog blog = db.Blogs.Include("User").Where(p => p.User.Username == username && p.BlogId != Constants.SERVERBLOGID).FirstOrDefault();
Models.Blog blog = db.Blogs.Include("User").Where(p => p.User.Username == username && p.BlogId != Config.BlogConfig.ServerBlogId).FirstOrDefault();
// find the blog specified
if (blog != null)
{
@@ -94,7 +94,7 @@ namespace Teknik.Areas.Blog.Controllers
public ActionResult GetPosts(int blogID, int startPostID, int count)
{
bool isAuth = User.IsInRole("Admin");
var posts = db.BlogPosts.Include("Blog").Include("Blog.User").Where(p => ((p.BlogId == blogID && !p.System) || (p.System && blogID == Constants.SERVERBLOGID)) &&
var posts = db.BlogPosts.Include("Blog").Include("Blog.User").Where(p => ((p.BlogId == blogID && !p.System) || (p.System && blogID == Config.BlogConfig.ServerBlogId)) &&
(p.Published || p.Blog.User.Username == User.Identity.Name || isAuth)).OrderByDescending(p => p.DatePosted).Skip(startPostID).Take(count).ToList();
List<PostViewModel> postViews = new List<PostViewModel>();
if (posts != null)
@@ -141,7 +141,7 @@ namespace Teknik.Areas.Blog.Controllers
{
if (User.IsInRole("Admin") || db.Blogs.Where(b => b.User.Username == User.Identity.Name).FirstOrDefault() != null)
{
bool system = (blogID == Constants.SERVERBLOGID);
bool system = (blogID == Config.BlogConfig.ServerBlogId);
if (system)
{
var user = db.Blogs.Include("User").Where(b => b.User.Username == User.Identity.Name);

+ 4
- 4
Teknik/Areas/Profile/Controllers/ProfileController.cs View File

@@ -227,7 +227,7 @@ namespace Teknik.Areas.Profile.Controllers
string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
client.Headers[HttpRequestHeader.ContentType] = "application/json";
Uri baseUri = new Uri(Config.GitConfig.Host);
Uri finalUri = new Uri(baseUri, "admin/users?token=" + Config.GitConfig.AccessToken);
Uri finalUri = new Uri(baseUri, "api/v1/admin/users?token=" + Config.GitConfig.AccessToken);
string result = client.UploadString(finalUri, "POST", json);
}
}
@@ -245,7 +245,7 @@ namespace Teknik.Areas.Profile.Controllers

// Generate blog for the user
var newBlog = db.Blogs.Create();
newBlog.UserId = db.Users.Where(u => u.Username == model.Username).Select(u => u.UserId).First();
newBlog.UserId = newUser.UserId;
db.Blogs.Add(newBlog);
db.SaveChanges();
}
@@ -306,7 +306,7 @@ namespace Teknik.Areas.Profile.Controllers
string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
client.Headers[HttpRequestHeader.ContentType] = "application/json";
Uri baseUri = new Uri(Config.GitConfig.Host);
Uri finalUri = new Uri(baseUri, "admin/users/" + User.Identity.Name + "?token=" + Config.GitConfig.AccessToken);
Uri finalUri = new Uri(baseUri, "api/v1/admin/users/" + User.Identity.Name + "?token=" + Config.GitConfig.AccessToken);
string result = client.UploadString(finalUri, "PATCH", json);
}
}
@@ -349,7 +349,7 @@ namespace Teknik.Areas.Profile.Controllers
if (Config.GitConfig.Enabled)
{
Uri baseUri = new Uri(Config.GitConfig.Host);
Uri finalUri = new Uri(baseUri, "admin/users/" + User.Identity.Name + "?token=" + Config.GitConfig.AccessToken);
Uri finalUri = new Uri(baseUri, "api/v1/admin/users/" + User.Identity.Name + "?token=" + Config.GitConfig.AccessToken);
WebRequest request = WebRequest.Create(finalUri);
request.Method = "DELETE";


+ 1
- 1
Teknik/Areas/RSS/Controllers/RSSController.cs View File

@@ -36,7 +36,7 @@ namespace Teknik.Areas.RSS.Controllers
bool isSystem = string.IsNullOrEmpty(username);
if (isSystem)
{
blog = db.Blogs.Include("BlogPosts").Include("User").Where(b => b.BlogId == Constants.SERVERBLOGID).FirstOrDefault();
blog = db.Blogs.Include("BlogPosts").Include("User").Where(b => b.BlogId == Config.BlogConfig.ServerBlogId).FirstOrDefault();
blogUrl = Url.SubRouteUrl("blog", "Blog.Blog");
}
else

+ 2
- 0
Teknik/Configuration/BlogConfig.cs View File

@@ -12,6 +12,7 @@ namespace Teknik.Configuration
public string Description { get; set; }
public int PostsToLoad { get; set; }
public int CommentsToLoad { get; set; }
public int ServerBlogId { get; set; }

public BlogConfig()
{
@@ -25,6 +26,7 @@ namespace Teknik.Configuration
Description = string.Empty;
PostsToLoad = 10;
CommentsToLoad = 10;
ServerBlogId = 1;
}
}
}

+ 1
- 3
Teknik/Helpers/Constants.cs View File

@@ -8,9 +8,7 @@ namespace Teknik.Helpers
{
public static class Constants
{
// Blog Constants
public static int SERVERBLOGID = 0;

public const string SERVERUSER = "Server Admin";
// Paste Constants
public static Dictionary<string, string> HIGHLIGHTFORMATS = new Dictionary<string, string>()
{

+ 1
- 1
Teknik/Helpers/MysqlDatabase.cs View File

@@ -128,7 +128,7 @@ namespace Teknik.Helpers
{
if (config.Server != string.Empty && config.Database != string.Empty && config.Username != string.Empty && config.Password != string.Empty)
{
string strCon = string.Format("Server={0}; database={1}; user={2}; password={3}; port={4}; charset=utf8", config.Server, config.Database, config.Username, config.Password, config.Port);
string strCon = string.Format("Server={0}; database={1}; user={2}; password={3}; port={4}; charset=utf8; Allow Zero Datetime=true;", config.Server, config.Database, config.Username, config.Password, config.Port);
Connection = new MySqlConnection(strCon);
try
{

+ 68
- 26
Teknik/Migrations/Configuration.cs View File

@@ -22,43 +22,64 @@ namespace Teknik.Migrations
Config config = Config.Load();
// Pre-populate with the default stuff

// Create server blog
Areas.Blog.Models.Blog serverBlog = new Areas.Blog.Models.Blog();
context.Blogs.Add(serverBlog);
// Create system blog
Areas.Profile.Models.User systemUser = new Areas.Profile.Models.User();
systemUser.Username = Constants.SERVERUSER;
systemUser.JoinDate = DateTime.Now;
systemUser.LastSeen = DateTime.Now;
systemUser.UserSettings = new Areas.Profile.Models.UserSettings();
systemUser.BlogSettings = new Areas.Profile.Models.BlogSettings();
systemUser.UploadSettings = new Areas.Profile.Models.UploadSettings();
context.Users.AddOrUpdate(systemUser);
context.SaveChanges();

Areas.Blog.Models.Blog systemBlog = new Areas.Blog.Models.Blog();
systemBlog.UserId = systemUser.UserId;
systemBlog.BlogId = config.BlogConfig.ServerBlogId;
context.Blogs.AddOrUpdate(systemBlog);
context.SaveChanges();

// Create roles and groups
Areas.Profile.Models.Role adminRole = new Areas.Profile.Models.Role();
adminRole.Name = "Admin";
adminRole.Description = "Allows complete access to user specific actions";
context.Roles.Add(adminRole);
context.Roles.AddOrUpdate(adminRole);

Areas.Profile.Models.Role podcastRole = new Areas.Profile.Models.Role();
podcastRole.Name = "Podcast";
podcastRole.Description = "Allows create/edit/delete access to podcasts";
context.Roles.Add(podcastRole);
context.Roles.AddOrUpdate(podcastRole);

Areas.Profile.Models.Group adminGroup = new Areas.Profile.Models.Group();
adminGroup.Name = "Administrators";
adminGroup.Description = "System Administrators with full access";
adminGroup.Roles = new List<Areas.Profile.Models.Role>();
adminGroup.Roles.Add(adminRole);
adminGroup.Roles.Add(podcastRole);
context.Groups.AddOrUpdate(adminGroup);

Areas.Profile.Models.Group podcastGroup = new Areas.Profile.Models.Group();
podcastGroup.Name = "Podcast";
podcastGroup.Description = "Podcast team members";
podcastGroup.Roles = new List<Areas.Profile.Models.Role>();
podcastGroup.Roles.Add(podcastRole);
context.Groups.AddOrUpdate(podcastGroup);

context.SaveChanges();
Areas.Profile.Models.Group memberGroup = new Areas.Profile.Models.Group();
memberGroup.Name = "Member";
memberGroup.Description = "The default member group with basic permissions";
context.Groups.AddOrUpdate(memberGroup);

context.SaveChanges();

if (config.DatabaseConfig.Migrate && !config.DevEnvironment)
{
// Convert legacy MySQL DB to new MS SQL DB
MysqlDatabase db = new MysqlDatabase(config.DatabaseConfig);
db.MysqlErrorEvent += Db_MysqlErrorEvent;

// Transfer transactions
var transRet = db.Query("SELECT * FROM transactions");
var transRet = db.Query("SELECT * FROM `transactions`");
foreach (var tran in transRet)
{
switch (tran["trans_type"].ToString())
@@ -66,29 +87,29 @@ namespace Teknik.Migrations
case "One-Time":
Areas.Transparency.Models.OneTime tr = new Areas.Transparency.Models.OneTime();
tr.DateSent = DateTime.Parse(tran["date_posted"].ToString());
tr.Amount = Int32.Parse(tran["amount"].ToString());
tr.Amount = Double.Parse(tran["amount"].ToString());
tr.Currency = tran["currency"].ToString();
tr.Recipient = tran["recipient"].ToString();
tr.Reason = tran["reason"].ToString();
context.Transactions.Add(tr);
context.Transactions.AddOrUpdate(tr);
break;
case "Bill":
Areas.Transparency.Models.Bill bill = new Areas.Transparency.Models.Bill();
bill.DateSent = DateTime.Parse(tran["date_posted"].ToString());
bill.Amount = Int32.Parse(tran["amount"].ToString());
bill.Amount = Double.Parse(tran["amount"].ToString());
bill.Currency = tran["currency"].ToString();
bill.Recipient = tran["recipient"].ToString();
bill.Reason = tran["reason"].ToString();
context.Transactions.Add(bill);
context.Transactions.AddOrUpdate(bill);
break;
case "Donation":
Areas.Transparency.Models.Donation don = new Areas.Transparency.Models.Donation();
don.DateSent = DateTime.Parse(tran["date_posted"].ToString());
don.Amount = Int32.Parse(tran["amount"].ToString());
don.Amount = Double.Parse(tran["amount"].ToString());
don.Currency = tran["currency"].ToString();
don.Sender = tran["sender"].ToString();
don.Reason = tran["reason"].ToString();
context.Transactions.Add(don);
context.Transactions.AddOrUpdate(don);
break;
}
}
@@ -97,7 +118,7 @@ namespace Teknik.Migrations
// Transfer Users and Blogs/Posts
Dictionary<int, int> userMapping = new Dictionary<int, int>();
Dictionary<int, int> postMapping = new Dictionary<int, int>();
var userRet = db.Query("SELECT * FROM users");
var userRet = db.Query("SELECT * FROM `users`");
foreach (var user in userRet)
{
// Create User
@@ -109,26 +130,36 @@ namespace Teknik.Migrations
newUser.Username = user["username"].ToString();
newUser.HashedPassword = user["password"].ToString();
newUser.JoinDate = DateTime.Parse(user["join_date"].ToString());
newUser.LastSeen = DateTime.Parse(user["last_seen"].ToString());
newUser.LastSeen = DateTime.Parse(user["join_date"].ToString());
newUser.UserSettings.About = user["about"].ToString();
newUser.UserSettings.Website = user["website"].ToString();
newUser.UserSettings.Quote = user["quote"].ToString();
newUser.BlogSettings.Title = user["blog_title"].ToString();
newUser.BlogSettings.Description = user["blog_desc"].ToString();
context.Users.Add(newUser);
if (user["site_admin"].ToString() == "1")
{
newUser.Groups.Add(adminGroup);
}
else
{
newUser.Groups.Add(memberGroup);
}
context.Users.AddOrUpdate(newUser);
context.SaveChanges();
int oldUserId = Int32.Parse(user["id"].ToString());
int userId = newUser.UserId;

userMapping.Add(Int32.Parse(user["id"].ToString()), userId);
userMapping.Add(oldUserId, userId);

// Create Blog for user
Areas.Blog.Models.Blog newBlog = new Areas.Blog.Models.Blog();
newBlog.UserId = userId;
context.Blogs.AddOrUpdate(newBlog);
context.SaveChanges();
int blogId = newBlog.BlogId;

// Transfer Blog Posts
var postRet = db.Query("SELECT * FROM blog WHERE author_id={0}", new object[] { userId });
var postRet = db.Query("SELECT * FROM `blog` WHERE `author_id` = {0}", new object[] { oldUserId });
if (postRet != null)
{
foreach (var post in postRet)
@@ -137,7 +168,7 @@ namespace Teknik.Migrations
Areas.Blog.Models.BlogPost newPost = new Areas.Blog.Models.BlogPost();
if (post["user_id"].ToString() == "0")
{
newPost.BlogId = 0;
newPost.BlogId = config.BlogConfig.ServerBlogId;
newPost.System = true;
}
else
@@ -145,12 +176,18 @@ namespace Teknik.Migrations
newPost.BlogId = blogId;
}
newPost.DatePosted = DateTime.Parse(post["date_posted"].ToString());
newPost.DatePublished = DateTime.Parse(post["date_published"].ToString());
newPost.DateEdited = DateTime.Parse(post["date_published"].ToString());
DateTime publishDate = DateTime.Now;
DateTime.TryParse(post["date_published"].ToString(), out publishDate);
if (publishDate < newPost.DatePosted)
{
publishDate = newPost.DatePosted;
}
newPost.DatePublished = publishDate;
newPost.DateEdited = publishDate;
newPost.Published = (post["published"].ToString() == "1");
newPost.Title = post["title"].ToString();
newPost.Article = post["post"].ToString();
context.BlogPosts.Add(newPost);
context.BlogPosts.AddOrUpdate(newPost);
context.SaveChanges();
postMapping.Add(Int32.Parse(post["id"].ToString()), newPost.BlogPostId);
}
@@ -158,7 +195,7 @@ namespace Teknik.Migrations
}

// Transfer Blog Comments
var commentRet = db.Query("SELECT * FROM comments WHERE service = 'blog'");
var commentRet = db.Query("SELECT * FROM `comments` WHERE `service` = 'blog'");
foreach (var comment in commentRet)
{
int postId = Int32.Parse(comment["reply_id"].ToString());
@@ -171,13 +208,13 @@ namespace Teknik.Migrations
newComment.Article = comment["post"].ToString();
newComment.DatePosted = DateTime.Parse(comment["date_posted"].ToString());
newComment.DateEdited = DateTime.Parse(comment["date_posted"].ToString());
context.BlogComments.Add(newComment);
context.BlogComments.AddOrUpdate(newComment);
context.SaveChanges();
}
}

// Transfer Pastes
var pasteRet = db.Query("SELECT * FROM paste");
var pasteRet = db.Query("SELECT * FROM `paste`");
foreach (var paste in pasteRet)
{
// If it's a password protected paste, we just skip it
@@ -194,11 +231,16 @@ namespace Teknik.Migrations
{
newPaste.UserId = userMapping[userId];
}
context.Pastes.Add(newPaste);
context.Pastes.AddOrUpdate(newPaste);
context.SaveChanges();
}
}
}
}

private void Db_MysqlErrorEvent(object sender, string e)
{
throw new NotImplementedException();
}
}
}

Loading…
Cancel
Save