added support for Model migrations

now we have a table core.migration and each model (User, Group, Item, ItemContent and a new Migration) have its own
row in the table with a version number

added to config:
db_make_migration_if_needed and db_stop_if_migration_fails (need description yet)
This commit is contained in:
2021-05-14 03:31:29 +02:00
parent 4df10de6b7
commit a94e09f0aa
16 changed files with 674 additions and 40 deletions

View File

@@ -165,5 +165,98 @@ bool ItemContent::CanContentBeHtmlFiltered()
}
bool ItemContent::do_migration(int & current_table_version)
{
bool ok = true;
ok = ok && morm::Model::do_migration(current_table_version, 1, this, &ItemContent::do_migration_to_1);
ok = ok && morm::Model::do_migration(current_table_version, 2, this, &ItemContent::do_migration_to_2);
ok = ok && morm::Model::do_migration(current_table_version, 3, this, &ItemContent::do_migration_to_3);
return ok;
}
bool ItemContent::do_migration_to_1()
{
const char * str = R"sql(
CREATE TABLE core.content (
id serial,
content text,
content_type smallint,
file_path character varying(2048),
file_fs smallint,
file_type smallint,
has_thumb smallint,
ref integer,
modify_index smallint,
hash character varying(255),
hash_type smallint,
file_size bigint
);
)sql";
db_query(str);
return true; // IMPROVEME remove me in the future: this is only for a moment until we do migration on all our sites
}
bool ItemContent::do_migration_to_2()
{
const char * str = R"sql(
alter table core.content
add column user_id integer,
add column group_id integer,
add column guest_name character varying(20),
add column modification_user_id integer,
add column privileges integer,
add column date_creation timestamp without time zone,
add column date_modification timestamp without time zone,
add column link_to character varying(2048),
add column link_redirect smallint,
add column meta text,
add column meta_admin text,
add column content_parsed text,
add column content_parsed_type smallint;
)sql";
return db_query(str);
}
bool ItemContent::do_migration_to_3()
{
const char * str[] = {
"alter table core.content rename column ref to \"references\";",
"alter table core.content rename column content to content_raw;",
"alter table core.content rename column content_type to content_raw_type;",
"alter table core.content rename column has_thumb to file_has_thumb;",
"alter table core.content rename column hash to file_hash;",
"alter table core.content rename column hash_type to file_hash_type;",
"alter table core.content drop column modify_index;",
"alter table core.content add column file_has_thumb_new boolean;",
"update core.content as c1 set file_has_thumb_new = (select case when file_has_thumb <> 0 then true else false end from core.content as c2 where c1.id = c2.id);",
"alter table core.content drop column file_has_thumb;",
"alter table core.content rename file_has_thumb_new to file_has_thumb;",
};
size_t len = sizeof(str) / sizeof(const char*);
for(size_t i=0 ; i < len ; ++i)
{
if( !db_query(str[i]) )
{
return false;
}
}
return true;
}
} // namespace Winix