Skip to content

Fail to run migration when default_table_options is set #4454

Closed

Description

@arnaudbriche

Past Issues Searched

  • I have searched open and closed issues to make sure that the bug has not yet been reported

Issue is a Bug Report

  • This is a bug report and not a feature request, nor asking for self-hosted support

Using official Plausible Cloud hosting or self-hosting?

Self-hosting

Describe the bug

I overrided default_table_options to configure Plausible to generate Replicated* tables and execute DDL statement with ON CLUSTER clause.
Here is the config I set in a file pointed to by EXTRA_CONFIG_PATH

import Config
    
config :ecto_ch,
  default_table_engine: "ReplicatedMergeTree",
  default_table_options: [{:cluster, "default"}]

When I run migrations with /entrypoint.sh db migrate I get the following error:

Loading plausible..
Starting dependencies..
Starting repos..
Running migrations for Elixir.Plausible.Repo
Running migrations for Elixir.Plausible.IngestRepo
warning: code block contains unused literal "Migration adds a ALIAS columns needed to keep DB schema and api\nproperty naming in sync to reduce overhead in code.\n" (remove the literal or assign it to _ to avoid warnings)
└─ lib/plausible-0.0.1/priv/ingest_repo/migrations/20240502115822_alias_api_prop_names.exs: Plausible.IngestRepo.Migrations.AliasApiPropNames (module)

** (ArgumentError) expected a keyword list as the second argument, got: ["PARTITION BY toYYYYMM(timestamp) ORDER BY (domain, toDate(timestamp), user_id) SETTINGS index_granularity = 8192"]
    (elixir 1.16.0) lib/keyword.ex:999: Keyword.merge/2
    (ecto_ch 0.3.5) lib/ecto/adapters/clickhouse/migration.ex:177: Ecto.Adapters.ClickHouse.Migration.table/1
    (ecto_ch 0.3.5) lib/ecto/adapters/clickhouse/migration.ex:17: Ecto.Adapters.ClickHouse.Migration.execute_ddl/1
    (ecto_sql 3.11.1) lib/ecto/adapters/sql.ex:1159: Ecto.Adapters.SQL.execute_ddl/4

Expected behavior

I expected migrations to run without error.

Screenshots

No response

Environment

- OS: linux (k8s)
- Browser:
- Browser Version:

Activity

  1. added
    self-hosting
    on Aug 21, 2024
  2. ruslandoga commented on Aug 21, 2024

    @ruslandoga
    Contributor

    👋 @arnaudbriche

    The error means that we can't merge default keyword options like [{:cluster, "default"}] with the raw options provided in the migrations like "PARTITION BY toYYYYMM(timestamp) ORDER BY (domain, toDate(timestamp), user_id) SETTINGS index_granularity = 8192" in https://github.com/plausible/analytics/blob/master/priv/ingest_repo/migrations/20200915070607_create_events_and_sessions.exs#L12-L14.

    I guess the fix for this could be to rewrite all Ecto migrations to use keyword options, but then it would still not achieve your goals since we also have a separate way of migrating (using templated raw sql) which doesn't use Ecto migrations and as such would't use ecto_ch default options.

  3. arnaudbriche commented on Aug 21, 2024

    @arnaudbriche
    Author

    Hi @ruslandoga

    Ok. So there is really no way to get an HA Clickhouse setup with Plausible ?
    Can we generate plain HTML from migrations some way or another so that we can at least edit the SQL manually ?

  4. ruslandoga commented on Aug 21, 2024

    @ruslandoga
    Contributor

    AFAIK Plausible Cloud runs HA ClickHouse setup so it should be possible without any changes to the codebase. There are some helper functions sprinkled throughout like Plausible.IngestRepo.clustered_table?: https://github.com/search?q=repo%3Aplausible%2Fanalytics%20cluster&type=code - and they do most of the work. I guess you'd need to somehow "bootstrap" the cluster and some initial tables and from there Plausible would keep doing the right thing for the new tables.

  5. arnaudbriche commented on Aug 21, 2024

    @arnaudbriche
    Author

    Yes. I see a lot of this in the code:

    <%= if @cluster? do %>
    ENGINE = ReplicatedMergeTree('/clickhouse/{cluster}/tables/{shard}/{database}/events_v2', '{replica}')
    <% else %>
    ENGINE = MergeTree()
    <% end %>
    

    But there is literally no documented way to set this clustervariable.
    Can anyone provide assistance ?

  6. ruslandoga commented on Aug 21, 2024

    @ruslandoga
    Contributor

    It's not documented since Plausible CE is mostly geared towards simple, single-host deployments for the sake of simplicity and easy of use :)

    Can anyone provide assistance ?

    I think you can try creating the tables manually (with the correct CLUSTER options) from (outdated) https://github.com/plausible/analytics/blob/master/priv/ingest_repo/structure.sql and then starting Plausible and running automated migrations.

  7. arnaudbriche commented on Aug 21, 2024

    @arnaudbriche
    Author

    Make sense that CE is not targeting big deployment.
    I built a solution in the form of an HTTP proxy that rewrite DDL statements on the fly to rewrite CREATE TABLE queries.
    It's working fine on v2.1.0 and v2.1.1 so it might be useful to the community.

    https://github.com/arnaudbriche/plausible-ddl-proxy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    self-hosting

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions