• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import common
2from autotest_lib.database import db_utils
3
4UP_SQL = """
5SET @group_id = (SELECT id FROM auth_group WHERE name = 'Basic Admin');
6
7INSERT IGNORE INTO auth_group_permissions (group_id, permission_id)
8SELECT @group_id, id FROM auth_permission WHERE codename IN (
9  'add_droneset', 'change_droneset', 'delete_droneset', 'add_drone',
10  'change_drone', 'delete_drone');
11"""
12
13DOWN_SQL = """
14DELETE auth_group_permissions.* FROM
15auth_group INNER JOIN auth_group_permissions ON (
16  auth_group.id = auth_group_permissions.group_id)
17INNER JOIN auth_permission ON (
18  auth_group_permissions.permission_id = auth_permission.id)
19WHERE auth_group.name = 'Basic Admin' AND codename IN (
20  'add_droneset', 'change_droneset', 'delete_droneset', 'add_drone',
21  'change_drone', 'delete_drone');
22"""
23
24
25def migrate_up(manager):
26    """
27    If the auth tables don't exist, we shouldn't try to set the permissions.
28
29    The auth tables will exist if this is an existing Autotest installation. If
30    they don't, then this is a fresh installation, and the user will run
31    `manage.py syncdb` later, which will add the proper permissions.
32    """
33    if db_utils.auth_tables_exist(manager):
34        manager.execute_script(UP_SQL)
35
36
37def migrate_down(manager):
38    if db_utils.auth_tables_exist(manager):
39        manager.execute_script(DOWN_SQL)
40