obevo icon indicating copy to clipboard operation
obevo copied to clipboard

Cross Schema Dependencies

Open IvanAtanasov opened this issue 6 years ago • 5 comments

Another issue we are having is cross schema dependencies. Reading the documentation this is supposed to work so could be user error.

For example

schema_one/table/my_table.sql

//// CHANGE name=create
CREATE TABLE my_table (
  col1  INT2 NOT NULL,
  col2 INT2 NOT NULL
)
GO

//// CHANGE name=foreign_key
ALTER TABLE my_table
  ADD CONSTRAINT foreign_key_example
FOREIGN KEY (col1)
REFERENCES schema_two.linked_table (col1)
MATCH SIMPLE
GO

schema_two/table/linked_table.sql

//// CHANGE name=create
CREATE TABLE linked_table (
  col1  INT2 NOT NULL,
  col2 INT2 NOT NULL
)
GO

I would expect when deploying this, obevo would find the dependency and therefore execute the linked_table creation first. This does not happen though.

Both schemas are included in the system-config.xml like this:

    <schemas>
        <schema name="schema_one"/>
        <schema name="schema_two"/>
    </schemas>

However it does seem to work when both tables are in the same schema.

Thanks, Ivan

IvanAtanasov avatar Mar 21 '18 12:03 IvanAtanasov

Hi Ivan, You are correct that the automatic discovery from text only works if it is in the same schema, and not across schemas.

For now, you can enforce the order using the "includeDependencies" attribute per the doc. Example below: //// CHANGE name=foreign_key includeDependencies=schema_two.linked_table.create

You can also depend on the object itself; for tables, this will default to the last change in the file: //// CHANGE name=foreign_key includeDependencies=schema_two.linked_table

Note - use the logical schema name when declaring the dependency, not the physical. i.e. if you map schema_one to schema_one_dev in one of your environments, your code should still refer to the dependency as schema_one.mytable

I will keep this issue open to track the feature for supporting dynamic cross schema dependencies.

shantstepanian avatar Mar 21 '18 22:03 shantstepanian

Hi,

Just got back to looking this and have tried your suggestion.

Unfortunately even explicitly adding the dependency as described does not work. The table with the dependency is still created before the other tables.

Any more ideas? Or could I be doing something wrong.

Thanks, Ivan

IvanAtanasov avatar Oct 05 '18 15:10 IvanAtanasov

I will try this on my side and get back to you

shantstepanian avatar Oct 05 '18 15:10 shantstepanian

The "includeDependencies" attribute should ideally work, but it looks like a bug on our side; I will look into that.

In the meantime, use the "dependencies" attribute

//// CHANGE name=foreign_key dependencies=schema_two.linked_table
ALTER TABLE my_table
  ADD CONSTRAINT foreign_key_example
FOREIGN KEY (col1)

shantstepanian avatar Oct 06 '18 12:10 shantstepanian

Updating the TODO for this ticket

  • Implement this cross-schema dependency discovery, notably in TextDependencyExtractor
  • Ensure that the explicit dependencies / includeDependencies have unit tests (notably for GraphEnricherImpl). I believe the includeDependencies issue in the last comment has been fixed; just need a double-check on the tests

shantstepanian avatar Jun 23 '20 16:06 shantstepanian