mirror of
https://github.com/carrotquest/django-clickhouse.git
synced 2025-02-16 19:00:34 +03:00
Fixed error in migrate. Wrote tests
This commit is contained in:
parent
2d4c8554cd
commit
66ec7dd5db
|
@ -1,4 +1,5 @@
|
||||||
from infi.clickhouse_orm.database import Database as InfiDatabase
|
from infi.clickhouse_orm.database import Database as InfiDatabase, ServerError
|
||||||
|
from infi.clickhouse_orm.migrations import MigrationHistory
|
||||||
|
|
||||||
from .configuration import config
|
from .configuration import config
|
||||||
from .exceptions import DBAliasError
|
from .exceptions import DBAliasError
|
||||||
|
@ -22,6 +23,21 @@ class Database(InfiDatabase):
|
||||||
raise NotImplementedError('This method is not supported by django-clickhouse.'
|
raise NotImplementedError('This method is not supported by django-clickhouse.'
|
||||||
' Use django_clickhouse.migrations module instead.')
|
' Use django_clickhouse.migrations module instead.')
|
||||||
|
|
||||||
|
def _get_applied_migrations(self, migrations_package_name):
|
||||||
|
# I don't want to create any tables here, so I changed method to work in without table
|
||||||
|
if not self.db_exists:
|
||||||
|
return []
|
||||||
|
|
||||||
|
query = "SELECT module_name from $table WHERE package_name = '%s'" % migrations_package_name
|
||||||
|
query = self._substitute(query, MigrationHistory)
|
||||||
|
try:
|
||||||
|
return set(obj.module_name for obj in self.select(query))
|
||||||
|
except ServerError as ex:
|
||||||
|
# Database doesn't exist or table doesn't exist
|
||||||
|
if ex.code in {81, 60}:
|
||||||
|
return set()
|
||||||
|
raise ex
|
||||||
|
|
||||||
|
|
||||||
class ConnectionProxy:
|
class ConnectionProxy:
|
||||||
_connections = {}
|
_connections = {}
|
||||||
|
|
|
@ -53,13 +53,17 @@ def migrate_app(app_label, db_alias, up_to=9999, database=None):
|
||||||
if config.DATABASES[db_alias].get('readonly', False):
|
if config.DATABASES[db_alias].get('readonly', False):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# Ignore force not migrated databases
|
||||||
|
if not config.DATABASES[db_alias].get('migrate', True):
|
||||||
|
return
|
||||||
|
|
||||||
migrations_package = "%s.%s" % (app_label, config.MIGRATIONS_PACKAGE)
|
migrations_package = "%s.%s" % (app_label, config.MIGRATIONS_PACKAGE)
|
||||||
|
|
||||||
if module_exists(migrations_package):
|
if module_exists(migrations_package):
|
||||||
database = database or connections[db_alias]
|
database = database or connections[db_alias]
|
||||||
|
|
||||||
applied_migrations = database._get_applied_migrations(migrations_package)
|
applied_migrations = database._get_applied_migrations(migrations_package)
|
||||||
modules = import_submodules(migrations_package)
|
modules = import_submodules(migrations_package)
|
||||||
|
|
||||||
unapplied_migrations = set(modules.keys()) - applied_migrations
|
unapplied_migrations = set(modules.keys()) - applied_migrations
|
||||||
|
|
||||||
for name in sorted(unapplied_migrations):
|
for name in sorted(unapplied_migrations):
|
||||||
|
@ -67,6 +71,8 @@ def migrate_app(app_label, db_alias, up_to=9999, database=None):
|
||||||
migration = modules[name].Migration()
|
migration = modules[name].Migration()
|
||||||
migration.apply(db_alias, database=database)
|
migration.apply(db_alias, database=database)
|
||||||
|
|
||||||
|
# Ensure that table for migration storing is created
|
||||||
|
database.create_table(MigrationHistory)
|
||||||
database.insert([
|
database.insert([
|
||||||
MigrationHistory(package_name=migrations_package, module_name=name, applied=datetime.date.today())
|
MigrationHistory(package_name=migrations_package, module_name=name, applied=datetime.date.today())
|
||||||
])
|
])
|
||||||
|
|
|
@ -58,6 +58,18 @@ CLICKHOUSE_DATABASES = {
|
||||||
'db_name': 'test_2',
|
'db_name': 'test_2',
|
||||||
'username': 'default',
|
'username': 'default',
|
||||||
'password': ''
|
'password': ''
|
||||||
|
},
|
||||||
|
'no_migrate': {
|
||||||
|
'db_name': 'test_3',
|
||||||
|
'username': 'default',
|
||||||
|
'password': '',
|
||||||
|
'migrate': False
|
||||||
|
},
|
||||||
|
'readonly': {
|
||||||
|
'db_name': 'test_3',
|
||||||
|
'username': 'default',
|
||||||
|
'password': '',
|
||||||
|
'readonly': True
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,3 +45,11 @@ class MigrateAppTest(TestCase):
|
||||||
def test_router_not_allowed(self):
|
def test_router_not_allowed(self):
|
||||||
migrate_app('tests', 'default')
|
migrate_app('tests', 'default')
|
||||||
self.assertFalse(table_exists(self.db, ClickHouseTestModel))
|
self.assertFalse(table_exists(self.db, ClickHouseTestModel))
|
||||||
|
|
||||||
|
def test_no_migrate_connections(self):
|
||||||
|
migrate_app('tests', 'no_migrate')
|
||||||
|
self.assertFalse(table_exists(connections['no_migrate'], ClickHouseTestModel))
|
||||||
|
|
||||||
|
def test_readonly_connections(self):
|
||||||
|
migrate_app('tests', 'readonly')
|
||||||
|
self.assertFalse(table_exists(connections['readonly'], ClickHouseTestModel))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user