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