Skip to content

Handle columns who use functions as defaults #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

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 ?

@@ -1074,6 +1074,13 @@ func (t *tableSQLVertexGenerator) GetAddAlterDependencies(table, _ schema.Table)
mustRun(t.GetSQLVertexId(table, diffTypeAddAlter)).after(buildTableVertexId(*table.ParentTable, diffTypeAddAlter)),
)
}

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.

@@ -1074,6 +1074,13 @@ func (t *tableSQLVertexGenerator) GetAddAlterDependencies(table, _ schema.Table)
mustRun(t.GetSQLVertexId(table, diffTypeAddAlter)).after(buildTableVertexId(*table.ParentTable, diffTypeAddAlter)),
)
}

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
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.

2 participants