hcl-lang icon indicating copy to clipboard operation
hcl-lang copied to clipboard

Support Schema template for `atlasgo.io`

Open datdao opened this issue 1 year ago • 2 comments

Hi guys,

I'm experimenting with using this library as the base for my language server, and I'm configuring a schema template for Atlas HCL. After a few days of trying to configure the schema to fit with Atlas, I'm stuck on some configuration about references. I hope you all can help me get through it

Here is an example of Atlas HCL to define database schema:

table "users" {
  column "id" {
    type = integer
  }
  primary_key {
    columns = [column.id]
  }
}

table "orders" {
  column "owner_id" {
    type = integer
  }
  foreign_key "owner_id" {
    columns     = [column.owner_id]
    ref_columns = [table.users.column.id]
    on_update   = NO_ACTION
    on_delete   = NO_ACTION
  }
}

The problems I'm facing are how to configure Nested block reference like table.users.column.id and Local reference like column.owner_id.

Screenshot 2023-10-12 at 18 52 31

I have tried defining ScopeID and Targetables, but they don't seem to work. I'm not sure if I'm missing some configuration, or if these features aren't supported yet.

Here is my config:


func tableBlockSchema() *schema.BlockSchema {
  return &schema.BlockSchema{
    Address: &schema.BlockAddrSchema{
      Steps: []schema.AddrStep{
        schema.StaticStep{Name: "table"},
        schema.LabelStep{Index: 0},
      },
      ScopeId:     "table",
      AsReference: true,
    },
    Labels: []*schema.LabelSchema{
      {
        Name: "name",
      },
    },
    Body: &schema.BodySchema{
      Blocks: map[string]*schema.BlockSchema{
        "column": &schema.BlockSchema{
          Address: &schema.BlockAddrSchema{
            Steps: []schema.AddrStep{
              schema.StaticStep{Name: "column"},
              schema.LabelStep{Index: 0},
            },
            ScopeId:     "column",
            AsReference: true,
          },
          Labels: []*schema.LabelSchema{
            {Name: "name"},
          },
          Type: schema.BlockTypeObject,
        },
        "primary_key": &schema.BlockSchema{
          Type: schema.BlockTypeObject,
          Body: &schema.BodySchema{
            Attributes: map[string]*schema.AttributeSchema{
              "columns": &schema.AttributeSchema{
                Constraint: schema.Set{
                  Elem: schema.OneOf{
                    schema.Reference{OfScopeId: "column"},
                  },
                },
              },
            },
          },
        },
        "foreign_key": &schema.BlockSchema{
          Type: schema.BlockTypeObject,
          Body: &schema.BodySchema{
            Attributes: map[string]*schema.AttributeSchema{
              "columns": &schema.AttributeSchema{
                Constraint: schema.Set{
                  Elem: schema.OneOf{
                    schema.Reference{OfScopeId: "column"},
                  },
                },
              },
              "ref_columns": &schema.AttributeSchema{
                Constraint: schema.Set{
                  Elem: schema.OneOf{
                    schema.Reference{OfScopeId: "table"},
                  },
                },
              },
            },
          },
        },
      },
    },
  }
}


datdao avatar Oct 12 '23 12:10 datdao

Hi @datdao First of all, thanks for the interest in the library. I'd caution (as per the Readme) that it's still very much work in progress and the API may change, but LS for AtlasGo looks exactly like a use case this library was designed to potentially solve. In practice though we never tested it outside of Terraform, so YMMV.


Would you mind also providing the code which decodes the configuration based on the schema, so I can understand what exactly you expect to happen and what isn't working?

FWIW We have a relatively decent test coverage of most parts of the code base, so that may help understand how things work, even if they may look confusing at first.

radeksimko avatar Oct 18 '23 07:10 radeksimko

@radeksimko: Hello, I'm a co-worker with @datdao. Thank you for your response here. We also want to contribute to this support if it's missing / undevelopment yet.

First, we will try to send a PR to demonstration of config using your existing unit-test. Then, we can discus what is the next step for this support.

giautm avatar Oct 19 '23 04:10 giautm