-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #363 from thematters/develop
Update production server
- Loading branch information
Showing
9 changed files
with
266 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
db/migrations/20190619172950_recreate_article_activity_view.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
const table = 'article_activity_view' | ||
|
||
const materialized = 'article_activity_materialized' | ||
|
||
exports.up = async knex => { | ||
// Drop materialzied view | ||
await knex.raw(`drop materialized view if exists ${materialized}`) | ||
|
||
// Drop old view | ||
await knex.raw(`drop view if exists ${table}`) | ||
|
||
// Create new view | ||
await knex.raw(` | ||
create view ${table} as | ||
select | ||
article.*, | ||
greatest ( | ||
latest_comment, | ||
latest_collected_by, | ||
latest_appreciation) as latest_activity | ||
from | ||
article | ||
left join ( | ||
/* last comment activity */ | ||
select | ||
max(created_at) as latest_comment, | ||
article_id | ||
from | ||
comment | ||
group by | ||
article_id) as c on article.id = c.article_id | ||
left join ( | ||
/* last collected by activity */ | ||
select | ||
max(created_at) as latest_collected_by, | ||
article_id | ||
from | ||
collection | ||
group by | ||
article_id) as a on article.id = a.article_id | ||
left join ( | ||
/* last appreciation activity */ | ||
select | ||
max(created_at) as latest_appreciation, | ||
reference_id | ||
from | ||
transaction | ||
where purpose = 'appreciate' | ||
group by | ||
reference_id) as ts on article.id = ts.reference_id | ||
`) | ||
|
||
// Re-create materialized view | ||
await knex.raw(` | ||
create materialized view ${materialized} as | ||
select * | ||
from article_activity_view | ||
`) | ||
} | ||
|
||
exports.down = async knex => { | ||
// Drop materialized view | ||
await knex.raw(`drop materialized view if exists ${materialized}`) | ||
|
||
// Drop new created view | ||
await knex.raw(`drop view if exists ${table}`) | ||
|
||
// Re-create old view | ||
await knex.raw(` | ||
create view ${table} as | ||
select | ||
article.*, | ||
greatest ( | ||
latest_comment, | ||
latest_downstream, | ||
latest_appreciation) as latest_activity | ||
from | ||
article | ||
left join ( | ||
/* last comment activity */ | ||
select | ||
max(created_at) as latest_comment, | ||
article_id | ||
from | ||
comment | ||
group by | ||
article_id) as c on article.id = c.article_id | ||
left join ( | ||
/* last downstream activity */ | ||
select | ||
max(created_at) as latest_downstream, | ||
upstream_id | ||
from | ||
article | ||
group by | ||
upstream_id) as a on article.id = a.upstream_id | ||
left join ( | ||
/* last appreciation activity */ | ||
select | ||
max(created_at) as latest_appreciation, | ||
reference_id | ||
from | ||
transaction | ||
where purpose = 'appreciate' | ||
group by | ||
reference_id) as ts on article.id = ts.reference_id | ||
`) | ||
|
||
// Re-create materialized view | ||
await knex.raw(` | ||
create materialized view ${materialized} as | ||
select * | ||
from article_activity_view | ||
`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.