From 514fd17ed9d9bc779d84234d0baba12c76d3dc4c Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Sat, 23 Apr 2022 13:56:38 -0700 Subject: [PATCH] Access token migration --- core/data/migrations.go | 75 +++++++++++++++++++++++++++-------------- core/data/users.go | 9 +++-- 2 files changed, 54 insertions(+), 30 deletions(-) diff --git a/core/data/migrations.go b/core/data/migrations.go index fed393e34..e2f31bfcd 100644 --- a/core/data/migrations.go +++ b/core/data/migrations.go @@ -12,24 +12,18 @@ import ( ) func migrateToSchema5(db *sql.DB) { - // Access tokens have been broken into its own table. - - // Authenticated bool added to the users table. - stmt, err := db.Prepare("ALTER TABLE users ADD authenticated_at timestamp DEFAULT null") - if err != nil { - log.Errorln("error running migration, you may experience issues: ", err) - } - defer stmt.Close() - _, err = stmt.Exec() - if err != nil { - log.Errorln("error running migration, you may experience issues: ", err) - } + // Create the access tokens table. + createAccessTokenTable(db) // Migrate the access tokens from the users table to the access tokens table. query := `SELECT id, access_token, created_at FROM users` rows, err := db.Query(query) - if err != nil || rows.Err() != nil { - log.Errorln("error migrating access tokens to schema v5", err, rows.Err()) + if err != nil { + log.Errorln("error migrating access tokens to schema v5", err) + return + } + if rows.Err() != nil { + log.Errorln("error migrating access tokens to schema v5", rows.Err()) return } defer rows.Close() @@ -47,7 +41,7 @@ func migrateToSchema5(db *sql.DB) { } valueStrings = append(valueStrings, "(?, ?, ?)") - valueArgs = append(valueArgs, userID, token, timestamp) + valueArgs = append(valueArgs, token, userID, timestamp) } smt := `INSERT INTO user_access_tokens(token, user_id, timestamp) VALUES %s ON CONFLICT DO NOTHING` @@ -59,31 +53,62 @@ func migrateToSchema5(db *sql.DB) { _, err = tx.Exec(smt, valueArgs...) if err != nil { _ = tx.Rollback() - log.Fatalln("Error inserting access tokens", err) + log.Errorln("Error inserting access tokens", err) } if err := tx.Commit(); err != nil { - log.Fatalln("Error committing transaction", err) + log.Errorln("Error committing transaction", err) } - // Remove old access token column from the users table. - stmt, err = db.Prepare("ALTER TABLE users DROP COLUMN access_token") + // 1. Authenticated bool added to the users table. + // 2. Access tokens are now stored in their own table. + // + // Long story short, the access_token used to be the primary key of the users + // table. However, now it's going to live in its own table. However, you + // cannot change the primary key. So we need to create a copy table, then + // migrate the access tokens, and then move the copy into place. + createTempTable := `CREATE TABLE IF NOT EXISTS users_copy ( + "id" TEXT, + "display_name" TEXT NOT NULL, + "display_color" NUMBER NOT NULL, + "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + "disabled_at" TIMESTAMP, + "previous_names" TEXT DEFAULT '', + "namechanged_at" TIMESTAMP, + "authenticated_at" TIMESTAMP, + "scopes" TEXT, + "type" TEXT DEFAULT 'STANDARD', + "last_used" DATETIME DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (id) + );CREATE INDEX user_id_disabled_at_index ON users (id, disabled_at); + CREATE INDEX user_id_index ON users (id); + CREATE INDEX user_id_disabled_index ON users (id, disabled_at); + CREATE INDEX user_disabled_at_index ON USERS (disabled_at);` + _, err = db.Exec(createTempTable) if err != nil { - log.Fatal(err) + log.Errorln("error running migration, you may experience issues: ", err) } - defer stmt.Close() - _, err = stmt.Exec() + + _, err = db.Exec(`INSERT INTO users_copy (id, display_name, display_color, created_at, disabled_at, previous_names, namechanged_at, scopes, type, last_used) + SELECT id, display_name, display_color, created_at, disabled_at, previous_names, namechanged_at, scopes, type, last_used FROM users;`) if err != nil { - log.Warnln(err) + log.Errorln("error running migration, you may experience issues: ", err) + } + + _, err = db.Exec(`PRAGMA foreign_keys = OFF;DROP TABLE "users";ALTER TABLE "users_copy" RENAME TO users;PRAGMA foreign_keys = ON;`) + if err != nil { + log.Errorln("error running migration, you may experience issues: ", err) } } func migrateToSchema4(db *sql.DB) { - // Access tokens have been broken into its own table. + // We now save the follow request object. stmt, err := db.Prepare("ALTER TABLE ap_followers ADD COLUMN request_object BLOB") if err != nil { - log.Fatal(err) + log.Errorln("Error running migration. This may be because you have already been running a dev version.", err) + return } defer stmt.Close() + _, err = stmt.Exec() if err != nil { log.Warnln(err) diff --git a/core/data/users.go b/core/data/users.go index a8a4e1698..d682776ad 100644 --- a/core/data/users.go +++ b/core/data/users.go @@ -41,11 +41,10 @@ func createUsersTable(db *sql.DB) { "type" TEXT DEFAULT 'STANDARD', "last_used" DATETIME DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id) - );CREATE INDEX index ON users (id, access_token, disabled_at); - CREATE INDEX id ON users (id); - CREATE INDEX id_disabled ON users (id, disabled_at); - CREATE INDEX access_token ON users (access_token); - CREATE INDEX disabled_at ON USERS (disabled_at);` + );CREATE INDEX user_id_disabled_at_index ON users (id, disabled_at); + CREATE INDEX user_id_index ON users (id); + CREATE INDEX user_id_disabled_index ON users (id, disabled_at); + CREATE INDEX user_disabled_at_index ON USERS (disabled_at);` stmt, err := db.Prepare(createTableSQL) if err != nil {