database

package
v0.0.0-...-35a0bdf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2022 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDuplicateIdentity is returned when trying to associate a remote identity
	// with a user account and the identity is already in use.
	ErrDuplicateIdentity = errors.New("remote identity registered with another user")
	// ErrEmptyGroup is returned when trying to create a group without users.
	ErrEmptyGroup = errors.New("cannot create group without users")
	// ErrDuplicateGroup is returned when trying to create a group with the same
	// name as a previously registered group.
	ErrDuplicateGroup = status.Error(codes.InvalidArgument, "group with this name already registered")
	// ErrUpdateGroup is returned when updating a group's enrollment fails.
	ErrUpdateGroup = errors.New("failed to update group enrollment")
	// ErrCourseExists is returned when trying to create an association in
	// the database for a DirectoryId that already exists in the database.
	ErrCourseExists = errors.New("course already exists on git provider")
	// ErrInsufficientAccess is returned when trying to update database
	// with insufficient access privileges.
	ErrInsufficientAccess = errors.New("user must be admin to perform this operation")
	// ErrCreateRepo is returned when trying to create repository with wrong argument.
	ErrCreateRepo = errors.New("failed to create repository; invalid arguments")
	// ErrNotEnrolled is returned when the requested user or group do not have
	// the expected association with the given course
	ErrNotEnrolled = errors.New("user or group not enrolled in the course")
)
View Source
var (
	// ErrInvalidSubmission is returned if the submission specify both UserID and GroupID or neither.
	ErrInvalidSubmission = errors.New("submission must specify exactly one of UserID or GroupID")
	// ErrInvalidAssignmentID is returned if assignment is not specified.
	ErrInvalidAssignmentID = errors.New("cannot create submission without an associated assignment")
)

Functions

func NewGORMLogger

func NewGORMLogger(zapLogger *zap.Logger) gormlogger.Interface

NewGORMLogger returns a zap-based logger for GORM. A logger instance is only returned if the LOGDB environment variable is set to a specific level. This logger is not recommended for production due to the high volume of SQL queries being performed and the associated noise in the logs; it is mainly useful for debugging database issues. If LOGDB is not set, the discard logger is returned.

Types

type Database

type Database interface {
	// CreateUserFromRemoteIdentity creates new user record from remote identity, sets user with ID 1 as admin.
	CreateUserFromRemoteIdentity(*pb.User, *pb.RemoteIdentity) error
	// AssociateUserWithRemoteIdentity associates user with the given remote identity.
	AssociateUserWithRemoteIdentity(userID uint64, provider string, remoteID uint64, accessToken string) error
	// UpdateAccessToken updates the access token for the given remote identity.
	// The supplied remote identity must contain Provider, RemoteID and AccessToken.
	UpdateAccessToken(*pb.RemoteIdentity) error
	// GetUserByRemoteIdentity returns the user for the given remote identity.
	// The supplied remote identity must contain Provider and RemoteID.
	GetUserByRemoteIdentity(*pb.RemoteIdentity) (*pb.User, error)

	// GetUser returns the given user, including remote identities.
	GetUser(uint64) (*pb.User, error)
	// GetUserByCourse returns the owner of the given login
	// with preloaded course matching the given query.
	GetUserByCourse(*pb.Course, string) (*pb.User, *pb.Course, error)
	// GetUserWithEnrollments returns the user by ID with preloaded user enrollments.
	GetUserWithEnrollments(uint64) (*pb.User, error)
	// GetUsers returns the users for the given set of user IDs.
	GetUsers(...uint64) ([]*pb.User, error)
	// UpdateUser updates the user's details, excluding remote identities.
	UpdateUser(*pb.User) error

	// CreateCourse creates a new course if user with given ID is admin, enrolls user as course teacher.
	CreateCourse(uint64, *pb.Course) error
	// GetCourse fetches course by ID. If withInfo is true, preloads course
	// assignments, active enrollments and groups.
	GetCourse(uint64, bool) (*pb.Course, error)
	// GetCourseByOrganizationID fetches course by organization ID.
	GetCourseByOrganizationID(organizationID uint64) (*pb.Course, error)
	// GetCourses returns a list of courses. If one or more course IDs are provided,
	// the corresponding courses are returned. Otherwise, all courses are returned.
	GetCourses(...uint64) ([]*pb.Course, error)
	// GetCoursesByUser returns all courses (with enrollment status)
	// for the given user id.
	// If enrollment statuses is provided, the set of courses returned
	// is filtered according to these enrollment statuses.
	GetCoursesByUser(userID uint64, statuses ...pb.Enrollment_UserStatus) ([]*pb.Course, error)
	// GetCourseTeachers returns a list of all teachers in a course.
	GetCourseTeachers(query *pb.Course) ([]*pb.User, error)
	// UpdateCourse updates course information.
	UpdateCourse(*pb.Course) error

	// CreateEnrollment creates a new pending enrollment.
	CreateEnrollment(*pb.Enrollment) error
	// RejectEnrollment removes the user enrollment from the database
	RejectEnrollment(userID, courseID uint64) error
	// UpdateEnrollmentStatus changes status of the course enrollment for the given user and course.
	UpdateEnrollment(*pb.Enrollment) error
	// GetEnrollmentByCourseAndUser returns a user enrollment for the given course ID.
	GetEnrollmentByCourseAndUser(courseID uint64, userID uint64) (*pb.Enrollment, error)
	// GetEnrollmentsByCourse fetches all course enrollments with given statuses.
	GetEnrollmentsByCourse(courseID uint64, statuses ...pb.Enrollment_UserStatus) ([]*pb.Enrollment, error)
	// GetEnrollmentsByUser fetches all enrollments for the given user
	GetEnrollmentsByUser(userID uint64, statuses ...pb.Enrollment_UserStatus) ([]*pb.Enrollment, error)

	// CreateGroup creates a new group and assign users to newly created group.
	CreateGroup(*pb.Group) error
	// UpdateGroup updates a group with the specified users and enrollments.
	UpdateGroup(group *pb.Group) error
	// UpdateGroupStatus updates status field of a group.
	UpdateGroupStatus(*pb.Group) error
	// DeleteGroup deletes a group and its corresponding enrollments.
	DeleteGroup(uint64) error
	// GetGroup returns the group with the specified group ID.
	GetGroup(uint64) (*pb.Group, error)
	// GetGroupsByCourse returns the groups for the given course.
	GetGroupsByCourse(courseID uint64, statuses ...pb.Group_GroupStatus) ([]*pb.Group, error)

	// CreateAssignment creates a new or updates an existing assignment.
	CreateAssignment(*pb.Assignment) error
	// GetAssignment returns assignment mathing the given query.
	GetAssignment(query *pb.Assignment) (*pb.Assignment, error)
	// GetAssignmentsByCourse returns a list of all assignments for the given course ID.
	GetAssignmentsByCourse(uint64, bool) ([]*pb.Assignment, error)
	// UpdateAssignments updates the specified list of assignments.
	UpdateAssignments([]*pb.Assignment) error
	// CreateBenchmark creates a new grading benchmark.
	CreateBenchmark(*pb.GradingBenchmark) error
	// UpdateBenchmark updates the given benchmark.
	UpdateBenchmark(*pb.GradingBenchmark) error
	// DeleteBenchmark deletes the given benchmark.
	DeleteBenchmark(*pb.GradingBenchmark) error
	// CreateCriterion creates a new grading criterion.
	CreateCriterion(*pb.GradingCriterion) error
	// UpdateCriterion updates the given criterion.
	UpdateCriterion(*pb.GradingCriterion) error
	// DeleteCriterion deletes the given criterion.
	DeleteCriterion(*pb.GradingCriterion) error

	// CreateSubmission creates a new submission record or updates the most
	// recent submission, as defined by the provided submissionQuery.
	// The submissionQuery must always specify the assignment, and may specify the ID of
	// either an individual student or a group, but not both.
	CreateSubmission(*pb.Submission) error
	// GetSubmission returns a single submission matching the given query.
	GetSubmission(query *pb.Submission) (*pb.Submission, error)
	// GetLastSubmissions returns a list of submission entries for the given course, matching the given query.
	GetLastSubmissions(courseID uint64, query *pb.Submission) ([]*pb.Submission, error)
	// GetSubmissions returns all submissions matching the query.
	GetSubmissions(*pb.Submission) ([]*pb.Submission, error)
	// GetAssignmentsWithSubmissions returns a list of assignments with the latest submissions for the given course.
	GetAssignmentsWithSubmissions(courseID uint64, requestType pb.SubmissionsForCourseRequest_Type, withBuildInfo bool) ([]*pb.Assignment, error)
	// UpdateSubmission updates the specified submission with approved or not approved.
	UpdateSubmission(*pb.Submission) error
	// UpdateSubmissions releases and/or approves all submissions with a certain score
	UpdateSubmissions(uint64, *pb.Submission) error
	// GetReview returns a single review matching the given query.
	GetReview(query *pb.Review) (*pb.Review, error)
	// CreateReview adds a new submission review.
	CreateReview(*pb.Review) error
	// UpdateReview updates the given review.
	UpdateReview(*pb.Review) error
	// DeleteReview removes all review records matching the query.
	DeleteReview(*pb.Review) error
	// GetBenchmarks return all benchmarks and criteria for an assignmend
	GetBenchmarks(*pb.Assignment) ([]*pb.GradingBenchmark, error)
	// CreateRepository creates a new repository.
	CreateRepository(repo *pb.Repository) error
	// GetRepositories returns repositories that match the given query.
	GetRepositories(query *pb.Repository) ([]*pb.Repository, error)
	// DeleteRepository deletes repository for the given remote provider's ID.
	DeleteRepository(remoteID uint64) error
	// GetRepositoriesWithIssues gets repositories with issues
	GetRepositoriesWithIssues(query *pb.Repository) ([]*pb.Repository, error)

	// GetTasks returns tasks that match the given query.
	GetTasks(query *pb.Task) ([]*pb.Task, error)
	// CreateIssues creates a batch of issues
	CreateIssues(issues []*pb.Issue) error
	// SynchronizeAssignmentTasks synchronizes all tasks of each assignment in a given course. Returns created, updated and deleted tasks
	SynchronizeAssignmentTasks(course *pb.Course, taskMap map[uint32]map[string]*pb.Task) ([]*pb.Task, []*pb.Task, error)
	// CreatePullRequest creates a pull request
	CreatePullRequest(pullRequest *pb.PullRequest) error
	// GetPullRequest returns the pull request matching the given query
	GetPullRequest(query *pb.PullRequest) (*pb.PullRequest, error)
	// HandleMergingPR handles merging a pull request
	HandleMergingPR(query *pb.PullRequest) error
	// DeletePullRequest updates the pull request matching the given query
	UpdatePullRequest(pullRequest *pb.PullRequest) error

	// UpdateSlipDays updates used slipdays for the given course enrollment
	UpdateSlipDays([]*pb.UsedSlipDays) error
}

