Documentation ¶
Index ¶
- Constants
- Variables
- func ApplyMergeStrategy(db Database, mapp []Mapping, mas []MergeAction) error
- func FindModifiedRows(pks map[string]string, from, in DatabaseDump) map[RecordID]map[string]any
- func FindRow(table, pk string, id any, dump DatabaseDump) map[string]any
- func NewPostgres(ctx context.Context, c Postgreser) (pgdb, error)
- func ReverseForeignKeyMapping(fks []ForeignKey, mapp []Mapping, rows DatabaseDump)
- func ReverseForeignKeyMappingRow(fks []ForeignKey, mapp []Mapping, row map[string]any)
- func ReversePrimaryKeyMapping(pks map[string]string, mapp []Mapping, dump DatabaseDump)
- func Upload(ctx context.Context, db Database, dump DatabaseDump) error
- type Database
- type DatabaseDump
- type ForeignKey
- type Mapping
- type MergeAction
- type Opt
- type Postgreser
- type RecordID
Constants ¶
const ( // DumpTableKey is a special field present in every row of an export. // It can be used to determine which table the row is from. // Note that the export may have rows from a table interleaved with rows from other tables. DumpTableKey = "%_tablename" )
Variables ¶
var LogFunc = log.Printf
Functions ¶
func ApplyMergeStrategy ¶
func ApplyMergeStrategy(db Database, mapp []Mapping, mas []MergeAction) error
func FindModifiedRows ¶
return a map of updates or deletes that would make "in" equal "from" the map key is the table and column that changed and the value is the new value
func NewPostgres ¶
func NewPostgres(ctx context.Context, c Postgreser) (pgdb, error)
NewPostgres returns a pgdb that can generate a Database for datapasta Upload and Download functions.
func ReverseForeignKeyMapping ¶
func ReverseForeignKeyMapping(fks []ForeignKey, mapp []Mapping, rows DatabaseDump)
reverse all the foreign keys of a dump
func ReverseForeignKeyMappingRow ¶
func ReverseForeignKeyMappingRow(fks []ForeignKey, mapp []Mapping, row map[string]any)
reverse all the foreign keys of an indivdual row
func ReversePrimaryKeyMapping ¶
func ReversePrimaryKeyMapping(pks map[string]string, mapp []Mapping, dump DatabaseDump)
reverse all the primary keys of a dump
Types ¶
type Database ¶
type Database interface { // SelectMatchingRows must return unseen records. // a Database can't be reused between clones, because it must do internal deduping. // `conds` will be a map of columns and the values they can have. SelectMatchingRows(tname string, conds map[string][]any) ([]map[string]any, error) // insert one record, returning the new id InsertRecord(record map[string]any) (any, error) // apply the updates from the cols to the row Update(id RecordID, cols map[string]any) error // delete the row Delete(id RecordID) error // Insert uploads a batch of records. // a Destination can't generally be reused between clones, as it may be inside a transaction. // it's recommended that callers use a Database that wraps a transaction. // // the records will have primary keys which must be handled. // the Database is responsible for exposing the resulting primary key mapping in some manner. Insert(records ...map[string]any) error // Mapping must return whatever mapping has been created by prior Inserts. // the implementation may internally choose to track this in the database or in memory. Mapping() ([]Mapping, error) // get foriegn key mapping ForeignKeys() []ForeignKey // get primary key mapping PrimaryKeys() map[string]string }
Database is the abstraction between the cloning tool and the database. The NewPostgres.NewClient method gives you an implementation for Postgres.
type DatabaseDump ¶
DatabaseDump is the output of a Download call, containing every record that was downloaded. It is safe to transport as JSON.
func Download ¶
func Download(ctx context.Context, db Database, startTable, startColumn string, startId any, opts ...Opt) (DatabaseDump, []string, error)
Download recursively downloads a dump of the database from a given starting point. the 2nd return is a trace that can help debug or understand what happened.
func FindMissingRows ¶
func FindMissingRows(pks map[string]string, from, in DatabaseDump) DatabaseDump
find rows in "from" that are missing in "in"
type ForeignKey ¶
type ForeignKey struct { BaseTable string `json:"base_table"` BaseCol string `json:"base_col"` ReferencingTable string `json:"referencing_table"` ReferencingCol string `json:"referencing_col"` }
ForeignKey contains every RERENCING column and the BASE column it refers to. This is used to recurse the database as a graph. Database implementations must provide a complete list of references.
type Mapping ¶
func FindMapping ¶
type MergeAction ¶
func GenerateMergeStrategy ¶
func GenerateMergeStrategy(pks map[string]string, base, main, branch DatabaseDump) []MergeAction
GenerateMergeStrategy returns every update or delete needed to merge branch into main note that conflicts will be intermingled in updates and deletes
func (MergeAction) String ¶
func (ma MergeAction) String() string
type Opt ¶
type Opt func(*downloadOpts)
Opt is a functional option that can be passed to Download.
func DontInclude ¶
DontInclude does not recurse into records from `table`, but still includes referenced records.
func DontRecurse ¶
DontRecurse includes records from `table`, but does not recurse into references to it.
type Postgreser ¶
type Postgreser interface { Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error) Query(context.Context, string, ...interface{}) (pgx.Rows, error) QueryRow(context.Context, string, ...interface{}) pgx.Row CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error) SendBatch(context.Context, *pgx.Batch) pgx.BatchResults }
Postgreser does postgres things. github.com/jackc/pgx/v4/pgxpool.Pool is one such implementation of postgres.
Directories ¶
Path | Synopsis |
---|---|
integrations package houses some utility functions for making or testing database integrations.
|
integrations package houses some utility functions for making or testing database integrations. |