from mongodb_migrations.base import BaseMigration class Migration(BaseMigration): name_template = "ind_{}_created_at" collections_list = ( "spotify_track", "spotify_track_isrc", "spotify_playlist", "spotify_album", "spotify_artist", "spotify_user", "spotify_search", "spotify_album_tracks", "apple_song", "apple_song_isrc", "apple_playlist", "apple_album", "apple_search", "cache", ) def upgrade(self): # ttl index 30 days for collection_name in self.collections_list: self.db[collection_name].create_index( "created_at", name=self.name_template.format(collection_name), expireAfterSeconds=(30 * 24 * 60 * 60), background=True, ) def downgrade(self): for collection_name in self.collections_list: self.db[collection_name].drop_index(self.name_template.format(collection_name))