Skip to content

generate ignoring IF NOT EXISTS on ADD COLUMN #1644

Closed
@meeech

Description

@meeech

Version

1.13.0

What happened?

Hi. New user, evaluating sqlc for use.

What Happens:

We have some migrations/*.sql. When running sqlc generate, all the .sql in this folder are being executed.

So i get errors like

migrations/V20220422073404__create_intitial_tables.sql:1:1: relation "integrations" already exists

and

migrations/V20220524081832__created_and_updated_at.sql:1:1: column "created_at" of relation "integrations" already exists

Which is fair, so I'm working to make these statements idempotent.

So in the first case (create_initial_tables.sql), I modify the SQL to use IF NOT EXISTS

CREATE table IF NOT EXISTS integrations ...

run sqlc generate and the error for that migration is resolved.

I try the same on for my 2nd sql where we are altering the table:

ALTER TABLE ONLY integrations ADD COLUMN IF NOT EXISTS created_at TIMESTAMP;

But I continue to the same error. Running this query directly with my sql tool generates no error.

Expectation:

I expect the ALTER TABLE ONLY integrations ADD COLUMN IF NOT EXISTS created_at TIMESTAMP; to respect the IF NOT EXISTS, and not give me an error on generate, similar to the CREATE table statement.

Questions:

  1. Is this a bug?
  2. If no, should I be doing something different to work around this issue?
  3. If yes, if you can point me to the relevant area, I can look to put together a PR. No promises though, pretty new to go at this point :D

Let me know if there are any other details I can provide.

thanks for your time

Relevant log output

No response

Database schema

No response

SQL queries

ALTER TABLE ONLY public.integrations ADD COLUMN IF NOT EXISTS created_at TIMESTAMP;

Configuration

version: "1"
project:
  id: "rt"
packages:
  - path: "internal/db"
    queries: "./queries"
    schema:
      - "./schema.sql"
      - "./migrations"
    engine: "postgresql"

Playground URL

No response

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions