Last active
June 13, 2016 08:38
-
-
Save stefanfoulis/c6b393d05751314addca to your computer and use it in GitHub Desktop.
reliably upgrading my plugins to the new django-cms 3.0 table names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Do not rename the table for plugins in the first "create" migration. | |
OR: if you do... make the create table statement only run if the table does not already exist. | |
""" | |
class Migration(): | |
def forwards(self, orm): | |
# ... | |
def backwards(self, orm): | |
# ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from . import rename_tables_new_to_old | |
class Migration(): | |
cms_plugin_table_mapping = ( | |
# (old_name, new_name), | |
('cmsplugin_filerfile', 'cmsplugin_filer_file_filerfile'), | |
) | |
def forwards(self, orm): | |
rename_tables_new_to_old(db, self.cms_plugin_table_mapping) | |
# ... | |
def backwards(self, orm): | |
rename_tables_new_to_old(db, self.cms_plugin_table_mapping) | |
# ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from . import rename_tables_new_to_old, rename_tables_old_to_new | |
class Migration(): | |
cms_plugin_table_mapping = ( | |
# (old_name, new_name), | |
('cmsplugin_filerfile', 'cmsplugin_filer_file_filerfile'), | |
) | |
def forwards(self, orm): | |
rename_tables_old_to_new(db, self.cms_plugin_table_mapping) | |
# ... | |
def backwards(self, orm): | |
rename_tables_new_to_old(db, self.cms_plugin_table_mapping) | |
# ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- coding: utf-8 -*- | |
def rename_tables(db, table_mapping, reverse=False): | |
""" | |
renames tables from source to destination name, if the source exists and the destination does | |
not exist yet. | |
""" | |
from django.db import connection | |
if reverse: | |
table_mapping = [(dst, src) for src, dst in table_mapping] | |
table_names = connection.introspection.table_names() | |
for source, destination in table_mapping: | |
if source in table_names and destination in table_names: | |
print u" WARNING: not renaming {0} to {1}, because both tables already exist.".format(source, destination) | |
elif source in table_names and destination not in table_names: | |
print u" - renaming {0} to {1}".format(source, destination) | |
db.rename_table(source, destination) | |
def rename_tables_old_to_new(db, table_mapping): | |
return rename_tables(db, table_mapping, reverse=False) | |
def rename_tables_new_to_old(db, table_mapping): | |
return rename_tables(db, table_mapping, reverse=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
helps a lot!