Skip to content

Commit

Permalink
Merge pull request #2874 from zotonic/0x-m_seo_sitemap-delete_before
Browse files Browse the repository at this point in the history
seo_sitemap: add m_seo_sitemap:delete_before/3. (0.x)
  • Loading branch information
robvandenbogaard committed Feb 24, 2022
2 parents 58a2296 + 452648e commit 72f3813
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
2 changes: 1 addition & 1 deletion modules/mod_seo_sitemap/mod_seo_sitemap.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
-mod_prio(600).
-mod_depends([seo]).
-mod_provides([seo_sitemap]).
-mod_schema(2).
-mod_schema(3).

-include_lib("zotonic.hrl").
-include("include/seo_sitemap.hrl").
Expand Down
34 changes: 32 additions & 2 deletions modules/mod_seo_sitemap/models/m_seo_sitemap.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
slice/3,
update/2,
delete_key/3,
delete_before/3,
delete_loc/2,

update_rsc/2,
Expand All @@ -42,6 +43,8 @@
-include_lib("zotonic.hrl").
-include("../include/seo_sitemap.hrl").

-define(SQL_TIMEOUT, 120000).

m_find_value(urlsets, #m{ value = undefined }, Context) ->
seo_sitemap:urlsets(Context);
m_find_value(urlset, #m{ value = undefined } = M, _Context) ->
Expand Down Expand Up @@ -212,7 +215,8 @@ delete_key(Source, all, Context) ->
delete from seo_sitemap
where source = $1",
[ Source ],
Context)
Context,
?SQL_TIMEOUT)
of
0 -> {error, enoent};
_ -> ok
Expand All @@ -229,6 +233,22 @@ delete_key(Source, Key, Context) ->
_ -> ok
end.

%% @doc Delete all keys in a source that have been modified before a certain date.
%% This is useful to cleanup after batch updates, as the insert always updates
%% the modified date.
-spec delete_before( binary(), calendar:datetime(), z:context() ) -> ok | {error, term()}.
delete_before(Source, Modified, Context) ->
case z_db:q("
delete from seo_sitemap
where source = $1
and modified < $2",
[ Source, Modified ],
Context,
?SQL_TIMEOUT)
of
0 -> {error, enoent};
_ -> ok
end.

%% @doc Delete a specific location from the sitemap.
-spec delete_loc( binary(), z:context() ) -> ok | {error, term()}.
Expand Down Expand Up @@ -424,6 +444,7 @@ install(Context) ->
", Context),
Indices = [
{"seo_sitemap_source_key_key", "source, key"},
{"seo_sitemap_source_modified_key", "source, modified"},
{"seo_sitemap_loc_key", "loc"},
{"fki_seo_sitemap_rsc_id", "rsc_id"},
{"fki_seo_sitemap_category_id", "category_id"}
Expand All @@ -434,7 +455,16 @@ install(Context) ->
rebuild_rsc(Context),
ok;
true ->
ok
case z_db:key_exists(seo_sitemap, seo_sitemap_source_modified_key, Context) of
false ->
[] = z_db:q("
create index seo_sitemap_source_modified_key
on seo_sitemap (source, modified)", Context),
z_db:flush(Context),
ok;
true ->
ok
end
end.


Expand Down
16 changes: 15 additions & 1 deletion src/db/z_db.erl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
update_sequence/3,
prepare_database/1,
table_exists/2,
key_exists/3,
create_table/3,
drop_table/2,
flush/1,
Expand Down Expand Up @@ -860,7 +861,7 @@ create_schema(_Site, Connection, Schema) ->
end.

%% @doc Check the information schema if a certain table exists in the context database.
-spec table_exists(string(), #context{}) -> boolean().
-spec table_exists(string() | atom(), #context{}) -> boolean().
table_exists(Table, Context) ->
Options = z_db_pool:get_database_options(Context),
Db = proplists:get_value(dbdatabase, Options),
Expand All @@ -875,6 +876,19 @@ table_exists(Table, Context) ->
0 -> false
end.

-spec key_exists(string() | atom(), binary() | string() | atom(), z:context()) -> boolean().
key_exists(Table, Key, Context) ->
Options = z_db_pool:get_database_options(Context),
Schema = proplists:get_value(dbschema, Options),
HasKey = z_db:q1("
select count(*)
from pg_indexes
where schemaname = $1
and tablename = $2
and indexname = $3",
[ Schema, Table, Key ],
Context),
HasKey >= 1.

%% @doc Make sure that a table is dropped, only when the table exists
drop_table(Name, Context) when is_atom(Name) ->
Expand Down

0 comments on commit 72f3813

Please sign in to comment.