ariga / atlas

Manage your database schema as code
https://atlasgo.io
Apache License 2.0
5.37k stars 236 forks source link

Extension comments not added to Terraform state when using Terraform apply with the Atlas provider #2757

Open ttrine opened 1 month ago

ttrine commented 1 month ago

Atlas itself detects no changes:

% atlas schema apply --dry-run -c file://schema/atlas.hcl --env dev
Schema is synced, no changes to be made

with the dev env configured as

env {
  name = atlas.env
  src = "file://schema/public.hcl"
  url = "postgres://127.0.0.1?sslmode=disable"
  dev = "postgres://127.0.0.1/atlas_dev?sslmode=disable"
}

But Terraform gives the following plan repeatedly on apply:

% terraform plan

      ...

      extension "google_columnar_engine" {
        schema  = schema.public
        version = "1.0"
    +   comment = "Google extension for columnar engine"
      }
      extension "google_db_advisor" {
        schema  = schema.public
        version = "1.0"
    +   comment = "Google extension for Database Advisor"
      }
      extension "hypopg" {
        schema  = schema.public
        version = "1.3.2"
    +   comment = "Hypothetical indexes for PostgreSQL"
      }
      extension "postgis" {
        schema  = schema.public
        version = "3.2.5"
        comment = "PostGIS geometry and geography spatial types and functions"
      }

I happen to be using GCP's AlloyDB which is essentially a managed PostgreSQL instance with some added Google-proprietary extensions. The Terraform configuration gives Atlas a separate database (on the same instance) to use as a dev database.

data "atlas_schema" "public" {
  src = file("${path.module}/schema/public.hcl")
  dev_url = "postgres://127.0.0.1/atlas_dev?sslmode=disable"
  depends_on = [
    postgresql_database.kateri,
    postgresql_database.atlas_dev
  ]
}

resource "atlas_schema" "public" {
  hcl = data.atlas_schema.public.hcl
  url = "postgres://127.0.0.1?sslmode=disable"
  dev_url = data.atlas_schema.public.dev_url

  depends_on = [ 
    postgresql_database.kateri,
    postgresql_database.atlas_dev
  ]
}

The dev database is empty except for the public schema.

It's possible this is user error but I haven't seen any references to this issue online or been able to track down the cause myself.

giautm commented 1 month ago

Thank you for report this, I'll look into it soon.

a8m commented 1 month ago

I think that's because the database (defined in TF) information schema does not return the comments as defined and returned from your local dev-database. You can simply remove these comments from the schema.hcl and re-run this.

ttrine commented 1 month ago

Thanks for your suggestion @a8m. I did try that, but for some reason it yields the same Terraform plan I gave above after several applications of terraform apply.

@giautm thank you. Please let me know if you need more detail to reproduce this - I'm not sure which extra information is relevant.