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

Use text/template for backfill query generation #632

Merged
merged 2 commits into from
Jan 29, 2025

Conversation

andrew-farries
Copy link
Collaborator

Generate the backfill query with a text/template instead of the batchStatementBuilder.

Using a text/template instead of the builder with its Sprintf statements make the structure of the query easier to see and modify as part of #583.

Part of #583

@andrew-farries andrew-farries marked this pull request as ready for review January 28, 2025 14:24
Copy link
Contributor

@kvch kvch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good. Do you intend to adopt text/template package in all SQL generation parts of pgroll?

@andrew-farries
Copy link
Collaborator Author

Changes look good. Do you intend to adopt text/template package in all SQL generation parts of pgroll?

I don't think it's worth doing everywhere, only for generation of longer SQL statements or functions. The only other place where it's currently used is for trigger and trigger function generation:

const Function = `CREATE OR REPLACE FUNCTION {{ .Name | qi }}()
RETURNS TRIGGER
LANGUAGE PLPGSQL
AS $$
DECLARE
{{- $schemaName := .SchemaName }}
{{- $tableName := .TableName }}
{{ range $name, $col := .Columns }}
{{- $name | qi }} {{ $schemaName | qi }}.{{ $tableName | qi}}.{{ $col.Name | qi }}%TYPE := NEW.{{ $col.Name | qi }};
{{ end -}}
latest_schema text;
search_path text;
BEGIN
SELECT current_setting
INTO search_path
FROM current_setting('search_path');
IF search_path {{- if eq .Direction "up" }} != {{- else }} = {{- end }} {{ .LatestSchema | ql }} {{ if .TestExpr -}} AND {{ .TestExpr }} {{ end -}} THEN
NEW.{{ .PhysicalColumn | qi }} = {{ .SQL }};
{{- if .ElseExpr }}
ELSE
{{ .ElseExpr }};
{{- end }}
END IF;
RETURN NEW;
END; $$
`

The short SQL statements that are generated elsewhere wouldn't benefit from the extra readability that using text/template brings.

Base automatically changed from add-backfill-options to main January 29, 2025 07:16
@andrew-farries andrew-farries force-pushed the use-text-template-for-backfill branch from 6fcc687 to d5af7fd Compare January 29, 2025 07:17
@andrew-farries andrew-farries merged commit cf50739 into main Jan 29, 2025
28 checks passed
@andrew-farries andrew-farries deleted the use-text-template-for-backfill branch January 29, 2025 07:21
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.

2 participants