Fixed error in migrate. Wrote tests

This commit is contained in:
M1ha 2018-12-18 17:33:34 +05:00
parent 2d4c8554cd
commit 66ec7dd5db
4 changed files with 44 additions and 2 deletions

View File

@ -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 .exceptions import DBAliasError
@ -22,6 +23,21 @@ class Database(InfiDatabase):
raise NotImplementedError('This method is not supported by django-clickhouse.'
' 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:
_connections = {}

View File

@ -53,13 +53,17 @@ def migrate_app(app_label, db_alias, up_to=9999, database=None):
if config.DATABASES[db_alias].get('readonly', False):
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)
if module_exists(migrations_package):
database = database or connections[db_alias]
applied_migrations = database._get_applied_migrations(migrations_package)
modules = import_submodules(migrations_package)
unapplied_migrations = set(modules.keys()) - applied_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.apply(db_alias, database=database)
# Ensure that table for migration storing is created
database.create_table(MigrationHistory)
database.insert([
MigrationHistory(package_name=migrations_package, module_name=name, applied=datetime.date.today())
])

View File

@ -58,6 +58,18 @@ CLICKHOUSE_DATABASES = {
'db_name': 'test_2',
'username': 'default',
'password': ''
},
'no_migrate': {
'db_name': 'test_3',
'username': 'default',
'password': '',
'migrate': False
},
'readonly': {
'db_name': 'test_3',
'username': 'default',
'password': '',
'readonly': True
}
}

View File

@ -45,3 +45,11 @@ class MigrateAppTest(TestCase):
def test_router_not_allowed(self):
migrate_app('tests', 'default')
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))