mirror of
https://github.com/processone/ejabberd.git
synced 2024-11-20 16:15:59 +01:00
Add update_primary_key command to sql schema updater
This commit is contained in:
parent
c5437235f3
commit
9ab60935a4
@ -394,6 +394,29 @@ get_current_version(Host, Module, Schemas) ->
|
||||
Version
|
||||
end.
|
||||
|
||||
sqlite_table_copy(Host, SchemaInfo, Table) ->
|
||||
ejabberd_sql:sql_transaction(Host,
|
||||
fun() ->
|
||||
TableName = Table#sql_table.name,
|
||||
NewTableName = <<"new_", TableName/binary>>,
|
||||
NewTable = Table#sql_table{name = NewTableName},
|
||||
create_table_t(SchemaInfo, NewTable),
|
||||
SQL2 = <<"INSERT INTO ", NewTableName/binary,
|
||||
" SELECT * FROM ", TableName/binary>>,
|
||||
?INFO_MSG("Copying table ~s to ~s:~n~s~n",
|
||||
[TableName, NewTableName, SQL2]),
|
||||
ejabberd_sql:sql_query_t(SQL2),
|
||||
SQL3 = <<"DROP TABLE ", TableName/binary>>,
|
||||
?INFO_MSG("Droping old table ~s:~n~s~n",
|
||||
[TableName, SQL2]),
|
||||
ejabberd_sql:sql_query_t(SQL3),
|
||||
SQL4 = <<"ALTER TABLE ", NewTableName/binary,
|
||||
" RENAME TO ", TableName/binary>>,
|
||||
?INFO_MSG("Renameing table ~s to ~s:~n~s~n",
|
||||
[NewTableName, TableName, SQL4]),
|
||||
ejabberd_sql:sql_query_t(SQL4)
|
||||
end).
|
||||
|
||||
format_type(#sql_schema_info{db_type = pgsql}, Column) ->
|
||||
case Column#sql_column.type of
|
||||
text -> <<"text">>;
|
||||
@ -697,9 +720,12 @@ format_create_table(#sql_schema_info{db_type = mysql} = SchemaInfo, Table) ->
|
||||
Table#sql_table.indices).
|
||||
|
||||
create_table(Host, SchemaInfo, Table) ->
|
||||
ejabberd_sql:sql_query(
|
||||
Host,
|
||||
ejabberd_sql:sql_query(Host,
|
||||
fun() ->
|
||||
create_table_t(SchemaInfo, Table)
|
||||
end).
|
||||
|
||||
create_table_t(SchemaInfo, Table) ->
|
||||
SQLs = format_create_table(SchemaInfo, Table),
|
||||
?INFO_MSG("Creating table ~s:~n~s~n",
|
||||
[Table#sql_table.name, SQLs]),
|
||||
@ -713,8 +739,7 @@ create_table(Host, SchemaInfo, Table) ->
|
||||
lists:foreach(
|
||||
fun(SQL) -> ejabberd_sql:sql_query_t(SQL) end,
|
||||
PostSQLs)
|
||||
end
|
||||
end).
|
||||
end.
|
||||
|
||||
create_tables(Host, Module, SchemaInfo, Schema) ->
|
||||
lists:foreach(
|
||||
@ -969,6 +994,74 @@ do_update_schema(Host, Module, SchemaInfo, Schema) ->
|
||||
ok
|
||||
end
|
||||
end;
|
||||
({update_primary_key, TableName, Columns1}) ->
|
||||
Columns =
|
||||
case ejabberd_sql:use_new_schema() of
|
||||
true ->
|
||||
Columns1;
|
||||
false ->
|
||||
lists:delete(
|
||||
<<"server_host">>, Columns1)
|
||||
end,
|
||||
{value, Table} =
|
||||
lists:keysearch(
|
||||
TableName, #sql_table.name, Schema#sql_schema.tables),
|
||||
{value, Index} =
|
||||
lists:keysearch(
|
||||
Columns, #sql_index.columns, Table#sql_table.indices),
|
||||
Res =
|
||||
case SchemaInfo#sql_schema_info.db_type of
|
||||
sqlite ->
|
||||
sqlite_table_copy(Host, SchemaInfo, Table);
|
||||
pgsql ->
|
||||
TableName = Table#sql_table.name,
|
||||
SQL1 = [<<"ALTER TABLE ">>, TableName, <<" DROP CONSTRAINT ",
|
||||
TableName/binary,"_pkey, ",
|
||||
"ADD PRIMARY KEY (">>,
|
||||
lists:join(
|
||||
<<", ">>,
|
||||
Index#sql_index.columns),
|
||||
<<");">>],
|
||||
SQL = iolist_to_binary(SQL1),
|
||||
?INFO_MSG("Update primary key ~s/~p:~n~s~n",
|
||||
[Table#sql_table.name,
|
||||
Index#sql_index.columns,
|
||||
SQL]),
|
||||
ejabberd_sql:sql_query(
|
||||
Host,
|
||||
fun(_DBType, _DBVersion) ->
|
||||
ejabberd_sql:sql_query_t(SQL)
|
||||
end);
|
||||
mysql ->
|
||||
TableName = Table#sql_table.name,
|
||||
SQL1 = [<<"ALTER TABLE ">>, TableName, <<" DROP PRIMARY KEY, "
|
||||
"ADD PRIMARY KEY (">>,
|
||||
lists:join(
|
||||
<<", ">>,
|
||||
lists:map(
|
||||
fun(Col) ->
|
||||
format_mysql_index_column(Table, Col)
|
||||
end, Index#sql_index.columns)),
|
||||
<<");">>],
|
||||
SQL = iolist_to_binary(SQL1),
|
||||
?INFO_MSG("Update primary key ~s/~p:~n~s~n",
|
||||
[Table#sql_table.name,
|
||||
Index#sql_index.columns,
|
||||
SQL]),
|
||||
ejabberd_sql:sql_query(
|
||||
Host,
|
||||
fun(_DBType, _DBVersion) ->
|
||||
ejabberd_sql:sql_query_t(SQL)
|
||||
end)
|
||||
end,
|
||||
case Res of
|
||||
{error, Error} ->
|
||||
?ERROR_MSG("Failed to update table ~s: ~p",
|
||||
[TableName, Error]),
|
||||
error(Error);
|
||||
_ ->
|
||||
ok
|
||||
end;
|
||||
({drop_index, TableName, Columns1}) ->
|
||||
Columns =
|
||||
case ejabberd_sql:use_new_schema() of
|
||||
|
Loading…
Reference in New Issue
Block a user