Skip to content

Conversation

@the-glu
Copy link
Contributor

@the-glu the-glu commented Feb 6, 2025

Description

Detect and add as dependency functions that can be used as default.

I'm really not sure I'm not missing some locations where I should process the new DependsOnFunctions field, but it's working for my case ;)

Motivation

See #191 for the base case issue, where default value use functions ;)

Testing

Tested locally, tests yet to be added :)

Copy link
Collaborator

@bplunkett-stripe bplunkett-stripe left a comment

Choose a reason for hiding this comment

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

Thanks for taking a stab at this!

Some changes to make:

  • Update the dependency generation
  • Update the schema_test to ensure the functions are correctly fetched
  • Create acceptance tests for this and validate the existing acceptance tests all pass. These will be your best friend. The code is fragile; the tests are robust.

Comment on lines +1424 to +1443

var deps []dependency = []dependency{
mustRun(csg.GetSQLVertexId(col, diffTypeDelete)).before(csg.GetSQLVertexId(col, diffTypeAddAlter)),
}, nil
}

for _, depFunction := range col.DependsOnFunctions {
deps = append(deps, mustRun(csg.GetSQLVertexId(col, diffTypeDelete)).after(buildFunctionVertexId(depFunction, diffTypeDelete)))
}

return deps, nil

}

func (csg *columnSQLVertexGenerator) GetDeleteDependencies(_ schema.Column) ([]dependency, error) {
return nil, nil
func (csg *columnSQLVertexGenerator) GetDeleteDependencies(col schema.Column) ([]dependency, error) {

var deps []dependency
for _, depFunction := range col.DependsOnFunctions {
deps = append(deps, mustRun(csg.GetSQLVertexId(col, diffTypeDelete)).before(buildFunctionVertexId(depFunction, diffTypeDelete)))
}
return deps, nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe this does nothing right now, because the functions exist in the root graph. Ideally, we merge the graphs, but that's too much effort right now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So they should be removed ?

)
}

for _, col := range table.Columns {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is the currently awkward part about column sql generation: add/delete of columns is pushed under table add/alter.

As a result, what I believe you need to do:

  • If the column is new/altered, then you need to build a dependency between the table and the function such that function add/alter comes BEFORE the table add/alter
  • If the column is being deleted, then you need to build a dependency between the table and the function such that the function deletes comes BEFORE the table add/alter
    ^
    The same is true if the table is being totally created or totally deleted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but that what I'm doing no ?

Here we're in table add/alter, and we add a dep for each column's function that have a dependency, and just bellow the delete (in opposite mode)

diffTypeDelete may be wrong there however ^^'

Copy link
Contributor Author

@the-glu the-glu left a comment

Choose a reason for hiding this comment

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

Sorry I took me so long to be able to fix that one, I had to focus on something else ^^'

I added some acceptance tests (in the function_cases_test) that do pass.

I'm not sure I understood what I would update in schema_test. Should I add the base test case to be sure it's handled?

I left my question about dependency generation on individual comments as well.

)
}

for _, col := range table.Columns {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but that what I'm doing no ?

Here we're in table add/alter, and we add a dep for each column's function that have a dependency, and just bellow the delete (in opposite mode)

diffTypeDelete may be wrong there however ^^'

Comment on lines +1424 to +1443

var deps []dependency = []dependency{
mustRun(csg.GetSQLVertexId(col, diffTypeDelete)).before(csg.GetSQLVertexId(col, diffTypeAddAlter)),
}, nil
}

for _, depFunction := range col.DependsOnFunctions {
deps = append(deps, mustRun(csg.GetSQLVertexId(col, diffTypeDelete)).after(buildFunctionVertexId(depFunction, diffTypeDelete)))
}

return deps, nil

}

func (csg *columnSQLVertexGenerator) GetDeleteDependencies(_ schema.Column) ([]dependency, error) {
return nil, nil
func (csg *columnSQLVertexGenerator) GetDeleteDependencies(col schema.Column) ([]dependency, error) {

var deps []dependency
for _, depFunction := range col.DependsOnFunctions {
deps = append(deps, mustRun(csg.GetSQLVertexId(col, diffTypeDelete)).before(buildFunctionVertexId(depFunction, diffTypeDelete)))
}
return deps, nil
Copy link
Contributor Author

Choose a reason for hiding this comment

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

So they should be removed ?

@bplunkett-stripe bplunkett-stripe self-requested a review April 1, 2025 13:06
@the-glu
Copy link
Contributor Author

the-glu commented Jun 17, 2025

Hi @bplunkett-stripe

Just a gentle reminder about this PR, when you have a moment, could you please take a quick look?

Thanks a lot!

@tonycosentini
Copy link
Contributor

@bplunkett-stripe is there anything preventing this from ending up in the tool? Not being able to modify columns that use functions as defaults is a fairly big limitations (at least in my use cases).

@bplunkett-stripe
Copy link
Collaborator

@bplunkett-stripe is there anything preventing this from ending up in the tool? Not being able to modify columns that use functions as defaults is a fairly big limitations (at least in my use cases).

I'll prioritize getting this merged in.

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.

3 participants