2024-03-11 14:15:11 +05:30
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
2025-02-04 14:53:36 +05:30
|
|
|
"context"
|
2024-03-11 14:15:11 +05:30
|
|
|
"os"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2025-03-20 21:01:41 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/factory"
|
|
|
|
|
"github.com/SigNoz/signoz/pkg/factory/factorytest"
|
|
|
|
|
"github.com/SigNoz/signoz/pkg/query-service/app/dashboards"
|
|
|
|
|
"github.com/SigNoz/signoz/pkg/query-service/dao"
|
|
|
|
|
"github.com/SigNoz/signoz/pkg/sqlmigration"
|
|
|
|
|
"github.com/SigNoz/signoz/pkg/sqlmigrator"
|
|
|
|
|
"github.com/SigNoz/signoz/pkg/sqlstore"
|
|
|
|
|
"github.com/SigNoz/signoz/pkg/sqlstore/sqlitesqlstore"
|
2025-01-10 18:43:35 +05:30
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2024-03-11 14:15:11 +05:30
|
|
|
)
|
|
|
|
|
|
2025-02-18 11:08:09 +05:30
|
|
|
func NewTestSqliteDB(t *testing.T) (sqlStore sqlstore.SQLStore, testDBFilePath string) {
|
2024-03-11 14:15:11 +05:30
|
|
|
testDBFile, err := os.CreateTemp("", "test-signoz-db-*")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("could not create temp file for test db: %v", err)
|
|
|
|
|
}
|
2025-01-10 18:43:35 +05:30
|
|
|
testDBFilePath = testDBFile.Name()
|
2024-03-11 14:15:11 +05:30
|
|
|
t.Cleanup(func() { os.Remove(testDBFilePath) })
|
|
|
|
|
testDBFile.Close()
|
|
|
|
|
|
2025-02-19 00:35:53 +05:30
|
|
|
sqlStore, err = sqlitesqlstore.New(context.Background(), factorytest.NewSettings(), sqlstore.Config{Provider: "sqlite", Sqlite: sqlstore.SqliteConfig{Path: testDBFilePath}})
|
2024-03-11 14:15:11 +05:30
|
|
|
if err != nil {
|
2025-02-04 14:53:36 +05:30
|
|
|
t.Fatalf("could not create test db sqlite store: %v", err)
|
2024-03-11 14:15:11 +05:30
|
|
|
}
|
|
|
|
|
|
2025-02-04 14:53:36 +05:30
|
|
|
sqlmigrations, err := sqlmigration.New(
|
|
|
|
|
context.Background(),
|
2025-02-19 00:35:53 +05:30
|
|
|
factorytest.NewSettings(),
|
2025-02-04 14:53:36 +05:30
|
|
|
sqlmigration.Config{},
|
|
|
|
|
factory.MustNewNamedMap(
|
|
|
|
|
sqlmigration.NewAddDataMigrationsFactory(),
|
|
|
|
|
sqlmigration.NewAddOrganizationFactory(),
|
|
|
|
|
sqlmigration.NewAddPreferencesFactory(),
|
|
|
|
|
sqlmigration.NewAddDashboardsFactory(),
|
|
|
|
|
sqlmigration.NewAddSavedViewsFactory(),
|
|
|
|
|
sqlmigration.NewAddAgentsFactory(),
|
|
|
|
|
sqlmigration.NewAddPipelinesFactory(),
|
|
|
|
|
sqlmigration.NewAddIntegrationsFactory(),
|
|
|
|
|
sqlmigration.NewAddLicensesFactory(),
|
|
|
|
|
sqlmigration.NewAddPatsFactory(),
|
2025-03-06 15:39:45 +05:30
|
|
|
sqlmigration.NewModifyDatetimeFactory(),
|
|
|
|
|
sqlmigration.NewModifyOrgDomainFactory(),
|
|
|
|
|
sqlmigration.NewUpdateOrganizationFactory(sqlStore),
|
2025-04-15 21:05:36 +05:30
|
|
|
sqlmigration.NewAddAlertmanagerFactory(sqlStore),
|
2025-03-12 17:18:11 +05:30
|
|
|
sqlmigration.NewUpdateDashboardAndSavedViewsFactory(sqlStore),
|
2025-03-20 13:59:52 +05:30
|
|
|
sqlmigration.NewUpdatePatAndOrgDomainsFactory(sqlStore),
|
2025-03-24 10:17:12 +05:30
|
|
|
sqlmigration.NewUpdatePipelines(sqlStore),
|
2025-04-15 21:05:36 +05:30
|
|
|
sqlmigration.NewDropLicensesSitesFactory(sqlStore),
|
|
|
|
|
sqlmigration.NewUpdateInvitesFactory(sqlStore),
|
|
|
|
|
sqlmigration.NewUpdatePatFactory(sqlStore),
|
2025-04-11 07:36:31 +05:30
|
|
|
sqlmigration.NewAddVirtualFieldsFactory(),
|
2025-04-15 21:05:36 +05:30
|
|
|
sqlmigration.NewUpdateIntegrationsFactory(sqlStore),
|
2025-02-04 14:53:36 +05:30
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("could not create test db sql migrations: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-19 00:35:53 +05:30
|
|
|
err = sqlmigrator.New(context.Background(), factorytest.NewSettings(), sqlStore, sqlmigrations, sqlmigrator.Config{}).Migrate(context.Background())
|
2025-02-04 14:53:36 +05:30
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("could not migrate test db sql migrations: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-18 11:08:09 +05:30
|
|
|
return sqlStore, testDBFilePath
|
2025-01-10 18:43:35 +05:30
|
|
|
}
|
|
|
|
|
|
2025-02-18 11:08:09 +05:30
|
|
|
func NewQueryServiceDBForTests(t *testing.T) sqlstore.SQLStore {
|
|
|
|
|
sqlStore, _ := NewTestSqliteDB(t)
|
2025-01-10 18:43:35 +05:30
|
|
|
|
2025-03-06 15:39:45 +05:30
|
|
|
err := dao.InitDao(sqlStore)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("could not initialize dao: %v", err)
|
|
|
|
|
}
|
2025-03-24 14:54:20 +05:30
|
|
|
_ = dashboards.InitDB(sqlStore)
|
2024-03-11 14:15:11 +05:30
|
|
|
|
2025-02-18 11:08:09 +05:30
|
|
|
return sqlStore
|
2024-03-11 14:15:11 +05:30
|
|
|
}
|