ctl: add role support to convert-to-sql command (#53849)

This commit is contained in:
Frédéric Péters 2021-07-05 09:28:44 +02:00
parent ea00edc923
commit d59cff0a70
2 changed files with 42 additions and 17 deletions

View File

@ -9,6 +9,7 @@ from quixote import get_publisher
from wcs.fields import BoolField
from wcs.formdef import FormDef
from wcs.roles import Role
from wcs.sql import cleanup_connection
from .utilities import clean_temporary_pub, create_temporary_pub, force_connections_close
@ -129,8 +130,24 @@ def test_data_is_migrated(pub, database, local_user, formdeffix):
call_command('convert_to_sql', '-d', 'example.net', '--database', database)
pub.load_site_options()
assert pub.site_options.has_option('options', 'postgresql')
assert len(pub.user_class.get_users_with_name_identifier('0123456789')) == 1
pub.is_using_postgresql = lambda: True
pub.set_config()
formdefs = FormDef.select()
assert len(formdefs) == 1
data_class = formdefs[0].data_class(mode='sql')
assert len(data_class.keys()) == 4
def test_users_and_roles(pub, database, local_user):
role = Role(name='Test Role')
role.store()
pub.load_site_options()
assert not pub.site_options.has_option('options', 'postgresql')
call_command('convert_to_sql', '-d', 'example.net', '--database', database)
pub.load_site_options()
assert pub.site_options.has_option('options', 'postgresql')
pub.is_using_postgresql = lambda: True
pub.set_config()
assert len(pub.user_class.get_users_with_name_identifier('0123456789')) == 1
assert pub.role_class.count() == 1

View File

@ -28,6 +28,7 @@ from wcs.formdef import FormDef
from wcs.qommon.misc import localstrftime
from wcs.qommon.publisher import UnknownTenantError, get_publisher_class
from wcs.qommon.storage import atomic_write
from wcs.roles import Role
from wcs.users import User
@ -50,6 +51,7 @@ class Command(BaseCommand):
self.setup_connection(**options)
sql.get_connection(new=True)
self.store_roles()
self.store_users()
self.store_forms()
self.publisher.write_cfg()
@ -84,28 +86,34 @@ class Command(BaseCommand):
atomic_write(options_file, force_bytes(stringio.getvalue()))
def store_users(self):
errors = []
print('converting users')
sql.do_user_table()
count = User.count()
for i, user_id in enumerate(User.keys()):
user = User.get(user_id)
user.__class__ = sql.SqlUser
try:
user.store()
except AssertionError:
errors.append((user, traceback.format_exc()))
self.update_progress(100 * i / count)
self.convert_objects('user', User, sql.SqlUser)
sql.SqlUser.fix_sequences()
def store_roles(self):
self.convert_objects('role', Role, sql.Role)
def convert_objects(self, object_name, object_class, object_sql_class):
errors = []
print('converting %ss' % object_name)
getattr(sql, 'do_%s_table' % object_name)()
count = object_class.count()
for i, obj_id in enumerate(object_class.keys()):
obj = object_class.get(obj_id)
obj.__class__ = object_sql_class
try:
obj.store()
except AssertionError:
errors.append((obj, traceback.format_exc()))
self.update_progress(100 * i / count)
if errors:
with open('error_user.log', 'w') as error_log:
for user, trace in errors:
error_log.write('user_id %s\n' % user.id)
with open('error_%s.log' % object_name, 'w') as error_log:
for obj, trace in errors:
error_log.write('obj_id %s\n' % obj.id)
error_log.write(trace)
error_log.write('-' * 80)
error_log.write('\n\n')
print('There were some errors, see error_user.log for details.')
print('There were some errors, see error_%s.log for details.' % object_name)
def store_forms(self):
errors = []