/modules/mod_backup/models/m_backup_revision.erl

https://github.com/arjan/zotonic · Erlang · 143 lines · 101 code · 23 blank · 19 comment · 0 complexity · 14f5bcdaac5ecede9294855b6e9ca50c MD5 · raw file

  1. %% @author Marc Worrell <marc@worrell.nl>
  2. %% @copyright 2012 Marc Worrell
  3. %% @doc Manage a resource's revisions.
  4. %% Copyright 2012 Marc Worrell
  5. %%
  6. %% Licensed under the Apache License, Version 2.0 (the "License");
  7. %% you may not use this file except in compliance with the License.
  8. %% You may obtain a copy of the License at
  9. %%
  10. %% http://www.apache.org/licenses/LICENSE-2.0
  11. %%
  12. %% Unless required by applicable law or agreed to in writing, software
  13. %% distributed under the License is distributed on an "AS IS" BASIS,
  14. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. %% See the License for the specific language governing permissions and
  16. %% limitations under the License.
  17. -module(m_backup_revision).
  18. -behaviour(gen_model).
  19. -export([
  20. m_find_value/3,
  21. m_to_list/2,
  22. m_value/2,
  23. save_revision/3,
  24. get_revision/2,
  25. list_revisions/2,
  26. list_revisions_assoc/2,
  27. install/1
  28. ]).
  29. -include("zotonic.hrl").
  30. -define(BACKUP_TYPE_PROPS, $P).
  31. m_find_value(list, #m{value=undefined} = M, _Context) ->
  32. M#m{value=list};
  33. m_find_value(Id, #m{value=list}, Context) when is_integer(Id) ->
  34. list_revisions_assoc(Id, Context);
  35. m_find_value(_X, #m{}, _Context) ->
  36. undefined.
  37. m_to_list(_m, _Context) ->
  38. [].
  39. m_value(_m, _Context) ->
  40. undefined.
  41. save_revision(Id, Props, Context) ->
  42. Version = proplists:get_value(version, Props),
  43. LastVersion = z_db:q1("select version from backup_revision where rsc_id = $1 order by created desc limit 1", [Id], Context),
  44. case Version of
  45. LastVersion when LastVersion =/= undefined ->
  46. ok;
  47. _ ->
  48. UserId = z_acl:user(Context),
  49. 1 = z_db:q("
  50. insert into backup_revision
  51. (rsc_id, type, version, user_id, user_name, data_type, data)
  52. values ($1, $2, $3, $4, $5, $6, $7)
  53. ", [
  54. Id,
  55. ?BACKUP_TYPE_PROPS,
  56. proplists:get_value(version, Props),
  57. UserId,
  58. z_string:truncate(
  59. z_trans:lookup_fallback(
  60. m_rsc:p_no_acl(UserId, title, Context),
  61. Context),
  62. 60),
  63. "erlang",
  64. erlang:term_to_binary(Props, [compressed])
  65. ],
  66. Context),
  67. ok = prune_revisions(Id, Context),
  68. ok
  69. end.
  70. get_revision(RevId, Context) ->
  71. case z_db:assoc_row("select * from backup_revision where id = $1", [RevId], Context) of
  72. undefined ->
  73. {error, notfound};
  74. Row ->
  75. R1 = proplists:delete(data, Row),
  76. {ok, [ {data, erlang:binary_to_term(proplists:get_value(data, Row)) } | R1 ]}
  77. end.
  78. list_revisions(Id, Context) ->
  79. z_db:q("
  80. select id, type, created, version, user_id, user_name
  81. from backup_revision
  82. where rsc_id = $1
  83. order by created desc", [Id], Context).
  84. list_revisions_assoc(Id, Context) ->
  85. z_db:assoc("
  86. select id, type, created, version, user_id, user_name
  87. from backup_revision
  88. where rsc_id = $1
  89. order by created desc", [Id], Context).
  90. %% @doc Prune the old revisions in the database. Drops revisions close to each other.
  91. prune_revisions(_Id, _Context) ->
  92. % TODO
  93. ok.
  94. %% @doc Install the revisions table.
  95. install(Context) ->
  96. case z_db:table_exists(backup_revision, Context) of
  97. false ->
  98. [] = z_db:q("
  99. create table backup_revision (
  100. id bigserial not null,
  101. type character(1) not null,
  102. rsc_id integer not null,
  103. created timestamp not null default current_timestamp,
  104. version integer,
  105. user_id integer,
  106. user_name character varying(80),
  107. filename character varying(400),
  108. note character varying(200),
  109. data_type character varying(10) not null,
  110. data bytea not null,
  111. primary key (id)
  112. )
  113. ", Context),
  114. [] = z_db:q("
  115. create index backup_revision_id_created on backup_revision (rsc_id, created)
  116. ", Context),
  117. ok;
  118. true ->
  119. ok
  120. end.