Database contains methods for manipulating the database.

type GormDB

type GormDB struct {
	// contains filtered or unexported fields
}

GormDB implements the Database interface.

func NewGormDB

func NewGormDB(path string, logger *zap.Logger) (*GormDB, error)

NewGormDB creates a new gorm database using the provided driver.

func (*GormDB) AssociateUserWithRemoteIdentity

func (db *GormDB) AssociateUserWithRemoteIdentity(uid uint64, provider string, remoteID uint64, accessToken string) error

AssociateUserWithRemoteIdentity associates remote identity with the user with given ID.

func (*GormDB) Close

func (db *GormDB) Close() error

func (*GormDB) CreateAssignment

func (db *GormDB) CreateAssignment(assignment *pb.Assignment) error

CreateAssignment creates a new assignment record.

func (*GormDB) CreateBenchmark

func (db *GormDB) CreateBenchmark(query *pb.GradingBenchmark) error

CreateBenchmark creates a new grading benchmark

func (*GormDB) CreateCourse

func (db *GormDB) CreateCourse(courseCreatorID uint64, course *pb.Course) error

CreateCourse creates a new course if user with given ID is admin, enrolls user as course teacher. The provided course must have a unique (GitHub) OrganizationID not already associated with existing course. Similarly, the course must have a unique course code and year.

func (*GormDB) CreateCriterion

func (db *GormDB) CreateCriterion(query *pb.GradingCriterion) error

CreateCriterion creates a new grading criterion

func (*GormDB) CreateEnrollment

func (db *GormDB) CreateEnrollment(enrollment *pb.Enrollment) error

CreateEnrollment creates a new pending enrollment.

func (*GormDB) CreateGroup

func (db *GormDB) CreateGroup(group *pb.Group) error

CreateGroup creates a new group and assign users to newly created group.

func (*GormDB) CreateIssues

func (db *GormDB) CreateIssues(issues []*pb.Issue) error

CreateIssues creates a batch of issues

func (*GormDB) CreatePullRequest

func (db *GormDB) CreatePullRequest(pullRequest *pb.PullRequest) error

CreatePullRequest creates a pull request. It is initially in the "draft" stage, signaling that it is not yet ready for review

func (*GormDB) CreateRepository

func (db *GormDB) CreateRepository(repo *pb.Repository) error

CreateRepository creates a new repository record.

func (*GormDB) CreateReview

func (db *GormDB) CreateReview(query *pb.Review) error

CreateReview creates a new submission review

func (*GormDB) CreateSubmission

func (db *GormDB) CreateSubmission(submission *pb.Submission) error

CreateSubmission creates a new submission record or updates the most recent submission, as defined by the provided submissionQuery. The submissionQuery must always specify the assignment, and may specify the ID of either an individual student or a group, but not both.

func (*GormDB) CreateUserFromRemoteIdentity

func (db *GormDB) CreateUserFromRemoteIdentity(user *pb.User, remoteIdentity *pb.RemoteIdentity) error

CreateUserFromRemoteIdentity creates new user record from remote identity, sets user with ID 1 as admin.

func (*GormDB) DeleteBenchmark

func (db *GormDB) DeleteBenchmark(query *pb.GradingBenchmark) error

DeleteBenchmark removes the given benchmark

func (*GormDB) DeleteCriterion

func (db *GormDB) DeleteCriterion(query *pb.GradingCriterion) error

DeleteCriterion removes the given criterion

func (*GormDB) DeleteGroup

func (db *GormDB) DeleteGroup(groupID uint64) error

DeleteGroup deletes a group and its corresponding enrollments.

func (*GormDB) DeleteRepository

func (db *GormDB) DeleteRepository(remoteID uint64) error

DeleteRepository deletes repository for the given remote provider's ID.

func (*GormDB) DeleteReview

func (db *GormDB) DeleteReview(query *pb.Review) error

DeleteReview removes all reviews matching the query

func (*GormDB) GetAssignment

func (db *GormDB) GetAssignment(query *pb.Assignment) (*pb.Assignment, error)

GetAssignment returns assignment with the given ID.

func (*GormDB) GetAssignmentsByCourse

func (db *GormDB) GetAssignmentsByCourse(courseID uint64, withGrading bool) ([]*pb.Assignment, error)

GetAssignmentsByCourse fetches all assignments for the given course ID.

func (*GormDB) GetAssignmentsWithSubmissions

func (db *GormDB) GetAssignmentsWithSubmissions(courseID uint64, submissionType pb.SubmissionsForCourseRequest_Type, withBuildInfo bool) ([]*pb.Assignment, error)

GetAssignmentsWithSubmissions returns all course assignments of requested type with preloaded submissions.

func (*GormDB) GetBenchmarks

func (db *GormDB) GetBenchmarks(query *pb.Assignment) ([]*pb.GradingBenchmark, error)

GetBenchmarks returns all benchmarks and associated criteria for a given assignment ID

func (*GormDB) GetCourse

func (db *GormDB) GetCourse(courseID uint64, withEnrollments bool) (*pb.Course, error)

GetCourse fetches course by ID. If withInfo is true, preloads course assignments, active enrollments and groups.

func (*GormDB) GetCourseByOrganizationID

func (db *GormDB) GetCourseByOrganizationID(did uint64) (*pb.Course, error)

GetCourseByOrganizationID fetches course by organization ID.

func (*GormDB) GetCourseTeachers

func (db *GormDB) GetCourseTeachers(query *pb.Course) ([]*pb.User, error)

GetCourseTeachers returns a list of all teachers in a course.

func (*GormDB) GetCourses

func (db *GormDB) GetCourses(courseIDs ...uint64) ([]*pb.Course, error)

GetCourses returns a list of courses. If one or more course ids are provided, the corresponding courses are returned. Otherwise, all courses are returned.

func (*GormDB) GetCoursesByUser

func (db *GormDB) GetCoursesByUser(userID uint64, statuses ...pb.Enrollment_UserStatus) ([]*pb.Course, error)

GetCoursesByUser returns all courses (with enrollment status) for the given user id. If enrollment statuses is provided, the set of courses returned is filtered according to these enrollment statuses.

func (*GormDB) GetEnrollmentByCourseAndUser

func (db *GormDB) GetEnrollmentByCourseAndUser(courseID uint64, userID uint64) (*pb.Enrollment, error)

GetEnrollmentByCourseAndUser returns a user enrollment for the given course ID.

func (*GormDB) GetEnrollmentsByCourse

func (db *GormDB) GetEnrollmentsByCourse(courseID uint64, statuses ...pb.Enrollment_UserStatus) ([]*pb.Enrollment, error)

GetEnrollmentsByCourse fetches all course enrollments with given statuses.

func (*GormDB) GetEnrollmentsByUser

func (db *GormDB) GetEnrollmentsByUser(userID uint64, statuses ...pb.Enrollment_UserStatus) ([]*pb.Enrollment, error)

GetEnrollmentsByUser returns all existing enrollments for the given user

func (*GormDB) GetGroup

func (db *GormDB) GetGroup(groupID uint64) (*pb.Group, error)

GetGroup returns the group with the specified group id.

func (*GormDB) GetGroupsByCourse

func (db *GormDB) GetGroupsByCourse(courseID uint64, statuses ...pb.Group_GroupStatus) ([]*pb.Group, error)

GetGroupsByCourse returns the groups for the given course.

func (*GormDB) GetLastSubmissions

func (db *GormDB) GetLastSubmissions(courseID uint64, query *pb.Submission) ([]*pb.Submission, error)

GetLastSubmissions returns all submissions for the active assignment for the given course. The query may specify both UserID and GroupID to fetch both user and group submissions.

func (*GormDB) GetPullRequest

func (db *GormDB) GetPullRequest(query *pb.PullRequest) (*pb.PullRequest, error)

GetPullRequest returns the pull request matching the given query

func (*GormDB) GetRepositories

func (db *GormDB) GetRepositories(query *pb.Repository) ([]*pb.Repository, error)

GetRepositories returns all repositories satisfying the given query.

func (*GormDB) GetRepositoriesWithIssues

func (db *GormDB) GetRepositoriesWithIssues(query *pb.Repository) ([]*pb.Repository, error)

GetRepositoriesWithIssues gets repositories with issues

func (*GormDB) GetReview

func (db *GormDB) GetReview(query *pb.Review) (*pb.Review, error)

GetReview fetches a review

func (*GormDB) GetSubmission

func (db *GormDB) GetSubmission(query *pb.Submission) (*pb.Submission, error)

GetSubmission fetches a submission record.

func (*GormDB) GetSubmissions

func (db *GormDB) GetSubmissions(query *pb.Submission) ([]*pb.Submission, error)

GetSubmissions returns all submissions matching the query.

func (*GormDB) GetTasks

func (db *GormDB) GetTasks(query *pb.Task) ([]*pb.Task, error)

GetTasks gets tasks based on query

func (*GormDB) GetUser

func (db *GormDB) GetUser(userID uint64) (*pb.User, error)

GetUser fetches a user by ID with remote identities.

func (*GormDB) GetUserByCourse

func (db *GormDB) GetUserByCourse(query *pb.Course, login string) (*pb.User, *pb.Course, error)

GetUserByCourse returns user and course matching the provided course query and the provided user login name.

func (*GormDB) GetUserByRemoteIdentity

func (db *GormDB) GetUserByRemoteIdentity(remote *pb.RemoteIdentity) (*pb.User, error)

GetUserByRemoteIdentity fetches user by remote identity.

func (*GormDB) GetUserWithEnrollments

func (db *GormDB) GetUserWithEnrollments(userID uint64) (*pb.User, error)

GetUserWithEnrollments returns user with the given ID with all enrollments.

func (*GormDB) GetUsers

func (db *GormDB) GetUsers(userIDs ...uint64) ([]*pb.User, error)

GetUsers fetches all users by provided IDs.

func (*GormDB) HandleMergingPR

func (db *GormDB) HandleMergingPR(pullRequest *pb.PullRequest) error

HandleMergingPR handles merging a pull request If a pull request has not been approved, it should not have been merged. We therefore do not delete the associated issue. To resume a working state, students are expected to reopen the issue that was closed from this merging, and create a new PR for it.

func (*GormDB) RejectEnrollment

func (db *GormDB) RejectEnrollment(userID, courseID uint64) error

RejectEnrollment removes the user enrollment from the database.

func (*GormDB) SynchronizeAssignmentTasks

func (db *GormDB) SynchronizeAssignmentTasks(course *pb.Course, taskMap map[uint32]map[string]*pb.Task) (createdTasks, updatedTasks []*pb.Task, err error)

SynchronizeAssignmentTasks synchronizes all tasks of each assignment in a given course. Returns created, updated and deleted tasks

func (*GormDB) UpdateAccessToken

func (db *GormDB) UpdateAccessToken(remote *pb.RemoteIdentity) error

UpdateAccessToken refreshes the token info for the given remote identity.

func (*GormDB) UpdateAssignments

func (db *GormDB) UpdateAssignments(assignments []*pb.Assignment) error

UpdateAssignments updates assignment information.

func (*GormDB) UpdateBenchmark

func (db *GormDB) UpdateBenchmark(query *pb.GradingBenchmark) error

UpdateBenchmark updates the given benchmark

func (*GormDB) UpdateCourse

func (db *GormDB) UpdateCourse(course *pb.Course) error

UpdateCourse updates course information.

func (*GormDB) UpdateCriterion

func (db *GormDB) UpdateCriterion(query *pb.GradingCriterion) error

UpdateCriterion updates the given criterion

func (*GormDB) UpdateEnrollment

func (db *GormDB) UpdateEnrollment(enrol *pb.Enrollment) error

UpdateEnrollment changes status and display state of the given enrollment.

func (*GormDB) UpdateGroup

func (db *GormDB) UpdateGroup(group *pb.Group) error

UpdateGroup updates a group with the specified users and enrollments.

func (*GormDB) UpdateGroupStatus

func (db *GormDB) UpdateGroupStatus(group *pb.Group) error

UpdateGroupStatus updates status field of a group.

func (*GormDB) UpdatePullRequest

func (db *GormDB) UpdatePullRequest(pullRequest *pb.PullRequest) error

DeletePullRequest updates the pull request matching the given query

func (*GormDB) UpdateReview

func (db *GormDB) UpdateReview(query *pb.Review) error

UpdateReview updates feedback text, review and ready status

func (*GormDB) UpdateSlipDays

func (db *GormDB) UpdateSlipDays(usedSlipDays []*pb.UsedSlipDays) error

UpdateSlipDays updates used slip days for the given course enrollment

func (*GormDB) UpdateSubmission

func (db *GormDB) UpdateSubmission(query *pb.Submission) error

UpdateSubmission updates submission with the given approved status.

func (*GormDB) UpdateSubmissions

func (db *GormDB) UpdateSubmissions(courseID uint64, query *pb.Submission) error

UpdateSubmissions approves and/or releases all submissions that have score equal or above the provided score for the given assignment ID

func (*GormDB) UpdateUser

func (db *GormDB) UpdateUser(user *pb.User) error

UpdateUser updates user information.

type Logger

type Logger struct {
	ZapLogger                 *zap.Logger
	LogLevel                  gormlogger.LogLevel
	SlowThreshold             time.Duration
	IgnoreRecordNotFoundError bool
}

Logger is an adaption of gorm.Logger that uses the zap logger. The logger is based on code from moul.io/zapgorm2.

func (Logger) Error

func (l Logger) Error(ctx context.Context, str string, args ...interface{})

func (Logger) Info

func (l Logger) Info(ctx context.Context, str string, args ...interface{})

func (Logger) LogMode

func (l Logger) LogMode(level gormlogger.LogLevel) gormlogger.Interface

func (Logger) Trace

func (l Logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)

func (Logger) Warn

func (l Logger) Warn(ctx context.Context, str string, args ...interface{})

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL