Skip to content

Commit

Permalink
Fix if prod check not working for custom SQL APIs (#4498)
Browse files Browse the repository at this point in the history
  • Loading branch information
begelundmuller authored Apr 3, 2024
1 parent 9f8603a commit 467b9f9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions runtime/drivers/sqlite/migrations/0019.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Fixes wrong default environment value in migration 0016.sql.
UPDATE instances SET environment = 'prod' WHERE environment = 'production';
13 changes: 10 additions & 3 deletions runtime/resolvers/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,17 @@ func newSQLWithRefs(ctx context.Context, opts *runtime.ResolverOptions, extraRef
return nil, err
}

inst, err := opts.Runtime.Instance(ctx, opts.InstanceID)
if err != nil {
return nil, err
}

olap, release, err := opts.Runtime.OLAP(ctx, opts.InstanceID, props.Connector)
if err != nil {
return nil, err
}

resolvedSQL, refs, err := buildSQL(props.SQL, olap.Dialect(), opts.Args, opts.UserAttributes, opts.ForExport)
resolvedSQL, refs, err := buildSQL(props.SQL, olap.Dialect(), opts.Args, inst, opts.UserAttributes, opts.ForExport)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -226,11 +231,13 @@ func (r *sqlResolver) generalExport(ctx context.Context, w io.Writer, filename s
}

// buildSQL resolves the SQL template and returns the resolved SQL and the resource names it references.
func buildSQL(sqlTemplate string, dialect drivers.Dialect, args, userAttributes map[string]any, forExport bool) (string, []*runtimev1.ResourceName, error) {
func buildSQL(sqlTemplate string, dialect drivers.Dialect, args map[string]any, inst *drivers.Instance, userAttributes map[string]any, forExport bool) (string, []*runtimev1.ResourceName, error) {
// Resolve the SQL template
var refs []*runtimev1.ResourceName
sql, err := compilerv1.ResolveTemplate(sqlTemplate, compilerv1.TemplateData{
User: userAttributes,
Environment: inst.Environment,
User: userAttributes,
Variables: inst.ResolveVariables(),
ExtraProps: map[string]any{
"args": args,
"export": forExport,
Expand Down

0 comments on commit 467b9f9

Please sign in to comment.