8. Changing live data · Your task

Your task

1. Migration. Write 007_view_count_and_soft_delete.sql: add view_count to posts (a whole number, never missing, starting at zero) and deleted_at (empty unless the post is deleted). Run it and check that existing posts got a view_count of 0.

2. View counter. Someone opens post 1. Bump its count by one and get the new count back in the same statement.

3. Rename. Change dev's username to devon. Confirm exactly one row changed.

4. The Friday mistake, safely. Start a transaction. Run UPDATE posts SET published = false; with no WHERE. Count the published posts. Roll back. Count again.

5. Publish drafts. devon wants all their drafts published. Return the id and title of each post you changed.

6. Soft delete. "Delete" post 8 the new way. Then update the homepage feed query from Level 5 so it never shows deleted posts.

7. Restore. The author changed their mind. Bring post 8 back.

8. Spam cleanup. bob turns out to be a spammer. Inside a transaction: count the rows in posts, comments, likes and follows, delete the user, count again. Work out why each number dropped by what it did, then decide whether to commit or roll back.