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

@@ -223,7 +223,64 @@ return !is_that_url;
}
void Item::do_migration(morm::ModelConnector * model_connector, Log & log)
void Item::propagate_connector()
{
item_content.set_connector(model_connector);
}
bool Item::do_migration(int & current_table_version)
{
bool ok = true;
ok = ok && morm::Model::do_migration(current_table_version, 1, this, &Item::do_migration_to_1);
ok = ok && morm::Model::do_migration(current_table_version, 2, this, &Item::do_migration_to_2);
ok = ok && morm::Model::do_migration(current_table_version, 3, this, &Item::do_migration_to_3);
ok = ok && morm::Model::do_migration(current_table_version, 4, this, &Item::do_migration_to_4);
return ok;
}
bool Item::do_migration_to_1()
{
const char * str = R"sql(
CREATE TABLE core.item (
user_id integer,
group_id integer,
privileges integer,
date_creation timestamp without time zone,
date_modification timestamp without time zone,
parent_id bigint,
type smallint,
id integer NOT NULL,
url character varying(255),
content_id bigint,
subject character varying(255),
guest_name character varying(20),
modification_user_id integer,
template character varying(255),
link_to character varying(2048),
link_redirect smallint,
sort_index integer,
meta text,
ameta text
);
)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
}
/*
* directories didn't have an ItemContent object, so now we create such objects
*/
bool Item::do_migration_to_2()
{
morm::Finder<Item> finder(model_connector);
@@ -234,17 +291,76 @@ void Item::do_migration(morm::ModelConnector * model_connector, Log & log)
eq(L"content_id", -1).
get_list();
PT::Log * log = model_connector->get_logger();
for(Item & item : list)
{
log << "updating item id: " << item.id << ", type: " << (int)item.type << ", url: " << item.url << ", subject: " << item.subject << logend << logsave;
if( log )
{
(*log) << "Item: adding a content row corresponding to item id: " << item.id << ", type: " << (int)item.type << ", url: " << item.url
<< ", subject: " << item.subject << PT::Log::logend;
}
item.item_content.set_save_mode(morm::Model::DO_INSERT_ON_SAVE);
item.save();
if( !item.save() )
return false;
}
return true;
}
void Item::propagate_connector()
bool Item::do_migration_to_3()
{
item_content.set_connector(model_connector);
const char * str[] = {
"update core.content set user_id = (select user_id from core.item where item.content_id = content.id limit 1);",
"update core.content set group_id = (select group_id from core.item where item.content_id = content.id limit 1);",
"update core.content set guest_name = (select guest_name from core.item where item.content_id = content.id limit 1);",
"update core.content set modification_user_id = (select modification_user_id from core.item where item.content_id = content.id limit 1);",
"update core.content set privileges = (select privileges from core.item where item.content_id = content.id limit 1);",
"update core.content set date_creation = (select date_creation from core.item where item.content_id = content.id limit 1);",
"update core.content set date_modification = (select date_modification from core.item where item.content_id = content.id limit 1);",
"update core.content set link_to = (select link_to from core.item where item.content_id = content.id limit 1);",
"update core.content set link_redirect = (select link_redirect from core.item where item.content_id = content.id limit 1);",
"update core.content set meta = (select meta from core.item where item.content_id = content.id limit 1);",
"update core.content set meta_admin = (select ameta from core.item where item.content_id = content.id limit 1);",
};
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;
}
bool Item::do_migration_to_4()
{
const char * str = R"sql(
alter table core.item
drop column user_id,
drop column group_id,
drop column guest_name,
drop column modification_user_id,
drop column privileges,
drop column date_creation,
drop column date_modification,
drop column link_to,
drop column link_redirect,
drop column meta,
drop column ameta;
)sql";
return db_query(str);
}