0

Access token migration

This commit is contained in:
Gabe Kangas 2022-04-23 13:56:38 -07:00
parent 93890f33c7
commit 514fd17ed9
No known key found for this signature in database
GPG Key ID: 9A56337728BC81EA
2 changed files with 54 additions and 30 deletions

View File

@ -12,24 +12,18 @@ import (
) )
func migrateToSchema5(db *sql.DB) { func migrateToSchema5(db *sql.DB) {
// Access tokens have been broken into its own table. // Create the access tokens table.
createAccessTokenTable(db)
// 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)
}
// Migrate the access tokens from the users table to the access tokens table. // Migrate the access tokens from the users table to the access tokens table.
query := `SELECT id, access_token, created_at FROM users` query := `SELECT id, access_token, created_at FROM users`
rows, err := db.Query(query) rows, err := db.Query(query)
if err != nil || rows.Err() != nil { if err != nil {
log.Errorln("error migrating access tokens to schema v5", err, rows.Err()) 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 return
} }
defer rows.Close() defer rows.Close()
@ -47,7 +41,7 @@ func migrateToSchema5(db *sql.DB) {
} }
valueStrings = append(valueStrings, "(?, ?, ?)") 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` 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...) _, err = tx.Exec(smt, valueArgs...)
if err != nil { if err != nil {
_ = tx.Rollback() _ = tx.Rollback()
log.Fatalln("Error inserting access tokens", err) log.Errorln("Error inserting access tokens", err)
} }
if err := tx.Commit(); err != nil { 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. // 1. Authenticated bool added to the users table.
stmt, err = db.Prepare("ALTER TABLE users DROP COLUMN access_token") // 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 { 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 { 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) { 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") stmt, err := db.Prepare("ALTER TABLE ap_followers ADD COLUMN request_object BLOB")
if err != nil { 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() defer stmt.Close()
_, err = stmt.Exec() _, err = stmt.Exec()
if err != nil { if err != nil {
log.Warnln(err) log.Warnln(err)

View File

@ -41,11 +41,10 @@ func createUsersTable(db *sql.DB) {
"type" TEXT DEFAULT 'STANDARD', "type" TEXT DEFAULT 'STANDARD',
"last_used" DATETIME DEFAULT CURRENT_TIMESTAMP, "last_used" DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id) PRIMARY KEY (id)
);CREATE INDEX index ON users (id, access_token, disabled_at); );CREATE INDEX user_id_disabled_at_index ON users (id, disabled_at);
CREATE INDEX id ON users (id); CREATE INDEX user_id_index ON users (id);
CREATE INDEX id_disabled ON users (id, disabled_at); CREATE INDEX user_id_disabled_index ON users (id, disabled_at);
CREATE INDEX access_token ON users (access_token); CREATE INDEX user_disabled_at_index ON USERS (disabled_at);`
CREATE INDEX disabled_at ON USERS (disabled_at);`
stmt, err := db.Prepare(createTableSQL) stmt, err := db.Prepare(createTableSQL)
if err != nil { if err != nil {