Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

2.6 KB
Raw Blame
package sqlite

import (
	"context"
	"database/sql"

	"github.com/pkg/errors"
	msqlite "modernc.org/sqlite"
	sqlite3 "modernc.org/sqlite/lib"

	"github.com/usememos/memos/store"
)

// IsRetryableTransactionError reports whether err is a transient SQLite locking failure.
func (*DB) IsRetryableTransactionError(err error) bool {
	var sqliteErr *msqlite.Error
	if !errors.As(err, &sqliteErr) {
		return false
	}
	code := sqliteErr.Code() & 0xff
	return code == sqlite3.SQLITE_BUSY || code == sqlite3.SQLITE_LOCKED
}

// ApplyAuthenticationConfigMutationTx validates and applies an auth mutation within tx, a serializable transaction.
func (*DB) ApplyAuthenticationConfigMutationTx(ctx context.Context, tx *sql.Tx, mutation *store.AuthenticationConfigMutation) error {
	state := &store.AuthenticationConfigState{}
	general := &store.InstanceSetting{}
	err := tx.QueryRowContext(ctx, `SELECT name, value, description FROM system_setting WHERE name = ?`, "GENERAL").Scan(
		&general.Name, &general.Value, &general.Description,
	)
	if err == nil {
		state.GeneralSetting = general
	} else if !errors.Is(err, sql.ErrNoRows) {
		return errors.Wrap(err, "failed to read GENERAL setting")
	}
	rows, err := tx.QueryContext(ctx, `SELECT id, uid FROM idp ORDER BY id`)
	if err != nil {
		return errors.Wrap(err, "failed to read identity providers")
	}
	defer rows.Close()
	for rows.Next() {
		provider := &store.IdentityProvider{}
		if err := rows.Scan(&provider.ID, &provider.UID); err != nil {
			rows.Close()
			return errors.Wrap(err, "failed to scan identity provider")
		}
		state.IdentityProviders = append(state.IdentityProviders, provider)
	}
	if err := rows.Err(); err != nil {
		rows.Close()
		return errors.Wrap(err, "failed to iterate identity providers")
	}
	if err := rows.Close(); err != nil {
		return errors.Wrap(err, "failed to close identity provider rows")
	}
	if mutation.Validate != nil {
		if err := mutation.Validate(state); err != nil {
			return err
		}
	}
	if setting := mutation.UpsertGeneralSetting; setting != nil {
		_, err = tx.ExecContext(ctx, `INSERT INTO system_setting (name, value, description) VALUES (?, ?, ?) ON CONFLICT(name) DO UPDATE SET value = EXCLUDED.value, description = EXCLUDED.description`, setting.Name, setting.Value, setting.Description)
	} else if id := mutation.DeleteIdentityProviderID; id != nil {
		_, err = tx.ExecContext(ctx, `DELETE FROM idp WHERE id = ?`, *id)
	} else {
		return errors.New("authentication configuration mutation has no operation")
	}
	if err != nil {
		return errors.Wrap(err, "failed to apply authentication configuration mutation")
	}
	return nil
}