sqlmesh icon indicating copy to clipboard operation
sqlmesh copied to clipboard

SQLmesh requires write access to the database

Open neutralboy opened this issue 1 year ago • 2 comments

I am trying to read from a MySQL source which I only have read only access to and write into a postgres source that I have full control over. My model is defined as below:

MODEL (
  name public.patient,
  cron '@daily',
  kind INCREMENTAL_BY_TIME_RANGE (
    time_column registration_date
  ),
  grain patient_id,
  dialect mysql
);

SELECT
  r.id AS patient_id,
  r.RegistrationDate AS registration_date,
  r.RegistrationNo AS mrn,
  r.PatientFullName AS name,
  r.DOB AS date_of_birth,
ra.PresentPINCode AS pin
FROM Base_DB.Registration AS r
INNER JOIN Base_DB.RegistrationAddress AS ra
  ON r.id = ra.RegistrationId

After reading the docs I realised that this should be defined as an external model and I have defined it as below

- name: Base_DB.Registration
  description: Patient Registration Table
  columns:
    id: int
    RegistrationDate: date
    RegistrationNo: text
    PatientFullName: text
    DOB: text
    GenderCode: int
    Email: text
    MobileNo: text
- name: Base_DB.RegistrationAddress
  description: Patient Registration Address Table
  columns:
    PresentPINCode: text

When I run sqlmesh plan prod or even dev for that matter I see the error below

SELECT command denied to user 'read_only'@'*****' for table `sqlmesh`.`_snapshots`
Unable to run a plan - Complete log
Traceback (most recent call last):
  File "/home/vscode/.local/lib/python3.10/site-packages/web/server/api/endpoints/plan.py", line 173, in _get_plan_builder
    plan_builder = context.plan_builder(
  File "/home/vscode/.local/lib/python3.10/site-packages/sqlmesh/core/analytics/__init__.py", line 110, in wrapper
    return func(*args, **kwargs)
  File "/home/vscode/.local/lib/python3.10/site-packages/sqlmesh/core/context.py", line 1259, in plan_builder
    snapshots = self._snapshots(models_override)
  File "/home/vscode/.local/lib/python3.10/site-packages/sqlmesh/core/context.py", line 2104, in _snapshots
    stored_snapshots = self.state_reader.get_snapshots(snapshots.values())
  File "/home/vscode/.local/lib/python3.10/site-packages/sqlmesh/core/state_sync/engine_adapter.py", line 759, in get_snapshots
    return self._get_snapshots(snapshot_ids)
  File "/home/vscode/.local/lib/python3.10/site-packages/sqlmesh/core/state_sync/engine_adapter.py", line 810, in _get_snapshots
    snapshots, cached_snapshots = self._snapshot_cache.get_or_load(
  File "/home/vscode/.local/lib/python3.10/site-packages/sqlmesh/core/snapshot/cache.py", line 50, in get_or_load
    loaded_snapshots = loader(snapshot_ids_to_load)
  File "/home/vscode/.local/lib/python3.10/site-packages/sqlmesh/core/state_sync/engine_adapter.py", line 791, in _loader
    ) in self._fetchall(query):
  File "/home/vscode/.local/lib/python3.10/site-packages/sqlmesh/core/state_sync/engine_adapter.py", line 199, in _fetchall
    return self.engine_adapter.fetchall(
  File "/home/vscode/.local/lib/python3.10/site-packages/sqlmesh/core/engine_adapter/base.py", line 1924, in fetchall
    self.execute(
  File "/home/vscode/.local/lib/python3.10/site-packages/sqlmesh/core/engine_adapter/base.py", line 2069, in execute
    self._execute(sql, **kwargs)
  File "/home/vscode/.local/lib/python3.10/site-packages/sqlmesh/core/engine_adapter/base.py", line 2075, in _execute
    self.cursor.execute(sql, **kwargs)
  File "/home/vscode/.local/lib/python3.10/site-packages/mysql/connector/cursor.py", line 537, in execute
    self._handle_result(self._connection.cmd_query(stmt))
  File "/home/vscode/.local/lib/python3.10/site-packages/mysql/connector/opentelemetry/context_propagation.py", line 97, in wrapper
    return method(cnx, *args, **kwargs)
  File "/home/vscode/.local/lib/python3.10/site-packages/mysql/connector/connection.py", line 872, in cmd_query
    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
  File "/home/vscode/.local/lib/python3.10/site-packages/mysql/connector/connection.py", line 648, in _handle_result
    raise get_exception(packet)
mysql.connector.errors.ProgrammingError: 1142 (42000): SELECT command denied to user 'read_only'@'****' for table `sqlmesh`.`_snapshots`
I went through the docs and even tried asking CoPilot about the repo on how I could store metadata elsewhere. So far I have had no luck in solving this.

Thanks in advance!

neutralboy avatar Jan 17 '25 17:01 neutralboy

I'm not sure you can do this unless you have a way to read from MYSQL and write to Postgres. They need to have some kind of bridge.

One way you can accomplish this is to make your main gateway/engine postgres, and to read from mysql through a Python model. You can convert that data into Pandas, and then upload it to postgres.

https://sqlmesh.readthedocs.io/en/stable/concepts/models/python_models/?h=python

tobymao avatar Jan 17 '25 17:01 tobymao

Thanks!

neutralboy avatar Jan 17 '25 17:01 neutralboy