|
| 1 | +//go:build dev |
| 2 | + |
| 3 | +package terminal |
| 4 | + |
| 5 | +import ( |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "github.com/lightninglabs/lightning-terminal/accounts" |
| 9 | + "github.com/lightninglabs/lightning-terminal/db" |
| 10 | + "github.com/lightningnetwork/lnd/clock" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + // DatabaseBackendSqlite is the name of the SQLite database backend. |
| 15 | + DatabaseBackendSqlite = "sqlite" |
| 16 | + |
| 17 | + // DatabaseBackendPostgres is the name of the Postgres database backend. |
| 18 | + DatabaseBackendPostgres = "postgres" |
| 19 | + |
| 20 | + // DatabaseBackendBbolt is the name of the bbolt database backend. |
| 21 | + DatabaseBackendBbolt = "bbolt" |
| 22 | + |
| 23 | + // defaultSqliteDatabaseFileName is the default name of the SQLite |
| 24 | + // database file. |
| 25 | + defaultSqliteDatabaseFileName = "litd.db" |
| 26 | +) |
| 27 | + |
| 28 | +// defaultSqliteDatabasePath is the default path under which we store |
| 29 | +// the SQLite database file. |
| 30 | +var defaultSqliteDatabasePath = filepath.Join( |
| 31 | + DefaultLitDir, DefaultNetwork, defaultSqliteDatabaseFileName, |
| 32 | +) |
| 33 | + |
| 34 | +// DevConfig is a struct that holds the configuration options for a development |
| 35 | +// environment. The purpose of this struct is to hold config options for |
| 36 | +// features not yet available in production. Since our itests are built with |
| 37 | +// the dev tag, we can test these features in our itests. |
| 38 | +// |
| 39 | +// nolint:lll |
| 40 | +type DevConfig struct { |
| 41 | + // DatabaseBackend is the database backend we will use for storing all |
| 42 | + // account related data. While this feature is still in development, we |
| 43 | + // include the bbolt type here so that our itests can continue to be |
| 44 | + // tested against a bbolt backend. Once the full bbolt to SQL migration |
| 45 | + // is complete, however, we will remove the bbolt option. |
| 46 | + DatabaseBackend string `long:"databasebackend" description:"The database backend to use for storing all account related data." choice:"bbolt" choice:"sqlite" choice:"postgres"` |
| 47 | + |
| 48 | + // Sqlite holds the configuration options for a SQLite database |
| 49 | + // backend. |
| 50 | + Sqlite *db.SqliteConfig `group:"sqlite" namespace:"sqlite"` |
| 51 | + |
| 52 | + // Postgres holds the configuration options for a Postgres database |
| 53 | + Postgres *db.PostgresConfig `group:"postgres" namespace:"postgres"` |
| 54 | +} |
| 55 | + |
| 56 | +// Validate checks that all the values set in our DevConfig are valid and uses |
| 57 | +// the passed parameters to override any defaults if necessary. |
| 58 | +func (c *DevConfig) Validate(dbDir, network string) error { |
| 59 | + // We'll update the database file location if it wasn't set. |
| 60 | + if c.Sqlite.DatabaseFileName == defaultSqliteDatabasePath { |
| 61 | + c.Sqlite.DatabaseFileName = filepath.Join( |
| 62 | + dbDir, network, defaultSqliteDatabaseFileName, |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +// defaultDevConfig returns a new DevConfig with default values set. |
| 70 | +func defaultDevConfig() *DevConfig { |
| 71 | + return &DevConfig{ |
| 72 | + Sqlite: &db.SqliteConfig{ |
| 73 | + DatabaseFileName: defaultSqliteDatabasePath, |
| 74 | + }, |
| 75 | + Postgres: &db.PostgresConfig{ |
| 76 | + Host: "localhost", |
| 77 | + Port: 5432, |
| 78 | + MaxOpenConnections: 10, |
| 79 | + }, |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// NewAccountStore creates a new account store based on the chosen database |
| 84 | +// backend. |
| 85 | +func NewAccountStore(cfg *Config, clock clock.Clock) (accounts.Store, error) { |
| 86 | + switch cfg.DatabaseBackend { |
| 87 | + case DatabaseBackendSqlite: |
| 88 | + // Before we initialize the SQLite store, we'll make sure that |
| 89 | + // the directory where we will store the database file exists. |
| 90 | + networkDir := filepath.Join(cfg.LitDir, cfg.Network) |
| 91 | + err := makeDirectories(networkDir) |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + |
| 96 | + sqlStore, err := db.NewSqliteStore(cfg.Sqlite) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + |
| 101 | + return accounts.NewSQLStore(sqlStore.BaseDB, clock), nil |
| 102 | + |
| 103 | + case DatabaseBackendPostgres: |
| 104 | + sqlStore, err := db.NewPostgresStore(cfg.Postgres) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + |
| 109 | + return accounts.NewSQLStore(sqlStore.BaseDB, clock), nil |
| 110 | + |
| 111 | + default: |
| 112 | + return accounts.NewBoltStore( |
| 113 | + filepath.Dir(cfg.MacaroonPath), accounts.DBFilename, |
| 114 | + clock, |
| 115 | + ) |
| 116 | + } |
| 117 | +} |
0 commit comments