Skip to content

Commit 9ab6093

Browse files
committed
Add update_primary_key command to sql schema updater
1 parent c543723 commit 9ab6093

File tree

1 file changed

+111
-18
lines changed

1 file changed

+111
-18
lines changed

src/ejabberd_sql_schema.erl

+111-18
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,29 @@ get_current_version(Host, Module, Schemas) ->
394394
Version
395395
end.
396396

397+
sqlite_table_copy(Host, SchemaInfo, Table) ->
398+
ejabberd_sql:sql_transaction(Host,
399+
fun() ->
400+
TableName = Table#sql_table.name,
401+
NewTableName = <<"new_", TableName/binary>>,
402+
NewTable = Table#sql_table{name = NewTableName},
403+
create_table_t(SchemaInfo, NewTable),
404+
SQL2 = <<"INSERT INTO ", NewTableName/binary,
405+
" SELECT * FROM ", TableName/binary>>,
406+
?INFO_MSG("Copying table ~s to ~s:~n~s~n",
407+
[TableName, NewTableName, SQL2]),
408+
ejabberd_sql:sql_query_t(SQL2),
409+
SQL3 = <<"DROP TABLE ", TableName/binary>>,
410+
?INFO_MSG("Droping old table ~s:~n~s~n",
411+
[TableName, SQL2]),
412+
ejabberd_sql:sql_query_t(SQL3),
413+
SQL4 = <<"ALTER TABLE ", NewTableName/binary,
414+
" RENAME TO ", TableName/binary>>,
415+
?INFO_MSG("Renameing table ~s to ~s:~n~s~n",
416+
[NewTableName, TableName, SQL4]),
417+
ejabberd_sql:sql_query_t(SQL4)
418+
end).
419+
397420
format_type(#sql_schema_info{db_type = pgsql}, Column) ->
398421
case Column#sql_column.type of
399422
text -> <<"text">>;
@@ -697,24 +720,26 @@ format_create_table(#sql_schema_info{db_type = mysql} = SchemaInfo, Table) ->
697720
Table#sql_table.indices).
698721

699722
create_table(Host, SchemaInfo, Table) ->
700-
ejabberd_sql:sql_query(
701-
Host,
702-
fun() ->
703-
SQLs = format_create_table(SchemaInfo, Table),
704-
?INFO_MSG("Creating table ~s:~n~s~n",
705-
[Table#sql_table.name, SQLs]),
706-
lists:foreach(
707-
fun(SQL) -> ejabberd_sql:sql_query_t(SQL) end, SQLs),
708-
case Table#sql_table.post_create of
709-
undefined ->
710-
ok;
711-
F when is_function(F, 1) ->
712-
PostSQLs = F(SchemaInfo),
713-
lists:foreach(
714-
fun(SQL) -> ejabberd_sql:sql_query_t(SQL) end,
715-
PostSQLs)
716-
end
717-
end).
723+
ejabberd_sql:sql_query(Host,
724+
fun() ->
725+
create_table_t(SchemaInfo, Table)
726+
end).
727+
728+
create_table_t(SchemaInfo, Table) ->
729+
SQLs = format_create_table(SchemaInfo, Table),
730+
?INFO_MSG("Creating table ~s:~n~s~n",
731+
[Table#sql_table.name, SQLs]),
732+
lists:foreach(
733+
fun(SQL) -> ejabberd_sql:sql_query_t(SQL) end, SQLs),
734+
case Table#sql_table.post_create of
735+
undefined ->
736+
ok;
737+
F when is_function(F, 1) ->
738+
PostSQLs = F(SchemaInfo),
739+
lists:foreach(
740+
fun(SQL) -> ejabberd_sql:sql_query_t(SQL) end,
741+
PostSQLs)
742+
end.
718743

719744
create_tables(Host, Module, SchemaInfo, Schema) ->
720745
lists:foreach(
@@ -969,6 +994,74 @@ do_update_schema(Host, Module, SchemaInfo, Schema) ->
969994
ok
970995
end
971996
end;
997+
({update_primary_key, TableName, Columns1}) ->
998+
Columns =
999+
case ejabberd_sql:use_new_schema() of
1000+
true ->
1001+
Columns1;
1002+
false ->
1003+
lists:delete(
1004+
<<"server_host">>, Columns1)
1005+
end,
1006+
{value, Table} =
1007+
lists:keysearch(
1008+
TableName, #sql_table.name, Schema#sql_schema.tables),
1009+
{value, Index} =
1010+
lists:keysearch(
1011+
Columns, #sql_index.columns, Table#sql_table.indices),
1012+
Res =
1013+
case SchemaInfo#sql_schema_info.db_type of
1014+
sqlite ->
1015+
sqlite_table_copy(Host, SchemaInfo, Table);
1016+
pgsql ->
1017+
TableName = Table#sql_table.name,
1018+
SQL1 = [<<"ALTER TABLE ">>, TableName, <<" DROP CONSTRAINT ",
1019+
TableName/binary,"_pkey, ",
1020+
"ADD PRIMARY KEY (">>,
1021+
lists:join(
1022+
<<", ">>,
1023+
Index#sql_index.columns),
1024+
<<");">>],
1025+
SQL = iolist_to_binary(SQL1),
1026+
?INFO_MSG("Update primary key ~s/~p:~n~s~n",
1027+
[Table#sql_table.name,
1028+
Index#sql_index.columns,
1029+
SQL]),
1030+
ejabberd_sql:sql_query(
1031+
Host,
1032+
fun(_DBType, _DBVersion) ->
1033+
ejabberd_sql:sql_query_t(SQL)
1034+
end);
1035+
mysql ->
1036+
TableName = Table#sql_table.name,
1037+
SQL1 = [<<"ALTER TABLE ">>, TableName, <<" DROP PRIMARY KEY, "
1038+
"ADD PRIMARY KEY (">>,
1039+
lists:join(
1040+
<<", ">>,
1041+
lists:map(
1042+
fun(Col) ->
1043+
format_mysql_index_column(Table, Col)
1044+
end, Index#sql_index.columns)),
1045+
<<");">>],
1046+
SQL = iolist_to_binary(SQL1),
1047+
?INFO_MSG("Update primary key ~s/~p:~n~s~n",
1048+
[Table#sql_table.name,
1049+
Index#sql_index.columns,
1050+
SQL]),
1051+
ejabberd_sql:sql_query(
1052+
Host,
1053+
fun(_DBType, _DBVersion) ->
1054+
ejabberd_sql:sql_query_t(SQL)
1055+
end)
1056+
end,
1057+
case Res of
1058+
{error, Error} ->
1059+
?ERROR_MSG("Failed to update table ~s: ~p",
1060+
[TableName, Error]),
1061+
error(Error);
1062+
_ ->
1063+
ok
1064+
end;
9721065
({drop_index, TableName, Columns1}) ->
9731066
Columns =
9741067
case ejabberd_sql:use_new_schema() of

0 commit comments

Comments
 (0)