Fix creating table indexes

This commit is contained in:
Gabe Kangas 2022-08-02 22:45:15 -07:00
parent 5d34279862
commit b2b77f5eb9
No known key found for this signature in database
GPG key ID: 9A56337728BC81EA
7 changed files with 39 additions and 85 deletions

View file

@ -24,19 +24,10 @@ func createFederationFollowersTable() {
"approved_at" TIMESTAMP, "approved_at" TIMESTAMP,
"disabled_at" TIMESTAMP, "disabled_at" TIMESTAMP,
"request_object" BLOB, "request_object" BLOB,
PRIMARY KEY (iri)); PRIMARY KEY (iri));`
CREATE INDEX iri_index ON ap_followers (iri); _datastore.MustExec(createTableSQL)
CREATE INDEX approved_at_index ON ap_followers (approved_at);` _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);`)
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)
}
} }
// GetFollowerCount will return the number of followers we're keeping track of. // GetFollowerCount will return the number of followers we're keeping track of.

View file

@ -202,17 +202,10 @@ func createFederatedActivitiesTable() {
"actor" TEXT NOT NULL, "actor" TEXT NOT NULL,
"type" TEXT NOT NULL, "type" TEXT NOT NULL,
"timestamp" TIMESTAMP NOT NULL "timestamp" TIMESTAMP NOT NULL
); );`
CREATE INDEX iri_actor_index ON ap_accepted_activities (iri,actor);`
stmt, err := _datastore.DB.Prepare(createTableSQL) _datastore.MustExec(createTableSQL)
if err != nil { _datastore.MustExec(`CREATE INDEX IF NOT EXISTS idx_iri_actor_index ON ap_accepted_activities (iri,actor);`)
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)
}
} }
func createFederationOutboxTable() { func createFederationOutboxTable() {
@ -223,20 +216,12 @@ func createFederationOutboxTable() {
"type" TEXT NOT NULL, "type" TEXT NOT NULL,
"created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP, "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
"live_notification" BOOLEAN DEFAULT FALSE, "live_notification" BOOLEAN DEFAULT FALSE,
PRIMARY KEY (iri)); 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);`
stmt, err := _datastore.DB.Prepare(createTableSQL) _datastore.MustExec(createTableSQL)
if err != nil { _datastore.MustExec(`CREATE INDEX IF NOT EXISTS idx_iri ON ap_outbox (iri);`)
log.Fatal(err) _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);`)
defer stmt.Close()
_, err = stmt.Exec()
if err != nil {
log.Warnln("error executing sql creating outbox table", createTableSQL, err)
}
} }
// GetOutboxPostCount will return the number of posts in the outbox. // GetOutboxPostCount will return the number of posts in the outbox.

View file

@ -6,7 +6,6 @@ import (
"github.com/owncast/owncast/core/data" "github.com/owncast/owncast/core/data"
"github.com/owncast/owncast/core/user" "github.com/owncast/owncast/core/user"
log "github.com/sirupsen/logrus"
"github.com/owncast/owncast/db" "github.com/owncast/owncast/db"
) )
@ -24,18 +23,9 @@ func Setup(db *data.Datastore) {
"type" TEXT NOT NULL, "type" TEXT NOT NULL,
"timestamp" DATE DEFAULT CURRENT_TIMESTAMP NOT NULL, "timestamp" DATE DEFAULT CURRENT_TIMESTAMP NOT NULL,
FOREIGN KEY(user_id) REFERENCES users(id) FOREIGN KEY(user_id) REFERENCES users(id)
);CREATE INDEX auth_token ON auth (token);` );`
_datastore.MustExec(createTableSQL)
stmt, err := db.DB.Prepare(createTableSQL) _datastore.MustExec(`CREATE INDEX IF NOT EXISTS idx_auth_token ON auth (token);`)
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
_, err = stmt.Exec()
if err != nil {
log.Fatalln(err)
}
} }
// AddAuth will add an external authentication token and type for a user. // AddAuth will add an external authentication token and type for a user.

View file

@ -118,18 +118,9 @@ func (ds *Datastore) Setup() {
"key" string NOT NULL PRIMARY KEY, "key" string NOT NULL PRIMARY KEY,
"value" BLOB, "value" BLOB,
"timestamp" DATE DEFAULT CURRENT_TIMESTAMP NOT NULL "timestamp" DATE DEFAULT CURRENT_TIMESTAMP NOT NULL
);CREATE INDEX IF NOT EXISTS messages_timestamp_index ON messages(timestamp);` );`
stmt, err := ds.DB.Prepare(createTableSQL) ds.MustExec(createTableSQL)
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
_, err = stmt.Exec()
if err != nil {
log.Fatalln(err)
}
if !HasPopulatedDefaults() { if !HasPopulatedDefaults() {
PopulateDefaults() PopulateDefaults()
@ -173,3 +164,16 @@ func (ds *Datastore) Reset() {
func GetDatastore() *Datastore { func GetDatastore() *Datastore {
return _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)
}
}

View file

@ -41,20 +41,12 @@ 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 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) MustExec(createTableSQL, db)
if err != nil { MustExec(`CREATE INDEX IF NOT EXISTS idx_user_id ON users (id);`, db)
log.Fatal(err) 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)
defer stmt.Close()
_, err = stmt.Exec()
if err != nil {
log.Warnln(err)
}
} }
// GetUsersCount will return the number of users in the database. // GetUsersCount will return the number of users in the database.

View file

@ -10,7 +10,7 @@ import (
func MustExec(s string, db *sql.DB) { func MustExec(s string, db *sql.DB) {
stmt, err := db.Prepare(s) stmt, err := db.Prepare(s)
if err != nil { if err != nil {
log.Fatal(err) log.Panic(err)
} }
defer stmt.Close() defer stmt.Close()
_, err = stmt.Exec() _, err = stmt.Exec()

View file

@ -17,18 +17,10 @@ func createNotificationsTable(db *sql.DB) {
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"channel" TEXT NOT NULL, "channel" TEXT NOT NULL,
"destination" TEXT NOT NULL, "destination" TEXT NOT NULL,
"created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP); "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP);`
CREATE INDEX channel_index ON notifications (channel);`
stmt, err := db.Prepare(createTableSQL) data.MustExec(createTableSQL, db)
if err != nil { data.MustExec(`CREATE INDEX IF NOT EXISTS idx_channel ON notifications (channel);`, db)
log.Fatal(err)
}
defer stmt.Close()
_, err = stmt.Exec()
if err != nil {
log.Warnln("error executing sql creating followers table", createTableSQL, err)
}
} }
// AddNotification saves a new user notification destination. // AddNotification saves a new user notification destination.