Folders: Switch order of the columns in folder table indexes so that org_id becomes first (#82454)
* Folders: Switch order of the columns in folder table so that org_id becomes first
This commit is contained in:
@@ -88,18 +88,21 @@ func (s *Service) DBMigration(db db.DB) {
|
||||
err := db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
var err error
|
||||
if db.GetDialect().DriverName() == migrator.SQLite {
|
||||
// covered by UQE_folder_org_id_uid
|
||||
_, err = sess.Exec(`
|
||||
INSERT INTO folder (uid, org_id, title, created, updated)
|
||||
SELECT uid, org_id, title, created, updated FROM dashboard WHERE is_folder = 1
|
||||
ON CONFLICT DO UPDATE SET title=excluded.title, updated=excluded.updated
|
||||
`)
|
||||
} else if db.GetDialect().DriverName() == migrator.Postgres {
|
||||
// covered by UQE_folder_org_id_uid
|
||||
_, err = sess.Exec(`
|
||||
INSERT INTO folder (uid, org_id, title, created, updated)
|
||||
SELECT uid, org_id, title, created, updated FROM dashboard WHERE is_folder = true
|
||||
ON CONFLICT(uid, org_id) DO UPDATE SET title=excluded.title, updated=excluded.updated
|
||||
`)
|
||||
} else {
|
||||
// covered by UQE_folder_org_id_uid
|
||||
_, err = sess.Exec(`
|
||||
INSERT INTO folder (uid, org_id, title, created, updated)
|
||||
SELECT * FROM (SELECT uid, org_id, title, created, updated FROM dashboard WHERE is_folder = 1) AS derived
|
||||
@@ -109,6 +112,8 @@ func (s *Service) DBMigration(db db.DB) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// covered by UQE_folder_org_id_uid
|
||||
_, err = sess.Exec(`
|
||||
DELETE FROM folder WHERE NOT EXISTS
|
||||
(SELECT 1 FROM dashboard WHERE dashboard.uid = folder.uid AND dashboard.org_id = folder.org_id AND dashboard.is_folder = true)
|
||||
|
||||
@@ -88,6 +88,7 @@ func (ss *sqlStore) Delete(ctx context.Context, UIDs []string, orgID int64) erro
|
||||
return nil
|
||||
}
|
||||
return ss.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
// covered by UQE_folder_org_id_uid
|
||||
s := fmt.Sprintf("DELETE FROM folder WHERE org_id=? AND uid IN (%s)", strings.Repeat("?, ", len(UIDs)-1)+"?")
|
||||
sqlArgs := make([]any, 0, len(UIDs)+2)
|
||||
sqlArgs = append(sqlArgs, s, orgID)
|
||||
@@ -140,6 +141,7 @@ func (ss *sqlStore) Update(ctx context.Context, cmd folder.UpdateFolderCommand)
|
||||
}
|
||||
|
||||
sql.WriteString(strings.Join(columnsToUpdate, ", "))
|
||||
// covered by UQE_folder_org_id_uid
|
||||
sql.WriteString(" WHERE uid = ? AND org_id = ?")
|
||||
args = append(args, cmd.UID, cmd.OrgID)
|
||||
|
||||
@@ -205,14 +207,17 @@ func (ss *sqlStore) Get(ctx context.Context, q folder.GetFolderQuery) (*folder.F
|
||||
}
|
||||
switch {
|
||||
case q.UID != nil:
|
||||
// covered UQE_folder_uid_org_id
|
||||
s.WriteString(" WHERE f0.uid = ? AND f0.org_id = ?")
|
||||
exists, err = sess.SQL(s.String(), q.UID, q.OrgID).Get(foldr)
|
||||
// nolint:staticcheck
|
||||
case q.ID != nil:
|
||||
s.WriteString(" WHERE f0.id = ?")
|
||||
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
|
||||
// covered by primary key
|
||||
exists, err = sess.SQL(s.String(), q.ID).Get(foldr)
|
||||
case q.Title != nil:
|
||||
// covered by UQE_folder_org_id_parent_uid_title
|
||||
s.WriteString(" WHERE f0.title = ? AND f0.org_id = ?")
|
||||
args := []any{*q.Title, q.OrgID}
|
||||
if q.ParentUID != nil {
|
||||
@@ -245,6 +250,7 @@ func (ss *sqlStore) GetParents(ctx context.Context, q folder.GetParentsQuery) ([
|
||||
}
|
||||
var folders []*folder.Folder
|
||||
|
||||
// covered by UQE_folder_org_id_uid
|
||||
recQuery := `
|
||||
WITH RECURSIVE RecQry AS (
|
||||
SELECT * FROM folder WHERE uid = ? AND org_id = ?
|
||||
@@ -295,6 +301,7 @@ func (ss *sqlStore) GetChildren(ctx context.Context, q folder.GetChildrenQuery)
|
||||
err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
sql := strings.Builder{}
|
||||
args := make([]any, 0, 2)
|
||||
// covered by UQE_folder_org_id_parent_uid_title
|
||||
if q.UID == "" {
|
||||
sql.WriteString("SELECT * FROM folder WHERE parent_uid IS NULL AND org_id=?")
|
||||
args = append(args, q.OrgID)
|
||||
@@ -342,6 +349,7 @@ func (ss *sqlStore) GetChildren(ctx context.Context, q folder.GetChildrenQuery)
|
||||
func (ss *sqlStore) getParentsMySQL(ctx context.Context, q folder.GetParentsQuery) (folders []*folder.Folder, err error) {
|
||||
err = ss.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
uid := ""
|
||||
// covered by UQE_folder_org_id_uid
|
||||
ok, err := sess.SQL("SELECT parent_uid FROM folder WHERE org_id=? AND uid=?", q.OrgID, q.UID).Get(&uid)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -351,6 +359,7 @@ func (ss *sqlStore) getParentsMySQL(ctx context.Context, q folder.GetParentsQuer
|
||||
}
|
||||
for {
|
||||
f := &folder.Folder{}
|
||||
// covered by UQE_folder_org_id_uid
|
||||
ok, err := sess.SQL("SELECT * FROM folder WHERE org_id=? AND uid=?", q.OrgID, uid).Get(f)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -454,6 +463,7 @@ func (ss *sqlStore) GetFolders(ctx context.Context, q getFoldersQuery) ([]*folde
|
||||
if q.WithFullpath || q.WithFullpathUIDs || len(q.ancestorUIDs) > 0 {
|
||||
s.WriteString(getFullpathJoinsSQL())
|
||||
}
|
||||
// covered by UQE_folder_org_id_uid
|
||||
s.WriteString(` WHERE f0.org_id=?`)
|
||||
args := []any{q.OrgID}
|
||||
if len(partialUIDs) > 0 {
|
||||
@@ -509,6 +519,7 @@ func (ss *sqlStore) GetDescendants(ctx context.Context, orgID int64, ancestor_ui
|
||||
}
|
||||
switch recursiveQueriesAreSupported {
|
||||
case true:
|
||||
// covered by UQE_folder_org_id_parent_uid_title
|
||||
recQuery := `
|
||||
WITH RECURSIVE RecQry AS (
|
||||
SELECT * FROM folder WHERE parent_uid = ? AND org_id = ?
|
||||
@@ -532,6 +543,7 @@ func (ss *sqlStore) GetDescendants(ctx context.Context, orgID int64, ancestor_ui
|
||||
s := strings.Builder{}
|
||||
args := make([]any, 0, 1+folder.MaxNestedFolderDepth)
|
||||
args = append(args, orgID)
|
||||
// covered by UQE_folder_org_id_uid
|
||||
s.WriteString(`SELECT f0.id, f0.org_id, f0.uid, f0.parent_uid, f0.title, f0.description, f0.created, f0.updated`)
|
||||
s.WriteString(` FROM folder f0`)
|
||||
s.WriteString(getFullpathJoinsSQL())
|
||||
@@ -585,6 +597,7 @@ func getFullapathUIDsSQL(dialect migrator.Dialect) string {
|
||||
func getFullpathJoinsSQL() string {
|
||||
joins := make([]string, 0, folder.MaxNestedFolderDepth)
|
||||
for i := 1; i <= folder.MaxNestedFolderDepth; i++ {
|
||||
// covered by UQE_folder_org_id_uid
|
||||
joins = append(joins, fmt.Sprintf(` LEFT JOIN folder f%d ON f%d.org_id = f%d.org_id AND f%d.uid = f%d.parent_uid`, i, i, i-1, i, i-1))
|
||||
}
|
||||
return strings.Join(joins, "\n")
|
||||
|
||||
Reference in New Issue
Block a user