From b2b77f5eb98ed670e18fe97843511e13abcb5657 Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Tue, 2 Aug 2022 22:45:15 -0700 Subject: [PATCH] Fix creating table indexes --- activitypub/persistence/followers.go | 17 ++++---------- activitypub/persistence/persistence.go | 31 +++++++------------------- auth/persistence.go | 16 +++---------- core/data/persistence.go | 26 ++++++++++++--------- core/data/users.go | 18 +++++---------- core/data/utils.go | 2 +- notifications/persistence.go | 14 +++--------- 7 files changed, 39 insertions(+), 85 deletions(-) diff --git a/activitypub/persistence/followers.go b/activitypub/persistence/followers.go index 61f173575..7d9e1298b 100644 --- a/activitypub/persistence/followers.go +++ b/activitypub/persistence/followers.go @@ -24,19 +24,10 @@ func createFederationFollowersTable() { "approved_at" TIMESTAMP, "disabled_at" TIMESTAMP, "request_object" BLOB, - PRIMARY KEY (iri)); - CREATE INDEX iri_index ON ap_followers (iri); - CREATE INDEX approved_at_index ON ap_followers (approved_at);` - - stmt, err := _datastore.DB.Prepare(createTableSQL) - if err != nil { - log.Fatal(err) - } - defer stmt.Close() - _, err = stmt.Exec() - if err != nil { - log.Warnln("error executing sql creating followers table", createTableSQL, err) - } + PRIMARY KEY (iri));` + _datastore.MustExec(createTableSQL) + _datastore.MustExec(`CREATE INDEX IF NOT EXISTS idx_iri ON ap_followers (iri);`) + _datastore.MustExec(`CREATE INDEX IF NOT EXISTS idx_approved_at ON ap_followers (approved_at);`) } // GetFollowerCount will return the number of followers we're keeping track of. diff --git a/activitypub/persistence/persistence.go b/activitypub/persistence/persistence.go index 26628f846..d375e7d2d 100644 --- a/activitypub/persistence/persistence.go +++ b/activitypub/persistence/persistence.go @@ -202,17 +202,10 @@ func createFederatedActivitiesTable() { "actor" TEXT NOT NULL, "type" TEXT NOT NULL, "timestamp" TIMESTAMP NOT NULL - ); - CREATE INDEX iri_actor_index ON ap_accepted_activities (iri,actor);` + );` - stmt, err := _datastore.DB.Prepare(createTableSQL) - if err != nil { - log.Fatal("error creating inbox table", err) - } - defer stmt.Close() - if _, err := stmt.Exec(); err != nil { - log.Fatal("error creating inbound federated activities table", err) - } + _datastore.MustExec(createTableSQL) + _datastore.MustExec(`CREATE INDEX IF NOT EXISTS idx_iri_actor_index ON ap_accepted_activities (iri,actor);`) } func createFederationOutboxTable() { @@ -223,20 +216,12 @@ func createFederationOutboxTable() { "type" TEXT NOT NULL, "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP, "live_notification" BOOLEAN DEFAULT FALSE, - PRIMARY KEY (iri)); - CREATE INDEX iri ON ap_outbox (iri); - CREATE INDEX type ON ap_outbox (type); - CREATE INDEX live_notification ON ap_outbox (live_notification);` + PRIMARY KEY (iri));` - stmt, err := _datastore.DB.Prepare(createTableSQL) - if err != nil { - log.Fatal(err) - } - defer stmt.Close() - _, err = stmt.Exec() - if err != nil { - log.Warnln("error executing sql creating outbox table", createTableSQL, err) - } + _datastore.MustExec(createTableSQL) + _datastore.MustExec(`CREATE INDEX IF NOT EXISTS idx_iri ON ap_outbox (iri);`) + _datastore.MustExec(`CREATE INDEX IF NOT EXISTS idx_type ON ap_outbox (type);`) + _datastore.MustExec(`CREATE INDEX IF NOT EXISTS idx_live_notification ON ap_outbox (live_notification);`) } // GetOutboxPostCount will return the number of posts in the outbox. diff --git a/auth/persistence.go b/auth/persistence.go index d644d0c25..0ab28cb89 100644 --- a/auth/persistence.go +++ b/auth/persistence.go @@ -6,7 +6,6 @@ import ( "github.com/owncast/owncast/core/data" "github.com/owncast/owncast/core/user" - log "github.com/sirupsen/logrus" "github.com/owncast/owncast/db" ) @@ -24,18 +23,9 @@ func Setup(db *data.Datastore) { "type" TEXT NOT NULL, "timestamp" DATE DEFAULT CURRENT_TIMESTAMP NOT NULL, FOREIGN KEY(user_id) REFERENCES users(id) - );CREATE INDEX auth_token ON auth (token);` - - stmt, err := db.DB.Prepare(createTableSQL) - if err != nil { - log.Fatal(err) - } - defer stmt.Close() - - _, err = stmt.Exec() - if err != nil { - log.Fatalln(err) - } + );` + _datastore.MustExec(createTableSQL) + _datastore.MustExec(`CREATE INDEX IF NOT EXISTS idx_auth_token ON auth (token);`) } // AddAuth will add an external authentication token and type for a user. diff --git a/core/data/persistence.go b/core/data/persistence.go index cd182f6e4..a765f0b72 100644 --- a/core/data/persistence.go +++ b/core/data/persistence.go @@ -118,18 +118,9 @@ func (ds *Datastore) Setup() { "key" string NOT NULL PRIMARY KEY, "value" BLOB, "timestamp" DATE DEFAULT CURRENT_TIMESTAMP NOT NULL - );CREATE INDEX IF NOT EXISTS messages_timestamp_index ON messages(timestamp);` + );` - stmt, err := ds.DB.Prepare(createTableSQL) - if err != nil { - log.Fatal(err) - } - defer stmt.Close() - - _, err = stmt.Exec() - if err != nil { - log.Fatalln(err) - } + ds.MustExec(createTableSQL) if !HasPopulatedDefaults() { PopulateDefaults() @@ -173,3 +164,16 @@ func (ds *Datastore) Reset() { func GetDatastore() *Datastore { return _datastore } + +// MustExec will execute a SQL statement on a provided database instance. +func (ds *Datastore) MustExec(s string) { + stmt, err := ds.DB.Prepare(s) + if err != nil { + log.Panic(err) + } + 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 d682776ad..d03593616 100644 --- a/core/data/users.go +++ b/core/data/users.go @@ -41,20 +41,12 @@ func createUsersTable(db *sql.DB) { "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);` + );` - stmt, err := db.Prepare(createTableSQL) - if err != nil { - log.Fatal(err) - } - defer stmt.Close() - _, err = stmt.Exec() - if err != nil { - log.Warnln(err) - } + MustExec(createTableSQL, db) + MustExec(`CREATE INDEX IF NOT EXISTS idx_user_id ON users (id);`, db) + MustExec(`CREATE INDEX IF NOT EXISTS idx_user_id_disabled ON users (id, disabled_at);`, db) + MustExec(`CREATE INDEX IF NOT EXISTS idx_user_disabled_at ON users (disabled_at);`, db) } // GetUsersCount will return the number of users in the database. diff --git a/core/data/utils.go b/core/data/utils.go index d5f82fd3f..bec9e09b1 100644 --- a/core/data/utils.go +++ b/core/data/utils.go @@ -10,7 +10,7 @@ import ( func MustExec(s string, db *sql.DB) { stmt, err := db.Prepare(s) if err != nil { - log.Fatal(err) + log.Panic(err) } defer stmt.Close() _, err = stmt.Exec() diff --git a/notifications/persistence.go b/notifications/persistence.go index 283e4e551..bc6cecec1 100644 --- a/notifications/persistence.go +++ b/notifications/persistence.go @@ -17,18 +17,10 @@ func createNotificationsTable(db *sql.DB) { "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "channel" TEXT NOT NULL, "destination" TEXT NOT NULL, - "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP); - CREATE INDEX channel_index ON notifications (channel);` + "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP);` - stmt, err := db.Prepare(createTableSQL) - if err != nil { - log.Fatal(err) - } - defer stmt.Close() - _, err = stmt.Exec() - if err != nil { - log.Warnln("error executing sql creating followers table", createTableSQL, err) - } + data.MustExec(createTableSQL, db) + data.MustExec(`CREATE INDEX IF NOT EXISTS idx_channel ON notifications (channel);`, db) } // AddNotification saves a new user notification destination.