Commit a74e3a29 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

Bookmark: Add missing ON DELETE CASCADE

Which fixes a constraint violation when removing a media that was
bookmarked.
Fix #429
parent 3a5ec1d4
Pipeline #203776 passed with stage
in 9 minutes and 52 seconds
......@@ -171,6 +171,22 @@ std::string Bookmark::schema( const std::string& tableName, uint32_t dbModel )
"UNIQUE(time,media_id) ON CONFLICT FAIL"
")";
}
if ( dbModel < 35 )
{
return "CREATE TABLE " + Table::Name +
"("
"id_bookmark INTEGER PRIMARY KEY AUTOINCREMENT,"
"time UNSIGNED INTEGER NOT NULL,"
"name TEXT,"
"description TEXT,"
"media_id UNSIGNED INTEGER NOT NULL,"
"creation_date UNSIGNED INTEGER NOT NULL,"
"type UNSIGNED INTEGER NOT NULL,"
"FOREIGN KEY(media_id) REFERENCES " +
Media::Table::Name + "(id_media),"
"UNIQUE(time,media_id) ON CONFLICT FAIL"
")";
}
return "CREATE TABLE " + Table::Name +
"("
"id_bookmark INTEGER PRIMARY KEY AUTOINCREMENT,"
......@@ -181,7 +197,7 @@ std::string Bookmark::schema( const std::string& tableName, uint32_t dbModel )
"creation_date UNSIGNED INTEGER NOT NULL,"
"type UNSIGNED INTEGER NOT NULL,"
"FOREIGN KEY(media_id) REFERENCES " +
Media::Table::Name + "(id_media),"
Media::Table::Name + "(id_media) ON DELETE CASCADE,"
"UNIQUE(time,media_id) ON CONFLICT FAIL"
")";
}
......
......@@ -6,3 +6,28 @@ Genre::trigger( Genre::Triggers::UpdateOnTrackDelete, 35 ),
"DROP TRIGGER " + Genre::triggerName( Genre::Triggers::DeleteEmpty, 34 ),
Genre::trigger( Genre::Triggers::DeleteEmpty, 35 ),
/* Add a missing ON DELETE CASCADE on media_id foreign key */
"CREATE TEMPORARY TABLE " + Bookmark::Table::Name + "_backup"
"("
"id_bookmark INTEGER PRIMARY KEY,"
"time UNSIGNED INTEGER,"
"name TEXT,"
"description TEXT,"
"media_id UNSIGNED INTEGER,"
"creation_date UNSIGNED INTEGER NOT NULL,"
"type UNSIGNED INTEGER NOT NULL"
")",
"INSERT INTO " + Bookmark::Table::Name + "_backup "
"SELECT * FROM " + Bookmark::Table::Name,
"DROP TABLE " + Bookmark::Table::Name,
Bookmark::schema( Bookmark::Table::Name, 35 ),
"INSERT INTO " + Bookmark::Table::Name +
" SELECT * FROM " + Bookmark::Table::Name + "_backup",
"DROP TABLE " + Bookmark::Table::Name + "_backup",
Bookmark::index( Bookmark::Indexes::MediaId, 35 ),
......@@ -331,6 +331,17 @@ static void FetchByTime( BookmarkTests* T )
ASSERT_EQ( b2->id(), fetched->id() );
}
static void DeleteMedia( BookmarkTests* T )
{
auto b = T->m->addBookmark( 123 );
ASSERT_NON_NULL( b );
T->ml->deleteMedia( T->m->id() );
b = T->ml->bookmark( b->id() );
ASSERT_EQ( nullptr, b );
}
int main( int ac, char** av )
{
INIT_TESTS_C( BookmarkTests );
......@@ -349,6 +360,7 @@ int main( int ac, char** av )
ADD_TEST( OrderByCreationDate );
ADD_TEST( Fetch );
ADD_TEST( FetchByTime );
ADD_TEST( DeleteMedia );
END_TESTS
}
......@@ -294,6 +294,7 @@ bookmark_tests = [
'OrderByCreationDate',
'Fetch',
'FetchByTime',
'DeleteMedia',
]
foreach t : bookmark_tests
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment