Repository: dolthub/dolt
File: go/cmd/dolt/commands/sqlserver/server.go (Go)

Code before:
			metListener, err = newMetricsListener(labels, cfg.Version, clusterController)
			return err
		},
		StopF: func() error {
			metListener.Close()
			return nil
		},
	}
	controller.Register(InitMetricsListener)

	InitLockSuperUser := &svcs.AnonService{
		InitF: func(ctx context.Context) error {
			mysqlDb := sqlEngine.GetUnderlyingEngine().Analyzer.Catalog.MySQLDb

			host := "localhost"
			// LocalConnectionUser is reserved for `dolt sql` otherwise the ephemeral user can't be created
			reserved := [...]string{LocalConnectionUser} // users necessary at startup to enter dolt sql-server

			// check for reserved user conflicts
			rd := mysqlDb.Reader()
			var warn strings.Builder
			conflicts := make([]mysql_db.UserPrimaryKey, 0, len(reserved))
			for _, user := range reserved {
				conflict, _ := rd.GetUser(mysql_db.UserPrimaryKey{Host: host, User: user})
				if conflict != nil && !conflict.IsEphemeral {
					warn.WriteString(fmt.Sprintf("Dropped persistent '%s@%s' as it conflicts with dolt reserved '%s@%s'", conflict.User, conflict.Host, user, host))
					conflicts = append(conflicts, mysql_db.UserPrimaryKey{Host: conflict.Host, User: conflict.User})
				}
			}
			rd.Close()

			ed := mysqlDb.Editor()
			defer ed.Close()

			for _, conflict := range conflicts {
				ed.RemoveUser(conflict)

				ed.RemoveRoleEdgesFromKey(mysql_db.RoleEdgesFromKey{
					FromHost: conflict.Host,
					FromUser: conflict.User,
				})

				ed.RemoveRoleEdgesToKey(mysql_db.RoleEdgesToKey{
					ToHost: conflict.Host,
					ToUser: conflict.User,
				})
			}

			if len(conflicts) > 0 {
				log.Warning(warn.String())
				sqlCtx, err := sqlEngine.NewDefaultContext(ctx)

Review comment (suggestion):
(minor) Instead of repeating the username@host (they'll always be the same, right), how about an error message like:
`Dropping persisted user '__dolt_local_user__@localhost' because this account name is reserved for Dolt`

Code after:
			return err
		},
		StopF: func() error {
			metListener.Close()
			return nil
		},
	}
	controller.Register(InitMetricsListener)

	InitLockSuperUser := &svcs.AnonService{
		InitF: func(ctx context.Context) error {
			mysqlDb := sqlEngine.GetUnderlyingEngine().Analyzer.Catalog.MySQLDb

			host := "localhost"
			// this is reserved for `dolt sql` connections when sql-server is running
			reservedUser := LocalConnectionUser

			rd := mysqlDb.Reader()
			conflict, _ := rd.GetUser(mysql_db.UserPrimaryKey{Host: host, User: reservedUser})
			rd.Close()

			var conflicts []mysql_db.UserPrimaryKey
			if conflict != nil && !conflict.IsEphemeral {
				logrus.Warnf("Dropping persisted '%s@%s' because this account name is reserved for Dolt", conflict.User, conflict.Host)
				conflicts = append(conflicts, mysql_db.UserPrimaryKey{Host: conflict.Host, User: conflict.User})
			}

			ed := mysqlDb.Editor()
			defer ed.Close()

			if len(conflicts) > 0 {
				c := conflicts[0]
				ed.RemoveUser(c)
				ed.RemoveRoleEdgesFromKey(mysql_db.RoleEdgesFromKey{
					FromHost: c.Host,
					FromUser: c.User,
				})
				ed.RemoveRoleEdgesToKey(mysql_db.RoleEdgesToKey{
					ToHost: c.Host,
					ToUser: c.User,
				})

				sqlCtx, err := sqlEngine.NewDefaultContext(ctx)
				if err != nil {
					return fmt.Errorf("failed to create SQL context: %v", err)
				}
				if err := mysqlDb.Persist(sqlCtx, ed); err != nil {
					return fmt.Errorf("failed to persist changes to privileges database: %v", err)
				}
			}