def handle(self, *args, **options): try: Database( CLICKHOUSE_DATABASE, db_url=CLICKHOUSE_HTTP_URL, username=CLICKHOUSE_USERNAME, password=CLICKHOUSE_PASSWORD, verify_ssl_cert=False, ).migrate("ee.clickhouse.migrations") print("migration successful") except Exception as e: print(e)
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)
def migrate(self, host, options): database = Database( CLICKHOUSE_DATABASE, db_url=host, username=CLICKHOUSE_USER, password=CLICKHOUSE_PASSWORD, cluster=CLICKHOUSE_CLUSTER, verify_ssl_cert=False, ) if options["plan"] or options["check"]: 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", None) 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["check"]: exit(1) 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"], replicated=CLICKHOUSE_REPLICATION) print("✅ Migration successful")
from infi.clickhouse_orm import Database Database('default').migrate('clicks.migrations')
import psutil, time, datetime from infi.clickhouse_orm import Database from models import CPUStats db = Database('demo') db.create_table(CPUStats) psutil.cpu_percent(percpu=True) # first sample should be discarded while True: time.sleep(1) stats = psutil.cpu_percent(percpu=True) timestamp = datetime.datetime.now() print(timestamp) db.insert([ CPUStats(timestamp=timestamp, cpu_id=cpu_id, cpu_percent=cpu_percent) for cpu_id, cpu_percent in enumerate(stats) ])
word = word + Style.RESET_ALL text.append(word) return ' '.join(text) def find(db, text): ''' Performs the search for the given text, and prints out the matches. ''' stems = prepare_search_terms(text) query = build_query(db, stems) print('\n' + Fore.MAGENTA + str(query) + Style.RESET_ALL + '\n') for match in query: text = get_matching_text(db, match.document, match.idx, match.idx + len(stems) - 1) print(Fore.CYAN + match.document + ':' + Style.RESET_ALL, text) if __name__ == '__main__': # Initialize colored output init() # Initialize database db = Database('default') # Search text = ' '.join(sys.argv[1:]) if text: find(db, text)