2025-01-17 15:54:48 +05:30
package sqlstore
import (
2025-03-05 18:50:48 +05:30
"context"
2025-01-17 15:54:48 +05:30
"database/sql"
2025-04-27 16:38:34 +05:30
"github.com/SigNoz/signoz/pkg/errors"
2025-01-17 15:54:48 +05:30
"github.com/uptrace/bun"
)
2025-03-05 18:50:48 +05:30
type SQLStoreTxOptions = sql . TxOptions
2025-01-17 15:54:48 +05:30
type SQLStore interface {
// SQLDB returns the underlying sql.DB.
SQLDB ( ) * sql . DB
2025-03-05 18:50:48 +05:30
2025-01-17 15:54:48 +05:30
// BunDB returns an instance of bun.DB. This is the recommended way to interact with the database.
BunDB ( ) * bun . DB
2025-03-05 18:50:48 +05:30
2025-03-06 15:39:45 +05:30
// Returns the dialect of the database.
Dialect ( ) SQLDialect
2025-03-05 18:50:48 +05:30
// RunInTxCtx runs the given callback in a transaction. It creates and injects a new context with the transaction.
// If a transaction is present in the context, it will be used.
RunInTxCtx ( ctx context . Context , opts * SQLStoreTxOptions , cb func ( ctx context . Context ) error ) error
// BunDBCtx returns an instance of bun.IDB for the given context.
// If a transaction is present in the context, it will be used. Otherwise, the default will be used.
BunDBCtx ( ctx context . Context ) bun . IDB
2025-04-27 16:38:34 +05:30
// WrapNotFoundErrf wraps the given error with the given message and returns it.
WrapNotFoundErrf ( err error , code errors . Code , format string , args ... any ) error
// WrapAlreadyExistsErrf wraps the given error with the given message and returns it.
WrapAlreadyExistsErrf ( err error , code errors . Code , format string , args ... any ) error
2025-01-17 15:54:48 +05:30
}
2025-02-17 21:13:40 +05:30
type SQLStoreHook interface {
bun . QueryHook
}
2025-03-06 15:39:45 +05:30
type SQLDialect interface {
2025-04-26 15:50:02 +05:30
// Returns the type of the column for the given table and column.
2025-03-25 04:05:40 +05:30
GetColumnType ( context . Context , bun . IDB , string , string ) ( string , error )
2025-04-26 15:50:02 +05:30
// Migrates an integer column to a timestamp column for the given table and column.
IntToTimestamp ( context . Context , bun . IDB , string , string ) error
// Migrates an integer column to a boolean column for the given table and column.
IntToBoolean ( context . Context , bun . IDB , string , string ) error
// Adds a not null default to the given column for the given table, column, columnType and defaultValue.
AddNotNullDefaultToColumn ( context . Context , bun . IDB , string , string , string , string ) error
// Checks if a column exists in a table for the given table and column.
2025-03-25 04:05:40 +05:30
ColumnExists ( context . Context , bun . IDB , string , string ) ( bool , error )
2025-04-26 15:50:02 +05:30
// Adds a column to a table for the given table, column and columnType.
2025-04-25 19:38:15 +05:30
AddColumn ( context . Context , bun . IDB , string , string , string ) error
2025-04-26 15:50:02 +05:30
// Drops a column from a table for the given table and column.
2025-04-25 19:38:15 +05:30
DropColumn ( context . Context , bun . IDB , string , string ) error
2025-04-26 15:50:02 +05:30
// Renames a column in a table for the given table, old column name and new column name.
RenameColumn ( context . Context , bun . IDB , string , string , string ) ( bool , error )
// Renames a table and modifies the given model for the given table, old model, new model, references and callback. The old model
// and new model must inherit bun.BaseModel.
2025-04-04 01:46:28 +05:30
RenameTableAndModifyModel ( context . Context , bun . IDB , interface { } , interface { } , [ ] string , func ( context . Context ) error ) error
2025-04-26 15:50:02 +05:30
// Updates the primary key for the given table, old model, new model, reference and callback. The old model and new model
// must inherit bun.BaseModel.
2025-04-04 01:25:24 +05:30
UpdatePrimaryKey ( context . Context , bun . IDB , interface { } , interface { } , string , func ( context . Context ) error ) error
2025-04-26 15:50:02 +05:30
// Adds a primary key to the given table, old model, new model, reference and callback. The old model and new model
// must inherit bun.BaseModel.
2025-04-04 01:25:24 +05:30
AddPrimaryKey ( context . Context , bun . IDB , interface { } , interface { } , string , func ( context . Context ) error ) error
2025-04-26 15:50:02 +05:30
// Drops the column and the associated foreign key constraint for the given table and column.
DropColumnWithForeignKeyConstraint ( context . Context , bun . IDB , interface { } , string ) error
2025-05-07 13:48:13 +05:30
// Checks if a table exists.
TableExists ( ctx context . Context , bun bun . IDB , table interface { } ) ( bool , error )
// Toggles foreign key constraint for the given database. This makes sense only for sqlite. This cannot take a transaction as an argument and needs to take the db
// as an argument.
ToggleForeignKeyConstraint ( ctx context . Context , bun * bun . DB , enable bool ) error
2025-03-06 15:39:45 +05:30
}