summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabe Kangas <gabek@real-ity.com>2022-04-23 13:56:38 -0700
committerGabe Kangas <gabek@real-ity.com>2022-04-23 15:38:04 -0700
commitb9f8def9c50e8614c278a8347de41c1fc6e9e07a (patch)
treebe7f90d266ec2433454cb4f21ee0ebe5dbc139ef
parente1c44d002b9b4d7ef3b716ee5e107e0dffe8da97 (diff)
Access token migrationtest-0012-migrations
-rw-r--r--core/data/migrations.go75
-rw-r--r--core/data/users.go9
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 {