carrotquest / django-clickhouse

This project's goal is to build Yandex ClickHouse database into Django project.
MIT License
101 stars 26 forks source link

[SOLVED]Unapplied migrations and no errors. Add to docs. #19

Closed The-Daishogun closed 3 years ago

The-Daishogun commented 3 years ago

Hello I am building a django backend which uses clickhouse to keep track of the number of clicks or downloads from a url. i've gone through the docs step by step to get it to work but unfortunately, even though the sql migrations are applied, click house migrations don't seem to do anything. and i don't get any errors which makes it very hard to track down the problem. here are my codes:

# clickhouse_models.py
from django_clickhouse.clickhouse_models import ClickHouseModel
from django_clickhouse.engines import MergeTree
from infi.clickhouse_orm import fields
from stats.models import Stat

class ClickHouseStat(ClickHouseModel):
    django_model = Stat

    id = fields.UInt32Field()
    download_count = fields.UInt32Field(default=0)
    url = fields.StringField()

    engine = MergeTree("download_count", ("download_count",))
# clickhouse_migrations/0001_initial.py
from django_clickhouse import migrations
from stats.clickhouse_models import ClickHouseStat

class Migration(migrations.Migration):
    operations = [migrations.CreateTable(ClickHouseStat)]
# settings.py
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.mysql",
        "NAME": "mysite_db",
        "USER": "mysite_db_user",
        "PASSWORD": "admin",
        "HOST": "localhost",
        "PORT": "",
    }
}

CLICKHOUSE_REDIS_CONFIG = {
    "host": "127.0.0.1",
    "port": 6379,
    "db": 10,
    "socket_timeout": 10,
}

CLICKHOUSE_DATABASES = {
    # Connection name to refer in using(...) method
    "default": {
        "db_name": "mysite_db",
        "username": "default",
        "password": "",
    }
}

CLICKHOUSE_CELERY_QUEUE = "clickhouse"

CELERYBEAT_SCHEDULE = {
    "clickhouse_auto_sync": {
        "task": "django_clickhouse.tasks.clickhouse_auto_sync",
        "schedule": timedelta(seconds=2),  # Every 2 seconds
        "options": {"expires": 1, "queue": CLICKHOUSE_CELERY_QUEUE},
    }
}

thank you in advance.

M1ha-Shvn commented 3 years ago

Hi. Is your Django app added to INSTALLED_APPS setting? Migrations are triggerred by post_migrate django signal for each installed app. You can see code here. When migration is applied during manage.py migrate call, you should see this message in output. If not, it means one of the following:

  1. post_migrate has not been called for default db and your app for some reason
  2. CLICKHOUSE_MIGRATE_WITH_DEFAULT_DB setting is set to False
  3. Database has readonly=True or migrate=False in its configuration
  4. your_app.clickhouse_migrations package can not be imported
The-Daishogun commented 3 years ago

Thank you for you quick response. the app is registered in INSTALLED_APPS since the sql migrations get applied. i also set the CLICKHOUSE_MIGRATE_WITH_DEFAULT_DB to True in settings.py and added migrate=True to the clickhouse database configurations but it didn't help.

i forced the migrations using

# manual migrations

import os
import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "podafarini.settings")
django.setup()
from django_clickhouse.migrations import migrate_app, clickhouse_migrate

migrate_app("stats", "default")

it worked fine. and i received the aforementioned message. but no luck with applying them automatically

M1ha-Shvn commented 3 years ago

Is django_clickhouse added to installed apps? You may also try calling some simple print statement on post_migrate in order to check if signal is called for default db and your app

The-Daishogun commented 3 years ago

Tdding django_clickhouse to installed apps was the solution. Thank you very much.

M1ha-Shvn commented 3 years ago

This should be added to docs. I'll do it a little bit later.

The-Daishogun commented 3 years ago

I thought i'd missed something when i read the docs. thank you so much.

M1ha-Shvn commented 3 years ago

Released