Skip to content

Commit

Permalink
Update SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Apr 4, 2024
1 parent 15de462 commit f62ce27
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
28 changes: 14 additions & 14 deletions MyApp.ServiceInterface/Data/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,24 @@ public void Init(IDbConnection db)

public void UpdateUsersReputation(IDbConnection db)
{
db.ExecuteNonQuery(@"update UserInfo set Reputation = UserScores.total
from (select createdBy, sum(count) as total from
(select createdBy, count(*) as count from post where CreatedBy is not null group by 1
union
select userName, count(*) as count from vote group by 1
union
select substring(id,instr(id,'-')+1) as userName, sum(UpVotes) as count from StatTotals where instr(id,'-') group by 1
union
select substring(id,instr(id,'-')+1) as userName, sum(DownVotes) as count from StatTotals where instr(id,'-') group by 1)
group by 1) as UserScores
where UserName = UserScores.CreatedBy");
db.ExecuteNonQuery(@"UPDATE UserInfo SET Reputation = UserScores.total
FROM (SELECT CreatedBy, sum(count) as total FROM
(SELECT CreatedBy, count(*) as count FROM post WHERE CreatedBy IS NOT NULL GROUP BY 1
UNION
SELECT UserName, count(*) as count FROM Vote GROUP BY 1
UNION
SELECT CreatedBy, sum(UpVotes) as count FROM StatTotals WHERE CreatedBy IS NOT NULL GROUP BY 1
UNION
SELECT CreatedBy, sum(DownVotes) as count FROM StatTotals WHERE CreatedBy IS NOT NULL GROUP BY 1)
GROUP BY 1) as UserScores
WHERE UserName = UserScores.CreatedBy");
}

public void UpdateUsersQuestions(IDbConnection db)
{
db.ExecuteNonQuery(@"update UserInfo set QuestionsCount = UserQuestions.total
from (select createdBy, count(*) as total from post where CreatedBy is not null group by 1) as UserQuestions
where UserName = UserQuestions.CreatedBy");
db.ExecuteNonQuery(@"UPDATE UserInfo SET QuestionsCount = UserQuestions.total
FROM (select createdBy, count(*) as total FROM post WHERE CreatedBy IS NOT NULL GROUP BY 1) as UserQuestions
WHERE UserName = UserQuestions.CreatedBy");
}

public void ResetInitialPostId(IDbConnection db)
Expand Down
1 change: 1 addition & 0 deletions MyApp/Migrations/Migration1000.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public override void Up()
Db.CreateTable<PostJob>();

Db.ExecuteSql("INSERT INTO UserInfo (UserId, UserName) SELECT Id, UserName FROM AspNetUsers");
Db.ExecuteSql("UPDATE StatTotals SET CreatedBy = substr(Id,instr(Id,'-')+1) WHERE instr(Id,'-') > 0 AND CreatedBy IS NULL");
}

public override void Down()
Expand Down

0 comments on commit f62ce27

Please sign in to comment.