From 3d7166c4aa6e059b641d6fbae28d910a2e4ca79e Mon Sep 17 00:00:00 2001 From: Alessio Date: Sun, 25 Jun 2023 22:53:49 -0300 Subject: [PATCH] BUGFIX: fix duplicate space participants error, making participants in a space unique --- persistence/schema.sql | 1 + persistence/space_queries.go | 2 +- persistence/versions.go | 35 +++++++++++++++++++++-------------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/persistence/schema.sql b/persistence/schema.sql index 148857c..abcc94c 100644 --- a/persistence/schema.sql +++ b/persistence/schema.sql @@ -142,6 +142,7 @@ create table space_participants(rowid integer primary key, user_id integer not null, space_id not null, + unique(user_id, space_id) foreign key(space_id) references spaces(id) -- No foreign key for users, since they may not be downloaded yet and I don't want to -- download every user who joins a space diff --git a/persistence/space_queries.go b/persistence/space_queries.go index d727954..75d6395 100644 --- a/persistence/space_queries.go +++ b/persistence/space_queries.go @@ -44,7 +44,7 @@ func (p Profile) SaveSpace(s scraper.Space) error { } if len(space_participants) > 0 { _, err = p.DB.NamedExec(` - insert into space_participants (user_id, space_id) values (:user_id, :space_id) + insert or replace into space_participants (user_id, space_id) values (:user_id, :space_id) `, space_participants) if err != nil { return fmt.Errorf("Error saving participants (space ID %q, participants: %#v):\n %w", s.ID, space_participants, err) diff --git a/persistence/versions.go b/persistence/versions.go index 3b86154..39b302f 100644 --- a/persistence/versions.go +++ b/persistence/versions.go @@ -8,8 +8,6 @@ import ( "offline_twitter/terminal_utils" ) -const ENGINE_DATABASE_VERSION = 17 - type VersionMismatchError struct { EngineVersion int DatabaseVersion int @@ -23,10 +21,7 @@ Please upgrade this application to a newer version to use this profile. Or down ) } -/** - * The Nth entry is the migration that moves you from version N to version N+1. - * `len(MIGRATIONS)` should always equal `ENGINE_DATABASE_VERSION`. - */ +// The Nth entry is the migration that moves you from version N to version N+1. var MIGRATIONS = []string{ `create table polls (rowid integer primary key, id integer unique not null check(typeof(id) = 'integer'), @@ -98,12 +93,26 @@ var MIGRATIONS = []string{ );`, `create index if not exists index_tweets_user_id on tweets (user_id);`, `alter table tweets add column is_expandable bool not null default 0;`, -} + `create table space_participants_uniq(rowid integer primary key, + user_id integer not null, + space_id not null, -/** - * This should only get called on a newly created Profile. - * Subsequent updates should change the number, not insert a new row. - */ + unique(user_id, space_id) + foreign key(space_id) references spaces(id) + -- No foreign key for users, since they may not be downloaded yet and I don't want to + -- download every user who joins a space + ); + + insert or replace into space_participants_uniq(rowid, user_id, space_id) select rowid, user_id, space_id from space_participants; + + drop table space_participants; + alter table space_participants_uniq rename to space_participants; + vacuum;`, +} +var ENGINE_DATABASE_VERSION = len(MIGRATIONS) + +// This should only get called on a newly created Profile. +// Subsequent updates should change the number, not insert a new row. func InitializeDatabaseVersion(db *sql.DB) { db.MustExec("insert into database_version (version_number) values (?)", ENGINE_DATABASE_VERSION) } @@ -142,9 +151,7 @@ func (p Profile) check_and_update_version() error { return nil } -/** - * Run all the migrations from version X to version Y, and update the `database_version` table's `version_number` - */ +// Run all the migrations from version X to version Y, and update the `database_version` table's `version_number` func (p Profile) UpgradeFromXToY(x int, y int) error { for i := x; i < y; i++ { fmt.Printf(terminal_utils.COLOR_CYAN)