Skip to content
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

[base + modules] Issue #670 - Remove side effects in manage_schema functions #671

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

robvandenbogaard
Copy link
Contributor

@robvandenbogaard robvandenbogaard commented Jul 7, 2020

From #670

To avoid concurrency problems in loading data models between dependent modules, remove (database) side effects from manage_schema/2 functions, because in its current form the intended use is to return the data model declaration and let Zotonic take care of installing/updating in dependency order.

@rl-king
Copy link
Contributor

rl-king commented Jul 7, 2020

ping @mworrell

@@ -168,9 +168,7 @@ manage_schema(_Version, Context) ->
]}
]}
]
},
z_datamodel:manage(?MODULE, Datamodel, Context),
schema:create_identity_if_not_exists(editor_dev, "redacteur", "redacteur", Context).
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Proposal to remove the automatic creation of editor_dev. If we need one we should provide it in fixtures.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I've never used it, maybe @loetie has

Copy link
Contributor

Choose a reason for hiding this comment

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

There is also an manage_data/2 function. That is called after the manage_schema/2 has been handled.

See https://test.zotonic.com/page/1353/modules

manage_data(_Version, Context) ->
    %% Whatever data needs to be installed after the datamodel
    %% has been installed.
    ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, thanks.

Copy link
Contributor

Choose a reason for hiding this comment

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

BTW schema is a dangerous name for a module. I can imagine that other projects will also try to use this generic name. Better prefix it with ginger_, as it is a Ginger-only module.

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, I completely agree, that struck me too.

@@ -168,9 +168,7 @@ manage_schema(_Version, Context) ->
]}
]}
]
},
z_datamodel:manage(?MODULE, Datamodel, Context),
schema:create_identity_if_not_exists(editor_dev, "redacteur", "redacteur", Context).
Copy link
Contributor

Choose a reason for hiding this comment

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

Where did this go?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It went nowhere, as I think it is not used / we should not use it (here) and rethink it when we need it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Use manage_data/2, it is called after manage_schema/2

manage_data(_Version, Context) ->
    %% Whatever data needs to be installed after the datamodel
    %% has been installed.
    ok.

@@ -14,12 +14,11 @@
]).

manage_schema(_Version, Context) ->
Copy link
Contributor

Choose a reason for hiding this comment

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

Context -> _Context (unused)

@@ -21,7 +21,7 @@
-include("zotonic.hrl").

manage_schema(_Version, Context) ->
Copy link
Contributor

Choose a reason for hiding this comment

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

Context -> _Context (unused)

@@ -16,7 +16,7 @@
]).

manage_schema(install, Context) ->
Datamodel = #datamodel{
#datamodel{
Copy link
Contributor

Choose a reason for hiding this comment

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

Context -> _Context (unused)

@@ -19,18 +19,20 @@
]).

manage_schema(install, Context) ->
Copy link
Contributor

Choose a reason for hiding this comment

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

Version is install?

Context -> _Context (unused)

Copy link
Contributor

Choose a reason for hiding this comment

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

Version type signature is:

-type manage_schema() :: install
                       | {upgrade, integer()}.

@@ -168,9 +168,7 @@ manage_schema(_Version, Context) ->
]}
]}
]
},
z_datamodel:manage(?MODULE, Datamodel, Context),
schema:create_identity_if_not_exists(editor_dev, "redacteur", "redacteur", Context).
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I've never used it, maybe @loetie has

z_datamodel:manage(?MODULE, Datamodel, Context),

%% TODO: Legacy code ahead! This function must not have side effects;
%% it should only return the data model.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should look into this case to see if it is still necessary to keep this here, or if it could be in a better place.

Copy link
Contributor

Choose a reason for hiding this comment

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

Use manage_data/2 to update/install data after the schema has been installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants