blob: 0001439f95781d6e52debd9ce8072ec64405a240 [file] [log] [blame]
showard250d84d2010-01-12 21:59:48 +00001TABLE_TYPE = object()
2VIEW_TYPE = object()
showardeab66ce2009-12-23 00:03:56 +00003
4
jamesren53cd3242010-05-07 21:33:28 +00005class NameMissingException(Exception):
6 pass
7
8
showardeab66ce2009-12-23 00:03:56 +00009def drop_views(manager, views):
10 """
11 Drops the specified views from the database
12
13 If a specified view does not exist in the database, this method fails
14 without modification
15
16 @param manager the migration manager
17 @param views the views to drop
18 """
jamesren53cd3242010-05-07 21:33:28 +000019 check_exists(manager, views, VIEW_TYPE)
showardeab66ce2009-12-23 00:03:56 +000020 for view in views:
21 manager.execute('DROP VIEW `%s`' % view)
22
23
24def rename(manager, mapping):
25 """
26 Renames specified tables in the database
27
28 Use this to rename a specified set of tables in a database. If a source in
29 the mapping does not exist, this method fails without modification.
30
31 @param manager the migration manager
32 @param mapping a dictionary of orig_name => new_name. Any table not matching
33 an entry in this dictionary will not be renamed
34 """
jamesren53cd3242010-05-07 21:33:28 +000035 check_exists(manager, (table for table, _ in mapping.iteritems()),
showard250d84d2010-01-12 21:59:48 +000036 TABLE_TYPE)
showardeab66ce2009-12-23 00:03:56 +000037 for orig_name, new_name in mapping.iteritems():
38 manager.execute('RENAME TABLE `%s` TO `%s`' % (orig_name, new_name))
39
40
showard250d84d2010-01-12 21:59:48 +000041def move_tables(manager, src_manager, tables):
42 """
43 Moves the specified tables from another database
44
45 If a table does not exist in the source database, this method fails without
46 modification
47
48 @param manager the migration manager
49 @param src_manager a migration manager that handles the source database
50 @param tables a list of tables to move
51 """
jamesren53cd3242010-05-07 21:33:28 +000052 check_exists(src_manager, tables, TABLE_TYPE)
showard250d84d2010-01-12 21:59:48 +000053 for table in tables:
54 manager.execute('RENAME TABLE `%(db)s`.`%(table)s` TO `%(table)s`'
55 % dict(db=src_manager.get_db_name(), table=table))
56
57
58def drop_database(manager):
59 """
60 Drops the database that the specified manager controls
61
62 @param manager the migration manager
63 """
64 manager.execute('DROP DATABASE `%s`' % manager.get_db_name())
65
66
jamesren53cd3242010-05-07 21:33:28 +000067def check_exists(manager, names, type):
showardeab66ce2009-12-23 00:03:56 +000068 """
69 Checks if the tables or views exists.
70
71 Raise an Exception if any of the names do not exist
72
73 @param manager the migration manager
74 @param names the table/view names
75 @param type one of 'TABLE' or 'VIEW'
76 """
showard250d84d2010-01-12 21:59:48 +000077 if type == TABLE_TYPE:
showardeab66ce2009-12-23 00:03:56 +000078 info_table = 'TABLES'
showard250d84d2010-01-12 21:59:48 +000079 elif type == VIEW_TYPE:
showardeab66ce2009-12-23 00:03:56 +000080 info_table = 'VIEWS'
81 else:
showard250d84d2010-01-12 21:59:48 +000082 raise Exception("type parameter must be either TABLE_TYPE or VIEW_TYPE")
showardeab66ce2009-12-23 00:03:56 +000083
84 query = ('SELECT table_name FROM information_schema.%s '
85 'WHERE table_schema = %%s' % info_table)
86 rows = manager.execute(query, manager.get_db_name())
87 existing_names = [row[0] for row in rows]
88
89 for name in names:
90 if name not in existing_names:
jamesren53cd3242010-05-07 21:33:28 +000091 raise NameMissingException(
92 '%s missing from database, stopping' % name)
93
94
jamesrenb41f6c92010-06-09 22:56:26 +000095def check_index_exists(manager, table_name, index_name):
96 """
97 Checks if a particular index exists on the table
98
99 @param manager the migration manager
100 @param table_name the table to check
101 @param index_name the index to check
102 """
103 query = ('SELECT 1 FROM information_schema.statistics '
104 'WHERE table_schema = %s AND table_name = %s AND index_name = %s')
105 rows = manager.execute(query, manager.get_db_name(), table_name, index_name)
106 return bool(rows)
107
108
jamesren53cd3242010-05-07 21:33:28 +0000109DJANGO_AUTH_TABLES = ('auth_group', 'auth_group_permissions', 'auth_permission')
110
111def auth_tables_exist(manager):
112 try:
113 check_exists(manager, DJANGO_AUTH_TABLES, TABLE_TYPE)
114 return True
115 except NameMissingException:
116 return False