Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally prepend schema for multi-tenancy #3502

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

nicksan222
Copy link

This PR adds the flag emit_schema_name.
This will put before all the tables a %s, and will replace them inside the function

The generated result would look something like this

const getItemsByIds = `-- name: GetItemsByIds :many
SELECT id FROM ` + "`" + `%s` + "`" + `.items
ORDER BY ` + "`" + `index` + "`" + ` ASC
`

// Will add schema name as parameter
func (q *Queries) GetItemsByIds(ctx context.Context, schema string) ([]uint32, error) {
        // Will replace at runtime the schema placeholder
	rows, err := q.db.QueryContext(ctx, strings.ReplaceAll(getItemsByIds, "%s", schema))
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []uint32
	for rows.Next() {
		var id uint32
		if err := rows.Scan(&id); err != nil {
			return nil, err
		}
		items = append(items, id)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant