|
| 1 | +using FruityFoundation.DataAccess.Abstractions; |
| 2 | + |
| 3 | +namespace WebApi.Scripts; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Add foreign key to reddit posts |
| 7 | +/// </summary> |
| 8 | +public class Script_2024_08_24_07_MakeRedditPostsIsTitleFetchedNotNull : IDbMaintenanceScript |
| 9 | +{ |
| 10 | + /// <inheritdoc /> |
| 11 | + public async Task Run(IDatabaseConnection<ReadWrite> dbConnection) |
| 12 | + { |
| 13 | + var columnIsNotNull = await dbConnection.ExecuteScalar<bool>( |
| 14 | + "SELECT \"notnull\" FROM pragma_table_info('reddit_posts') WHERE name = 'is_title_fetched'"); |
| 15 | + |
| 16 | + if (columnIsNotNull) |
| 17 | + return; |
| 18 | + |
| 19 | + // First remove the foreign key on reddit_comments -> reddit_posts.reddit_post_id |
| 20 | + await dbConnection.Execute( |
| 21 | + """ |
| 22 | + DROP TABLE IF EXISTS reddit_comments_dg_tmp; |
| 23 | + create table reddit_comments_dg_tmp |
| 24 | + ( |
| 25 | + reddit_post_id TEXT not null |
| 26 | + constraint PK_RedditComments_RedditPostId |
| 27 | + primary key, |
| 28 | + reddit_comment_id TEXT not null, |
| 29 | + posted_at TEXT not null, |
| 30 | + last_updated_at TEXT not null |
| 31 | + ); |
| 32 | +
|
| 33 | + insert into reddit_comments_dg_tmp(reddit_post_id, reddit_comment_id, posted_at, last_updated_at) |
| 34 | + select reddit_post_id, reddit_comment_id, posted_at, last_updated_at |
| 35 | + from reddit_comments; |
| 36 | +
|
| 37 | + drop table reddit_comments; |
| 38 | +
|
| 39 | + alter table reddit_comments_dg_tmp |
| 40 | + rename to reddit_comments; |
| 41 | + """); |
| 42 | + |
| 43 | + // Make is_title_fetched not null |
| 44 | + await dbConnection.Execute( |
| 45 | + """ |
| 46 | + DROP TABLE IF EXISTS reddit_posts_dg_tmp; |
| 47 | +
|
| 48 | + create table reddit_posts_dg_tmp |
| 49 | + ( |
| 50 | + reddit_post_id TEXT not null |
| 51 | + constraint PK_reddit_posts_reddit_post_id |
| 52 | + primary key, |
| 53 | + post_title TEXT, |
| 54 | + is_title_fetched INTEGER not null |
| 55 | + ); |
| 56 | + |
| 57 | + insert into reddit_posts_dg_tmp(reddit_post_id, post_title, is_title_fetched) |
| 58 | + select reddit_post_id, post_title, is_title_fetched |
| 59 | + from reddit_posts; |
| 60 | + |
| 61 | + drop table reddit_posts; |
| 62 | + |
| 63 | + alter table reddit_posts_dg_tmp |
| 64 | + rename to reddit_posts; |
| 65 | + |
| 66 | + create index IX_reddit_posts_is_title_fetched_false |
| 67 | + on reddit_posts (is_title_fetched) |
| 68 | + where reddit_posts.is_title_fetched = 0; |
| 69 | + """); |
| 70 | + |
| 71 | + // Re-add the foreign key on reddit_comments -> reddit_posts.reddit_post_id |
| 72 | + await dbConnection.Execute( |
| 73 | + """ |
| 74 | + create table reddit_comments_dg_tmp |
| 75 | + ( |
| 76 | + reddit_post_id TEXT not null |
| 77 | + constraint PK_RedditComments_RedditPostId |
| 78 | + primary key |
| 79 | + constraint FK_reddit_comments_reddit_posts_reddit_post_id |
| 80 | + references reddit_posts, |
| 81 | + reddit_comment_id TEXT not null, |
| 82 | + posted_at TEXT not null, |
| 83 | + last_updated_at TEXT not null |
| 84 | + ); |
| 85 | +
|
| 86 | + insert into reddit_comments_dg_tmp(reddit_post_id, reddit_comment_id, posted_at, last_updated_at) |
| 87 | + select reddit_post_id, reddit_comment_id, posted_at, last_updated_at |
| 88 | + from reddit_comments; |
| 89 | +
|
| 90 | + drop table reddit_comments; |
| 91 | +
|
| 92 | + alter table reddit_comments_dg_tmp |
| 93 | + rename to reddit_comments; |
| 94 | + """); |
| 95 | + } |
| 96 | +} |
0 commit comments