def django_db_setup(django_db_setup, django_db_keepdb): database = Database( CLICKHOUSE_DATABASE, db_url=CLICKHOUSE_HTTP_URL, username=CLICKHOUSE_USER, password=CLICKHOUSE_PASSWORD, verify_ssl_cert=CLICKHOUSE_VERIFY, ) if not django_db_keepdb: try: database.drop_database() except: pass if not django_db_keepdb or not database.db_exists: database.create_database() database.migrate("ee.clickhouse.migrations") # Make DELETE / UPDATE synchronous to avoid flaky tests sync_execute("SET mutations_sync = 1") yield if not django_db_keepdb: try: database.drop_database() except: pass
def handle(self, *args, **options): from django.test.runner import DiscoverRunner as TestRunner test_runner = TestRunner(interactive=False) test_runner.setup_databases() test_runner.setup_test_environment() if is_clickhouse_enabled(): from infi.clickhouse_orm import Database # type: ignore from posthog.settings import ( CLICKHOUSE_DATABASE, CLICKHOUSE_HTTP_URL, CLICKHOUSE_PASSWORD, CLICKHOUSE_REPLICATION, CLICKHOUSE_USER, CLICKHOUSE_VERIFY, ) database = Database( CLICKHOUSE_DATABASE, db_url=CLICKHOUSE_HTTP_URL, username=CLICKHOUSE_USER, password=CLICKHOUSE_PASSWORD, verify_ssl_cert=CLICKHOUSE_VERIFY, ) try: database.create_database() except: pass database.migrate("ee.clickhouse.migrations", replicated=CLICKHOUSE_REPLICATION)
def handle(self, *args, **options): database = Database( CLICKHOUSE_DATABASE, db_url=CLICKHOUSE_HTTP_URL, username=CLICKHOUSE_USER, password=CLICKHOUSE_PASSWORD, verify_ssl_cert=False, ) if options["plan"]: print("List of clickhouse migrations to be applied:") for migration_name in self.get_migrations(database, options["upto"]): print(f"Migration would get applied: {migration_name}") else: print("Clickhouse migrations up to date!") elif options["fake"]: for migration_name in self.get_migrations(database, options["upto"]): print(f"Faked migration: {migration_name}") database.insert([ MigrationHistory( package_name=MIGRATIONS_PACKAGE_NAME, module_name=migration_name, applied=datetime.date.today(), ) ]) print("Migrations done") else: database.migrate(MIGRATIONS_PACKAGE_NAME, options["upto"]) print("Migration successful")
def migrate(self, host, options): database = Database( CLICKHOUSE_DATABASE, db_url=host, username=CLICKHOUSE_USER, password=CLICKHOUSE_PASSWORD, verify_ssl_cert=False, ) if options["plan"]: print("List of clickhouse migrations to be applied:") migrations = list(self.get_migrations(database, options["upto"])) for migration_name, operations in migrations: print(f"Migration would get applied: {migration_name}") for op in operations: sql = getattr(op, "_sql") if options["print_sql"] and sql is not None: print(indent("\n\n".join(sql), " ")) if len(migrations) == 0: print("Clickhouse migrations up to date!") elif options["fake"]: for migration_name, _ in self.get_migrations( database, options["upto"]): print(f"Faked migration: {migration_name}") database.insert([ MigrationHistory( package_name=MIGRATIONS_PACKAGE_NAME, module_name=migration_name, applied=datetime.date.today(), ) ]) print("Migrations done") else: database.migrate(MIGRATIONS_PACKAGE_NAME, options["upto"]) print("Migration successful")
def handle(self, *args, **options): if not TEST: raise ValueError( "TEST environment variable needs to be set for this command to function" ) from django.test.runner import DiscoverRunner as TestRunner test_runner = TestRunner(interactive=False) test_runner.setup_databases() test_runner.setup_test_environment() from infi.clickhouse_orm import Database from posthog.settings import ( CLICKHOUSE_CLUSTER, CLICKHOUSE_DATABASE, CLICKHOUSE_HTTP_URL, CLICKHOUSE_PASSWORD, CLICKHOUSE_REPLICATION, CLICKHOUSE_USER, CLICKHOUSE_VERIFY, ) database = Database( CLICKHOUSE_DATABASE, db_url=CLICKHOUSE_HTTP_URL, username=CLICKHOUSE_USER, password=CLICKHOUSE_PASSWORD, cluster=CLICKHOUSE_CLUSTER, verify_ssl_cert=CLICKHOUSE_VERIFY, ) try: database.create_database() except: pass database.migrate("ee.clickhouse.migrations", replicated=CLICKHOUSE_REPLICATION)