Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rehash DB Migration throw an error when processing elasticsearch queries #7272

Closed
gfmellado opened this issue Jan 10, 2025 · 7 comments
Closed

Comments

@gfmellado
Copy link

gfmellado commented Jan 10, 2025

Issue Summary

The command docker compose run --rm server manage db upgrade throws an error when migrating elasticsearch queries from redash 10.1.0 to latest stable version 25.1.0.

Steps to Reproduce

  1. Run docker compose run --rm server manage db upgrade
  2. An error appears:
[2025-01-10 13:14:36,026][PID:1][INFO][alembic.runtime.migration] Context impl PostgresqlImpl.
[2025-01-10 13:14:36,026][PID:1][INFO][alembic.runtime.migration] Will assume transactional DDL.
[2025-01-10 13:14:36,054][PID:1][INFO][alembic.runtime.migration] Running upgrade 89bc7873a3e0 -> fd4fc850d7ea, Convert user details to jsonb and move user profile image url into details column
[2025-01-10 13:14:36,077][PID:1][INFO][alembic.runtime.migration] Running upgrade fd4fc850d7ea -> 1038c2174f5d, Make case insensitive hash of query text
[2025-01-10 13:14:36,435][PID:1][INFO][alembic.runtime.migration] Running upgrade 1038c2174f5d -> 7ce5925f832b, create sqlalchemy_searchable expressions
[2025-01-10 13:14:36,443][PID:1][INFO][alembic.runtime.migration] Running upgrade 7ce5925f832b -> 7205816877ec, change type of json fields from varchar to json
[2025-01-10 13:14:50,910][PID:1][INFO][alembic.runtime.migration] Running upgrade 7205816877ec -> 9e8c841d1a30, fix_hash
Updating hash for query 3 from be97932b23b1bed2b983949e63023fe9 to e0227632e57bf0f88784416c1ea8a65e
Query 5 has parameters. Hash might be incorrect.
Updating hash for query 5 from eb33e755efdbb127df8f6b5032246aca to ad5a52bac3147148583d8e09a28f683e
Updating hash for query 110 from 9a4ece6db120184bab3aff83645707ae to 9a4ece6db120184bab3aff83645707ae
Updating hash for query 12 from 7d263214d4cec29081ef91edfdf448d4 to 7d263214d4cec29081ef91edfdf448d4
Query 65 has parameters. Hash might be incorrect.
Updating hash for query 65 from 967c317f21bb44b82d4104eb6f697b79 to 0e0c9271ffcfcdbe8528e4ce0067e1bf
Updating hash for query 288 from cf18af4b3fe6d73ec4d58d4c50a0c725 to cf18af4b3fe6d73ec4d58d4c50a0c725
Updating hash for query 268 from c5ad543ef6eace5f76ec74661a29e520 to 5d1041bbcc6049fb8a937114e564e567
Query 6 has parameters. Hash might be incorrect.
Updating hash for query 6 from d71e116599c5d29df7bd9bd876212071 to dec7db5dce4bfeea317d7225950f53d8
Query 77 has parameters. Hash might be incorrect.
Updating hash for query 77 from e9624fe1defccfa314f44524e8172b71 to e459e961cdc17210cfd5caac94e3f808
Traceback (most recent call last):
  File "/app/manage.py", line 9, in <module>
    manager()
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/flask/cli.py", line 357, in decorator
    return __ctx.invoke(f, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/flask_migrate/cli.py", line 134, in upgrade
    _upgrade(directory, revision, sql, tag, x_arg)
  File "/usr/local/lib/python3.10/site-packages/flask_migrate/__init__.py", line 95, in wrapped
    f(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/flask_migrate/__init__.py", line 280, in upgrade
    command.upgrade(config, revision, sql=sql, tag=tag)
  File "/usr/local/lib/python3.10/site-packages/alembic/command.py", line 403, in upgrade
    script.run_env()
  File "/usr/local/lib/python3.10/site-packages/alembic/script/base.py", line 583, in run_env
    util.load_python_file(self.dir, "env.py")
  File "/usr/local/lib/python3.10/site-packages/alembic/util/pyfiles.py", line 95, in load_python_file
    module = load_module_py(module_id, path)
  File "/usr/local/lib/python3.10/site-packages/alembic/util/pyfiles.py", line 113, in load_module_py
    spec.loader.exec_module(module)  # type: ignore
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/app/migrations/env.py", line 93, in <module>
    run_migrations_online()
  File "/app/migrations/env.py", line 85, in run_migrations_online
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "/usr/local/lib/python3.10/site-packages/alembic/runtime/environment.py", line 948, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/usr/local/lib/python3.10/site-packages/alembic/runtime/migration.py", line 627, in run_migrations
    step.migration_fn(**kw)
  File "/app/migrations/versions/9e8c841d1a30_fix_hash.py", line 58, in upgrade
    new_hash = update_query_hash(record)
  File "/app/migrations/versions/9e8c841d1a30_fix_hash.py", line 26, in update_query_hash
    query_runner = get_query_runner(record['type'], {}) if record['type'] else BaseQueryRunner({})
  File "/app/redash/query_runner/__init__.py", line 433, in get_query_runner
    return query_runner_class(configuration)
  File "/app/redash/query_runner/elasticsearch.py", line 95, in __init__
    if self.server_url[-1] == "/":
IndexError: string index out of range

We've isolated the issue to all elasticsearch queries.

Technical details:

  • Redash Version: 25.1.0
  • Browser/OS: Chrome/macOS
  • How did you install Redash: Docker
@eradman
Copy link
Collaborator

eradman commented Jan 10, 2025

We thought this was fixed in #7258, but there's obviously still a flaw in the logic.

@gfmellado can you post a screen shot of your Elasticsearch data source connection?

@gfmellado
Copy link
Author

Sure, this is one of them, we have 3, but they all follow the same pattern.

Captura de pantalla 2025-01-10 a las 18 53 27

@eradman
Copy link
Collaborator

eradman commented Jan 10, 2025

Thanks, that's simple enough. I tried setting up an Elasticsearch instance on Digitalocean and Vultr to test with, but was unable to connect (curl -X POST/GET worked).
We may need someone with experience this platform to debug. There is also a call on in the docs for a maintainer for this data source: https://redash.io/help/data-sources/querying/elasticsearch/

@justinclift
Copy link
Member

@stankovic-marko @g5n828 @NicolasLM @wwl717195673 Do you still use Redash and Elasticsearch? We're looking for people to help out with the Elasticsearch data source and make sure it's working ok with the new 25.1.0 release. 😄

@wm3
Copy link
Contributor

wm3 commented Jan 18, 2025

Pardon me for jumping in.

I’m not sure if it’s okay to proceed with the process while the server_url of Elasticsearch data source hasn’t been set, but if this behavior is intentional, a simple check for an empty string should suffice.

self.server_url = self.configuration.get("server", "")
if self.server_url[-1] == "/":
self.server_url = self.server_url[:-1]

diff --git a/redash/query_runner/elasticsearch.py b/redash/query_runner/elasticsearch.py
index 3eab57dc..dae7592d 100644
--- a/redash/query_runner/elasticsearch.py
+++ b/redash/query_runner/elasticsearch.py
@@ -92,7 +92,7 @@ class BaseElasticSearch(BaseQueryRunner):
             logger.setLevel(logging.DEBUG)
 
         self.server_url = self.configuration.get("server", "")
-        if self.server_url[-1] == "/":
+        if self.server_url != "" and self.server_url[-1] == "/":
             self.server_url = self.server_url[:-1]
 
         basic_auth_user = self.configuration.get("basic_auth_user", None)

In my local environment, I was able to continue the migration after making this fix.

@eradman
Copy link
Collaborator

eradman commented Jan 20, 2025

@wm3 that looks better, although we probably want

f self.server_url and self.server_url[-1] == "/":

Can you create a PR with this change?

wm3 added a commit to wm3/redash that referenced this issue Jan 21, 2025
wm3 added a commit to wm3/redash that referenced this issue Jan 21, 2025
wm3 added a commit to wm3/redash that referenced this issue Jan 21, 2025
@wm3
Copy link
Contributor

wm3 commented Jan 22, 2025

The following IndexError report also seems to be related to this issue.

eradman pushed a commit to wm3/redash that referenced this issue Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants