Documentation
¶
Index ¶
- type Database
- func (d *Database) ApplyEmbeddedDbSchema(version int64) error
- func (d *Database) Close() error
- func (d *Database) DeleteSpammer(tx *sqlx.Tx, id int64) error
- func (d *Database) GetSpammer(id int64) (*Spammer, error)
- func (d *Database) GetSpammers() ([]*Spammer, error)
- func (d *Database) GetSpamoorState(key string, returnValue interface{}) (interface{}, error)
- func (d *Database) Init() error
- func (d *Database) InsertSpammer(tx *sqlx.Tx, spammer *Spammer) error
- func (d *Database) RunDBTransaction(handler func(tx *sqlx.Tx) error) error
- func (d *Database) SetSpamoorState(tx *sqlx.Tx, key string, value interface{}) error
- func (d *Database) UpdateSpammer(tx *sqlx.Tx, spammer *Spammer) error
- type Spammer
- type SpamoorState
- type SqliteDatabaseConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Database ¶
type Database struct {
ReaderDb *sqlx.DB // Database connection for read operations
// contains filtered or unexported fields
}
Database manages SQLite database connections with WAL mode and connection pooling. It provides both reader and writer connections with mutex protection for write operations and embedded schema migration support using goose.
func NewDatabase ¶
func NewDatabase(config *SqliteDatabaseConfig, logger logrus.FieldLogger) *Database
NewDatabase creates a new Database instance with the specified configuration and logger. The database connections are not initialized until Init() is called.
func (*Database) ApplyEmbeddedDbSchema ¶
ApplyEmbeddedDbSchema applies database schema migrations using embedded SQL files. Supports different migration strategies: -2 (all), -1 (one up), or specific version. Uses goose migration library with allowMissing option for flexible schema management.
func (*Database) Close ¶
Close closes the database writer connection. Should be called during application shutdown to ensure proper cleanup.
func (*Database) DeleteSpammer ¶
DeleteSpammer removes a spammer record from the database within a transaction. Permanently deletes the spammer and all associated data. Returns an error if the deletion fails or the spammer doesn't exist.
func (*Database) GetSpammer ¶
GetSpammer retrieves a single spammer by ID from the database. Returns the spammer entity or an error if not found or database access fails.
func (*Database) GetSpammers ¶
GetSpammers retrieves all spammers from the database ordered by creation time (newest first). Returns a slice of spammer entities or an error if database access fails.
func (*Database) GetSpamoorState ¶
GetSpamoorState retrieves and deserializes a state value by key. The returnValue parameter should be a pointer to the target type for unmarshaling. Returns the deserialized value or an error if the key doesn't exist or JSON parsing fails.
func (*Database) Init ¶
Init initializes the database connections with WAL mode and connection pooling. Sets default connection limits (50 max open, 10 max idle) if not specified. Enables WAL mode for better concurrent access and configures connection timeouts.
func (*Database) InsertSpammer ¶
InsertSpammer creates a new spammer record in the database within a transaction. Updates the spammer's ID field with the generated database ID after insertion. Returns an error if the insertion fails or transaction is invalid.
func (*Database) RunDBTransaction ¶
RunDBTransaction executes a function within a database transaction with automatic rollback. The transaction is protected by a mutex to ensure sequential write operations. Automatically rolls back on error and commits on success.
func (*Database) SetSpamoorState ¶
SetSpamoorState stores a state value by key with JSON serialization. If tx is nil, creates and manages its own transaction automatically. Uses INSERT OR REPLACE to handle both new entries and updates atomically. Returns an error if JSON marshaling or database operation fails.
func (*Database) UpdateSpammer ¶
UpdateSpammer modifies an existing spammer record in the database within a transaction. Updates all mutable fields: name, description, config, status, and state. The ID and creation timestamp remain unchanged.
type Spammer ¶
type Spammer struct {
ID int64 `db:"id"` // Unique identifier for the spammer instance
Scenario string `db:"scenario"` // Name of the scenario to execute
Name string `db:"name"` // Human-readable name for the spammer
Description string `db:"description"` // Detailed description of what this spammer does
Config string `db:"config"` // YAML configuration for scenario and wallet settings
Status int `db:"status"` // Current execution status (see SpammerStatus constants)
CreatedAt int64 `db:"created_at"` // Unix timestamp when the spammer was created
State string `db:"state"` // Persistent state data for scenario execution
}
Spammer represents a database entity for storing spammer configuration and state. Maps to the "spammers" table with fields for scenario definition, execution status, and persistent state management. The status field tracks execution state using SpammerStatus constants.
type SpamoorState ¶
type SpamoorState struct {
Key string `db:"key"` // Unique identifier for the state entry
Value string `db:"value"` // JSON-serialized value for the state data
}
SpamoorState represents a key-value pair for storing application-wide configuration state. Maps to the "spamoor_state" table for persisting settings like scenario counters, first launch flags, and other global application state that survives restarts.
type SqliteDatabaseConfig ¶
type SqliteDatabaseConfig struct {
File string `yaml:"file"` // Database file path
MaxOpenConns int `yaml:"maxOpenConns"` // Maximum number of open connections to the database
MaxIdleConns int `yaml:"maxIdleConns"` // Maximum number of idle connections in the pool
}
SqliteDatabaseConfig defines the configuration for SQLite database connections. It specifies the database file path and connection pool limits for managing concurrent database access efficiently.