django migrate with clickhouse

110 Views Asked by At

I have a project with two databases: PostgreSQL and Clickhouse.

To interact with Clickhouse, I use the django-clickhouse-backend lib. For a long time, I did not use the created migrations for this database, but created tables using raw SQL. Now it's time to apply all the previous migrations, but I ran into a problem.

To avoid all conflicts, I had to significantly edit the existing migration files, after that I locally modeled and checked the application of these migrations in the existing database and everything was OK. But when I tried to apply these migrations on the production server, I got the following error:

# python manage.py migrate --database clickhouse --fake-initial
Operations to perform:
  Apply all migrations: admin, auth, authtoken, celery_app, contenttypes, django_dramatiq, edl, sessions, users
Running migrations:
  Applying contenttypes.0001_initial...Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/clickhouse_driver/dbapi/cursor.py", line 111, in execute
    response = execute(
               ^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/clickhouse_backend/driver/client.py", line 53, in execute
    rv = self.process_ordinary_query(
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/clickhouse_driver/client.py", line 571, in process_ordinary_query
    return self.receive_result(with_column_types=with_column_types,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/clickhouse_driver/client.py", line 204, in receive_result
    return result.get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/clickhouse_driver/result.py", line 50, in get_result
    for packet in self.packet_generator:
  File "/usr/local/lib/python3.11/site-packages/clickhouse_driver/client.py", line 220, in packet_generator
    packet = self.receive_packet()
             ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/clickhouse_driver/client.py", line 237, in receive_packet
    raise packet.exception
clickhouse_driver.errors.ServerException: Code: 57.
DB::Exception: Table edl.django_migrations already exists. Stack trace:

0. DB::Exception::Exception(DB::Exception::MessageMasked&&, int, bool) @ 0x000000000cdd11b7 in /usr/bin/clickhouse
1. DB::Exception::Exception<String&, String, String>(int, FormatStringHelperImpl<std::type_identity<String&>::type, std::type_identity<String>::type, std::type_identity<String>::type>, String&, String&&, String&&) @ 0x0000000006c9791e in /usr/bin/clickhouse
2. DB::InterpreterCreateQuery::doCreateTable(DB::ASTCreateQuery&, DB::InterpreterCreateQuery::TableProperties const&, std::unique_ptr<DB::DDLGuard, std::default_delete<DB::DDLGuard>>&) @ 0x00000000125e309f in /usr/bin/clickhouse
3. DB::InterpreterCreateQuery::createTable(DB::ASTCreateQuery&) @ 0x00000000125d9ddf in /usr/bin/clickhouse
4. DB::InterpreterCreateQuery::execute() @ 0x00000000125e725e in /usr/bin/clickhouse
5. DB::executeQueryImpl(char const*, char const*, std::shared_ptr<DB::Context>, bool, DB::QueryProcessingStage::Enum, DB::ReadBuffer*) @ 0x0000000012be45f5 in /usr/bin/clickhouse
6. DB::executeQuery(String const&, std::shared_ptr<DB::Context>, bool, DB::QueryProcessingStage::Enum) @ 0x0000000012bde2fe in /usr/bin/clickhouse
7. DB::TCPHandler::runImpl() @ 0x0000000013aa32c9 in /usr/bin/clickhouse
8. DB::TCPHandler::run() @ 0x0000000013ab7c79 in /usr/bin/clickhouse
9. Poco::Net::TCPServerConnection::start() @ 0x000000001658ffb4 in /usr/bin/clickhouse
10. Poco::Net::TCPServerDispatcher::run() @ 0x00000000165911b1 in /usr/bin/clickhouse
11. Poco::PooledThread::run() @ 0x000000001669c8c7 in /usr/bin/clickhouse
12. Poco::ThreadImpl::runnableEntry(void*) @ 0x000000001669ab5c in /usr/bin/clickhouse
13. ? @ 0x00007fa57718bac3 in ?
14. ? @ 0x00007fa57721da40 in ?

The fact is that the edl.django_migrations table does not exist initially, but it is created after executing this command.

I tried execute python mamage.py migrate --database clickhouse --fake, but it didn't help.

I tried migrating the django_migrations table from PostgreSQL to Clickhouse and execute python mamage.py migrate --database clickhouse --fake, but it also didn't give results.

0

There are 0 best solutions below