-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Oleg Strokachuk
committed
Jan 3, 2022
1 parent
cdc6724
commit 4272483
Showing
9 changed files
with
252 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
""" | ||
Simple file-based migrations for clickhouse | ||
""" | ||
__version__ = "0.1.4" | ||
__version__ = "0.1.5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import logging | ||
from pathlib import Path | ||
from typing import List | ||
|
||
from clickhouse_driver import Client | ||
|
||
from clickhouse_migrations.defaults import DB_HOST, DB_PASSWORD, DB_USER | ||
from clickhouse_migrations.migrator import Migrator | ||
from clickhouse_migrations.types import Migration, MigrationStorage | ||
|
||
|
||
class ClickhouseCluster: | ||
def __init__( | ||
self, | ||
db_host: str = DB_HOST, | ||
db_user: str = DB_USER, | ||
db_password: str = DB_PASSWORD, | ||
): | ||
self.db_host = db_host | ||
self.db_user = db_user | ||
self.db_password = db_password | ||
|
||
def connection(self, db_name: str) -> Client: | ||
return Client( | ||
self.db_host, user=self.db_user, password=self.db_password, database=db_name | ||
) | ||
|
||
def create_db(self, db_name): | ||
with self.connection("") as conn: | ||
conn.execute(f"CREATE DATABASE IF NOT EXISTS {db_name}") | ||
|
||
def init_schema(self, db_name): | ||
with self.connection(db_name) as conn: | ||
migrator = Migrator(conn) | ||
migrator.init_schema() | ||
|
||
def show_tables(self, db_name): | ||
with self.connection(db_name) as conn: | ||
result = conn.execute("show tables") | ||
return [t[0] for t in result] | ||
|
||
def migrate( | ||
self, | ||
db_name: str, | ||
migration_path: Path, | ||
create_db_if_no_exists: bool = True, | ||
): | ||
storage = MigrationStorage(migration_path) | ||
migrations = storage.migrations() | ||
|
||
return self.apply_migrations( | ||
db_name, migrations, create_db_if_no_exists=create_db_if_no_exists | ||
) | ||
|
||
def apply_migrations( | ||
self, | ||
db_name: str, | ||
migrations: List[Migration], | ||
create_db_if_no_exists: bool = True, | ||
) -> List[Migration]: | ||
|
||
if create_db_if_no_exists: | ||
self.create_db(db_name) | ||
|
||
logging.info("Total migrations to apply: %d", len(migrations)) | ||
|
||
with self.connection(db_name) as conn: | ||
migrator = Migrator(conn) | ||
migrator.init_schema() | ||
return migrator.apply_migration(migrations) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.