5 class NameMissingException(Exception):
9 def drop_views(manager
, views
):
11 Drops the specified views from the database
13 If a specified view does not exist in the database, this method fails
16 @param manager the migration manager
17 @param views the views to drop
19 check_exists(manager
, views
, VIEW_TYPE
)
21 manager
.execute('DROP VIEW `%s`' % view
)
24 def rename(manager
, mapping
):
26 Renames specified tables in the database
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.
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
35 check_exists(manager
, (table
for table
, _
in mapping
.iteritems()),
37 for orig_name
, new_name
in mapping
.iteritems():
38 manager
.execute('RENAME TABLE `%s` TO `%s`' % (orig_name
, new_name
))
41 def move_tables(manager
, src_manager
, tables
):
43 Moves the specified tables from another database
45 If a table does not exist in the source database, this method fails without
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
52 check_exists(src_manager
, tables
, TABLE_TYPE
)
54 manager
.execute('RENAME TABLE `%(db)s`.`%(table)s` TO `%(table)s`'
55 % dict(db
=src_manager
.get_db_name(), table
=table
))
58 def drop_database(manager
):
60 Drops the database that the specified manager controls
62 @param manager the migration manager
64 manager
.execute('DROP DATABASE `%s`' % manager
.get_db_name())
67 def check_exists(manager
, names
, type):
69 Checks if the tables or views exists.
71 Raise an Exception if any of the names do not exist
73 @param manager the migration manager
74 @param names the table/view names
75 @param type one of 'TABLE' or 'VIEW'
77 if type == TABLE_TYPE
:
79 elif type == VIEW_TYPE
:
82 raise Exception("type parameter must be either TABLE_TYPE or VIEW_TYPE")
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
]
90 if name
not in existing_names
:
91 raise NameMissingException(
92 '%s missing from database, stopping' % name
)
95 def check_index_exists(manager
, table_name
, index_name
):
97 Checks if a particular index exists on the table
99 @param manager the migration manager
100 @param table_name the table to check
101 @param index_name the index to check
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
)
109 DJANGO_AUTH_TABLES
= ('auth_group', 'auth_group_permissions', 'auth_permission')
111 def auth_tables_exist(manager
):
113 check_exists(manager
, DJANGO_AUTH_TABLES
, TABLE_TYPE
)
115 except NameMissingException
: