serieseight / statamic-audit-log

Statamic v2 Addon for logging system events.
MIT License
4 stars 1 forks source link

Bug: user_id should be nullable #9

Closed miloslavkostir closed 4 years ago

miloslavkostir commented 4 years ago

Column user_id can be nullable: if you create content manualy (create .md file) without id, statamic automatically fill id into .md file. And there is no user_id. In this case you get error: PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null in ...

aryehraber commented 4 years ago

Hi @miloslavkostir, this shouldn't technically be necessary as AuditLog only tracks changes done via the CP (where a user must be logged in). Manually creating & adding files to a project will bypass Statamic's event system completely and AuditLog won't be aware of the additions/changes.

What's the exact scenario that you're hitting this error?

miloslavkostir commented 4 years ago

Hi, try this:

  1. log out from CP
  2. manualy create new simple page (index.md) without id e.g.:
    title: Bug
    fieldset: default
  3. while you're logged out refresh frontend (any site). You can refresh homepage and will get this error:
    QueryException in Connection.php line 666:
    SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null (SQL: insert into `audit_log` (`user_id`, `data_id`, `locale`, `event`, `meta`, `snapshot`, `updated_at`, `created_at`) values (, 8ec5b14c-7b1d-4623-85e6-1cfcdb98ee0c, , Statamic\Events\Data\PageSaved,...

    What happened: when you refresh site, Statamic detects page without id and edits it this way:

    title: Bug
    fieldset: default
    id: 6033c391-867e-4d4c-a697-95aace056461

    and saves it - this triggers the event Statamic\Events\Data\PageSaved, but no user is logged in.

aryehraber commented 4 years ago

Nice catch @miloslavkostir! I don't think I've ever hit this edge-case as I'm always logged in during local dev 😉