NewDBFromConfig instantiates a DB based on a configuration.
(dbc *DBConfig)
| 725 | |
| 726 | // NewDBFromConfig instantiates a DB based on a configuration. |
| 727 | func NewDBFromConfig(dbc *DBConfig) (*litestream.DB, error) { |
| 728 | if dbc.MetaDir != nil { |
| 729 | return nil, fmt.Errorf("'meta-dir' can only be used with a directory") |
| 730 | } |
| 731 | |
| 732 | configPath, err := expand(dbc.Path) |
| 733 | if err != nil { |
| 734 | return nil, err |
| 735 | } |
| 736 | |
| 737 | // Initialize database with given path. |
| 738 | db := litestream.NewDB(configPath) |
| 739 | |
| 740 | // Override default database settings if specified in configuration. |
| 741 | if dbc.MetaPath != nil { |
| 742 | expandedMetaPath, err := expand(*dbc.MetaPath) |
| 743 | if err != nil { |
| 744 | return nil, fmt.Errorf("failed to expand meta path: %w", err) |
| 745 | } |
| 746 | dbc.MetaPath = &expandedMetaPath |
| 747 | db.SetMetaPath(expandedMetaPath) |
| 748 | } |
| 749 | if dbc.MonitorInterval != nil { |
| 750 | db.MonitorInterval = *dbc.MonitorInterval |
| 751 | } |
| 752 | if dbc.CheckpointInterval != nil { |
| 753 | db.CheckpointInterval = *dbc.CheckpointInterval |
| 754 | } |
| 755 | if dbc.BusyTimeout != nil { |
| 756 | db.BusyTimeout = *dbc.BusyTimeout |
| 757 | } |
| 758 | if dbc.MinCheckpointPageN != nil { |
| 759 | db.MinCheckpointPageN = *dbc.MinCheckpointPageN |
| 760 | } |
| 761 | if dbc.TruncatePageN != nil { |
| 762 | db.TruncatePageN = *dbc.TruncatePageN |
| 763 | } |
| 764 | |
| 765 | // Instantiate and attach replica. |
| 766 | // v0.3.x and before supported multiple replicas but that was dropped to |
| 767 | // ensure there's a single remote data authority. |
| 768 | switch { |
| 769 | case dbc.Replica == nil && len(dbc.Replicas) == 0: |
| 770 | return nil, fmt.Errorf("must specify replica for database") |
| 771 | case dbc.Replica != nil && len(dbc.Replicas) > 0: |
| 772 | return nil, fmt.Errorf("cannot specify 'replica' and 'replicas' on a database") |
| 773 | case len(dbc.Replicas) > 1: |
| 774 | return nil, fmt.Errorf("multiple replicas on a single database are no longer supported") |
| 775 | } |
| 776 | |
| 777 | var rc *ReplicaConfig |
| 778 | if dbc.Replica != nil { |
| 779 | rc = dbc.Replica |
| 780 | } else { |
| 781 | rc = dbc.Replicas[0] |
| 782 | } |
| 783 | |
| 784 | r, err := NewReplicaFromConfig(rc, db) |
no test coverage detected