mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 01:53:42 +01:00
133 lines
5.3 KiB
Go
133 lines
5.3 KiB
Go
// Package dbcopy copies the rows of a sqlite database of the application
|
|
// into an empty postgres database, for the --migrate-sqlite-to-postgres
|
|
// command. Only the cli/copycmd package imports it.
|
|
//
|
|
// Two databases are open in one process. The process engine
|
|
// (dbengine.SetActive) is postgres, set when main opens the target through
|
|
// the application factory, which is right for everything that runs on the
|
|
// target: the schema build, the repositories, the startup migration. The
|
|
// sqlite source is opened by OpenSource without the factory and is only used
|
|
// in this package, and only through gorm builder calls on its own handle
|
|
// (Raw with constant SQL, clause.Table and clause.Column), which quote with
|
|
// the dialector of that handle. It is never given to a repository, a seed
|
|
// function, a database migration or a service, which would quote for the
|
|
// process engine.
|
|
package dbcopy
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"time"
|
|
|
|
"github.com/phishingclub/phishingclub/database"
|
|
"go.uber.org/zap"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var (
|
|
// ErrTarget is returned for a target the copy can not use
|
|
ErrTarget = errors.New("the target database can not be used")
|
|
// ErrTargetNotEmpty is returned when the target holds rows
|
|
ErrTargetNotEmpty = errors.New("the target database is not empty")
|
|
// ErrSchema is returned when the schema is not what the copy knows
|
|
ErrSchema = errors.New("the database schema is not what this version expects")
|
|
// ErrSourceOlder is returned when the sqlite schema lacks tables or
|
|
// columns of this version
|
|
ErrSourceOlder = errors.New("the sqlite database is older than this version")
|
|
// ErrUnknownData is returned when the sqlite database holds data in
|
|
// tables or columns this version does not have
|
|
ErrUnknownData = errors.New("the sqlite database holds data this version does not have")
|
|
// ErrInvalidValues is returned when source values can not be copied
|
|
ErrInvalidValues = errors.New("the sqlite database holds values that can not be copied")
|
|
// ErrOrphans is returned when rows point at missing rows and --force
|
|
// was not given
|
|
ErrOrphans = errors.New("rows point at rows that do not exist")
|
|
// ErrSourceChanged is returned when the sqlite file was written while
|
|
// the copy ran
|
|
ErrSourceChanged = errors.New("the sqlite database changed during the copy, stop the server and run the copy again")
|
|
// ErrCopy is returned when postgres refused rows
|
|
ErrCopy = errors.New("postgres refused rows of the copy")
|
|
// ErrVerify is returned when the copied rows do not add up
|
|
ErrVerify = errors.New("the copied rows do not match the source")
|
|
// ErrLockLost is returned when the instance lock is no longer held
|
|
ErrLockLost = errors.New(
|
|
"the instance lock of the target was lost during the copy, its postgres session ended; " +
|
|
"check the network and the idle limits of a proxy, load balancer or firewall " +
|
|
"between this host and postgres",
|
|
)
|
|
// ErrCancelled is returned when the copy was stopped, for example with
|
|
// Ctrl+C
|
|
ErrCancelled = errors.New("the copy was cancelled, everything was rolled back")
|
|
)
|
|
|
|
// Options are the inputs of Run
|
|
type Options struct {
|
|
// Source is the read only sqlite database from OpenSource
|
|
Source *gorm.DB
|
|
// Target is the empty postgres database, opened with database.FromConfig
|
|
Target *gorm.DB
|
|
// Lock is the instance lock held on the target for the whole run
|
|
Lock *database.InstanceLock
|
|
// DryRun rolls back at the end and only reports
|
|
DryRun bool
|
|
// Force leaves out rows whose foreign key points at a missing row
|
|
Force bool
|
|
// Out gets the progress lines, nil writes nothing
|
|
Out io.Writer
|
|
// Logger is used by the schema build
|
|
Logger *zap.SugaredLogger
|
|
// SourceInUse, when set, is asked right before the commit whether
|
|
// another process has the sqlite file open, see SourceProbe
|
|
SourceInUse func() error
|
|
// FilePath and AssetPath are the data directory and its assets folder,
|
|
// used to read installed Bait Box assets for their baselines
|
|
FilePath string
|
|
AssetPath string
|
|
|
|
// afterFirstPass runs between the two passes, for tests
|
|
afterFirstPass func()
|
|
}
|
|
|
|
// Run copies every row of the source into the target in one transaction.
|
|
// The target must be empty; its schema is made first. The returned report is
|
|
// complete also when an error is returned, as far as the copy got. On a
|
|
// dry run, or when anything is refused, nothing is committed.
|
|
func Run(ctx context.Context, opts Options) (*Report, error) {
|
|
if opts.Source == nil || opts.Target == nil || opts.Lock == nil || opts.Logger == nil {
|
|
return nil, errors.New("dbcopy: source, target, lock and logger are required")
|
|
}
|
|
if !database.IsSQLite(opts.Source) {
|
|
return nil, errors.New("dbcopy: the source is not a sqlite database")
|
|
}
|
|
if !database.IsPostgres(opts.Target) {
|
|
return nil, errors.New("dbcopy: the target is not a postgres database")
|
|
}
|
|
if opts.Out == nil {
|
|
opts.Out = io.Discard
|
|
}
|
|
r := &runner{
|
|
opts: opts,
|
|
report: &Report{DryRun: opts.DryRun, Force: opts.Force},
|
|
}
|
|
start := time.Now()
|
|
err := r.run(ctx)
|
|
r.report.Duration = time.Since(start)
|
|
if err != nil && ctx.Err() != nil {
|
|
// a stop from outside, Ctrl+C or the loss of the instance lock,
|
|
// whatever step it broke
|
|
err = context.Cause(ctx)
|
|
if errors.Is(err, context.Canceled) {
|
|
err = ErrCancelled
|
|
}
|
|
}
|
|
r.report.Err = err
|
|
return r.report, err
|
|
}
|
|
|
|
// logf writes a progress line
|
|
func (r *runner) logf(format string, args ...any) {
|
|
_, _ = fmt.Fprintf(r.opts.Out, format+"\n", args...)
|
|
}
|