diff --git a/bot/constants.py b/bot/constants.py index f50111af..d11c33da 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -1,2 +1,2 @@ CONFIG_FILE = "config/bot.conf" -DATA_VERSION = 0 +DATA_VERSION = 1 diff --git a/bot/modules/todo/Tasklist.py b/bot/modules/todo/Tasklist.py index aeb44c42..19fbf729 100644 --- a/bot/modules/todo/Tasklist.py +++ b/bot/modules/todo/Tasklist.py @@ -129,7 +129,6 @@ class Tasklist: Update the in-memory tasklist from data and regenerate the pages """ self.tasklist = data.tasklist.fetch_rows_where( - guildid=self.member.guild.id, userid=self.member.id, _extra=("AND last_updated_at > timezone('utc', NOW()) - INTERVAL '24h' " "ORDER BY created_at ASC, taskid ASC") @@ -331,7 +330,7 @@ class Tasklist: if unrewarded: # Select tasks to reward up to the limit of rewards - recent_rewards = data.tasklist_rewards.queries.count_recent_for(self.member.guild.id, self.member.id) + recent_rewards = data.tasklist_rewards.queries.count_recent_for(self.member.id) max_to_reward = max((task_reward_limit - recent_rewards, 0)) reward_tasks = unrewarded[:max_to_reward] @@ -354,7 +353,6 @@ class Tasklist: # Track reward data.tasklist_rewards.insert( - guildid=self.member.guild.id, userid=self.member.id, reward_count=rewarding_count ) @@ -376,12 +374,12 @@ class Tasklist: Add provided tasks to the task list """ insert = [ - (self.member.guild.id, self.member.id, task) + (self.member.id, task) for task in tasks ] return data.tasklist.insert_many( *insert, - insert_keys=('guildid', 'userid', 'content') + insert_keys=('userid', 'content') ) def _delete_tasks(self, *indexes): @@ -460,7 +458,6 @@ class Tasklist: # Fetch accurate count of current tasks count = data.tasklist.select_one_where( select_columns=("COUNT(*)",), - guildid=self.member.guild.id, userid=self.member.id )[0] diff --git a/bot/modules/todo/data.py b/bot/modules/todo/data.py index 80cb87cf..f340197a 100644 --- a/bot/modules/todo/data.py +++ b/bot/modules/todo/data.py @@ -2,7 +2,7 @@ from data import RowTable, Table tasklist = RowTable( 'tasklist', - ('taskid', 'guildid', 'userid', 'content', 'complete', 'rewarded', 'created_at', 'last_updated_at'), + ('taskid', 'userid', 'content', 'complete', 'rewarded', 'created_at', 'last_updated_at'), 'taskid' ) @@ -25,13 +25,13 @@ tasklist_rewards = Table('tasklist_reward_history') @tasklist_rewards.save_query -def count_recent_for(guildid, userid, interval='24h'): +def count_recent_for(userid, interval='24h'): with tasklist_rewards.conn: with tasklist_rewards.conn.cursor() as curs: curs.execute( "SELECT SUM(reward_count) FROM tasklist_reward_history " "WHERE " - "guildid = {} AND userid = {}" - "AND reward_time > timezone('utc', NOW()) - INTERVAL '{}'".format(guildid, userid, interval) + "userid = {}" + "AND reward_time > timezone('utc', NOW()) - INTERVAL '{}'".format(userid, interval) ) return curs.fetchone()[0] or 0 diff --git a/data/migration/v0-v1/migration.sql b/data/migration/v0-v1/migration.sql new file mode 100644 index 00000000..11a785e2 --- /dev/null +++ b/data/migration/v0-v1/migration.sql @@ -0,0 +1,8 @@ +ALTER TABLE tasklist DROP COLUMN guildid; +CREATE INDEX tasklist_users ON tasklist (userid); + +ALTER TABLE tasklist_reward_history DROP COLUMN guildid; +CREATE INDEX tasklist_reward_history_users ON tasklist_reward_history (userid, reward_time); + + +INSERT INTO VersionHistory (version, author) VALUES (1, 'Migration v0-v1'); diff --git a/data/migration/v0-v1/notes.md b/data/migration/v0-v1/notes.md new file mode 100644 index 00000000..4f93f8cb --- /dev/null +++ b/data/migration/v0-v1/notes.md @@ -0,0 +1 @@ +The purpose of this migration is to remove the guild dependency on tasklist tasks, so that a user's tasklist is available across all guilds. diff --git a/data/schema.sql b/data/schema.sql index a0c07f45..4c44f0ad 100644 --- a/data/schema.sql +++ b/data/schema.sql @@ -92,7 +92,6 @@ CREATE INDEX workout_sessions_members ON workout_sessions (guildid, userid); -- Tasklist data {{{ CREATE TABLE tasklist( taskid SERIAL PRIMARY KEY, - guildid BIGINT NOT NULL, userid BIGINT NOT NULL, content TEXT NOT NULL, complete BOOL DEFAULT FALSE, @@ -100,7 +99,7 @@ CREATE TABLE tasklist( created_at TIMESTAMP DEFAULT (now() at time zone 'utc'), last_updated_at TIMESTAMP DEFAULT (now() at time zone 'utc') ); -CREATE INDEX tasklist_members ON tasklist (guildid, userid); +CREATE INDEX tasklist_users ON tasklist (userid); CREATE TABLE tasklist_channels( guildid BIGINT NOT NULL, @@ -109,12 +108,11 @@ CREATE TABLE tasklist_channels( CREATE INDEX tasklist_channels_guilds ON tasklist_channels (guildid); CREATE TABLE tasklist_reward_history( - guildid BIGINT NOT NULL, userid BIGINT NOT NULL, reward_time TIMESTAMP DEFAULT (now() at time zone 'utc'), reward_count INTEGER ); -CREATE INDEX tasklist_reward_history_members ON tasklist_reward_history (guildid, userid, reward_time); +CREATE INDEX tasklist_reward_history_users ON tasklist_reward_history (userid, reward_time); -- }}} -- Reminder data {{{