diff --git a/modules/store/src/main/resources/db/migration/h2/V1.39.1__file_meta_migration.sql b/modules/store/src/main/resources/db/migration/h2/V1.39.1__file_meta_migration.sql new file mode 100644 index 00000000..bcad45aa --- /dev/null +++ b/modules/store/src/main/resources/db/migration/h2/V1.39.1__file_meta_migration.sql @@ -0,0 +1,94 @@ +-- drop constraints to be able to update file ids +alter table "addon_archive" drop constraint "CONSTRAINT_8B982"; +alter table "attachment_archive" drop constraint "CONSTRAINT_13E"; +alter table "attachment" drop constraint "CONSTRAINT_8AF"; +alter table "attachment_source" drop constraint "CONSTRAINT_698"; +alter table "classifier_model" drop constraint "CONSTRAINT_BC7B"; +alter table "download_query" drop constraint "CONSTRAINT_3ABF"; +alter table "attachment_preview" drop constraint "CONSTRAINT_2D8"; + +-- create temporary tables holding old and new ids +create table "temp_prefixes"( + old_prefix varchar(255) not null primary key, + new_prefix varchar(255) not null +); +insert into "temp_prefixes" +select concat(name, '/'), concat(id, '/') from collective; + +create table "temp_file_ids"( + old_id varchar(255) not null primary key, + old_prefix varchar(255) not null, + new_prefix varchar(255) not null, + new_id varchar(255) not null +); + +with ids_orig(old_id, prefix) as + (select file_id, substring(file_id, 0, position('/' in file_id)) + from filemeta fm + ) +insert into "temp_file_ids" +select fm.old_id, tp.old_prefix, tp.new_prefix, replace(fm.old_id, tp.old_prefix, tp.new_prefix) as new_id +from ids_orig fm +inner join "temp_prefixes" tp on fm.prefix = tp.old_prefix; + +-- remove orphaned files and chunks +delete from filemeta +where "file_id" not in (select "old_id" from "temp_file_ids"); + +delete from filechunk +where "file_id" not in (select "old_id" from "temp_file_ids"); + +-- update all references +update "filemeta" fm set "file_id" = + (select t.new_id from "temp_file_ids" t where t."old_id" = fm."file_id"); + +update "addon_archive" aa set "file_id" = + (select t.new_id from "temp_file_ids" t where t."old_id" = aa."file_id"); + +update "attachment_archive" aa set "file_id" = + (select t.new_id from "temp_file_ids" t where t."old_id" = aa."file_id"); + +update "attachment" a set "filemetaid" = + (select t.new_id from "temp_file_ids" t where t."old_id" = a."filemetaid"); + +update "attachment_source" a set "file_id" = + (select t.new_id from "temp_file_ids" t where t."old_id" = a."file_id"); + +update "classifier_model" cm set "file_id" = + (select t.new_id from "temp_file_ids" t where t."old_id" = cm."file_id"); + +update "download_query" dq set "file_id" = + (select t.new_id from "temp_file_ids" t where t."old_id" = dq."file_id"); + +update "attachment_preview" ap set "file_id" = + (select t.new_id from "temp_file_ids" t where t."old_id" = ap."file_id"); + +-- update filechunks +update "filechunk" fc set "file_id" = + (select t.new_id from "temp_file_ids" t where t."old_id" = fc."file_id"); + +-- re-create the constraints +alter table "addon_archive" add constraint "addon_archive_file_id_fkey" +foreign key ("file_id") references "filemeta"("file_id"); + +alter table "attachment_archive" add constraint "attachment_archive_file_id_fkey" +foreign key ("file_id") references "filemeta"("file_id"); + +alter table "attachment" add constraint "attachment_filemetaid_fkey" +foreign key ("filemetaid") references "filemeta"("file_id"); + +alter table "attachment_source" add constraint "attachment_source_file_id_fkey" +foreign key ("file_id") references "filemeta"("file_id"); + +alter table "classifier_model" add constraint "classifier_model_file_id_fkey" +foreign key ("file_id") references "filemeta"("file_id"); + +alter table "download_query" add constraint "download_query_file_id_fkey" +foreign key ("file_id") references "filemeta"("file_id"); + +alter table "attachment_preview" add constraint "attachment_preview_file_id_fkey" +foreign key ("file_id") references "filemeta"("file_id"); + +-- drop temporary tables +drop table "temp_file_ids"; +drop table "temp_prefixes"; diff --git a/modules/store/src/main/resources/db/migration/mariadb/V1.39.0__collective_id.sql b/modules/store/src/main/resources/db/migration/mariadb/V1.39.0__collective_id.sql index aa6616ea..4732ec83 100644 --- a/modules/store/src/main/resources/db/migration/mariadb/V1.39.0__collective_id.sql +++ b/modules/store/src/main/resources/db/migration/mariadb/V1.39.0__collective_id.sql @@ -1,6 +1,5 @@ -- add new id column alter table `collective` add column `id` int auto_increment not null unique; -create unique index `collective_id_idx` on `collective`(`id`); -- change references: source alter table `source` add column `coll_id` int not null default 0; @@ -17,7 +16,6 @@ alter table `source` alter column `coll_id` drop default; alter table `tag` add column `coll_id` int not null default 0; update `tag` t set `coll_id` = (select id from collective where `cid` = t.`cid`); create index `tag_coll_id_idx` on `tag`(`coll_id`); -create unique index `tag_coll_id_name_idx` on `tag`(`coll_id`, `name`); alter table `tag` add constraint `tag_coll_id_name_key` unique(`coll_id`, `name`); alter table `tag` add constraint `tag_coll_id_fkey` foreign key (`coll_id`) references `collective`(`id`); alter table `tag` drop constraint `tag_ibfk_1`; @@ -29,7 +27,6 @@ alter table `tag` alter column `coll_id` drop default; alter table `user_` add column `coll_id` int not null default 0; update `user_` t set `coll_id` = (select id from collective where `cid` = t.`cid`); create index `user__coll_id_idx` on `user_`(`coll_id`); -create unique index `user__coll_id_login_idx` on `user_`(`coll_id`, `login`); alter table `user_` add constraint `user__coll_id_login_key` unique(`coll_id`, `login`); alter table `user_` add constraint `user__coll_id_fkey` foreign key (`coll_id`) references `collective`(`id`); alter table `user_` drop constraint `user__ibfk_1`; @@ -41,7 +38,6 @@ alter table `user_` alter column `coll_id` drop default; alter table `query_bookmark` add column `coll_id` int not null default 0; update `query_bookmark` t set `coll_id` = (select id from collective where `cid` = t.`cid`); create index `query_bookmark_coll_id_idx` on `query_bookmark`(`coll_id`); -create unique index `query_bookmark_coll_id__user_id_name_idx` on `query_bookmark`(`coll_id`, `__user_id`, `name`); alter table `query_bookmark` add constraint `query_bookmark_coll_id__user_id_name_key` unique(`coll_id`, `__user_id`, `name`); alter table `query_bookmark` add constraint `query_bookmark_coll_id_fkey` foreign key (`coll_id`) references `collective`(`id`); alter table `query_bookmark` drop constraint `query_bookmark_ibfk_2`; @@ -53,7 +49,6 @@ alter table `query_bookmark` alter column `coll_id` drop default; alter table `person` add column `coll_id` int not null default 0; update `person` t set `coll_id` = (select id from collective where `cid` = t.`cid`); create index `person_coll_id_idx` on `person`(`coll_id`); -create unique index `person_coll_id_name_idx` on `person`(`coll_id`, `name`); alter table `person` add constraint `person_coll_id_name_key` unique(`coll_id`, `name`); alter table `person` add constraint `person_coll_id_fkey` foreign key (`coll_id`) references `collective`(`id`); alter table `person` drop constraint `person_ibfk_1`; @@ -65,7 +60,6 @@ alter table `person` alter column `coll_id` drop default; alter table `organization` add column `coll_id` int not null default 0; update `organization` t set `coll_id` = (select id from collective where `cid` = t.`cid`); create index `organization_coll_id_idx` on `organization`(`coll_id`); -create unique index `organization_coll_id_name_idx` on `organization`(`coll_id`, `name`); alter table `organization` add constraint `organization_coll_id_name_key` unique(`coll_id`, `name`); alter table `organization` add constraint `organization_coll_id_fkey` foreign key (`coll_id`) references `collective`(`id`); alter table `organization` drop constraint `cid`; @@ -77,7 +71,6 @@ alter table `organization` alter column `coll_id` drop default; alter table `item_link` add column `coll_id` int not null default 0; update `item_link` t set `coll_id` = (select id from collective where `cid` = t.`cid`); create index `item_link_coll_id_idx` on `item_link`(`coll_id`); -create unique index `item_link_coll_id_item1_item2_idx` on `item_link`(`coll_id`, `item1`, `item2`); alter table `item_link` add constraint `item_link_coll_id_item1_item2_key` unique(`coll_id`, `item1`, `item2`); alter table `item_link` add constraint `item_link_coll_id_fkey` foreign key (`coll_id`) references `collective`(`id`); alter table `item_link` drop constraint `item_link_ibfk_1`; @@ -100,7 +93,6 @@ update `folder` t set `coll_id` = (select id from collective where `cid` = t.`ci alter table `folder` drop constraint `folder_ibfk_1`; alter table `folder` drop constraint `name`; create index `folder_coll_id_idx` on `folder`(`coll_id`); -create unique index `folder_coll_id_name_idx` on `folder`(`coll_id`, `name`); alter table `folder` add constraint `folder_coll_id_name_key` unique(`coll_id`, `name`); alter table `folder` add constraint `folder_coll_id_fkey` foreign key (`coll_id`) references `collective`(`id`); alter table `folder` drop column `cid`; @@ -110,7 +102,6 @@ alter table `folder` alter column `coll_id` drop default; alter table `equipment` add column `coll_id` int not null default 0; update `equipment` t set `coll_id` = (select id from collective where `cid` = t.`cid`); create index `equipment_coll_id_idx` on `equipment`(`coll_id`); -create unique index `equipment_coll_id_name_idx` on `equipment`(`coll_id`, `name`); alter table `equipment` add constraint `equipment_coll_id_name_key` unique(`coll_id`, `name`); alter table `equipment` add constraint `equipment_coll_id_fkey` foreign key (`coll_id`) references `collective`(`id`); alter table `equipment` drop constraint `equipment_ibfk_1`; @@ -124,7 +115,7 @@ alter table `empty_trash_setting` add column `coll_id` int not null default 0; update `empty_trash_setting` t set `coll_id` = (select id from collective where `cid` = t.`cid`); create index `empty_trash_setting_coll_id_idx` on `empty_trash_setting`(`coll_id`); alter table `empty_trash_setting` add constraint `empty_trash_setting_coll_id_fkey` -foreign key (`coll_id`) references `collective`(`id`); + foreign key (`coll_id`) references `collective`(`id`); alter table `empty_trash_setting` drop constraint `empty_trash_setting_ibfk_1`; alter table `empty_trash_setting` drop column `cid`; alter table `empty_trash_setting` alter column `coll_id` drop default; @@ -144,7 +135,6 @@ alter table `download_query` alter column `coll_id` drop default; alter table `custom_field` add column `coll_id` int not null default 0; update `custom_field` t set `coll_id` = (select id from collective where `cid` = t.`cid`); create index `custom_field_coll_id_idx` on `custom_field`(`coll_id`); -create unique index `custom_field_coll_id_name_idx` on `custom_field`(`coll_id`, `name`); alter table `custom_field` add constraint `custom_field_coll_id_name_key` unique(`coll_id`, `name`); alter table `custom_field` add constraint `custom_field_coll_id_fkey` foreign key (`coll_id`) references `collective`(`id`); alter table `custom_field` drop constraint `custom_field_ibfk_1`; @@ -166,7 +156,6 @@ alter table `collective_password` alter column `coll_id` drop default; alter table `client_settings_collective` add column `coll_id` int not null default 0; update `client_settings_collective` t set `coll_id` = (select id from collective where `cid` = t.`cid`); create index `client_settings_collective_coll_id_idx` on `client_settings_collective`(`coll_id`); -create unique index `client_settings_collective_coll_id_client_id_idx` on `client_settings_collective`(`coll_id`, `client_id`); alter table `client_settings_collective` add constraint `client_settings_collective_coll_id_name_key` unique(`coll_id`, `client_id`); alter table `client_settings_collective` add constraint `client_settings_collective_coll_id_fkey` foreign key (`coll_id`) references `collective`(`id`); alter table `client_settings_collective` drop constraint `client_settings_collective_ibfk_1`; @@ -188,7 +177,6 @@ alter table `classifier_setting` alter column `coll_id` drop default; alter table `classifier_model` add column `coll_id` int not null default 0; update `classifier_model` t set `coll_id` = (select id from collective where `cid` = t.`cid`); create index `classifier_model_coll_id_idx` on `classifier_model`(`coll_id`); -create unique index `classifier_model_coll_id_name_idx` on `classifier_model`(`coll_id`, `name`); alter table `classifier_model` add constraint `classifier_model_coll_id_name_key` unique(`coll_id`, `name`); alter table `classifier_model` add constraint `classifier_model_coll_id_fkey` foreign key (`coll_id`) references `collective`(`id`); alter table `classifier_model` drop constraint `classifier_model_ibfk_1`; @@ -216,8 +204,6 @@ alter table `addon_archive` drop constraint `addon_archive_ibfk_1`; alter table `addon_archive` drop index `addon_archive_cid_idx`; create index `addon_archive_coll_id_idx` on `addon_archive`(`coll_id`); -create unique index `addon_archive_coll_id_name_version_idx` on `addon_archive`(`coll_id`, `name`, `version`); -create unique index `addon_archive_coll_id_original_url_idx` on `addon_archive`(`coll_id`, `original_url`); alter table `addon_archive` add constraint `addon_archive_coll_id_name_version_key` unique(`coll_id`, `name`, `version`); alter table `addon_archive` add constraint `addon_archive_coll_id_original_url_key` unique(`coll_id`, `original_url`); alter table `addon_archive` add constraint `addon_archive_coll_id_fkey` foreign key (`coll_id`) references `collective`(`id`); @@ -228,5 +214,4 @@ alter table `addon_archive` alter column `coll_id` drop default; alter table `collective` drop primary key; alter table `collective` add constraint `collective_id_pkey` primary key (`id`); alter table `collective` rename column `cid` to `name`; -create unique index `collective_name_idx` on `collective`(`name`); alter table `collective` add constraint `collective_name_key` unique(`name`); diff --git a/modules/store/src/main/resources/db/migration/mariadb/V1.39.1__file_meta_migration.sql b/modules/store/src/main/resources/db/migration/mariadb/V1.39.1__file_meta_migration.sql new file mode 100644 index 00000000..f887cc54 --- /dev/null +++ b/modules/store/src/main/resources/db/migration/mariadb/V1.39.1__file_meta_migration.sql @@ -0,0 +1,92 @@ +-- drop constraints to be able to update file ids +alter table `addon_archive` drop constraint `addon_archive_ibfk_2`; +alter table `attachment_archive` drop constraint `attachment_archive_ibfk_1`; +alter table `attachment` drop constraint `attachment_ibfk_2`; +alter table `attachment_source` drop constraint `attachment_source_ibfk_1`; +alter table `classifier_model` drop constraint `classifier_model_ibfk_2`; +alter table `download_query` drop constraint `download_query_ibfk_2`; +alter table `attachment_preview` drop constraint `attachment_preview_ibfk_1`; + +-- create temporary tables holding old and new ids +create table `temp_prefixes`( + old_prefix varchar(255) not null primary key, + new_prefix varchar(255) not null +); +insert into `temp_prefixes` +select concat(name, '/'), concat(id, '/') from collective; + +create table `temp_file_ids`( + old_id varchar(255) not null primary key, + old_prefix varchar(255) not null, + new_prefix varchar(255) not null, + new_id varchar(255) not null +); + + +insert into `temp_file_ids` +select fm.old_id, tp.old_prefix, tp.new_prefix, replace(fm.old_id, tp.old_prefix, tp.new_prefix) as new_id +from + (select file_id as old_id, substring(file_id, 1, position('/' in file_id)) as prefix from filemeta) fm +inner join `temp_prefixes` tp on fm.prefix = tp.old_prefix; + +-- remove orphaned files and chunks +delete from filemeta +where `file_id` not in (select `old_id` from `temp_file_ids`); + +delete from filechunk +where `file_id` not in (select `old_id` from `temp_file_ids`); + +-- update all references +update `filemeta` fm set `file_id` = + (select t.new_id from `temp_file_ids` t where t.`old_id` = fm.`file_id`); + +update `addon_archive` aa set `file_id` = + (select t.new_id from `temp_file_ids` t where t.`old_id` = aa.`file_id`); + +update `attachment_archive` aa set `file_id` = + (select t.new_id from `temp_file_ids` t where t.`old_id` = aa.`file_id`); + +update `attachment` a set `filemetaid` = + (select t.new_id from `temp_file_ids` t where t.`old_id` = a.`filemetaid`); + +update `attachment_source` a set `file_id` = + (select t.new_id from `temp_file_ids` t where t.`old_id` = a.`file_id`); + +update `classifier_model` cm set `file_id` = + (select t.new_id from `temp_file_ids` t where t.`old_id` = cm.`file_id`); + +update `download_query` dq set `file_id` = + (select t.new_id from `temp_file_ids` t where t.`old_id` = dq.`file_id`); + +update `attachment_preview` ap set `file_id` = + (select t.new_id from `temp_file_ids` t where t.`old_id` = ap.`file_id`); + +-- update filechunks +update `filechunk` fc set `file_id` = + (select t.new_id from `temp_file_ids` t where t.`old_id` = fc.`file_id`); + +-- re-create the constraints +alter table `addon_archive` add constraint `addon_archive_file_id_fkey` +foreign key (`file_id`) references `filemeta`(`file_id`); + +alter table `attachment_archive` add constraint `attachment_archive_file_id_fkey` +foreign key (`file_id`) references `filemeta`(`file_id`); + +alter table `attachment` add constraint `attachment_filemetaid_fkey` +foreign key (`filemetaid`) references `filemeta`(`file_id`); + +alter table `attachment_source` add constraint `attachment_source_file_id_fkey` +foreign key (`file_id`) references `filemeta`(`file_id`); + +alter table `classifier_model` add constraint `classifier_model_file_id_fkey` +foreign key (`file_id`) references `filemeta`(`file_id`); + +alter table `download_query` add constraint `download_query_file_id_fkey` +foreign key (`file_id`) references `filemeta`(`file_id`); + +alter table `attachment_preview` add constraint `attachment_preview_file_id_fkey` +foreign key (`file_id`) references `filemeta`(`file_id`); + +-- drop temporary tables +drop table `temp_file_ids`; +drop table `temp_prefixes`; diff --git a/modules/store/src/main/resources/db/migration/postgresql/V1.39.0__collective_id.sql b/modules/store/src/main/resources/db/migration/postgresql/V1.39.0__collective_id.sql index 6cc6fe15..0a71b955 100644 --- a/modules/store/src/main/resources/db/migration/postgresql/V1.39.0__collective_id.sql +++ b/modules/store/src/main/resources/db/migration/postgresql/V1.39.0__collective_id.sql @@ -29,7 +29,8 @@ create index "user__coll_id_idx" on "user_"("coll_id"); create unique index "user__coll_id_login_idx" on "user_"("coll_id", "login"); alter table "user_" add constraint "user__coll_id_login_key" unique using index "user__coll_id_login_idx"; alter table "user_" add constraint "user__coll_id_fkey" foreign key ("coll_id") references "collective"("id"); -alter table "user_" drop constraint "user__cid_fkey"; +alter table "user_" drop constraint if exists "user__cid_fkey"; +alter table "user_" drop constraint if exists "user_cid_fkey"; alter table "user_" drop column "cid"; alter table "user_" alter column "coll_id" drop default; diff --git a/modules/store/src/main/resources/db/migration/postgresql/V1.39.1__file_meta_migration.sql b/modules/store/src/main/resources/db/migration/postgresql/V1.39.1__file_meta_migration.sql new file mode 100644 index 00000000..9216be55 --- /dev/null +++ b/modules/store/src/main/resources/db/migration/postgresql/V1.39.1__file_meta_migration.sql @@ -0,0 +1,95 @@ +-- drop constraints to be able to update file ids +alter table "addon_archive" drop constraint "addon_archive_file_id_fkey"; +alter table "attachment_archive" drop constraint "attachment_archive_file_id_fkey"; +alter table "attachment" drop constraint "attachment_filemetaid_fkey"; +alter table "attachment_source" drop constraint "attachment_source_file_id_fkey"; +alter table "classifier_model" drop constraint "classifier_model_file_id_fkey"; +alter table "download_query" drop constraint "download_query_file_id_fkey"; +alter table "attachment_preview" drop constraint "attachment_preview_file_id_fkey"; + +-- create temporary tables holding old and new ids +create table "temp_prefixes"( + old_prefix varchar(255) not null primary key, + new_prefix varchar(255) not null +); +insert into "temp_prefixes" +select concat(name, '/'), concat(id, '/') from collective; + +create table "temp_file_ids"( + old_id varchar(255) not null primary key, + old_prefix varchar(255) not null, + new_prefix varchar(255) not null, + new_id varchar(255) not null +); + +with ids_orig(old_id, prefix) as + (select file_id, concat(substring(file_id, 0, position('/' in file_id)), '/') + from filemeta fm + ) +insert into "temp_file_ids" +select fm.old_id, tp.old_prefix, tp.new_prefix, replace(fm.old_id, tp.old_prefix, tp.new_prefix) as new_id +from ids_orig fm +inner join "temp_prefixes" tp on fm.prefix = tp.old_prefix; + +-- remove orphaned files and chunks +delete from filemeta +where "file_id" not in (select "old_id" from "temp_file_ids"); + +delete from filechunk +where "file_id" not in (select "old_id" from "temp_file_ids"); + + +-- update all references +update "filemeta" fm set "file_id" = + (select t.new_id from "temp_file_ids" t where t."old_id" = fm."file_id"); + +update "addon_archive" aa set "file_id" = + (select t.new_id from "temp_file_ids" t where t."old_id" = aa."file_id"); + +update "attachment_archive" aa set "file_id" = + (select t.new_id from "temp_file_ids" t where t."old_id" = aa."file_id"); + +update "attachment" a set "filemetaid" = + (select t.new_id from "temp_file_ids" t where t."old_id" = a."filemetaid"); + +update "attachment_source" a set "file_id" = + (select t.new_id from "temp_file_ids" t where t."old_id" = a."file_id"); + +update "classifier_model" cm set "file_id" = + (select t.new_id from "temp_file_ids" t where t."old_id" = cm."file_id"); + +update "download_query" dq set "file_id" = + (select t.new_id from "temp_file_ids" t where t."old_id" = dq."file_id"); + +update "attachment_preview" ap set "file_id" = + (select t.new_id from "temp_file_ids" t where t."old_id" = ap."file_id"); + +-- update filechunks +update "filechunk" fc set "file_id" = + (select t.new_id from "temp_file_ids" t where t."old_id" = fc."file_id"); + +-- re-create the constraints +alter table "addon_archive" add constraint "addon_archive_file_id_fkey" +foreign key ("file_id") references "filemeta"("file_id"); + +alter table "attachment_archive" add constraint "attachment_archive_file_id_fkey" +foreign key ("file_id") references "filemeta"("file_id"); + +alter table "attachment" add constraint "attachment_filemetaid_fkey" +foreign key ("filemetaid") references "filemeta"("file_id"); + +alter table "attachment_source" add constraint "attachment_source_file_id_fkey" +foreign key ("file_id") references "filemeta"("file_id"); + +alter table "classifier_model" add constraint "classifier_model_file_id_fkey" +foreign key ("file_id") references "filemeta"("file_id"); + +alter table "download_query" add constraint "download_query_file_id_fkey" +foreign key ("file_id") references "filemeta"("file_id"); + +alter table "attachment_preview" add constraint "attachment_preview_file_id_fkey" +foreign key ("file_id") references "filemeta"("file_id"); + +-- drop temporary tables +drop table "temp_file_ids"; +drop table "temp_prefixes";