2018-12-18 15:33:34 +03:00
|
|
|
from infi.clickhouse_orm.database import Database as InfiDatabase, ServerError
|
|
|
|
from infi.clickhouse_orm.migrations import MigrationHistory
|
2018-11-15 14:50:38 +03:00
|
|
|
|
|
|
|
from .configuration import config
|
2018-11-16 11:14:40 +03:00
|
|
|
from .exceptions import DBAliasError
|
2018-11-15 14:50:38 +03:00
|
|
|
|
2018-11-15 15:37:58 +03:00
|
|
|
|
2018-11-16 11:14:40 +03:00
|
|
|
class Database(InfiDatabase):
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
infi_kwargs = {
|
|
|
|
k: kwargs[k]
|
|
|
|
for k in ('db_name', 'db_url', 'username', 'password', 'readonly', 'autocreate')
|
|
|
|
if k in kwargs
|
|
|
|
}
|
|
|
|
super(Database, self).__init__(**infi_kwargs)
|
|
|
|
|
2018-11-23 09:04:31 +03:00
|
|
|
def drop_database(self):
|
|
|
|
# BUG fix https://github.com/Infinidat/infi.clickhouse_orm/issues/89
|
|
|
|
super(Database, self).drop_database()
|
|
|
|
self.db_exists = False
|
|
|
|
|
2018-11-16 11:14:40 +03:00
|
|
|
def migrate(self, migrations_package_name, up_to=9999):
|
|
|
|
raise NotImplementedError('This method is not supported by django-clickhouse.'
|
|
|
|
' Use django_clickhouse.migrations module instead.')
|
|
|
|
|
2018-12-18 15:33:34 +03:00
|
|
|
def _get_applied_migrations(self, migrations_package_name):
|
2018-12-18 16:28:23 +03:00
|
|
|
raise NotImplementedError("This method is not supported by django_clickhouse.")
|
2018-12-18 15:33:34 +03:00
|
|
|
|
2018-11-16 11:14:40 +03:00
|
|
|
|
2018-11-15 14:50:38 +03:00
|
|
|
class ConnectionProxy:
|
|
|
|
_connections = {}
|
|
|
|
|
|
|
|
def get_connection(self, alias):
|
2018-11-15 15:37:58 +03:00
|
|
|
if alias is None:
|
2018-11-28 13:30:19 +03:00
|
|
|
alias = config.DEFAULT_DB_ALIAS
|
2018-11-15 15:37:58 +03:00
|
|
|
|
2018-11-15 14:50:38 +03:00
|
|
|
if alias not in self._connections:
|
|
|
|
if alias not in config.DATABASES:
|
|
|
|
raise DBAliasError(alias)
|
|
|
|
|
|
|
|
self._connections[alias] = Database(**config.DATABASES[alias])
|
|
|
|
|
|
|
|
return self._connections[alias]
|
|
|
|
|
2018-11-15 15:37:58 +03:00
|
|
|
def __getitem__(self, item):
|
2018-11-15 14:50:38 +03:00
|
|
|
return self.get_connection(item)
|
|
|
|
|
|
|
|
|
|
|
|
connections = ConnectionProxy()
|