-
Notifications
You must be signed in to change notification settings - Fork 636
Enable postgis extensions automatically #2760
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
Merged
benjaminjb
merged 3 commits into
CrunchyData:master
from
benjaminjb:enable-postgis-extensions-automatically-clean
Oct 8, 2021
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,7 @@ import ( | |
| "github.com/crunchydata/postgres-operator/internal/logging" | ||
| "github.com/crunchydata/postgres-operator/internal/naming" | ||
| "github.com/crunchydata/postgres-operator/internal/pgaudit" | ||
| "github.com/crunchydata/postgres-operator/internal/postgis" | ||
| "github.com/crunchydata/postgres-operator/internal/postgres" | ||
| pgpassword "github.com/crunchydata/postgres-operator/internal/postgres/password" | ||
| "github.com/crunchydata/postgres-operator/internal/util" | ||
|
|
@@ -188,7 +189,7 @@ func (r *Reconciler) reconcilePostgresDatabases( | |
|
|
||
| // Calculate a hash of the SQL that should be executed in PostgreSQL. | ||
|
|
||
| var pgAuditOK bool | ||
| var pgAuditOK, postgisInstallOK bool | ||
| create := func(ctx context.Context, exec postgres.Executor) error { | ||
| if pgAuditOK = pgaudit.EnableInPostgreSQL(ctx, exec) == nil; !pgAuditOK { | ||
| // pgAudit can only be enabled after its shared library is loaded, | ||
|
|
@@ -201,6 +202,18 @@ func (r *Reconciler) reconcilePostgresDatabases( | |
| "Unable to install pgAudit; try restarting PostgreSQL") | ||
| } | ||
|
|
||
| // Enabling PostGIS extensions is a one-way operation | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the comment(s) 👍 |
||
| // e.g., you can take a PostgresCluster and turn it into a PostGISCluster, | ||
| // but you cannot reverse the process, as that would potentially remove an extension | ||
| // that is being used by some database/tables | ||
| if cluster.Spec.PostGISVersion != "" { | ||
| if postgisInstallOK = postgis.EnableInPostgreSQL(ctx, exec) == nil; !postgisInstallOK { | ||
| // TODO(benjb): Investigate under what conditions postgis would fail install | ||
| r.Recorder.Event(cluster, corev1.EventTypeWarning, "PostGISDisabled", | ||
| "Unable to install PostGIS") | ||
| } | ||
| } | ||
|
|
||
| return postgres.CreateDatabasesInPostgreSQL(ctx, exec, databases.List()) | ||
| } | ||
|
|
||
|
|
@@ -232,7 +245,7 @@ func (r *Reconciler) reconcilePostgresDatabases( | |
| log := logging.FromContext(ctx).WithValues("revision", revision) | ||
| err = errors.WithStack(create(logging.NewContext(ctx, log), podExecutor)) | ||
| } | ||
| if err == nil && pgAuditOK { | ||
| if err == nil && pgAuditOK && postgisInstallOK { | ||
| cluster.Status.DatabaseRevision = revision | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| Copyright 2021 Crunchy Data Solutions, Inc. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package postgis | ||
|
|
||
| import ( | ||
| "context" | ||
| "strings" | ||
|
|
||
| "github.com/crunchydata/postgres-operator/internal/logging" | ||
| "github.com/crunchydata/postgres-operator/internal/postgres" | ||
| ) | ||
|
|
||
| // EnableInPostgreSQL installs triggers for the following extensions into every database: | ||
| // - postgis | ||
| // - postgis_topology | ||
| // - fuzzystrmatch | ||
| // - postgis_tiger_geocoder | ||
| func EnableInPostgreSQL(ctx context.Context, exec postgres.Executor) error { | ||
| log := logging.FromContext(ctx) | ||
|
|
||
| stdout, stderr, err := exec.ExecInAllDatabases(ctx, | ||
| strings.Join([]string{ | ||
| // Quiet NOTICE messages from IF NOT EXISTS statements. | ||
| // - https://www.postgresql.org/docs/current/runtime-config-client.html | ||
| `SET client_min_messages = WARNING;`, | ||
|
|
||
| `CREATE EXTENSION IF NOT EXISTS postgis;`, | ||
| `CREATE EXTENSION IF NOT EXISTS postgis_topology;`, | ||
| `CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;`, | ||
| `CREATE EXTENSION IF NOT EXISTS postgis_tiger_geocoder;`, | ||
| }, "\n"), | ||
| map[string]string{ | ||
| "ON_ERROR_STOP": "on", // Abort when any one statement fails. | ||
| "QUIET": "on", // Do not print successful statements to stdout. | ||
| }) | ||
|
|
||
| log.V(1).Info("enabled PostGIS and related extensions", "stdout", stdout, "stderr", stderr) | ||
|
|
||
| return err | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| Copyright 2021 Crunchy Data Solutions, Inc. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package postgis | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "io" | ||
| "io/ioutil" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "gotest.tools/v3/assert" | ||
| ) | ||
|
|
||
| func TestEnableInPostgreSQL(t *testing.T) { | ||
| expected := errors.New("whoops") | ||
| exec := func( | ||
| _ context.Context, stdin io.Reader, stdout, stderr io.Writer, command ...string, | ||
| ) error { | ||
| assert.Assert(t, stdout != nil, "should capture stdout") | ||
| assert.Assert(t, stderr != nil, "should capture stderr") | ||
|
|
||
| assert.Assert(t, strings.Contains(strings.Join(command, "\n"), | ||
| `SELECT datname FROM pg_catalog.pg_database`, | ||
| ), "expected all databases and templates") | ||
|
|
||
| b, err := ioutil.ReadAll(stdin) | ||
| assert.NilError(t, err) | ||
| assert.Equal(t, string(b), `SET client_min_messages = WARNING; | ||
| CREATE EXTENSION IF NOT EXISTS postgis; | ||
| CREATE EXTENSION IF NOT EXISTS postgis_topology; | ||
| CREATE EXTENSION IF NOT EXISTS fuzzystrmatch; | ||
| CREATE EXTENSION IF NOT EXISTS postgis_tiger_geocoder;`) | ||
|
|
||
| return expected | ||
| } | ||
|
|
||
| ctx := context.Background() | ||
| assert.Equal(t, expected, EnableInPostgreSQL(ctx, exec)) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see changes like this as separate commits ⛏️