mirror of
https://github.com/processone/ejabberd.git
synced 2024-11-20 16:15:59 +01:00
Add option update_sql_schema_timeout to allow schema update use longer timeouts
This also makes batch of schema updates to single table use transaction, which should help in not leaving table in inconsistent state if some update steps fails (unless you use mysql where you can't rollback changes to table schemas).
This commit is contained in:
parent
f56739fd9f
commit
ead87e3727
@ -164,6 +164,7 @@
|
||||
-export([sql_username/0, sql_username/1]).
|
||||
-export([trusted_proxies/0]).
|
||||
-export([update_sql_schema/0]).
|
||||
-export([update_sql_schema_timeout/0, update_sql_schema_timeout/1]).
|
||||
-export([use_cache/0, use_cache/1]).
|
||||
-export([validate_stream/0]).
|
||||
-export([version/0]).
|
||||
@ -1109,6 +1110,13 @@ trusted_proxies() ->
|
||||
update_sql_schema() ->
|
||||
ejabberd_config:get_option({update_sql_schema, global}).
|
||||
|
||||
-spec update_sql_schema_timeout() -> 'infinity' | pos_integer().
|
||||
update_sql_schema_timeout() ->
|
||||
update_sql_schema_timeout(global).
|
||||
-spec update_sql_schema_timeout(global | binary()) -> 'infinity' | pos_integer().
|
||||
update_sql_schema_timeout(Host) ->
|
||||
ejabberd_config:get_option({update_sql_schema_timeout, Host}).
|
||||
|
||||
-spec use_cache() -> boolean().
|
||||
use_cache() ->
|
||||
use_cache(global).
|
||||
|
@ -264,6 +264,8 @@ opt_type(new_sql_schema) ->
|
||||
econf:bool();
|
||||
opt_type(update_sql_schema) ->
|
||||
econf:bool();
|
||||
opt_type(update_sql_schema_timeout) ->
|
||||
econf:timeout(second, infinity);
|
||||
opt_type(oauth_access) ->
|
||||
econf:acl();
|
||||
opt_type(oauth_cache_life_time) ->
|
||||
@ -613,6 +615,7 @@ options() ->
|
||||
{net_ticktime, timer:seconds(60)},
|
||||
{new_sql_schema, ?USE_NEW_SQL_SCHEMA_DEFAULT},
|
||||
{update_sql_schema, true},
|
||||
{update_sql_schema_timeout, timer:minutes(5)},
|
||||
{oauth_access, none},
|
||||
{oauth_cache_life_time,
|
||||
fun(Host) -> ejabberd_config:get_option({cache_life_time, Host}) end},
|
||||
|
@ -929,6 +929,12 @@ doc() ->
|
||||
"This option was added in ejabberd 23.10, "
|
||||
"and enabled by default since 24.06. "
|
||||
"The default value is 'true'.")}},
|
||||
{update_sql_schema_timeout,
|
||||
#{value => "timeout()",
|
||||
note => "added in 24.07",
|
||||
desc =>
|
||||
?T("Time allocated to SQL schema update queries. "
|
||||
"The default value is set to 5 minutes.")}},
|
||||
{oauth_access,
|
||||
#{value => ?T("AccessName"),
|
||||
desc => ?T("By default creating OAuth tokens is not allowed. "
|
||||
|
@ -226,6 +226,13 @@ store_version(Host, Module, Version) ->
|
||||
["!module=%(SModule)s",
|
||||
"version=%(Version)d"]).
|
||||
|
||||
store_version_t(Module, Version) ->
|
||||
SModule = misc:atom_to_binary(Module),
|
||||
?SQL_UPSERT_T(
|
||||
"schema_version",
|
||||
["!module=%(SModule)s",
|
||||
"version=%(Version)d"]).
|
||||
|
||||
table_exists(Host, Table) ->
|
||||
ejabberd_sql:sql_query(
|
||||
Host,
|
||||
@ -398,9 +405,7 @@ get_current_version(Host, Module, Schemas) ->
|
||||
Version
|
||||
end.
|
||||
|
||||
sqlite_table_copy(Host, SchemaInfo, Table) ->
|
||||
ejabberd_sql:sql_transaction(Host,
|
||||
fun() ->
|
||||
sqlite_table_copy_t(SchemaInfo, Table) ->
|
||||
TableName = Table#sql_table.name,
|
||||
NewTableName = <<"new_", TableName/binary>>,
|
||||
NewTable = Table#sql_table{name = NewTableName},
|
||||
@ -418,8 +423,7 @@ sqlite_table_copy(Host, SchemaInfo, Table) ->
|
||||
" RENAME TO ", TableName/binary>>,
|
||||
?INFO_MSG("Renameing table ~s to ~s:~n~s~n",
|
||||
[NewTableName, TableName, SQL4]),
|
||||
ejabberd_sql:sql_query_t(SQL4)
|
||||
end).
|
||||
ejabberd_sql:sql_query_t(SQL4).
|
||||
|
||||
format_type(#sql_schema_info{db_type = pgsql}, Column) ->
|
||||
case Column#sql_column.type of
|
||||
@ -875,6 +879,7 @@ update_schema(Host, Module, RawSchemas) ->
|
||||
end.
|
||||
|
||||
do_update_schema(Host, Module, SchemaInfo, Schema) ->
|
||||
F = fun() ->
|
||||
lists:foreach(
|
||||
fun({add_column, TableName, ColumnName}) ->
|
||||
{value, Table} =
|
||||
@ -884,8 +889,7 @@ do_update_schema(Host, Module, SchemaInfo, Schema) ->
|
||||
lists:keysearch(
|
||||
ColumnName, #sql_column.name, Table#sql_table.columns),
|
||||
Res =
|
||||
ejabberd_sql:sql_query(
|
||||
Host,
|
||||
ejabberd_sql:sql_query_t(
|
||||
fun(DBType, _DBVersion) ->
|
||||
Def = format_column_def(SchemaInfo, Column),
|
||||
Default = format_default(SchemaInfo, Column),
|
||||
@ -924,8 +928,7 @@ do_update_schema(Host, Module, SchemaInfo, Schema) ->
|
||||
end;
|
||||
({drop_column, TableName, ColumnName}) ->
|
||||
Res =
|
||||
ejabberd_sql:sql_query(
|
||||
Host,
|
||||
ejabberd_sql:sql_query_t(
|
||||
fun(_DBType, _DBVersion) ->
|
||||
SQL = [<<"ALTER TABLE ">>,
|
||||
TableName,
|
||||
@ -965,8 +968,7 @@ do_update_schema(Host, Module, SchemaInfo, Schema) ->
|
||||
#{ignore := true} -> ok;
|
||||
_ ->
|
||||
Res =
|
||||
ejabberd_sql:sql_query(
|
||||
Host,
|
||||
ejabberd_sql:sql_query_t(
|
||||
fun() ->
|
||||
case Index#sql_index.meta of
|
||||
#{primary_key := true} ->
|
||||
@ -1016,11 +1018,11 @@ do_update_schema(Host, Module, SchemaInfo, Schema) ->
|
||||
Res =
|
||||
case SchemaInfo#sql_schema_info.db_type of
|
||||
sqlite ->
|
||||
sqlite_table_copy(Host, SchemaInfo, Table);
|
||||
sqlite_table_copy_t(SchemaInfo, Table);
|
||||
pgsql ->
|
||||
TableName = Table#sql_table.name,
|
||||
SQL1 = [<<"ALTER TABLE ">>, TableName, <<" DROP CONSTRAINT ",
|
||||
TableName/binary,"_pkey, ",
|
||||
TableName/binary, "_pkey, ",
|
||||
"ADD PRIMARY KEY (">>,
|
||||
lists:join(
|
||||
<<", ">>,
|
||||
@ -1031,8 +1033,7 @@ do_update_schema(Host, Module, SchemaInfo, Schema) ->
|
||||
[Table#sql_table.name,
|
||||
Index#sql_index.columns,
|
||||
SQL]),
|
||||
ejabberd_sql:sql_query(
|
||||
Host,
|
||||
ejabberd_sql:sql_query_t(
|
||||
fun(_DBType, _DBVersion) ->
|
||||
ejabberd_sql:sql_query_t(SQL)
|
||||
end);
|
||||
@ -1052,8 +1053,7 @@ do_update_schema(Host, Module, SchemaInfo, Schema) ->
|
||||
[Table#sql_table.name,
|
||||
Index#sql_index.columns,
|
||||
SQL]),
|
||||
ejabberd_sql:sql_query(
|
||||
Host,
|
||||
ejabberd_sql:sql_query_t(
|
||||
fun(_DBType, _DBVersion) ->
|
||||
ejabberd_sql:sql_query_t(SQL)
|
||||
end)
|
||||
@ -1081,8 +1081,7 @@ do_update_schema(Host, Module, SchemaInfo, Schema) ->
|
||||
[TableName, Columns]);
|
||||
{ok, IndexName} ->
|
||||
Res =
|
||||
ejabberd_sql:sql_query(
|
||||
Host,
|
||||
ejabberd_sql:sql_query_t(
|
||||
fun(DBType, _DBVersion) ->
|
||||
SQL =
|
||||
case DBType of
|
||||
@ -1112,8 +1111,9 @@ do_update_schema(Host, Module, SchemaInfo, Schema) ->
|
||||
end
|
||||
end
|
||||
end, Schema#sql_schema.update),
|
||||
store_version(Host, Module, Schema#sql_schema.version).
|
||||
|
||||
store_version_t(Module, Schema#sql_schema.version)
|
||||
end,
|
||||
ejabberd_sql:sql_transaction(Host, F, ejabberd_option:update_sql_schema_timeout(), 1).
|
||||
|
||||
print_schema(SDBType, SDBVersion, SNewSchema) ->
|
||||
{DBType, DBVersion} =
|
||||
|
Loading…
Reference in New Issue
Block a user