Skip to content

Add event trigger resource management#463

Open
Fabianoshz wants to merge 11 commits into
cyrilgdn:mainfrom
Fabianoshz:main
Open

Add event trigger resource management#463
Fabianoshz wants to merge 11 commits into
cyrilgdn:mainfrom
Fabianoshz:main

Conversation

@Fabianoshz

@Fabianoshz Fabianoshz commented Aug 22, 2024

Copy link
Copy Markdown

I'm opening this as a draft in case anyone wants to give feedback or test the code.

This should allow us to manage event triggers in this provider, here's how the resource would look like:

resource "postgresql_event_trigger" "event_trigger" {
  name = "event_trigger_test"

  database = postgresql_database.database.name
  schema = postgresql_schema.schema.name

  function = postgresql_function.function.name
  on = "ddl_command_end"

  owner = "postgres"

  status = "enable"

  filter {
    variable = "TAG"
    values = [
      "CREATE TABLE",
      "ALTER TABLE",
    ]
  }
}

Closes #398

@Fabianoshz
Fabianoshz marked this pull request as ready for review August 27, 2024 15:44
@cyrilgdn
cyrilgdn force-pushed the main branch 3 times, most recently from f2c2e47 to dea1401 Compare September 8, 2024 17:06
@Fabianoshz

Copy link
Copy Markdown
Author

@cyrilgdn I've used this to add a few hundred event triggers where I work, our fork has a few more commits with some fixes of things that we found after opening the PR, I can push the fixes to this branch but it's not clear to me if you're interested in that, let me know either way.

@cyrilgdn

Copy link
Copy Markdown
Owner

Hi @Fabianoshz ,

Thanks for your work and for opening this PR,
I'm interested yes, I'm just lacking a bit of time to review it properly but will have a look as soon as possible 👍

@Fabianoshz

Copy link
Copy Markdown
Author

Perfect, I take some time this week to push the other fixes we've added on our fork

@cyrilgdn cyrilgdn left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks again for you work, here are some comments. Basically:

  • The ID used in setID needs to be based on database name / event name and not only event name
  • The update function doesn't work well for some cases, I explained how to test it easily in the TestCase directly
  • The Exists function can be removed
  • A small bug in the read function for the owner
  • and a few minor comments

let me know if you need help to fix that.

Comment thread postgresql/resource_postgresql_event_trigger.go Outdated
Comment thread postgresql/resource_postgresql_event_trigger.go Outdated
Comment thread postgresql/resource_postgresql_event_trigger.go Outdated
Comment thread postgresql/resource_postgresql_event_trigger.go Outdated
Comment thread postgresql/resource_postgresql_event_trigger.go Outdated
Comment thread postgresql/resource_postgresql_event_trigger.go Outdated
b = bytes.NewBufferString("ALTER EVENT TRIGGER ")
fmt.Fprint(b, pq.QuoteIdentifier(eventTriggerName))

eventTriggerEnabled := d.Get(eventTriggerStatusAttr).(string)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Does it work for enable_replica and enable_always ?
it seems to generate ALTER EVENT TRIGGER name enable_replica where the documentation says:

ALTER EVENT TRIGGER name ENABLE [ REPLICA | ALWAYS ]

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I actually checked with your test by adding an extra step like:

            {
                Config: fmt.Sprintf(testAccPostgreSQLEventTriggerConfig, dbName, schemas[0], "enable"),
                Check: resource.ComposeTestCheckFunc(
                    testAccCheckPostgresqlEventTriggerExists("postgresql_event_trigger.event_trigger", dbName),
                ),
            },
            {
                Config: fmt.Sprintf(testAccPostgreSQLEventTriggerConfig, dbName, schemas[0], "enable_always"),
                Check: resource.ComposeTestCheckFunc(
                    testAccCheckPostgresqlEventTriggerExists("postgresql_event_trigger.event_trigger", dbName),
                ),
            },

(and changing the conf definition to take %[3]s instead of "enabled" directly)
so it tries to alter the status of the event and it indeed fails with:

Error: pq: syntax error at or near "enable_always"

Full PG logs being:

ERROR:  syntax error at or near "enable_always" at character 34
STATEMENT:  ALTER EVENT TRIGGER "event_trigger_test" enable_always

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I've added a map to fix this

Comment thread postgresql/resource_postgresql_event_trigger.go Outdated
return nil
}

func resourcePostgreSQLEventTriggerUpdate(db *DBConnection, d *schema.ResourceData) error {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

You don't manage the renaming of an event properly, similar to the comment on status you can test easily by updating your test case with a new step that rename it, e.g.:

    {
        Config: fmt.Sprintf(testAccPostgreSQLEventTriggerConfig, dbName, schemas[0], "test_event"),
        Check: resource.ComposeTestCheckFunc(
            testAccCheckPostgresqlEventTriggerExists("postgresql_event_trigger.event_trigger", dbName),
        ),
    },
    {
        Config: fmt.Sprintf(testAccPostgreSQLEventTriggerConfig, dbName, schemas[0], "test_event_renamed"),
        Check: resource.ComposeTestCheckFunc(
            testAccCheckPostgresqlEventTriggerExists("postgresql_event_trigger.event_trigger", dbName),
        ),
    },

In this case, this test will fail with:

Error: pq: event trigger "test_event_renamed" does not exist

You can simply check here if the name changed with something like:

	if d.HasChange(eventTriggerNameAttr) {
		old, new := d.GetChange(eventTriggerNameAttr)
		if _, err := txn.Exec(
			fmt.Sprintf("ALTER EVENT TRIGGER %s RENAME TO %s", pq.QuoteIdentifier(old), pq.QuoteIdentifier(new)),
		); err != nil {
			return err
		}
	}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I've just added a rename, I've also added your step to the tests too.

Comment thread postgresql/resource_postgresql_event_trigger.go Outdated
@Fabianoshz

Copy link
Copy Markdown
Author

@cyrilgdn thanks for throughout review, I will start working on fixing in the next few days

@Fabianoshz Fabianoshz left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@cyrilgdn sorry for the delay, I've addressed your requests, let me know if you think I should change anything, I will also test this on my environment in the meantime.

return nil
}

func resourcePostgreSQLEventTriggerUpdate(db *DBConnection, d *schema.ResourceData) error {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I've just added a rename, I've also added your step to the tests too.

b = bytes.NewBufferString("ALTER EVENT TRIGGER ")
fmt.Fprint(b, pq.QuoteIdentifier(eventTriggerName))

eventTriggerEnabled := d.Get(eventTriggerStatusAttr).(string)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I've added a map to fix this

@Fabianoshz
Fabianoshz requested a review from cyrilgdn November 27, 2024 17:18
@Temurson

Copy link
Copy Markdown

My organization would also benefit from this feature! @Fabianoshz really appreciate your contribution here.
@cyrilgdn please let me know if I can help in any way to get this feature in!

@retrry

retrry commented May 7, 2025

Copy link
Copy Markdown

This would be very nice to have in provider! It would benefit me too.

@zachary-povey

Copy link
Copy Markdown

@cyrilgdn - any chance this could get reviewed again? Happy to help wherever possible, at the minute without it we'd need to combine use of the provider with coupled manual DB management that we're hoping to avoid.

@n-badtke-cg

n-badtke-cg commented Jul 17, 2026

Copy link
Copy Markdown

If somebody is interested, I have built arm64 and amd64 binaries of the provider: https://github.com/n-badtke-cg/terraform-provider-postgresql/tree/build/postgresql_event_trigger/dist

you could download one of the binaries and use it from local in your tofu/terraform CLI with a .tofurc/.terraformrc config which is redirecting the pull source from internet to local file until this PR is getting merged :) See https://stackoverflow.com/a/73743106

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.

Feature Request: Add event trigger management

6 participants