From df2e970ceeb0b4a31d7d816a0aab6a4bc7690e27 Mon Sep 17 00:00:00 2001 From: Nick Zelei <2420177+nickzelei@users.noreply.github.com> Date: Thu, 22 Aug 2024 16:57:25 -0700 Subject: [PATCH] NEOS-1345: split integration test suites (#2504) --- .github/workflows/go.yml | 28 +- worker/pkg/query-builder/integration_test.go | 108 --- worker/pkg/query-builder/query-builder.go | 779 +-------------- .../query-builder_integration_test.go | 891 ------------------ .../pkg/query-builder/query-builder_test.go | 512 +--------- worker/pkg/query-builder/testdata/setup.sql | 450 --------- .../pkg/query-builder/testdata/teardown.sql | 1 - 7 files changed, 30 insertions(+), 2739 deletions(-) delete mode 100644 worker/pkg/query-builder/integration_test.go delete mode 100644 worker/pkg/query-builder/query-builder_integration_test.go delete mode 100644 worker/pkg/query-builder/testdata/setup.sql delete mode 100644 worker/pkg/query-builder/testdata/teardown.sql diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index f9e26cda12..edfab6ab29 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -55,8 +55,8 @@ jobs: - name: Vet run: go vet ./... - test: - name: test + unit-test: + name: Unit Tests runs-on: ubuntu-latest steps: - name: Checkout @@ -70,7 +70,29 @@ jobs: - name: Test run: | - go test -timeout 1800s -v -race -coverprofile=coverage.out -covermode=atomic ./... + go test -race -coverprofile=unit-coverage.out -covermode=atomic ./... + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v4 + env: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} + + integration-tests: + name: Integration Tests + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version-file: go.mod + cache-dependency-path: go.sum + + - name: Run Integration Tests + run: | + go test -race -timeout 1800s -coverprofile=integration-coverage.out -covermode=atomic -run TestIntegrationTestSuite ./... env: INTEGRATION_TESTS_ENABLED: 1 diff --git a/worker/pkg/query-builder/integration_test.go b/worker/pkg/query-builder/integration_test.go deleted file mode 100644 index 2df119f0c1..0000000000 --- a/worker/pkg/query-builder/integration_test.go +++ /dev/null @@ -1,108 +0,0 @@ -package querybuilder - -import ( - "context" - "os" - "time" - - "github.com/jackc/pgx/v5/pgxpool" - pg_queries "github.com/nucleuscloud/neosync/backend/gen/go/db/dbschemas/postgresql" - "github.com/stretchr/testify/suite" - "github.com/testcontainers/testcontainers-go" - testpg "github.com/testcontainers/testcontainers-go/modules/postgres" - "github.com/testcontainers/testcontainers-go/wait" -) - -type IntegrationTestSuite struct { - suite.Suite - - pgpool *pgxpool.Pool - querier pg_queries.Querier - - setupSql string - teardownSql string - - ctx context.Context - - pgcontainer *testpg.PostgresContainer - - schema string -} - -func (s *IntegrationTestSuite) SetupSuite() { - s.ctx = context.Background() - s.schema = "genbenthosconfigs_querybuilder" - - pgcontainer, err := testpg.Run( - s.ctx, - "postgres:15", - testcontainers.WithWaitStrategy( - wait.ForLog("database system is ready to accept connections"). - WithOccurrence(2).WithStartupTimeout(5*time.Second), - ), - ) - if err != nil { - panic(err) - } - s.pgcontainer = pgcontainer - connstr, err := pgcontainer.ConnectionString(s.ctx) - if err != nil { - panic(err) - } - - setupSql, err := os.ReadFile("./testdata/setup.sql") - if err != nil { - panic(err) - } - s.setupSql = string(setupSql) - - teardownSql, err := os.ReadFile("./testdata/teardown.sql") - if err != nil { - panic(err) - } - s.teardownSql = string(teardownSql) - - pool, err := pgxpool.New(s.ctx, connstr) - if err != nil { - panic(err) - } - s.pgpool = pool - s.querier = pg_queries.New() -} - -// Runs before each test -func (s *IntegrationTestSuite) SetupTest() { - _, err := s.pgpool.Exec(s.ctx, s.setupSql) - if err != nil { - panic(err) - } -} - -func (s *IntegrationTestSuite) TearDownTest() { - _, err := s.pgpool.Exec(s.ctx, s.teardownSql) - if err != nil { - panic(err) - } -} - -func (s *IntegrationTestSuite) TearDownSuite() { - if s.pgpool != nil { - s.pgpool.Close() - } - if s.pgcontainer != nil { - err := s.pgcontainer.Terminate(s.ctx) - if err != nil { - panic(err) - } - } -} - -// func TestIntegrationTestSuite(t *testing.T) { -// evkey := "INTEGRATION_TESTS_ENABLED" -// shouldRun := os.Getenv(evkey) -// if shouldRun != "1" { -// slog.Warn(fmt.Sprintf("skipping integration tests, set %s=1 to enable", evkey)) -// return -// } -// suite.Run(t, new(IntegrationTestSuite)) -// } diff --git a/worker/pkg/query-builder/query-builder.go b/worker/pkg/query-builder/query-builder.go index 9b76cde01c..36aeb0635f 100644 --- a/worker/pkg/query-builder/query-builder.go +++ b/worker/pkg/query-builder/query-builder.go @@ -1,18 +1,9 @@ package querybuilder import ( - "crypto/sha256" - "errors" "fmt" - "strings" "github.com/doug-martin/goqu/v9" - sqlmanager_shared "github.com/nucleuscloud/neosync/backend/pkg/sqlmanager/shared" - tabledependency "github.com/nucleuscloud/neosync/backend/pkg/table-dependency" - utils "github.com/nucleuscloud/neosync/backend/pkg/utils" - pg_query "github.com/pganalyze/pg_query_go/v5" - pgquery "github.com/wasilibs/go-pgquery" - "github.com/xwb1989/sqlparser" // import the dialect _ "github.com/doug-martin/goqu/v9/dialect/mysql" @@ -20,20 +11,6 @@ import ( "github.com/doug-martin/goqu/v9/exp" ) -const ( - innerJoin joinType = "INNER" -) - -type joinType string - -type sqlJoin struct { - JoinType joinType - JoinTable string - BaseTable string - Alias *string - JoinColumnsMap map[string]string // map of joinColumn to baseColumn -} - type SubsetReferenceKey struct { Table string Columns []string @@ -70,6 +47,10 @@ func BuildSelectQuery( return formatSqlQuery(sql), nil } +func formatSqlQuery(sql string) string { + return fmt.Sprintf("%s;", sql) +} + func BuildSelectLimitQuery( driver, table string, limit uint, @@ -83,154 +64,6 @@ func BuildSelectLimitQuery( return sql, nil } -func BuildSelectJoinQuery( - driver, table string, - columns []string, - joins []*sqlJoin, - whereClauses []string, -) (string, error) { - builder := goqu.Dialect(driver) - sqltable := goqu.I(table) - - selectColumns := make([]any, len(columns)) - for i, col := range columns { - selectColumns[i] = buildSqlIdentifier(table, col) - } - query := builder.From(sqltable).Select(selectColumns...) - // joins - for _, j := range joins { - if j == nil { - continue - } - joinConditionTable := j.JoinTable - if j.Alias != nil && *j.Alias != "" { - joinConditionTable = *j.Alias - } - joinCondition := goqu.Ex{} - for joinCol, baseCol := range j.JoinColumnsMap { - joinCondition[buildSqlIdentifier(joinConditionTable, joinCol)] = goqu.I(buildSqlIdentifier(j.BaseTable, baseCol)) - } - if j.JoinType == innerJoin { - var joinTable exp.Expression - joinTable = goqu.I(j.JoinTable) - if j.Alias != nil && *j.Alias != "" { - joinTable = goqu.I(j.JoinTable).As(*j.Alias) - } - query = query.InnerJoin( - joinTable, - goqu.On(joinCondition), - ) - } - } - // where - goquWhere := []exp.Expression{} - for _, w := range whereClauses { - goquWhere = append(goquWhere, goqu.L(w)) - } - query = query.Where(goqu.And(goquWhere...)) - - sql, _, err := query.ToSQL() - if err != nil { - return "", err - } - return formatSqlQuery(sql), nil -} - -func BuildSelectRecursiveQuery( - driver, table string, - columns []string, - columnInfoMap map[string]*sqlmanager_shared.ColumnInfo, - dependencies []*selfReferencingCircularDependency, - joins []*sqlJoin, - whereClauses []string, -) (string, error) { - recursiveCteAlias := "related" - var builder goqu.DialectWrapper - if driver == sqlmanager_shared.MysqlDriver { - opts := goqu.DefaultDialectOptions() - opts.QuoteRune = '`' - opts.SupportsWithCTERecursive = true - opts.SupportsWithCTE = true - dialectName := "custom-mysql-dialect" - goqu.RegisterDialect(dialectName, opts) - builder = goqu.Dialect(dialectName) - } else { - builder = goqu.Dialect(driver) - } - - sqltable := goqu.I(table) - - selectColumns := make([]any, len(columns)) - for i, col := range columns { - colInfo := columnInfoMap[col] - if driver == sqlmanager_shared.PostgresDriver && colInfo != nil && colInfo.DataType == "json" { - selectColumns[i] = goqu.L("to_jsonb(?)", goqu.I(buildSqlIdentifier(table, col))).As(col) - } else { - selectColumns[i] = buildSqlIdentifier(table, col) - } - } - selectQuery := builder.From(sqltable).Select(selectColumns...) - - initialSelect := selectQuery - // joins - for _, j := range joins { - if j == nil { - continue - } - joinCondition := goqu.Ex{} - for joinCol, baseCol := range j.JoinColumnsMap { - joinCondition[buildSqlIdentifier(j.JoinTable, joinCol)] = goqu.I(buildSqlIdentifier(j.BaseTable, baseCol)) - } - if j.JoinType == innerJoin { - table := goqu.I(j.JoinTable) - initialSelect = initialSelect.InnerJoin( - table, - goqu.On(joinCondition), - ) - } - } - - // where - goquWhere := []exp.Expression{} - for _, w := range whereClauses { - goquWhere = append(goquWhere, goqu.L(w)) - } - initialSelect = initialSelect.Where(goqu.And(goquWhere...)) - - // inner join on foreign keys - goquOnOrEx := []exp.Expression{} - for _, d := range dependencies { - goquOnAndEx := []exp.Expression{} - for _, fk := range d.ForeignKeyColumns { - if len(fk) > 1 { - for idx, col := range fk { - goquOnAndEx = append(goquOnAndEx, goqu.Ex{buildSqlIdentifier(table, d.PrimaryKeyColumns[idx]): goqu.I(buildSqlIdentifier(recursiveCteAlias, col))}) - } - goquOnOrEx = append(goquOnOrEx, goqu.And(goquOnAndEx...)) - } else { - goquOnOrEx = append(goquOnOrEx, goqu.Ex{buildSqlIdentifier(table, d.PrimaryKeyColumns[0]): goqu.I(buildSqlIdentifier(recursiveCteAlias, fk[0]))}) - } - } - } - - recursiveSelect := selectQuery - recursiveSelect = recursiveSelect.InnerJoin(goqu.I(recursiveCteAlias), goqu.On(goqu.Or(goquOnOrEx...))) - - // union - unionQuery := initialSelect.Union(recursiveSelect) - - selectCols := make([]any, len(columns)) - for i, col := range columns { - selectCols[i] = col - } - recursiveQuery := builder.From(goqu.T(recursiveCteAlias)).WithRecursive(recursiveCteAlias, unionQuery).SelectDistinct(selectCols...) - sql, _, err := recursiveQuery.ToSQL() - if err != nil { - return "", err - } - return formatSqlQuery(sql), nil -} - func BuildInsertQuery( driver, table string, columns []string, @@ -307,607 +140,3 @@ func BuildTruncateQuery( } return query, nil } - -// returns map of schema.table -> select query -func BuildSelectQueryMap( - driver string, - tableDependencies map[string][]*sqlmanager_shared.ForeignConstraint, - runConfigs []*tabledependency.RunConfig, - subsetByForeignKeyConstraints bool, - groupedColumnInfo map[string]map[string]*sqlmanager_shared.ColumnInfo, -) (map[string]map[tabledependency.RunType]string, error) { - insertRunConfigMap := map[string]*tabledependency.RunConfig{} - for _, cfg := range runConfigs { - if cfg.RunType == tabledependency.RunTypeInsert { - insertRunConfigMap[cfg.Table] = cfg - } - } - - // map of table -> where clause - tableWhereMap := map[string]string{} - for table, config := range insertRunConfigMap { - if config.WhereClause != nil && *config.WhereClause != "" { - qualifiedWhere, err := qualifyWhereColumnNames(driver, *config.WhereClause, table) - if err != nil { - return nil, err - } - tableWhereMap[table] = qualifiedWhere - } - } - - if !subsetByForeignKeyConstraints || len(tableDependencies) == 0 || len(tableWhereMap) == 0 { - queryRunTypeMap, err := buildQueryMapNoSubsetConstraints(driver, runConfigs) - if err != nil { - return nil, err - } - return queryRunTypeMap, nil - } - - subsetConfigs, err := buildTableSubsetQueryConfigs(driver, tableDependencies, tableWhereMap, insertRunConfigMap) - if err != nil { - return nil, err - } - - queryRunTypeMap := map[string]map[tabledependency.RunType]string{} - for _, runConfig := range runConfigs { - if _, ok := queryRunTypeMap[runConfig.Table]; !ok { - queryRunTypeMap[runConfig.Table] = map[tabledependency.RunType]string{} - } - columns := runConfig.SelectColumns - subsetConfig := subsetConfigs[runConfig.Table] - columnInfoMap := groupedColumnInfo[runConfig.Table] - sql, err := buildTableQuery(driver, runConfig.Table, columns, subsetConfig, columnInfoMap) - if err != nil { - return nil, err - } - queryRunTypeMap[runConfig.Table][runConfig.RunType] = sql - } - return queryRunTypeMap, nil -} - -func buildTableQuery( - driver, table string, - columns []string, - config *subsetQueryConfig, - columnInfoMap map[string]*sqlmanager_shared.ColumnInfo, -) (string, error) { - if len(config.SelfReferencingCircularDependency) != 0 { - sql, err := BuildSelectRecursiveQuery( - driver, - table, - columns, - columnInfoMap, - config.SelfReferencingCircularDependency, - config.Joins, - config.WhereClauses, - ) - if err != nil { - return "", fmt.Errorf("unable to build recursive select query: %w", err) - } - return sql, err - } else if len(config.Joins) == 0 { - where := strings.Join(config.WhereClauses, " AND ") - sql, err := BuildSelectQuery(driver, table, columns, &where) - if err != nil { - return "", fmt.Errorf("unable to build select query: %w", err) - } - return sql, nil - } else { - sql, err := BuildSelectJoinQuery(driver, table, columns, config.Joins, config.WhereClauses) - if err != nil { - return "", fmt.Errorf("unable to build select query with joins: %w", err) - } - return sql, nil - } -} - -func buildQueryMapNoSubsetConstraints( - driver string, - runConfigs []*tabledependency.RunConfig, -) (map[string]map[tabledependency.RunType]string, error) { - queryRunTypeMap := map[string]map[tabledependency.RunType]string{} - for _, config := range runConfigs { - if _, ok := queryRunTypeMap[config.Table]; !ok { - queryRunTypeMap[config.Table] = map[tabledependency.RunType]string{} - } - query, err := BuildSelectQuery(driver, config.Table, config.SelectColumns, config.WhereClause) - if err != nil { - return nil, fmt.Errorf("unable to build select query: %w", err) - } - queryRunTypeMap[config.Table][config.RunType] = query - } - return queryRunTypeMap, nil -} - -type tableSubset struct { - Joins []*sqlJoin - WhereClauses []string -} - -// recusively builds join for subset table -func buildSubsetJoins(table string, data map[string][]*SubsetColumnConstraint, whereClauses map[string]string, visited map[string]bool) *tableSubset { - joins := []*sqlJoin{} - wheres := []string{} - - if seen := visited[table]; seen { - return &tableSubset{ - Joins: joins, - WhereClauses: wheres, - } - } - visited[table] = true - - if condition, exists := whereClauses[table]; exists { - wheres = append(wheres, condition) - } - - if columns, exists := data[table]; exists { - for _, col := range columns { - if col.ForeignKey.Table == "" && col.ForeignKey.Columns == nil { - continue - } - if !visited[col.ForeignKey.Table] { - // handle aliased table - var alias *string - joinTable := col.ForeignKey.Table - if col.ForeignKey.OriginalTable != nil && *col.ForeignKey.OriginalTable != "" { - alias = &col.ForeignKey.Table - joinTable = *col.ForeignKey.OriginalTable - } - - joinColMap := map[string]string{} - for idx, c := range col.ForeignKey.Columns { - joinColMap[c] = col.Columns[idx] - } - joins = append(joins, &sqlJoin{ - JoinType: innerJoin, - JoinTable: joinTable, - BaseTable: table, - Alias: alias, - JoinColumnsMap: joinColMap, - }) - - sub := buildSubsetJoins(col.ForeignKey.Table, data, whereClauses, visited) - joins = append(joins, sub.Joins...) - wheres = append(wheres, sub.WhereClauses...) - } - } - } - return &tableSubset{ - Joins: joins, - WhereClauses: wheres, - } -} - -type subsetQueryConfig struct { - Joins []*sqlJoin - WhereClauses []string - SelfReferencingCircularDependency []*selfReferencingCircularDependency -} - -func buildTableSubsetQueryConfigs(driver string, tableConstraints map[string][]*sqlmanager_shared.ForeignConstraint, whereClauses map[string]string, runConfigMap map[string]*tabledependency.RunConfig) (map[string]*subsetQueryConfig, error) { - configs := map[string]*subsetQueryConfig{} - - filteredConstraints := filterForeignKeysWithSubset(runConfigMap, tableConstraints, whereClauses) - subset, err := buildAliasReferences(driver, filteredConstraints, whereClauses) - if err != nil { - return nil, err - } - - for table := range subset.ColumnConstraints { - visited := map[string]bool{} - tableSubset := buildSubsetJoins(table, subset.ColumnConstraints, subset.WhereClauses, visited) - constraints := tableConstraints[table] - selfRefCd := getSelfReferencingColumns(table, constraints) - configs[table] = &subsetQueryConfig{ - Joins: tableSubset.Joins, - WhereClauses: tableSubset.WhereClauses, - SelfReferencingCircularDependency: selfRefCd, - } - } - return configs, nil -} - -type subsetConstraints struct { - ColumnConstraints map[string][]*SubsetColumnConstraint - WhereClauses map[string]string -} - -func buildAliasReferences(driver string, constraints map[string][]*sqlmanager_shared.ForeignConstraint, whereClauses map[string]string) (*subsetConstraints, error) { - updatedConstraints := map[string][]*SubsetColumnConstraint{} - aliasReference := map[string]string{} // alias name to table name - updatedWheres := map[string]string{} - - for table, where := range whereClauses { - updatedWheres[table] = where - } - - for table, colDefs := range constraints { - if len(colDefs) == 0 { - updatedConstraints[table] = []*SubsetColumnConstraint{} - } else { - updatedConstraints[table] = processAliasConstraints(table, colDefs, updatedConstraints, aliasReference) - } - } - - if err := updateAliasReferences(driver, updatedConstraints, aliasReference, updatedWheres); err != nil { - return nil, err - } - - return &subsetConstraints{ - ColumnConstraints: updatedConstraints, - WhereClauses: updatedWheres, - }, nil -} - -// creates alias table reference if there is a double reference -func processAliasConstraints( - table string, - colDefs []*sqlmanager_shared.ForeignConstraint, - updatedConstraints map[string][]*SubsetColumnConstraint, - aliasReference map[string]string, - // seenTables map[string]struct{}, -) []*SubsetColumnConstraint { - if _, exists := updatedConstraints[table]; exists { - return updatedConstraints[table] - } - - tableCount := map[string]int{} - for _, colDef := range colDefs { - if colDef.ForeignKey != nil { - tableCount[colDef.ForeignKey.Table]++ - } - } - - newColDefs := []*SubsetColumnConstraint{} - for _, colDef := range colDefs { - if colDef.ForeignKey.Table == table { - continue // self reference skip - } - - if count := tableCount[colDef.ForeignKey.Table]; count > 1 { - // create aliased table - newTable := fmt.Sprintf("%s_%s", strings.ReplaceAll(colDef.ForeignKey.Table, ".", "_"), strings.Join(colDef.Columns, "_")) - alias := aliasHash(newTable) - aliasReference[alias] = colDef.ForeignKey.Table - newColDefs = append(newColDefs, &SubsetColumnConstraint{ - Columns: colDef.Columns, - ForeignKey: &SubsetReferenceKey{ - Table: alias, - OriginalTable: &colDef.ForeignKey.Table, - Columns: colDef.ForeignKey.Columns, - }, - }) - } else { - newColDefs = append(newColDefs, &SubsetColumnConstraint{ - Columns: colDef.Columns, - ForeignKey: &SubsetReferenceKey{ - Table: colDef.ForeignKey.Table, - Columns: colDef.ForeignKey.Columns, - }, - }) - } - } - - updatedConstraints[table] = newColDefs - return newColDefs -} - -// follows constraints and updates references to alias tables that were created -func updateAliasReferences( - driver string, - updatedConstraints map[string][]*SubsetColumnConstraint, - aliasReference map[string]string, - updatedWheres map[string]string, -) error { - for alias, table := range aliasReference { - if _, exists := updatedConstraints[alias]; exists { - continue - } - - colDefs := updatedConstraints[table] - newColDefs := []*SubsetColumnConstraint{} - - for _, c := range colDefs { - newAlias := aliasHash(fmt.Sprintf("%s_%s", alias, strings.ReplaceAll(c.ForeignKey.Table, ".", "_"))) - aliasReference[newAlias] = c.ForeignKey.Table - newColDefs = append(newColDefs, &SubsetColumnConstraint{ - Columns: c.Columns, - ForeignKey: &SubsetReferenceKey{ - Columns: c.ForeignKey.Columns, - OriginalTable: &c.ForeignKey.Table, - Table: newAlias, - }, - }) - } - - updatedConstraints[alias] = newColDefs - - where := updatedWheres[table] - if where != "" { - aliasedWhere, err := qualifyWhereWithTableAlias(driver, where, alias) - if err != nil { - return err - } - updatedWheres[alias] = aliasedWhere - } - - delete(aliasReference, alias) - if err := updateAliasReferences(driver, updatedConstraints, aliasReference, updatedWheres); err != nil { - return err - } - } - return nil -} - -// check if table or any parent table has a where clause -func shouldSubsetTable(table string, data map[string][]*sqlmanager_shared.ForeignConstraint, whereClauses map[string]string, visited map[string]bool) bool { - if _, exists := whereClauses[table]; exists { - return true - } - if visited[table] { - return false - } - visited[table] = true - - if columns, exists := data[table]; exists { - for _, col := range columns { - if col.ForeignKey.Table != "" { - if shouldSubsetTable(col.ForeignKey.Table, data, whereClauses, visited) { - return true - } - } - } - } - return false -} - -// filters out any foreign keys that are not involved in the subset (where clauses) and tables not in the map -func filterForeignKeysWithSubset(runConfigMap map[string]*tabledependency.RunConfig, constraints map[string][]*sqlmanager_shared.ForeignConstraint, whereClauses map[string]string) map[string][]*sqlmanager_shared.ForeignConstraint { - tableSubsetMap := map[string]bool{} // map of which tables to subset - for table := range runConfigMap { - visited := map[string]bool{} - tableSubsetMap[table] = shouldSubsetTable(table, constraints, whereClauses, visited) - } - - filteredConstraints := map[string][]*sqlmanager_shared.ForeignConstraint{} - for table := range runConfigMap { - filteredConstraints[table] = []*sqlmanager_shared.ForeignConstraint{} - if tableConstraints, ok := constraints[table]; ok { - for _, colDef := range tableConstraints { - if exists := tableSubsetMap[colDef.ForeignKey.Table]; exists { - filteredConstraints[table] = append(filteredConstraints[table], colDef) - } - } - } - } - return filteredConstraints -} - -func aliasHash(input string) string { - hash := fmt.Sprintf("%x", sha256.Sum256([]byte(input))) - if len(hash) > 14 { - hash = hash[:14] - } - return hash -} - -func qualifyWhereWithTableAlias(driver, where, alias string) (string, error) { - query := goqu.Dialect(driver).From(goqu.T(alias)).Select("*").Where(goqu.L(where)) - sql, _, err := query.ToSQL() - if err != nil { - return "", err - } - var updatedSql string - switch driver { - case sqlmanager_shared.MysqlDriver: - sql, err := qualifyMysqlWhereColumnNames(sql, nil, alias) - if err != nil { - return "", err - } - updatedSql = sql - case sqlmanager_shared.PostgresDriver: - sql, err := qualifyPostgresWhereColumnNames(sql, nil, alias) - if err != nil { - return "", err - } - updatedSql = sql - default: - return "", errors.New("unsupported sql driver type") - } - index := strings.Index(strings.ToLower(updatedSql), "where") - if index == -1 { - // "where" not found - return "", fmt.Errorf("unable to qualify where column names") - } - startIndex := index + len("where") - return strings.TrimSpace(updatedSql[startIndex:]), nil -} - -func qualifyWhereColumnNames(driver, where, table string) (string, error) { - sql, err := BuildSelectQuery(driver, table, []string{"*"}, &where) - if err != nil { - return "", err - } - schema, tableName := sqlmanager_shared.SplitTableKey(table) - var updatedSql string - switch driver { - case sqlmanager_shared.MysqlDriver: - sql, err := qualifyMysqlWhereColumnNames(sql, &schema, tableName) - if err != nil { - return "", err - } - updatedSql = sql - case sqlmanager_shared.PostgresDriver: - sql, err := qualifyPostgresWhereColumnNames(sql, &schema, tableName) - if err != nil { - return "", err - } - updatedSql = sql - default: - return "", errors.New("unsupported sql driver type") - } - index := strings.Index(strings.ToLower(updatedSql), "where") - if index == -1 { - // "where" not found - return "", fmt.Errorf("unable to qualify where column names") - } - startIndex := index + len("where") - - return strings.TrimSpace(updatedSql[startIndex:]), nil -} - -func getPrimaryToForeignTableMapFromRunConfigs(runConfigs []*tabledependency.RunConfig) map[string][]string { - dpMap := map[string]map[string]struct{}{} - - for _, cfg := range runConfigs { - if _, exists := dpMap[cfg.Table]; !exists { - dpMap[cfg.Table] = map[string]struct{}{} - } - for _, dep := range cfg.DependsOn { - if _, exists := dpMap[dep.Table]; !exists { - dpMap[dep.Table] = map[string]struct{}{} - } - dpMap[dep.Table][cfg.Table] = struct{}{} - } - } - tableDependencyMap := map[string][]string{} - for table, fkTables := range dpMap { - for t := range fkTables { - tableDependencyMap[table] = append(tableDependencyMap[table], t) - } - } - - return tableDependencyMap -} - -type selfReferencingCircularDependency struct { - PrimaryKeyColumns []string - ForeignKeyColumns [][]string -} - -func getSelfReferencingColumns(table string, constraints []*sqlmanager_shared.ForeignConstraint) []*selfReferencingCircularDependency { - result := []*selfReferencingCircularDependency{} - resultMap := map[string]*selfReferencingCircularDependency{} - - for _, c := range constraints { - if c.ForeignKey.Table != table { - continue - } - pkHash := utils.ToSha256(fmt.Sprintf("%s%s", c.ForeignKey.Table, strings.Join(c.ForeignKey.Columns, ""))) - if _, exists := resultMap[pkHash]; !exists { - resultMap[pkHash] = &selfReferencingCircularDependency{ - PrimaryKeyColumns: c.ForeignKey.Columns, - ForeignKeyColumns: [][]string{}, - } - } - resultMap[pkHash].ForeignKeyColumns = append(resultMap[pkHash].ForeignKeyColumns, c.Columns) - } - - for _, r := range resultMap { - result = append(result, r) - } - - return result -} - -func formatSqlQuery(sql string) string { - return fmt.Sprintf("%s;", sql) -} - -func buildSqlIdentifier(identifiers ...string) string { - return strings.Join(identifiers, ".") -} - -func qualifyPostgresWhereColumnNames(sql string, schema *string, table string) (string, error) { - tree, err := pgquery.Parse(sql) - if err != nil { - return "", err - } - - for _, stmt := range tree.GetStmts() { - selectStmt := stmt.GetStmt().GetSelectStmt() - - if selectStmt.WhereClause != nil { - updatePostgresExpr(schema, table, selectStmt.WhereClause) - } - } - updatedSql, err := pgquery.Deparse(tree) - if err != nil { - return "", err - } - return updatedSql, nil -} - -func updatePostgresExpr(schema *string, table string, node *pg_query.Node) { - switch expr := node.Node.(type) { - case *pg_query.Node_SubLink: - updatePostgresExpr(schema, table, node.GetSubLink().GetTestexpr()) - case *pg_query.Node_BoolExpr: - for _, arg := range expr.BoolExpr.GetArgs() { - updatePostgresExpr(schema, table, arg) - } - case *pg_query.Node_AExpr: - updatePostgresExpr(schema, table, expr.AExpr.GetLexpr()) - updatePostgresExpr(schema, table, expr.AExpr.Rexpr) - case *pg_query.Node_ColumnDef: - case *pg_query.Node_ColumnRef: - col := node.GetColumnRef() - isQualified := false - var colName *string - // find col name and check if already has schema + table name - for _, f := range col.Fields { - val := f.GetString_().GetSval() - if schema != nil && val == *schema { - continue - } - if val == table { - isQualified = true - break - } - colName = &val - } - if !isQualified && colName != nil && *colName != "" { - fields := []*pg_query.Node{} - if schema != nil && *schema != "" { - fields = append(fields, pg_query.MakeStrNode(*schema)) - } - fields = append(fields, []*pg_query.Node{ - pg_query.MakeStrNode(table), - pg_query.MakeStrNode(*colName), - }...) - col.Fields = fields - } - } -} - -func qualifyMysqlWhereColumnNames(sql string, schema *string, table string) (string, error) { - stmt, err := sqlparser.Parse(sql) - if err != nil { - return "", err - } - - switch stmt := stmt.(type) { //nolint:gocritic - case *sqlparser.Select: - err = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { - switch node := node.(type) { //nolint:gocritic - case *sqlparser.ComparisonExpr: - if col, ok := node.Left.(*sqlparser.ColName); ok { - s := "" - if schema != nil && *schema != "" { - s = *schema - } - col.Qualifier.Qualifier = sqlparser.NewTableIdent(s) - col.Qualifier.Name = sqlparser.NewTableIdent(table) - } - return false, nil - } - return true, nil - }, stmt) - if err != nil { - return "", err - } - } - - return sqlparser.String(stmt), nil -} diff --git a/worker/pkg/query-builder/query-builder_integration_test.go b/worker/pkg/query-builder/query-builder_integration_test.go deleted file mode 100644 index f7fdc09cff..0000000000 --- a/worker/pkg/query-builder/query-builder_integration_test.go +++ /dev/null @@ -1,891 +0,0 @@ -package querybuilder - -// import ( -// "fmt" - -// sqlmanager_shared "github.com/nucleuscloud/neosync/backend/pkg/sqlmanager/shared" -// tabledependency "github.com/nucleuscloud/neosync/backend/pkg/table-dependency" -// "github.com/stretchr/testify/require" -// ) - -// func (s *IntegrationTestSuite) Test_BuildQueryMap_DoubleReference() { -// whereId := "id = 1" -// tableDependencies := map[string][]*sqlmanager_shared.ForeignConstraint{ -// "genbenthosconfigs_querybuilder.department": { -// {Columns: []string{"company_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.company", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.transaction": { -// {Columns: []string{"department_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.department", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.expense_report": { -// {Columns: []string{"department_source_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.department", Columns: []string{"id"}}}, -// {Columns: []string{"department_destination_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.department", Columns: []string{"id"}}}, -// {Columns: []string{"transaction_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.transaction", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.expense": { -// {Columns: []string{"report_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.expense_report", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.item": { -// {Columns: []string{"expense_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.expense", Columns: []string{"id"}}}, -// }, -// } -// dependencyConfigs := []*tabledependency.RunConfig{ -// {Table: "genbenthosconfigs_querybuilder.company", RunType: tabledependency.RunTypeInsert, SelectColumns: []string{"id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{}, WhereClause: &whereId}, -// {Table: "genbenthosconfigs_querybuilder.department", RunType: tabledependency.RunTypeInsert, SelectColumns: []string{"id", "company_id"}, InsertColumns: []string{"id", "company_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.company", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.transaction", RunType: tabledependency.RunTypeInsert, SelectColumns: []string{"id", "department_id"}, InsertColumns: []string{"id", "department_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.department", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.expense_report", RunType: tabledependency.RunTypeInsert, SelectColumns: []string{"id", "department_source_id", "department_destination_id", "transaction_id"}, InsertColumns: []string{"id", "department_source_id", "department_destination_id", "transaction_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.department", Columns: []string{"id"}}, {Table: "genbenthosconfigs_querybuilder.transaction", Columns: []string{"id"}}}}, -// } - -// expectedValues := map[string]map[string][]int64{ -// "genbenthosconfigs_querybuilder.company": { -// "id": {1}, -// }, -// "genbenthosconfigs_querybuilder.department": { -// "company_id": {1}, -// }, -// "genbenthosconfigs_querybuilder.expense_report": { -// "department_source_id": {1, 2}, -// "department_destination_id": {1, 2}, -// }, -// "genbenthosconfigs_querybuilder.transaction": { -// "department_id": {1, 2}, -// }, -// } - -// expectedCount := map[string]int{ -// "genbenthosconfigs_querybuilder.company": 1, -// "genbenthosconfigs_querybuilder.department": 2, -// "genbenthosconfigs_querybuilder.expense_report": 2, -// "genbenthosconfigs_querybuilder.transaction": 2, -// } - -// sqlMap, err := BuildSelectQueryMap(sqlmanager_shared.PostgresDriver, tableDependencies, dependencyConfigs, true, map[string]map[string]*sqlmanager_shared.ColumnInfo{}) -// require.NoError(s.T(), err) -// require.Equal(s.T(), len(expectedValues), len(sqlMap)) -// for table, selectQueryRunType := range sqlMap { -// sql := selectQueryRunType[tabledependency.RunTypeInsert] -// require.NotEmpty(s.T(), sql) -// rows, err := s.pgpool.Query(s.ctx, sql) -// require.NoError(s.T(), err) - -// columnDescriptions := rows.FieldDescriptions() - -// tableExpectedValues, ok := expectedValues[table] -// require.True(s.T(), ok) - -// rowCount := 0 -// for rows.Next() { -// rowCount++ -// values, err := rows.Values() -// require.NoError(s.T(), err) - -// for i, col := range values { -// colName := columnDescriptions[i].Name - -// allowedValues, ok := tableExpectedValues[colName] -// if ok { -// value := col.(int64) -// require.Containsf(s.T(), allowedValues, value, fmt.Sprintf("table: %s column: %s ", table, colName)) -// } -// } -// } -// rows.Close() -// require.Equalf(s.T(), rowCount, expectedCount[table], fmt.Sprintf("table: %s ", table)) -// } -// } - -// func (s *IntegrationTestSuite) Test_BuildQueryMap_DoubleReference_Complex() { -// whereId := "id = 1" -// tableDependencies := map[string][]*sqlmanager_shared.ForeignConstraint{ -// "genbenthosconfigs_querybuilder.department": { -// {Columns: []string{"company_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.company", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.transaction": { -// {Columns: []string{"department_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.department", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.expense_report": { -// {Columns: []string{"department_source_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.department", Columns: []string{"id"}}}, -// {Columns: []string{"department_destination_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.department", Columns: []string{"id"}}}, -// {Columns: []string{"transaction_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.transaction", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.expense": { -// {Columns: []string{"report_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.expense_report", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.item": { -// {Columns: []string{"expense_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.expense", Columns: []string{"id"}}}, -// }, -// } -// dependencyConfigs := []*tabledependency.RunConfig{ -// {Table: "genbenthosconfigs_querybuilder.company", RunType: tabledependency.RunTypeInsert, SelectColumns: []string{"id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{}, WhereClause: &whereId}, -// {Table: "genbenthosconfigs_querybuilder.department", RunType: tabledependency.RunTypeInsert, SelectColumns: []string{"id", "company_id"}, InsertColumns: []string{"id", "company_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.company", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.transaction", RunType: tabledependency.RunTypeInsert, SelectColumns: []string{"id", "department_id"}, InsertColumns: []string{"id", "department_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.department", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.expense_report", RunType: tabledependency.RunTypeInsert, SelectColumns: []string{"id", "department_source_id", "department_destination_id", "transaction_id"}, InsertColumns: []string{"id", "department_source_id", "department_destination_id", "transaction_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.department", Columns: []string{"id"}}, {Table: "genbenthosconfigs_querybuilder.transaction", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.expense", RunType: tabledependency.RunTypeInsert, SelectColumns: []string{"id", "report_id"}, InsertColumns: []string{"id", "report_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.expense_report", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.item", RunType: tabledependency.RunTypeInsert, SelectColumns: []string{"id", "expense_id"}, InsertColumns: []string{"id", "expense_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.expense", Columns: []string{"id"}}}}, -// } - -// expectedValues := map[string]map[string][]int64{ -// "genbenthosconfigs_querybuilder.company": { -// "id": {1}, -// }, -// "genbenthosconfigs_querybuilder.department": { -// "company_id": {1}, -// }, -// "genbenthosconfigs_querybuilder.expense_report": { -// "department_source_id": {1, 2}, -// "department_destination_id": {1, 2}, -// }, -// "genbenthosconfigs_querybuilder.transaction": { -// "department_id": {1, 2}, -// }, -// "genbenthosconfigs_querybuilder.expense": { -// "report_id": {3, 1}, -// }, -// "genbenthosconfigs_querybuilder.item": { -// "expense_id": {3, 2}, -// }, -// } - -// expectedCount := map[string]int{ -// "genbenthosconfigs_querybuilder.company": 1, -// "genbenthosconfigs_querybuilder.department": 2, -// "genbenthosconfigs_querybuilder.expense_report": 2, -// "genbenthosconfigs_querybuilder.transaction": 2, -// "genbenthosconfigs_querybuilder.expense": 2, -// "genbenthosconfigs_querybuilder.item": 2, -// } - -// sqlMap, err := BuildSelectQueryMap(sqlmanager_shared.PostgresDriver, tableDependencies, dependencyConfigs, true, map[string]map[string]*sqlmanager_shared.ColumnInfo{}) -// require.NoError(s.T(), err) -// require.Equal(s.T(), len(expectedValues), len(sqlMap)) -// for table, selectQueryRunType := range sqlMap { -// sql := selectQueryRunType[tabledependency.RunTypeInsert] -// require.NotEmpty(s.T(), sql) -// rows, err := s.pgpool.Query(s.ctx, sql) -// require.NoError(s.T(), err) - -// columnDescriptions := rows.FieldDescriptions() - -// tableExpectedValues, ok := expectedValues[table] -// require.True(s.T(), ok) - -// rowCount := 0 -// for rows.Next() { -// rowCount++ -// values, err := rows.Values() -// require.NoError(s.T(), err) - -// for i, col := range values { -// colName := columnDescriptions[i].Name -// allowedValues, ok := tableExpectedValues[colName] -// if ok { -// value := col.(int64) -// require.Containsf(s.T(), allowedValues, value, fmt.Sprintf("table: %s column: %s ", table, colName)) -// } -// } -// } -// rows.Close() -// require.Equalf(s.T(), rowCount, expectedCount[table], fmt.Sprintf("table: %s ", table)) -// } -// } - -// func (s *IntegrationTestSuite) Test_BuildQueryMap_DoubleRootSubset() { -// whereCreated := "created > '2023-06-03'" -// tableDependencies := map[string][]*sqlmanager_shared.ForeignConstraint{ -// "genbenthosconfigs_querybuilder.test_2_c": { -// {Columns: []string{"a_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.test_2_a", Columns: []string{"id"}}}, -// {Columns: []string{"b_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.test_2_b", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.test_2_d": { -// {Columns: []string{"c_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.test_2_c", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.test_2_e": { -// {Columns: []string{"c_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.test_2_c", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.test_2_a": { -// {Columns: []string{"x_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.test_2_x", Columns: []string{"id"}}}, -// }, -// } -// dependencyConfigs := []*tabledependency.RunConfig{ -// {Table: "genbenthosconfigs_querybuilder.test_2_x", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{}, WhereClause: &whereCreated}, -// {Table: "genbenthosconfigs_querybuilder.test_2_b", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{}, WhereClause: &whereCreated}, -// {Table: "genbenthosconfigs_querybuilder.test_2_a", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "x_id"}, InsertColumns: []string{"id", "x_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.test_2_x", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.test_2_c", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "a_id", "b_id"}, InsertColumns: []string{"id", "a_id", "b_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.test_2_a", Columns: []string{"id"}}, {Table: "genbenthosconfigs_querybuilder.test_2_b", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.test_2_d", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "c_id"}, InsertColumns: []string{"id", "c_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.test_2_c", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.test_2_e", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "c_id"}, InsertColumns: []string{"id", "c_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.test_2_c", Columns: []string{"id"}}}}, -// } - -// expectedValues := map[string]map[string][]int64{ -// "genbenthosconfigs_querybuilder.test_2_x": { -// "id": {3, 4, 5}, -// }, -// "genbenthosconfigs_querybuilder.test_2_b": { -// "id": {3, 4, 5}, -// }, -// "genbenthosconfigs_querybuilder.test_2_a": { -// "x_id": {3, 4, 5}, -// }, -// "genbenthosconfigs_querybuilder.test_2_c": { -// "a_id": {3, 4}, -// "x_id": {3, 4}, -// }, -// "genbenthosconfigs_querybuilder.test_2_d": { -// "c_id": {3, 4}, -// }, -// "genbenthosconfigs_querybuilder.test_2_e": { -// "c_id": {3, 4}, -// }, -// } - -// expectedCount := map[string]int{ -// "genbenthosconfigs_querybuilder.test_2_x": 3, -// "genbenthosconfigs_querybuilder.test_2_b": 3, -// "genbenthosconfigs_querybuilder.test_2_a": 4, -// "genbenthosconfigs_querybuilder.test_2_c": 2, -// "genbenthosconfigs_querybuilder.test_2_d": 2, -// "genbenthosconfigs_querybuilder.test_2_e": 2, -// } - -// sqlMap, err := BuildSelectQueryMap(sqlmanager_shared.PostgresDriver, tableDependencies, dependencyConfigs, true, map[string]map[string]*sqlmanager_shared.ColumnInfo{}) -// require.NoError(s.T(), err) -// require.Equal(s.T(), len(expectedValues), len(sqlMap)) -// for table, selectQueryRunType := range sqlMap { -// sql := selectQueryRunType[tabledependency.RunTypeInsert] -// require.NotEmpty(s.T(), sql) - -// rows, err := s.pgpool.Query(s.ctx, sql) -// require.NoError(s.T(), err) - -// columnDescriptions := rows.FieldDescriptions() - -// tableExpectedValues, ok := expectedValues[table] -// require.Truef(s.T(), ok, fmt.Sprintf("table: %s missing expected values", table)) - -// rowCount := 0 -// for rows.Next() { -// rowCount++ -// values, err := rows.Values() -// require.NoError(s.T(), err) - -// for i, col := range values { -// colName := columnDescriptions[i].Name -// allowedValues, ok := tableExpectedValues[colName] -// if ok { -// value := col.(int64) -// require.Containsf(s.T(), allowedValues, value, fmt.Sprintf("table: %s column: %s ", table, colName)) -// } -// } -// } -// rows.Close() -// require.Equalf(s.T(), rowCount, expectedCount[table], fmt.Sprintf("table: %s ", table)) -// } -// } - -// func (s *IntegrationTestSuite) Test_BuildQueryMap_MultipleRoots() { -// whereId := "id = 1" -// whereId4 := "id in (4,5)" -// tableDependencies := map[string][]*sqlmanager_shared.ForeignConstraint{ -// "genbenthosconfigs_querybuilder.test_3_b": { -// {Columns: []string{"a_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.test_3_a", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.test_3_c": { -// {Columns: []string{"b_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.test_3_b", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.test_3_d": { -// {Columns: []string{"c_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.test_3_c", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.test_3_e": { -// {Columns: []string{"d_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.test_3_d", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.test_3_g": { -// {Columns: []string{"f_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.test_3_f", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.test_3_h": { -// {Columns: []string{"g_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.test_3_g", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.test_3_i": { -// {Columns: []string{"h_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.test_3_h", Columns: []string{"id"}}}, -// }, -// } -// dependencyConfigs := []*tabledependency.RunConfig{ -// {Table: "genbenthosconfigs_querybuilder.test_3_a", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{}}, -// {Table: "genbenthosconfigs_querybuilder.test_3_b", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "a_id"}, InsertColumns: []string{"id", "a_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.test_3_a", Columns: []string{"id"}}}, WhereClause: &whereId}, -// {Table: "genbenthosconfigs_querybuilder.test_3_c", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "b_id"}, InsertColumns: []string{"id", "b_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.test_3_b", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.test_3_d", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "c_id"}, InsertColumns: []string{"id", "c_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.test_3_c", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.test_3_e", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "d_id"}, InsertColumns: []string{"id", "d_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.test_3_d", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.test_3_f", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{}, WhereClause: &whereId4}, -// {Table: "genbenthosconfigs_querybuilder.test_3_g", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "f_id"}, InsertColumns: []string{"id", "f_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.test_3_f", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.test_3_h", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "g_id"}, InsertColumns: []string{"id", "g_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.test_3_g", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.test_3_i", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "h_id"}, InsertColumns: []string{"id", "h_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.test_3_h", Columns: []string{"id"}}}}, -// } - -// expectedValues := map[string]map[string][]int64{ -// "genbenthosconfigs_querybuilder.test_3_a": { -// "id": {1, 2, 3, 4, 5}, -// }, -// "genbenthosconfigs_querybuilder.test_3_b": { -// "a_id": {3}, -// }, -// "genbenthosconfigs_querybuilder.test_3_c": { -// "b_id": {1}, -// }, -// "genbenthosconfigs_querybuilder.test_3_d": { -// "c_id": {3}, -// }, -// "genbenthosconfigs_querybuilder.test_3_e": { -// "d_id": {5}, -// }, -// "genbenthosconfigs_querybuilder.test_3_f": { -// "id": {4, 5}, -// }, -// "genbenthosconfigs_querybuilder.test_3_g": { -// "f_id": {4, 5}, -// }, -// "genbenthosconfigs_querybuilder.test_3_h": { -// "g_id": {1, 3}, -// }, -// "genbenthosconfigs_querybuilder.test_3_i": { -// "h_id": {4}, -// }, -// } - -// expectedCount := map[string]int{ -// "genbenthosconfigs_querybuilder.test_3_a": 5, -// "genbenthosconfigs_querybuilder.test_3_b": 1, -// "genbenthosconfigs_querybuilder.test_3_c": 1, -// "genbenthosconfigs_querybuilder.test_3_d": 1, -// "genbenthosconfigs_querybuilder.test_3_e": 1, -// "genbenthosconfigs_querybuilder.test_3_f": 2, -// "genbenthosconfigs_querybuilder.test_3_g": 2, -// "genbenthosconfigs_querybuilder.test_3_h": 2, -// "genbenthosconfigs_querybuilder.test_3_i": 1, -// } - -// sqlMap, err := BuildSelectQueryMap(sqlmanager_shared.PostgresDriver, tableDependencies, dependencyConfigs, true, map[string]map[string]*sqlmanager_shared.ColumnInfo{}) -// require.NoError(s.T(), err) -// require.Equal(s.T(), len(expectedValues), len(sqlMap)) -// for table, selectQueryRunType := range sqlMap { -// sql := selectQueryRunType[tabledependency.RunTypeInsert] -// require.NotEmpty(s.T(), sql) - -// rows, err := s.pgpool.Query(s.ctx, sql) -// require.NoError(s.T(), err) - -// columnDescriptions := rows.FieldDescriptions() -// tableExpectedValues, ok := expectedValues[table] -// require.Truef(s.T(), ok, fmt.Sprintf("table: %s missing expected values", table)) - -// rowCount := 0 -// for rows.Next() { -// rowCount++ -// values, err := rows.Values() -// require.NoError(s.T(), err) - -// for i, col := range values { -// colName := columnDescriptions[i].Name - -// allowedValues, ok := tableExpectedValues[colName] -// if ok { -// value := col.(int64) -// require.Containsf(s.T(), allowedValues, value, fmt.Sprintf("table: %s column: %s ", table, colName)) -// } -// } -// } -// rows.Close() -// require.Equalf(s.T(), rowCount, expectedCount[table], fmt.Sprintf("table: %s ", table)) -// } -// } - -// func (s *IntegrationTestSuite) Test_BuildQueryMap_MultipleSubsets() { -// whereId := "id in (3,4,5)" -// whereId4 := "id = 4" -// tableDependencies := map[string][]*sqlmanager_shared.ForeignConstraint{ -// "genbenthosconfigs_querybuilder.test_3_b": { -// {Columns: []string{"a_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.test_3_a", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.test_3_c": { -// {Columns: []string{"b_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.test_3_b", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.test_3_d": { -// {Columns: []string{"c_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.test_3_c", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.test_3_e": { -// {Columns: []string{"d_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.test_3_d", Columns: []string{"id"}}}, -// }, -// } -// dependencyConfigs := []*tabledependency.RunConfig{ -// {Table: "genbenthosconfigs_querybuilder.test_3_a", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{}, WhereClause: &whereId}, -// {Table: "genbenthosconfigs_querybuilder.test_3_b", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "a_id"}, InsertColumns: []string{"id", "a_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.test_3_a", Columns: []string{"id"}}}, WhereClause: &whereId4}, -// {Table: "genbenthosconfigs_querybuilder.test_3_c", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "b_id"}, InsertColumns: []string{"id", "b_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.test_3_b", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.test_3_d", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "c_id"}, InsertColumns: []string{"id", "c_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.test_3_c", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.test_3_e", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "d_id"}, InsertColumns: []string{"id", "d_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.test_3_d", Columns: []string{"id"}}}}, -// } - -// expectedValues := map[string]map[string][]int64{ -// "genbenthosconfigs_querybuilder.test_3_a": { -// "id": {3, 4, 5}, -// }, -// "genbenthosconfigs_querybuilder.test_3_b": { -// "a_id": {4}, -// }, -// "genbenthosconfigs_querybuilder.test_3_c": { -// "b_id": {4}, -// }, -// "genbenthosconfigs_querybuilder.test_3_d": { -// "c_id": {2}, -// }, -// "genbenthosconfigs_querybuilder.test_3_e": { -// "d_id": {4}, -// }, -// } - -// expectedCount := map[string]int{ -// "genbenthosconfigs_querybuilder.test_3_a": 3, -// "genbenthosconfigs_querybuilder.test_3_b": 1, -// "genbenthosconfigs_querybuilder.test_3_c": 1, -// "genbenthosconfigs_querybuilder.test_3_d": 1, -// "genbenthosconfigs_querybuilder.test_3_e": 1, -// } - -// sqlMap, err := BuildSelectQueryMap(sqlmanager_shared.PostgresDriver, tableDependencies, dependencyConfigs, true, map[string]map[string]*sqlmanager_shared.ColumnInfo{}) -// require.NoError(s.T(), err) -// require.Equal(s.T(), len(expectedValues), len(sqlMap)) -// for table, selectQueryRunType := range sqlMap { -// sql := selectQueryRunType[tabledependency.RunTypeInsert] -// require.NotEmpty(s.T(), sql) - -// rows, err := s.pgpool.Query(s.ctx, sql) -// require.NoError(s.T(), err) - -// columnDescriptions := rows.FieldDescriptions() -// tableExpectedValues, ok := expectedValues[table] -// require.Truef(s.T(), ok, fmt.Sprintf("table: %s missing expected values", table)) - -// rowCount := 0 -// for rows.Next() { -// rowCount++ -// values, err := rows.Values() -// require.NoError(s.T(), err) - -// for i, col := range values { -// colName := columnDescriptions[i].Name -// allowedValues, ok := tableExpectedValues[colName] -// if ok { -// value := col.(int64) -// require.Containsf(s.T(), allowedValues, value, fmt.Sprintf("table: %s column: %s ", table, colName)) -// } -// } -// } -// rows.Close() -// require.Equalf(s.T(), rowCount, expectedCount[table], fmt.Sprintf("table: %s ", table)) -// } -// } - -// func (s *IntegrationTestSuite) Test_BuildQueryMap_MultipleSubsets_SubsetsByForeignKeysOff() { -// whereId := "id in (4,5)" -// whereId4 := "id = 4" -// tableDependencies := map[string][]*sqlmanager_shared.ForeignConstraint{ -// "genbenthosconfigs_querybuilder.test_3_b": { -// {Columns: []string{"a_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.test_3_a", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.test_3_c": { -// {Columns: []string{"b_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.test_3_b", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.test_3_d": { -// {Columns: []string{"c_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.test_3_c", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.test_3_e": { -// {Columns: []string{"d_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.test_3_d", Columns: []string{"id"}}}, -// }, -// } -// dependencyConfigs := []*tabledependency.RunConfig{ -// {Table: "genbenthosconfigs_querybuilder.test_3_a", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{}, WhereClause: &whereId}, -// {Table: "genbenthosconfigs_querybuilder.test_3_b", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "a_id"}, InsertColumns: []string{"id", "a_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.test_3_a", Columns: []string{"id"}}}, WhereClause: &whereId4}, -// {Table: "genbenthosconfigs_querybuilder.test_3_c", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "b_id"}, InsertColumns: []string{"id", "b_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.test_3_b", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.test_3_d", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "c_id"}, InsertColumns: []string{"id", "c_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.test_3_c", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.test_3_e", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "d_id"}, InsertColumns: []string{"id", "d_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.test_3_d", Columns: []string{"id"}}}}, -// } - -// expectedValues := map[string]map[string][]int64{ -// "genbenthosconfigs_querybuilder.test_3_a": { -// "id": {4, 5}, -// }, -// "genbenthosconfigs_querybuilder.test_3_b": { -// "a_id": {4}, -// }, -// "genbenthosconfigs_querybuilder.test_3_c": { -// "b_id": {1, 2, 3, 4, 5}, -// }, -// "genbenthosconfigs_querybuilder.test_3_d": { -// "c_id": {1, 2, 3, 4, 5}, -// }, -// "genbenthosconfigs_querybuilder.test_3_e": { -// "d_id": {1, 2, 3, 4, 5}, -// }, -// } - -// expectedCount := map[string]int{ -// "genbenthosconfigs_querybuilder.test_3_a": 2, -// "genbenthosconfigs_querybuilder.test_3_b": 1, -// "genbenthosconfigs_querybuilder.test_3_c": 5, -// "genbenthosconfigs_querybuilder.test_3_d": 5, -// "genbenthosconfigs_querybuilder.test_3_e": 5, -// } - -// sqlMap, err := BuildSelectQueryMap(sqlmanager_shared.PostgresDriver, tableDependencies, dependencyConfigs, false, map[string]map[string]*sqlmanager_shared.ColumnInfo{}) -// require.NoError(s.T(), err) -// require.Equal(s.T(), len(expectedValues), len(sqlMap)) -// for table, selectQueryRunType := range sqlMap { -// sql := selectQueryRunType[tabledependency.RunTypeInsert] -// require.NotEmpty(s.T(), sql) - -// rows, err := s.pgpool.Query(s.ctx, sql) -// require.NoError(s.T(), err) - -// columnDescriptions := rows.FieldDescriptions() -// tableExpectedValues, ok := expectedValues[table] -// require.Truef(s.T(), ok, fmt.Sprintf("table: %s missing expected values", table)) - -// rowCount := 0 -// for rows.Next() { -// rowCount++ -// values, err := rows.Values() -// require.NoError(s.T(), err) - -// for i, col := range values { -// colName := columnDescriptions[i].Name -// allowedValues, ok := tableExpectedValues[colName] -// if ok { -// value := col.(int64) -// require.Containsf(s.T(), allowedValues, value, fmt.Sprintf("table: %s column: %s ", table, colName)) -// } -// } -// } -// rows.Close() -// require.Equalf(s.T(), rowCount, expectedCount[table], fmt.Sprintf("table: %s ", table)) -// } -// } - -// func (s *IntegrationTestSuite) Test_BuildQueryMap_CircularDependency() { -// whereId := "id in (1,5)" -// tableDependencies := map[string][]*sqlmanager_shared.ForeignConstraint{ -// "genbenthosconfigs_querybuilder.addresses": { -// {Columns: []string{"order_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.orders", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.customers": { -// {Columns: []string{"address_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.addresses", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.orders": { -// {Columns: []string{"customer_id"}, NotNullable: []bool{false}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.customers", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.payments": { -// {Columns: []string{"customer_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.customers", Columns: []string{"id"}}}, -// }, -// } -// dependencyConfigs := []*tabledependency.RunConfig{ -// {Table: "genbenthosconfigs_querybuilder.orders", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "customer_id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{}}, -// {Table: "genbenthosconfigs_querybuilder.addresses", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "order_id"}, InsertColumns: []string{"id", "order_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.orders", Columns: []string{"id"}}}, WhereClause: &whereId}, -// {Table: "genbenthosconfigs_querybuilder.customers", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "address_id"}, InsertColumns: []string{"id", "address_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.addresses", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.payments", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "customer_id"}, InsertColumns: []string{"id", "customer_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.customers", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.orders", RunType: tabledependency.RunTypeUpdate, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "customer_id"}, InsertColumns: []string{"customer_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.orders", Columns: []string{"id"}}, {Table: "genbenthosconfigs_querybuilder.customers", Columns: []string{"id"}}}}, -// } - -// expectedValues := map[string]map[string][]int64{ -// "genbenthosconfigs_querybuilder.orders": { -// "customer_id": {2, 3}, -// }, -// "genbenthosconfigs_querybuilder.addresses": { -// "order_id": {1, 5}, -// }, -// "genbenthosconfigs_querybuilder.customers": { -// "address_id": {1, 5}, -// }, -// "genbenthosconfigs_querybuilder.payments": { -// "customer_id": {2}, -// }, -// } - -// expectedCount := map[string]int{ -// "genbenthosconfigs_querybuilder.orders": 2, -// "genbenthosconfigs_querybuilder.addresses": 2, -// "genbenthosconfigs_querybuilder.customers": 2, -// "genbenthosconfigs_querybuilder.payments": 1, -// } - -// sqlMap, err := BuildSelectQueryMap(sqlmanager_shared.PostgresDriver, tableDependencies, dependencyConfigs, true, map[string]map[string]*sqlmanager_shared.ColumnInfo{}) -// require.NoError(s.T(), err) -// require.Equal(s.T(), len(expectedValues), len(sqlMap)) -// for table, selectQueryRunType := range sqlMap { -// sql := selectQueryRunType[tabledependency.RunTypeInsert] -// require.NotEmpty(s.T(), sql) - -// rows, err := s.pgpool.Query(s.ctx, sql) -// require.NoError(s.T(), err) - -// columnDescriptions := rows.FieldDescriptions() -// tableExpectedValues, ok := expectedValues[table] -// require.Truef(s.T(), ok, fmt.Sprintf("table: %s missing expected values", table)) - -// rowCount := 0 -// for rows.Next() { -// rowCount++ -// values, err := rows.Values() -// require.NoError(s.T(), err) - -// for i, col := range values { -// colName := columnDescriptions[i].Name -// allowedValues, ok := tableExpectedValues[colName] -// if ok { -// value := col.(int64) -// require.Containsf(s.T(), allowedValues, value, fmt.Sprintf("table: %s column: %s ", table, colName)) -// } -// } -// } -// rows.Close() -// require.Equalf(s.T(), rowCount, expectedCount[table], fmt.Sprintf("table: %s ", table)) -// } -// } - -// func (s *IntegrationTestSuite) Test_BuildQueryMap_NoForeignKeys() { -// whereId := "id in (1,5)" -// tableDependencies := map[string][]*sqlmanager_shared.ForeignConstraint{} -// dependencyConfigs := []*tabledependency.RunConfig{ -// {Table: "genbenthosconfigs_querybuilder.company", RunType: tabledependency.RunTypeInsert, SelectColumns: []string{"id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{}}, -// {Table: "genbenthosconfigs_querybuilder.test_2_x", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{}, WhereClause: &whereId}, -// {Table: "genbenthosconfigs_querybuilder.test_2_b", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{}, WhereClause: &whereId}, -// {Table: "genbenthosconfigs_querybuilder.test_3_a", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{}}, -// } - -// expectedValues := map[string]map[string][]int64{ -// "genbenthosconfigs_querybuilder.company": { -// "id": {1, 2, 3}, -// }, -// "genbenthosconfigs_querybuilder.test_2_x": { -// "id": {1, 5}, -// }, -// "genbenthosconfigs_querybuilder.test_2_b": { -// "id": {1, 5}, -// }, -// "genbenthosconfigs_querybuilder.test_3_a": { -// "customer_id": {1, 2, 3, 4, 5}, -// }, -// } - -// expectedCount := map[string]int{ -// "genbenthosconfigs_querybuilder.company": 3, -// "genbenthosconfigs_querybuilder.test_2_x": 2, -// "genbenthosconfigs_querybuilder.test_2_b": 2, -// "genbenthosconfigs_querybuilder.test_3_a": 5, -// } - -// sqlMap, err := BuildSelectQueryMap(sqlmanager_shared.PostgresDriver, tableDependencies, dependencyConfigs, true, map[string]map[string]*sqlmanager_shared.ColumnInfo{}) -// require.NoError(s.T(), err) -// require.Equal(s.T(), len(expectedValues), len(sqlMap)) - -// for table, selectQueryRunType := range sqlMap { -// sql := selectQueryRunType[tabledependency.RunTypeInsert] -// require.NotEmpty(s.T(), sql) - -// rows, err := s.pgpool.Query(s.ctx, sql) -// require.NoError(s.T(), err) - -// columnDescriptions := rows.FieldDescriptions() -// tableExpectedValues, ok := expectedValues[table] -// require.Truef(s.T(), ok, fmt.Sprintf("table: %s missing expected values", table)) - -// rowCount := 0 -// for rows.Next() { -// rowCount++ -// values, err := rows.Values() -// require.NoError(s.T(), err) - -// for i, col := range values { -// colName := columnDescriptions[i].Name -// allowedValues, ok := tableExpectedValues[colName] -// if ok { -// value := col.(int64) -// require.Containsf(s.T(), allowedValues, value, fmt.Sprintf("table: %s column: %s ", table, colName)) -// } -// } -// } -// rows.Close() -// require.Equalf(s.T(), rowCount, expectedCount[table], fmt.Sprintf("table: %s ", table)) -// } -// } - -// func (s *IntegrationTestSuite) Test_BuildQueryMap_NoForeignKeys_NoSubsets() { -// tableDependencies := map[string][]*sqlmanager_shared.ForeignConstraint{} -// dependencyConfigs := []*tabledependency.RunConfig{ -// {Table: "genbenthosconfigs_querybuilder.company", RunType: tabledependency.RunTypeInsert, SelectColumns: []string{"id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{}}, -// {Table: "genbenthosconfigs_querybuilder.test_2_x", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{}}, -// {Table: "genbenthosconfigs_querybuilder.test_2_b", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{}}, -// {Table: "genbenthosconfigs_querybuilder.test_3_a", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{}}, -// } - -// expectedCount := map[string]int{ -// "genbenthosconfigs_querybuilder.company": 3, -// "genbenthosconfigs_querybuilder.test_2_x": 5, -// "genbenthosconfigs_querybuilder.test_2_b": 5, -// "genbenthosconfigs_querybuilder.test_3_a": 5, -// } - -// sqlMap, err := BuildSelectQueryMap(sqlmanager_shared.PostgresDriver, tableDependencies, dependencyConfigs, true, map[string]map[string]*sqlmanager_shared.ColumnInfo{}) -// require.NoError(s.T(), err) -// require.Equal(s.T(), len(expectedCount), len(sqlMap)) -// for table, selectQueryRunType := range sqlMap { -// sql := selectQueryRunType[tabledependency.RunTypeInsert] -// require.NotEmpty(s.T(), sql) - -// rows, err := s.pgpool.Query(s.ctx, sql) -// require.NoError(s.T(), err) - -// rowCount := 0 -// for rows.Next() { -// rowCount++ -// } -// rows.Close() - -// tableExpectedCount, ok := expectedCount[table] -// require.Truef(s.T(), ok, fmt.Sprintf("table: %s missing expected row counts", table)) -// require.Equalf(s.T(), rowCount, tableExpectedCount, fmt.Sprintf("table: %s ", table)) -// } -// } - -// func (s *IntegrationTestSuite) Test_BuildQueryMap_SubsetCompositeKeys() { -// whereId := "id in (3,5)" -// tableDependencies := map[string][]*sqlmanager_shared.ForeignConstraint{ -// "genbenthosconfigs_querybuilder.employees": { -// {Columns: []string{"division_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.division", Columns: []string{"id"}}}, -// }, -// "genbenthosconfigs_querybuilder.projects": { -// {Columns: []string{"responsible_employee_id", "responsible_division_id"}, NotNullable: []bool{true, true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.employees", Columns: []string{"id", "division_id"}}}, -// }, -// } -// dependencyConfigs := []*tabledependency.RunConfig{ -// {Table: "genbenthosconfigs_querybuilder.division", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{}, WhereClause: &whereId}, -// {Table: "genbenthosconfigs_querybuilder.employees", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id", "division_id"}, SelectColumns: []string{"id", "division_id"}, InsertColumns: []string{"id", "division_id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.division", Columns: []string{"id"}}}}, -// {Table: "genbenthosconfigs_querybuilder.projects", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "responsible_employee_id", "responsible_division_id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.employees", Columns: []string{"id", "division_id"}}}}, -// } - -// expectedValues := map[string]map[string][]int64{ -// "genbenthosconfigs_querybuilder.division": { -// "id": {3, 5}, -// }, -// "genbenthosconfigs_querybuilder.employees": { -// "division_id": {3, 5}, -// "id": {8, 10}, -// }, -// "genbenthosconfigs_querybuilder.projects": { -// "responsible_division_id": {3, 5}, -// "responsible_employee_id": {8, 10}, -// }, -// } - -// expectedCount := map[string]int{ -// "genbenthosconfigs_querybuilder.division": 2, -// "genbenthosconfigs_querybuilder.employees": 2, -// "genbenthosconfigs_querybuilder.projects": 2, -// } - -// sqlMap, err := BuildSelectQueryMap(sqlmanager_shared.PostgresDriver, tableDependencies, dependencyConfigs, true, map[string]map[string]*sqlmanager_shared.ColumnInfo{}) -// require.NoError(s.T(), err) -// require.Equal(s.T(), len(expectedValues), len(sqlMap)) -// for table, selectQueryRunType := range sqlMap { -// sql := selectQueryRunType[tabledependency.RunTypeInsert] -// require.NotEmpty(s.T(), sql) - -// rows, err := s.pgpool.Query(s.ctx, sql) -// require.NoError(s.T(), err) - -// columnDescriptions := rows.FieldDescriptions() -// tableExpectedValues, ok := expectedValues[table] -// require.Truef(s.T(), ok, fmt.Sprintf("table: %s missing expected values", table)) - -// rowCount := 0 -// for rows.Next() { -// rowCount++ -// values, err := rows.Values() -// require.NoError(s.T(), err) - -// for i, col := range values { -// colName := columnDescriptions[i].Name -// allowedValues, ok := tableExpectedValues[colName] -// if ok { -// value := col.(int64) -// require.Containsf(s.T(), allowedValues, value, fmt.Sprintf("table: %s column: %s ", table, colName)) -// } -// } -// } -// rows.Close() -// require.Equalf(s.T(), rowCount, expectedCount[table], fmt.Sprintf("table: %s ", table)) -// } -// } - -// func (s *IntegrationTestSuite) Test_BuildQueryMap_SubsetSelfReferencing() { -// whereId := "id in (3,5)" -// tableDependencies := map[string][]*sqlmanager_shared.ForeignConstraint{ -// "genbenthosconfigs_querybuilder.bosses": { -// {Columns: []string{"manager_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.bosses", Columns: []string{"id"}}}, -// {Columns: []string{"big_boss_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.bosses", Columns: []string{"manager_id"}}}, -// }, -// "genbenthosconfigs_querybuilder.minions": { -// {Columns: []string{"boss_id"}, NotNullable: []bool{true}, ForeignKey: &sqlmanager_shared.ForeignKey{Table: "genbenthosconfigs_querybuilder.bosses", Columns: []string{"big_boss_id"}}}, -// }, -// } -// dependencyConfigs := []*tabledependency.RunConfig{ -// {Table: "genbenthosconfigs_querybuilder.bosses", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "manager_id", "big_boss_id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{}, WhereClause: &whereId}, -// {Table: "genbenthosconfigs_querybuilder.minions", RunType: tabledependency.RunTypeInsert, PrimaryKeys: []string{"id"}, SelectColumns: []string{"id", "boss_id"}, InsertColumns: []string{"id"}, DependsOn: []*tabledependency.DependsOn{{Table: "genbenthosconfigs_querybuilder.bosses", Columns: []string{"manager_id"}}}}, -// } - -// expectedValues := map[string]map[string][]int64{ -// "genbenthosconfigs_querybuilder.bosses": { -// "id": {1, 2, 3, 4, 5}, -// "manager_id": {1, 2, 3, 4}, -// "boss_id": {1, 2, 3}, -// }, -// "genbenthosconfigs_querybuilder.minions": { -// "boss_id": {1, 3}, -// }, -// } - -// expectedCount := map[string]int{ -// "genbenthosconfigs_querybuilder.bosses": 5, -// "genbenthosconfigs_querybuilder.minions": 2, -// } - -// sqlMap, err := BuildSelectQueryMap(sqlmanager_shared.PostgresDriver, tableDependencies, dependencyConfigs, true, map[string]map[string]*sqlmanager_shared.ColumnInfo{}) -// require.NoError(s.T(), err) -// require.Equal(s.T(), len(expectedValues), len(sqlMap)) -// for table, selectQueryRunType := range sqlMap { -// sql := selectQueryRunType[tabledependency.RunTypeInsert] -// require.NotEmpty(s.T(), sql) - -// rows, err := s.pgpool.Query(s.ctx, sql) -// require.NoError(s.T(), err) - -// columnDescriptions := rows.FieldDescriptions() -// tableExpectedValues, ok := expectedValues[table] -// require.Truef(s.T(), ok, fmt.Sprintf("table: %s missing expected values", table)) - -// rowCount := 0 -// for rows.Next() { -// rowCount++ -// values, err := rows.Values() -// require.NoError(s.T(), err) - -// for i, col := range values { -// colName := columnDescriptions[i].Name -// if (colName == "manager_id" || colName == "big_boss_id") && col == nil { -// continue -// } -// allowedValues, ok := tableExpectedValues[colName] -// if ok { -// value := col.(int64) -// require.Containsf(s.T(), allowedValues, value, fmt.Sprintf("table: %s column: %s ", table, colName)) -// } -// } -// } -// rows.Close() -// require.Equalf(s.T(), rowCount, expectedCount[table], fmt.Sprintf("table: %s ", table)) -// } -// } diff --git a/worker/pkg/query-builder/query-builder_test.go b/worker/pkg/query-builder/query-builder_test.go index 697cb91dd0..b588970846 100644 --- a/worker/pkg/query-builder/query-builder_test.go +++ b/worker/pkg/query-builder/query-builder_test.go @@ -5,11 +5,10 @@ import ( "testing" sqlmanager_shared "github.com/nucleuscloud/neosync/backend/pkg/sqlmanager/shared" - tabledependency "github.com/nucleuscloud/neosync/backend/pkg/table-dependency" "github.com/stretchr/testify/require" ) -func Test_buildSelectQuery(t *testing.T) { +func Test_BuildSelectQuery(t *testing.T) { tests := []struct { name string driver string @@ -70,515 +69,6 @@ func Test_buildSelectQuery(t *testing.T) { } } -func Test_buildSelectJoinQuery(t *testing.T) { - tests := []struct { - name string - driver string - table string - columns []string - joins []*sqlJoin - whereClauses []string - expected string - }{ - { - name: "simple", - driver: sqlmanager_shared.PostgresDriver, - table: "public.a", - columns: []string{"id", "name", "email"}, - joins: []*sqlJoin{ - { - JoinType: innerJoin, - JoinTable: "public.b", - BaseTable: "public.a", - JoinColumnsMap: map[string]string{ - "a_id": "id", - }, - }, - }, - whereClauses: []string{`"public"."a"."name" = 'alisha'`, `"public"."b"."email" = 'fake@email.com'`}, - expected: `SELECT "public"."a"."id", "public"."a"."name", "public"."a"."email" FROM "public"."a" INNER JOIN "public"."b" ON ("public"."b"."a_id" = "public"."a"."id") WHERE ("public"."a"."name" = 'alisha' AND "public"."b"."email" = 'fake@email.com');`, - }, - { - name: "multiple joins", - driver: sqlmanager_shared.PostgresDriver, - table: "public.a", - columns: []string{"id", "name", "email"}, - joins: []*sqlJoin{ - { - JoinType: innerJoin, - JoinTable: "public.b", - BaseTable: "public.a", - JoinColumnsMap: map[string]string{ - "a_id": "id", - }, - }, - { - JoinType: innerJoin, - JoinTable: "public.c", - BaseTable: "public.b", - JoinColumnsMap: map[string]string{ - "b_id": "id", - }, - }, - }, - whereClauses: []string{`"public"."a"."name" = 'alisha'`, `"public"."b"."id" = 1`}, - expected: `SELECT "public"."a"."id", "public"."a"."name", "public"."a"."email" FROM "public"."a" INNER JOIN "public"."b" ON ("public"."b"."a_id" = "public"."a"."id") INNER JOIN "public"."c" ON ("public"."c"."b_id" = "public"."b"."id") WHERE ("public"."a"."name" = 'alisha' AND "public"."b"."id" = 1);`, - }, - { - name: "composite foreign key", - driver: sqlmanager_shared.PostgresDriver, - table: "public.a", - columns: []string{"id", "name", "email"}, - joins: []*sqlJoin{ - { - JoinType: innerJoin, - JoinTable: "public.b", - BaseTable: "public.a", - JoinColumnsMap: map[string]string{ - "a_id": "id", - "aa_id": "other_id", - }, - }, - }, - whereClauses: []string{`"public"."a"."name" = 'alisha'`, `"public"."b"."email" = 'fake@email.com'`}, - expected: `SELECT "public"."a"."id", "public"."a"."name", "public"."a"."email" FROM "public"."a" INNER JOIN "public"."b" ON (("public"."b"."a_id" = "public"."a"."id") AND ("public"."b"."aa_id" = "public"."a"."other_id")) WHERE ("public"."a"."name" = 'alisha' AND "public"."b"."email" = 'fake@email.com');`, - }, - } - - for _, tt := range tests { - t.Run(fmt.Sprintf("%s_%s", t.Name(), tt.name), func(t *testing.T) { - response, err := BuildSelectJoinQuery(tt.driver, tt.table, tt.columns, tt.joins, tt.whereClauses) - require.NoError(t, err) - require.Equal(t, tt.expected, response) - }) - } -} - -func Test_buildSelectRecursiveQuery(t *testing.T) { - tests := []struct { - name string - driver string - table string - columns []string - columnInfoMap map[string]*sqlmanager_shared.ColumnInfo - joins []*sqlJoin - whereClauses []string - dependencies []*selfReferencingCircularDependency - primaryKeyCol [][]string - expected string - }{ - { - name: "one foreign key no joins", - driver: sqlmanager_shared.PostgresDriver, - table: "public.employees", - columns: []string{"employee_id", "name", "manager_id"}, - columnInfoMap: map[string]*sqlmanager_shared.ColumnInfo{}, - joins: []*sqlJoin{}, - whereClauses: []string{`"public"."employees"."name" = 'alisha'`}, - dependencies: []*selfReferencingCircularDependency{ - {PrimaryKeyColumns: []string{"employee_id"}, ForeignKeyColumns: [][]string{{"manager_id"}}}, - }, - expected: `WITH RECURSIVE related AS (SELECT "public"."employees"."employee_id", "public"."employees"."name", "public"."employees"."manager_id" FROM "public"."employees" WHERE "public"."employees"."name" = 'alisha' UNION (SELECT "public"."employees"."employee_id", "public"."employees"."name", "public"."employees"."manager_id" FROM "public"."employees" INNER JOIN "related" ON ("public"."employees"."employee_id" = "related"."manager_id"))) SELECT DISTINCT "employee_id", "name", "manager_id" FROM "related";`, - }, - { - name: "json field", - driver: sqlmanager_shared.PostgresDriver, - table: "public.employees", - columns: []string{"employee_id", "name", "manager_id", "additional_info"}, - columnInfoMap: map[string]*sqlmanager_shared.ColumnInfo{"additional_info": {DataType: "json"}}, - joins: []*sqlJoin{}, - whereClauses: []string{`"public"."employees"."name" = 'alisha'`}, - dependencies: []*selfReferencingCircularDependency{ - {PrimaryKeyColumns: []string{"employee_id"}, ForeignKeyColumns: [][]string{{"manager_id"}}}, - }, - expected: `WITH RECURSIVE related AS (SELECT "public"."employees"."employee_id", "public"."employees"."name", "public"."employees"."manager_id", to_jsonb("public"."employees"."additional_info") AS "additional_info" FROM "public"."employees" WHERE "public"."employees"."name" = 'alisha' UNION (SELECT "public"."employees"."employee_id", "public"."employees"."name", "public"."employees"."manager_id", to_jsonb("public"."employees"."additional_info") AS "additional_info" FROM "public"."employees" INNER JOIN "related" ON ("public"."employees"."employee_id" = "related"."manager_id"))) SELECT DISTINCT "employee_id", "name", "manager_id", "additional_info" FROM "related";`, - }, - { - name: "json field mysql", - driver: sqlmanager_shared.MysqlDriver, - table: "public.employees", - columns: []string{"employee_id", "name", "manager_id", "additional_info"}, - columnInfoMap: map[string]*sqlmanager_shared.ColumnInfo{"additional_info": {DataType: "json"}}, - joins: []*sqlJoin{}, - whereClauses: []string{`"public"."employees"."name" = 'alisha'`}, - dependencies: []*selfReferencingCircularDependency{ - {PrimaryKeyColumns: []string{"employee_id"}, ForeignKeyColumns: [][]string{{"manager_id"}}}, - }, - expected: "WITH RECURSIVE related AS (SELECT `public`.`employees`.`employee_id`, `public`.`employees`.`name`, `public`.`employees`.`manager_id`, `public`.`employees`.`additional_info` FROM `public`.`employees` WHERE \"public\".\"employees\".\"name\" = 'alisha' UNION (SELECT `public`.`employees`.`employee_id`, `public`.`employees`.`name`, `public`.`employees`.`manager_id`, `public`.`employees`.`additional_info` FROM `public`.`employees` INNER JOIN `related` ON (`public`.`employees`.`employee_id` = `related`.`manager_id`))) SELECT DISTINCT `employee_id`, `name`, `manager_id`, `additional_info` FROM `related`;", - }, - { - name: "multiple foreign keys and joins", - driver: sqlmanager_shared.PostgresDriver, - table: "public.employees", - columns: []string{"employee_id", "name", "manager_id", "department_id", "big_boss_id"}, - columnInfoMap: map[string]*sqlmanager_shared.ColumnInfo{}, - joins: []*sqlJoin{ - { - JoinType: innerJoin, - JoinTable: "public.departments", - BaseTable: "public.employees", - JoinColumnsMap: map[string]string{ - "id": "department_id", - }, - }, - }, - whereClauses: []string{`"public"."employees"."name" = 'alisha'`, `"public"."departments"."department_id" = 1`}, - dependencies: []*selfReferencingCircularDependency{ - {PrimaryKeyColumns: []string{"employee_id"}, ForeignKeyColumns: [][]string{{"manager_id"}, {"big_boss_id"}}}, - }, - expected: `WITH RECURSIVE related AS (SELECT "public"."employees"."employee_id", "public"."employees"."name", "public"."employees"."manager_id", "public"."employees"."department_id", "public"."employees"."big_boss_id" FROM "public"."employees" INNER JOIN "public"."departments" ON ("public"."departments"."id" = "public"."employees"."department_id") WHERE ("public"."employees"."name" = 'alisha' AND "public"."departments"."department_id" = 1) UNION (SELECT "public"."employees"."employee_id", "public"."employees"."name", "public"."employees"."manager_id", "public"."employees"."department_id", "public"."employees"."big_boss_id" FROM "public"."employees" INNER JOIN "related" ON (("public"."employees"."employee_id" = "related"."manager_id") OR ("public"."employees"."employee_id" = "related"."big_boss_id")))) SELECT DISTINCT "employee_id", "name", "manager_id", "department_id", "big_boss_id" FROM "related";`, - }, - { - name: "composite foreign keys", - driver: sqlmanager_shared.PostgresDriver, - table: "public.employees", - columns: []string{"employee_id", "department_id", "name", "manager_id", "building_id", "division_id"}, - columnInfoMap: map[string]*sqlmanager_shared.ColumnInfo{}, - joins: []*sqlJoin{ - { - JoinType: innerJoin, - JoinTable: "public.departments", - BaseTable: "public.employees", - JoinColumnsMap: map[string]string{ - "id": "building_id", - "other_id": "another_id", - }, - }, - }, - whereClauses: []string{`"public"."employees"."name" = 'alisha'`, `"public"."departments"."building_id" = 1`}, - dependencies: []*selfReferencingCircularDependency{ - {PrimaryKeyColumns: []string{"employee_id", "department_id"}, ForeignKeyColumns: [][]string{{"manager_id", "division_id"}}}, - }, - expected: `WITH RECURSIVE related AS (SELECT "public"."employees"."employee_id", "public"."employees"."department_id", "public"."employees"."name", "public"."employees"."manager_id", "public"."employees"."building_id", "public"."employees"."division_id" FROM "public"."employees" INNER JOIN "public"."departments" ON (("public"."departments"."id" = "public"."employees"."building_id") AND ("public"."departments"."other_id" = "public"."employees"."another_id")) WHERE ("public"."employees"."name" = 'alisha' AND "public"."departments"."building_id" = 1) UNION (SELECT "public"."employees"."employee_id", "public"."employees"."department_id", "public"."employees"."name", "public"."employees"."manager_id", "public"."employees"."building_id", "public"."employees"."division_id" FROM "public"."employees" INNER JOIN "related" ON (("public"."employees"."employee_id" = "related"."manager_id") AND ("public"."employees"."department_id" = "related"."division_id")))) SELECT DISTINCT "employee_id", "department_id", "name", "manager_id", "building_id", "division_id" FROM "related";`, - }, - } - - for _, tt := range tests { - t.Run(fmt.Sprintf("%s_%s", t.Name(), tt.name), func(t *testing.T) { - response, err := BuildSelectRecursiveQuery(tt.driver, tt.table, tt.columns, tt.columnInfoMap, tt.dependencies, tt.joins, tt.whereClauses) - require.NoError(t, err) - require.Equal(t, tt.expected, response) - }) - } -} - -func Test_filterForeignKeysWithSubset_partialtables(t *testing.T) { - var emptyWhere *string - where := "id = '36f594af-6d53-4a48-a9b7-b889e2df349e'" - runConfigMap := map[string]*tabledependency.RunConfig{ - "circle.addresses": { - Table: "circle.addresses", - InsertColumns: []string{"id"}, - SelectColumns: []string{"id"}, - DependsOn: []*tabledependency.DependsOn{}, - RunType: tabledependency.RunTypeInsert, - PrimaryKeys: []string{"id"}, - WhereClause: &where, - }, - - "circle.customers": { - Table: "circle.customers", - InsertColumns: []string{"id", "address_id"}, - SelectColumns: []string{"id", "address_id"}, - DependsOn: []*tabledependency.DependsOn{{Table: "circle.addresses", Columns: []string{"id"}}}, - RunType: tabledependency.RunTypeInsert, - PrimaryKeys: []string{"id"}, - WhereClause: emptyWhere, - }, - } - - constraints := map[string][]*sqlmanager_shared.ForeignConstraint{ - "circle.addresses": { - { - Columns: []string{"order_id"}, - NotNullable: []bool{false}, - ForeignKey: &sqlmanager_shared.ForeignKey{Table: "circle.orders", Columns: []string{"id"}}, - }, - }, - "circle.customers": { - { - Columns: []string{"address_id"}, - NotNullable: []bool{false}, - ForeignKey: &sqlmanager_shared.ForeignKey{Table: "circle.addresses", Columns: []string{"id"}}, - }, - }, - } - - whereClauses := map[string]string{ - "circle.addresses": "id = '36f594af-6d53-4a48-a9b7-b889e2df349e'", - } - - expected := map[string][]*sqlmanager_shared.ForeignConstraint{ - "circle.addresses": {}, - "circle.customers": { - { - Columns: []string{"address_id"}, - NotNullable: []bool{false}, - ForeignKey: &sqlmanager_shared.ForeignKey{Table: "circle.addresses", Columns: []string{"id"}}, - }, - }, - } - - actual := filterForeignKeysWithSubset(runConfigMap, constraints, whereClauses) - require.Equal(t, expected, actual) -} - -func Test_qualifyWhereColumnNames_mysql(t *testing.T) { - tests := []struct { - name string - where string - table string - expected string - }{ - { - name: "simple", - where: "name = 'alisha'", - table: "public.a", - expected: "public.a.name = 'alisha'", - }, - { - name: "simple", - where: "public.a.name = 'alisha'", - table: "public.a", - expected: "public.a.name = 'alisha'", - }, - { - name: "multiple", - where: "name = 'alisha' and id = 1", - table: "public.a", - expected: "public.a.name = 'alisha' and public.a.id = 1", - }, - { - name: "where subquery", - where: "film_id IN(SELECT film_id FROM film_category INNER JOIN category USING(category_id) WHERE name='Action')", - table: "public.film", - expected: "public.film.film_id in (select film_id from film_category join category using (category_id) where name = 'Action')", - }, - { - name: "quoted column names", - where: "`name with space` = 'hey' and PascalCase = 'other'", - table: "public.order", - expected: "public.`order`.`name with space` = 'hey' and public.`order`.PascalCase = 'other'", - }, - } - - for _, tt := range tests { - t.Run(fmt.Sprintf("%s_%s", t.Name(), tt.name), func(t *testing.T) { - response, err := qualifyWhereColumnNames(sqlmanager_shared.MysqlDriver, tt.where, tt.table) - require.NoError(t, err) - require.Equal(t, tt.expected, response) - }) - } -} - -func Test_qualifyWhereColumnNames_postgres(t *testing.T) { - tests := []struct { - name string - where string - table string - expected string - }{ - { - name: "simple", - where: "name = 'alisha'", - table: "public.a", - expected: `public.a.name = 'alisha'`, - }, - { - name: "simple", - where: "public.a.name = 'alisha'", - table: "public.a", - expected: `public.a.name = 'alisha'`, - }, - { - name: "simple", - where: `"bad name" = 'alisha'`, - table: "public.order", - expected: `public."order"."bad name" = 'alisha'`, - }, - { - name: "multiple", - where: "name = 'alisha' and id = 1 or age = 2", - table: "public.a", - expected: `(public.a.name = 'alisha' AND public.a.id = 1) OR public.a.age = 2`, - }, - { - name: "where subquery", - where: "film_id IN(SELECT film_id FROM film_category INNER JOIN category USING(category_id) WHERE name='Action');", - table: "public.film", - expected: `public.film.film_id IN (SELECT film_id FROM film_category JOIN category USING (category_id) WHERE name = 'Action')`, - }, - } - - for _, tt := range tests { - t.Run(fmt.Sprintf("%s_%s", t.Name(), tt.name), func(t *testing.T) { - response, err := qualifyWhereColumnNames(sqlmanager_shared.PostgresDriver, tt.where, tt.table) - require.NoError(t, err) - require.Equal(t, tt.expected, response) - }) - } -} - -func Test_qualifyWhereWithTableAlias(t *testing.T) { - tests := []struct { - driver string - name string - where string - alias string - expected string - }{ - { - driver: sqlmanager_shared.PostgresDriver, - name: "simple", - where: "name = 'alisha'", - alias: "alias", - expected: `alias.name = 'alisha'`, - }, - { - driver: sqlmanager_shared.PostgresDriver, - name: "hash alias", - where: "composite_keys.department.department_id = '1'", - alias: "50d89c0f3af602", - expected: `"50d89c0f3af602".department_id = '1'`, - }, - { - driver: sqlmanager_shared.PostgresDriver, - name: "simple", - where: "public.a.name = 'alisha'", - alias: "alias", - expected: `alias.name = 'alisha'`, - }, - { - driver: sqlmanager_shared.PostgresDriver, - name: "multiple", - where: "name = 'alisha' and id = 1 or age = 2", - alias: "alias", - expected: `(alias.name = 'alisha' AND alias.id = 1) OR alias.age = 2`, - }, - { - driver: sqlmanager_shared.MysqlDriver, - name: "simple", - where: "name = 'alisha'", - alias: "alias", - expected: `alias.name = 'alisha'`, - }, - { - driver: sqlmanager_shared.MysqlDriver, - name: "simple", - where: "public.a.name = 'alisha'", - alias: "alias", - expected: `alias.name = 'alisha'`, - }, - { - driver: sqlmanager_shared.MysqlDriver, - name: "multiple", - where: "name = 'alisha' and id = 1 or age = 2", - alias: "alias", - expected: `alias.name = 'alisha' and alias.id = 1 or alias.age = 2`, - }, - { - driver: sqlmanager_shared.MysqlDriver, - name: "hash alias", - where: "composite_keys.department.department_id = '1'", - alias: "50d89c0f3af602", - expected: "`50d89c0f3af602`.department_id = '1'", - }, - } - - for _, tt := range tests { - t.Run(fmt.Sprintf("%s_%s", t.Name(), tt.name), func(t *testing.T) { - response, err := qualifyWhereWithTableAlias(tt.driver, tt.where, tt.alias) - require.NoError(t, err) - require.Equal(t, tt.expected, response) - }) - } -} - -func TestGetPrimaryToForeignTableMapFromRunConfigs(t *testing.T) { - tests := []struct { - name string - runConfigs []*tabledependency.RunConfig - expected map[string][]string - }{ - { - name: "no configs", - runConfigs: nil, - expected: map[string][]string{}, - }, - { - name: "single config without dependencies", - runConfigs: []*tabledependency.RunConfig{ - {Table: "table1", DependsOn: nil}, - }, - expected: map[string][]string{}, - }, - { - name: "single config with one dependency", - runConfigs: []*tabledependency.RunConfig{ - {Table: "table1", DependsOn: []*tabledependency.DependsOn{{Table: "table2"}}}, - }, - expected: map[string][]string{ - "table2": {"table1"}, - }, - }, - { - name: "multiple configs with shared dependencies", - runConfigs: []*tabledependency.RunConfig{ - {Table: "table1", DependsOn: []*tabledependency.DependsOn{{Table: "table2"}}}, - {Table: "table3", DependsOn: []*tabledependency.DependsOn{{Table: "table2"}}}, - }, - expected: map[string][]string{ - "table2": {"table1", "table3"}, - }, - }, - { - name: "config with multiple unique dependencies", - runConfigs: []*tabledependency.RunConfig{ - {Table: "table1", DependsOn: []*tabledependency.DependsOn{{Table: "table2"}, {Table: "table3"}}}, - }, - expected: map[string][]string{ - "table2": {"table1"}, - "table3": {"table1"}, - }, - }, - { - name: "circular dependencies", - runConfigs: []*tabledependency.RunConfig{ - {Table: "table1", DependsOn: []*tabledependency.DependsOn{{Table: "table2"}}}, - {Table: "table2", DependsOn: []*tabledependency.DependsOn{{Table: "table1"}}}, - }, - expected: map[string][]string{ - "table2": {"table1"}, - "table1": {"table2"}, - }, - }, - { - name: "multiple configs with duplicates", - runConfigs: []*tabledependency.RunConfig{ - {Table: "table1", DependsOn: []*tabledependency.DependsOn{{Table: "table2"}}}, - {Table: "table1", DependsOn: []*tabledependency.DependsOn{{Table: "table2"}}}, - {Table: "table3", DependsOn: []*tabledependency.DependsOn{{Table: "table2"}}}, - }, - expected: map[string][]string{ - "table2": {"table1", "table3"}, - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - actual := getPrimaryToForeignTableMapFromRunConfigs(tt.runConfigs) - for table, dependencies := range actual { - expected, exists := tt.expected[table] - require.True(t, exists) - for _, dep := range dependencies { - require.Contains(t, expected, dep) - } - } - }) - } -} - func Test_BuildUpdateQuery(t *testing.T) { tests := []struct { name string diff --git a/worker/pkg/query-builder/testdata/setup.sql b/worker/pkg/query-builder/testdata/setup.sql deleted file mode 100644 index dc10350c5e..0000000000 --- a/worker/pkg/query-builder/testdata/setup.sql +++ /dev/null @@ -1,450 +0,0 @@ -CREATE SCHEMA IF NOT EXISTS genbenthosconfigs_querybuilder; - -SET search_path TO genbenthosconfigs_querybuilder; -CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; - --- Test_BuildQueryMap_DoubleReference -CREATE TABLE - IF NOT EXISTS company ( - "id" BIGSERIAL NOT NULL, - "name" text NOT NULL, - "url" text NULL, - "employee_count" integer NULL, - "uuid" uuid NOT NULL DEFAULT uuid_generate_v4 (), - CONSTRAINT company_pkey PRIMARY KEY (id), - CONSTRAINT company_uuid_key UNIQUE (uuid) - ); -CREATE TABLE - IF NOT EXISTS department ( - "id" BIGSERIAL NOT NULL, - "name" text NOT NULL, - "url" text NULL, - "company_id" bigint NOT NULL, -- to be fk - "user_id" bigint NULL, - "uuid" uuid NOT NULL DEFAULT uuid_generate_v4 (), - CONSTRAINT department_pkey PRIMARY KEY (id), - CONSTRAINT department_uuid_key UNIQUE (uuid), - CONSTRAINT department_company_id_fkey FOREIGN KEY (company_id) REFERENCES company (id) ON DELETE CASCADE - ); -CREATE TABLE IF NOT EXISTS transaction ( - id bigint NOT NULL, - amount double precision NOT NULL, - created timestamp without time zone, - updated timestamp without time zone, - department_id bigint, -- to be fk - date date, - currency text NOT NULL, - settings json DEFAULT '{ - "historicalCount": 0, - "vacation": false, - "conference": true, - "travel": true - }'::json NOT NULL, - description text, - timezone text DEFAULT 'America/New_York'::text NOT NULL, - uuid uuid DEFAULT uuid_generate_v4() NOT NULL, - CONSTRAINT transaction_pkey PRIMARY KEY (id), - CONSTRAINT transaction_department_id_fkey FOREIGN KEY (department_id) REFERENCES department (ID) ON DELETE CASCADE -); -CREATE TABLE IF NOT EXISTS expense_report ( - id bigint NOT NULL, - invoice_id text, - date date NOT NULL, - amount numeric(15,2), - department_source_id bigint, -- fk - department_destination_id bigint, --fk - created timestamp without time zone, - updated timestamp without time zone, - currency character varying(5), - transaction_type integer NOT NULL, - paid boolean DEFAULT false, - transaction_id bigint, -- fk - adjustment_amount numeric(15,2), - CONSTRAINT transaction_type_valid_values CHECK ((transaction_type = ANY (ARRAY[1, 2]))), - CONSTRAINT expense_report_pkey PRIMARY KEY (id), - CONSTRAINT expense_report_dept_source_fkey FOREIGN KEY (department_source_id) REFERENCES department (ID) ON DELETE CASCADE, - CONSTRAINT expense_report_dept_destination_fkey FOREIGN KEY (department_destination_id) REFERENCES department (ID) ON DELETE CASCADE, - CONSTRAINT expense_report_transaction_fkey FOREIGN KEY (transaction_id) REFERENCES transaction (ID) ON DELETE CASCADE -); - -CREATE TABLE IF NOT EXISTS expense ( - id bigint NOT NULL PRIMARY KEY, - report_id bigint, - CONSTRAINT expense_report_d_fkey FOREIGN KEY (report_id) REFERENCES expense_report (ID) ON DELETE CASCADE -); - - -CREATE TABLE IF NOT EXISTS item ( - id bigint NOT NULL PRIMARY KEY, - expense_id bigint, - CONSTRAINT expense_id_fkey FOREIGN KEY (expense_id) REFERENCES expense (ID) ON DELETE CASCADE -); - - -INSERT INTO company (name, url, employee_count, uuid) -VALUES - ('Acme Corporation', 'www.acme.com', 500, uuid_generate_v4()), - ('Global Enterprises', 'globalenterprises.net', 1200, uuid_generate_v4()), - ('Tech Innovations', 'www.techinnovations.io', 250, uuid_generate_v4()); - -INSERT INTO department (name, url, company_id, uuid) -VALUES - ('Marketing', 'marketing.acme.com', 1, uuid_generate_v4()), -- Acme Corporation - ('Sales', 'sales.acme.com', 1, uuid_generate_v4()), - ('Finance', null, 2, uuid_generate_v4()), -- Global Enterprises - ('R&D', 'rnd.techinnovations.io', 3, uuid_generate_v4()); -- Tech Innovations - -INSERT INTO transaction (id, amount, created, updated, department_id, date, currency, description, uuid) -VALUES - (1, 250.50, now() - interval '2 weeks', now(), 1, '2024-05-01', 'USD', 'Office Supplies', uuid_generate_v4()), - (2, 1250.00, now() - interval '5 days', now(), 2, '2024-05-06', 'GBP', 'Travel Expenses', uuid_generate_v4()), - (3, 87.25, now() - interval '1 month', now(), 3, '2024-04-20', 'EUR', 'Lunch Meeting', uuid_generate_v4()); - -INSERT INTO expense_report (id, invoice_id, date, amount, department_source_id, department_destination_id, created, updated, currency, transaction_type, paid, adjustment_amount, transaction_id) -VALUES - (1, 'INV-1234', '2024-05-03', 500.00, 1, 2, now() - interval '15 days', now(), 'USD', 1, true, null, 1), - (2,'INV-5678', '2024-04-28', 128.75, 3, 1, now() - interval '20 days', now() - interval '1 day', 'CAD', 2, false, 12.50, 3), - (3,'INV-5678', '2024-04-28', 128.75, 2, 1, now() - interval '20 days', now() - interval '1 day', 'CAD', 2, false, 12.50, 2); - - -INSERT INTO expense (id, report_id) VALUES -(1, 2), -(2, 3), -(3, 1); - --- Insert statements for item -INSERT INTO item (id, expense_id) VALUES -(1, 3), -(2, 1), -(3, 2); - - --- Test_BuildQueryMap_DoubleRootSubset -CREATE TABLE test_2_x ( - id BIGINT NOT NULL PRIMARY KEY, - name text, - created timestamp without time zone -); - -CREATE TABLE test_2_b ( - id BIGINT NOT NULL PRIMARY KEY, - name text, - created timestamp without time zone -); - -CREATE TABLE test_2_a ( - id BIGINT NOT NULL PRIMARY KEY, - x_id BIGINT NOT NULL, - CONSTRAINT test2_x FOREIGN KEY (x_id) REFERENCES test_2_x (id) ON DELETE CASCADE -); - -CREATE TABLE test_2_c ( - id BIGINT NOT NULL PRIMARY KEY, - name text, - created timestamp without time zone, - a_id BIGINT NOT NULL, - b_id BIGINT NOT NULL, - CONSTRAINT test2_a FOREIGN KEY (a_id) REFERENCES test_2_a (id) ON DELETE CASCADE, - CONSTRAINT test2_b FOREIGN KEY (b_id) REFERENCES test_2_b (id) ON DELETE CASCADE -); - -CREATE TABLE test_2_d ( - id BIGINT NOT NULL PRIMARY KEY, - c_id BIGINT NOT NULL, - CONSTRAINT test2_x FOREIGN KEY (c_id) REFERENCES test_2_c (id) ON DELETE CASCADE -); - - -CREATE TABLE test_2_e ( - id BIGINT NOT NULL PRIMARY KEY, - c_id BIGINT NOT NULL, - CONSTRAINT test2_x FOREIGN KEY (c_id) REFERENCES test_2_c (id) ON DELETE CASCADE -); - -INSERT INTO test_2_x (id, name, created) VALUES -(1, 'Xander', '2023-06-01 10:00:00'), -(2, 'Xena', '2023-06-02 11:00:00'), -(3, 'Xavier', '2023-06-03 12:00:00'), -(4, 'Xiomara', '2023-06-04 13:00:00'), -(5, 'Xaviera', '2023-06-05 14:00:00'); - -INSERT INTO test_2_b (id, name, created) VALUES -(1, 'Beta1', '2023-06-01 15:00:00'), -(2, 'Beta2', '2023-06-02 16:00:00'), -(3, 'Beta3', '2023-06-03 17:00:00'), -(4, 'Beta4', '2023-06-04 18:00:00'), -(5, 'Beta5', '2023-06-05 19:00:00'); - -INSERT INTO test_2_a (id, x_id) VALUES -(1, 5), -(2, 4), -(3, 3), -(4, 3), -(5, 1); - -INSERT INTO test_2_c (id, name, created, a_id, b_id) VALUES -(1, 'Gamma1', '2023-06-01 20:00:00', 1, 1), -(2, 'Gamma2', '2023-06-02 21:00:00', 2, 2), -(3, 'Gamma3', '2023-06-03 22:00:00', 3, 3), -(4, 'Gamma4', '2023-06-04 23:00:00', 4, 4), -(5, 'Gamma5', '2023-06-05 00:00:00', 5, 5); - -INSERT INTO test_2_d (id, c_id) VALUES -(1, 1), -(2, 2), -(3, 3), -(4, 4), -(5, 5); - -INSERT INTO test_2_e (id, c_id) VALUES -(1, 1), -(2, 2), -(3, 3), -(4, 4), -(5, 5); - --- Test_BuildQueryMap_MultipleRoots, Test_BuildQueryMap_MultipleSubset, Test_BuildQueryMap_MultipleSubsets_SubsetsByForeignKeysOff -CREATE TABLE test_3_a ( - id BIGINT NOT NULL PRIMARY KEY -); -CREATE TABLE test_3_b ( - id BIGINT NOT NULL PRIMARY KEY, - a_id BIGINT NOT NULL, - CONSTRAINT test3_a FOREIGN KEY (a_id) REFERENCES test_3_a (id) ON DELETE CASCADE -); - CREATE TABLE test_3_c ( - id BIGINT NOT NULL PRIMARY KEY, - b_id BIGINT NOT NULL, - CONSTRAINT test3_b FOREIGN KEY (b_id) REFERENCES test_3_b (id) ON DELETE CASCADE -); - CREATE TABLE test_3_d ( - id BIGINT NOT NULL PRIMARY KEY, - c_id BIGINT NOT NULL, - CONSTRAINT test3_c FOREIGN KEY (c_id) REFERENCES test_3_c (id) ON DELETE CASCADE -); - CREATE TABLE test_3_e ( - id BIGINT NOT NULL PRIMARY KEY, - d_id BIGINT NOT NULL, - CONSTRAINT test3_d FOREIGN KEY (d_id) REFERENCES test_3_d (id) ON DELETE CASCADE -); -CREATE TABLE test_3_f ( - id BIGINT NOT NULL PRIMARY KEY -); -CREATE TABLE test_3_g ( - id BIGINT NOT NULL PRIMARY KEY, - f_id BIGINT NOT NULL, - CONSTRAINT test3_f FOREIGN KEY (f_id) REFERENCES test_3_f (id) ON DELETE CASCADE -); - CREATE TABLE test_3_h ( - id BIGINT NOT NULL PRIMARY KEY, - g_id BIGINT NOT NULL, - CONSTRAINT test3_g FOREIGN KEY (g_id) REFERENCES test_3_g (id) ON DELETE CASCADE -); -CREATE TABLE test_3_i ( - id BIGINT NOT NULL PRIMARY KEY, - h_id BIGINT NOT NULL, - CONSTRAINT test3_h FOREIGN KEY (h_id) REFERENCES test_3_h (id) ON DELETE CASCADE -); - -INSERT INTO test_3_a (id) VALUES -(1), -(2), -(3), -(4), -(5); -INSERT INTO test_3_b (id, a_id) VALUES -(1, 3), -(2, 5), -(3, 1), -(4, 4), -(5, 2); -INSERT INTO test_3_c (id, b_id) VALUES -(1, 2), -(2, 4), -(3, 1), -(4, 3), -(5, 5); -INSERT INTO test_3_d (id, c_id) VALUES -(1, 5), -(2, 1), -(3, 4), -(4, 2), -(5, 3); -INSERT INTO test_3_e (id, d_id) VALUES -(1, 2), -(2, 4), -(3, 1), -(4, 5), -(5, 3); -INSERT INTO test_3_f (id) VALUES -(1), -(2), -(3), -(4), -(5); -INSERT INTO test_3_g (id, f_id) VALUES -(1, 5), -(2, 1), -(3, 4), -(4, 2), -(5, 3); -INSERT INTO test_3_h (id, g_id) VALUES -(1, 4), -(2, 2), -(3, 5), -(4, 1), -(5, 3); -INSERT INTO test_3_i (id,h_id) VALUES -(1, 4), -(8, 2), -(3, 3), -(9, 1), -(5, 3); - --- circular dependency tests -CREATE TABLE addresses ( - id BIGINT NOT NULL PRIMARY KEY, - order_id BIGINT NULL -); - -CREATE TABLE customers ( - id BIGINT NOT NULL PRIMARY KEY, - address_id BIGINT, - CONSTRAINT fk_address - FOREIGN KEY (address_id) - REFERENCES addresses (id) -); - -CREATE TABLE orders ( - id BIGINT NOT NULL PRIMARY KEY, - customer_id BIGINT, - CONSTRAINT fk_customer - FOREIGN KEY (customer_id) - REFERENCES customers (id) -); - -CREATE TABLE payments ( - id BIGINT NOT NULL PRIMARY KEY, - customer_id BIGINT, - CONSTRAINT fk_customer - FOREIGN KEY (customer_id) - REFERENCES customers (id) -); - -INSERT INTO addresses (id, order_id) VALUES -(1, 1), -(2, 2), -(3, 3), -(4, 4), -(5, 5); - -INSERT INTO customers (id, address_id) VALUES -(1, 3), -(2, 5), -(3, 1), -(4, 4), -(5, 2); - -INSERT INTO orders (id, customer_id) VALUES -(1, 5), -(2, 3), -(3, 1), -(4, 4), -(5, 2); - -INSERT INTO payments (id, customer_id) VALUES -(1, 4), -(2, 2), -(3, 1); - - --- Adding the foreign key constraint to create the circular dependency -ALTER TABLE addresses -ADD CONSTRAINT fk_order -FOREIGN KEY (order_id) -REFERENCES orders (id); - - --- composite keys -CREATE TABLE division ( - id BIGINT PRIMARY KEY, - division_name VARCHAR(100), - location VARCHAR(100) -); -CREATE TABLE employees ( - id BIGINT, - division_id BIGINT, - first_name VARCHAR(50), - last_name VARCHAR(50), - email VARCHAR(100), - PRIMARY KEY (id, division_id), - FOREIGN KEY (division_id) REFERENCES division (id) - ON DELETE CASCADE - ON UPDATE CASCADE -); -CREATE TABLE projects ( - id BIGINT PRIMARY KEY, - project_name VARCHAR(100), - start_date DATE, - end_date DATE, - responsible_employee_id BIGINT, - responsible_division_id BIGINT, - CONSTRAINT FK_Projects_Employees FOREIGN KEY (responsible_employee_id, responsible_division_id) - REFERENCES employees (id, division_id) - ON DELETE SET NULL - ON UPDATE CASCADE -); - -INSERT INTO division (id, division_name, location) VALUES -(1, 'Marketing', 'New York'), -(2, 'Finance', 'London'), -(3, 'Human Resources', 'San Francisco'), -(4, 'IT', 'Berlin'), -(5, 'Customer Service', 'Tokyo'); - - -INSERT INTO employees (id, division_id, first_name, last_name, email) VALUES -(6, 1, 'Alice', 'Johnson', 'alice.johnson@example.com'), -(7, 2, 'Bob', 'Smith', 'bob.smith@example.com'), -(8, 3, 'Carol', 'Martinez', 'carol.martinez@example.com'), -(9, 4, 'David', 'Lee', 'david.lee@example.com'), -(10, 5, 'Eva', 'Kim', 'eva.kim@example.com'); - - -INSERT INTO projects (id, project_name, start_date, end_date, responsible_employee_id, responsible_division_id) VALUES -(11, 'Website Redesign', '2023-05-01', '2023-10-01', 6, 1), -(12, 'Financial Audit', '2023-06-15', '2023-07-15', 7, 2), -(13, 'Hiring Initiative', '2023-09-01', '2024-01-31', 8, 3), -(14, 'Software Development', '2023-05-20', '2023-12-20', 9, 4), -(15, 'Customer Feedback Analysis', '2023-07-01', '2023-11-30', 10, 5); - - --- self referencing -CREATE TABLE bosses ( - id BIGINT PRIMARY KEY, - manager_id BIGINT, - big_boss_id BIGINT, - FOREIGN KEY (manager_id) REFERENCES bosses (id) ON UPDATE CASCADE ON DELETE CASCADE, - FOREIGN KEY (big_boss_id) REFERENCES bosses (id) ON UPDATE CASCADE ON DELETE CASCADE -); - -CREATE TABLE minions ( - id BIGINT PRIMARY KEY, - boss_id BIGINT, - FOREIGN KEY (boss_id) REFERENCES bosses (id) ON UPDATE CASCADE ON DELETE CASCADE -); - -INSERT INTO bosses (id, manager_id, big_boss_id) VALUES -(1, NULL, NULL), -(2, 1, NULL), -(3, 2, 1), -(4, 3, 2), -(5, 4, 3), -(6,NULL,NULL); - -INSERT INTO minions (id, boss_id) VALUES -(1, 4), -(2, 3), -(3, 1), -(4, 5), -(5, 2); diff --git a/worker/pkg/query-builder/testdata/teardown.sql b/worker/pkg/query-builder/testdata/teardown.sql deleted file mode 100644 index 6ea269d81a..0000000000 --- a/worker/pkg/query-builder/testdata/teardown.sql +++ /dev/null @@ -1 +0,0 @@ -DROP SCHEMA IF EXISTS genbenthosconfigs_querybuilder CASCADE;