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

Database updater mechanism #43

Closed
arjan opened this Issue Sep 5, 2011 · 4 comments

Comments

Projects
None yet
2 participants
@arjan
Member

arjan commented Sep 5, 2011

From http://code.google.com/p/zotonic/issues/detail?id=37

Create an updating mechanism which allows fundamental database changes to
happen between Zotonic releases.

@arjan

This comment has been minimized.

Show comment
Hide comment
@arjan

arjan Oct 13, 2011

Member

The current z_db:ensure_table/2 routine, which does magic to keep a database table up to date, is not ideal. It cannot know about data modifications since those are module specific.

A better way is to let modules have a "schema version" property and let the module manager call an upgrade procedure, so that modules can write their own update scripts (adding/removing/renaming columns, indexes, denormalizing data, etc)

-schema_version(3).

%% @doc Clean install according to current schema
schema_manage(install, Context) ->
  %% create your tables..
  z_db:q("CREATE TABLE ....", Context),
  %% but also manage the rsc datamodel!
  z_datamodel:manage(#datamodel{...});

%% the first upgrader for schema 1 -> 2
schema_manage({upgrade, 2}, Context) ->
  z_db:q("...");

%% @doc Upgrader for schema 2 -> 3
schema_manage({upgrade, 3}, Context) ->
  z_db:q("...").

Of course, this is optional for modules and only needed for those modules that manage their own data.

Member

arjan commented Oct 13, 2011

The current z_db:ensure_table/2 routine, which does magic to keep a database table up to date, is not ideal. It cannot know about data modifications since those are module specific.

A better way is to let modules have a "schema version" property and let the module manager call an upgrade procedure, so that modules can write their own update scripts (adding/removing/renaming columns, indexes, denormalizing data, etc)

-schema_version(3).

%% @doc Clean install according to current schema
schema_manage(install, Context) ->
  %% create your tables..
  z_db:q("CREATE TABLE ....", Context),
  %% but also manage the rsc datamodel!
  z_datamodel:manage(#datamodel{...});

%% the first upgrader for schema 1 -> 2
schema_manage({upgrade, 2}, Context) ->
  z_db:q("...");

%% @doc Upgrader for schema 2 -> 3
schema_manage({upgrade, 3}, Context) ->
  z_db:q("...").

Of course, this is optional for modules and only needed for those modules that manage their own data.

@kaos

This comment has been minimized.

Show comment
Hide comment
@kaos

kaos Oct 13, 2011

Member

And possibly a schema_manage({downgrade, N}, Context) would be useful. Which downgrades from N+1 to N.

Member

kaos commented Oct 13, 2011

And possibly a schema_manage({downgrade, N}, Context) would be useful. Which downgrades from N+1 to N.

@arjan

This comment has been minimized.

Show comment
Hide comment
@arjan

arjan Oct 13, 2011

Member

Implemented in fde398d

Member

arjan commented Oct 13, 2011

Implemented in fde398d

@arjan arjan closed this Oct 13, 2011

@arjan

This comment has been minimized.

Show comment
Hide comment
@arjan

arjan Oct 13, 2011

Member

Note that old datamodel/0 and datamodel/1 functions in modules need to be rewritten.

The most simple schema updater looks like this:

-mod_schema(1).

-export([manage_schema/2]).

manage_schema(install, _Context) ->
    #datamodel{....}.
Member

arjan commented Oct 13, 2011

Note that old datamodel/0 and datamodel/1 functions in modules need to be rewritten.

The most simple schema updater looks like this:

-mod_schema(1).

-export([manage_schema/2]).

manage_schema(install, _Context) ->
    #datamodel{....}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment