transmeta/management/commands/sync_transmeta_db.py
def get_sync_sql(self, field_name, db_change_langs, model, db_table_fields):
        """ returns SQL needed for sync schema for a new translatable field """
        qn = connection.ops.quote_name
        style = no_style()
        sql_output = []
        db_table = model._meta.db_table
        was_translatable_before = self.was_translatable_before(field_name, db_table_fields)
        default_f = self.get_default_field(field_name, model)
        default_f_required = default_f and self.get_field_required_in_db(db_table,
                                                    default_f.name,
                                                    value_not_implemented=False)
        for lang in db_change_langs:
            new_field = get_real_fieldname(field_name, lang)
            try:
                f = model._meta.get_field(new_field)
                col_type = self.get_type_of_db_field(field_name, model)
                field_column = f.column
            except FieldDoesNotExist:  # columns in db, removed the settings.LANGUGES
                field_column = new_field
                col_type = self.get_type_of_db_field(field_name, model)
            field_sql = [style.SQL_FIELD(qn(field_column)), style.SQL_COLTYPE(col_type)]

            alter_colum_set = 'ALTER COLUMN %s SET' % qn(field_column)
            if default_f:
                alter_colum_drop = 'ALTER COLUMN %s DROP' % qn(field_column)
            not_null = style.SQL_KEYWORD('NOT NULL')

            if 'mysql' in backend.__name__:
                alter_colum_set = 'MODIFY %s %s' % (qn(field_column), col_type)
                not_null = style.SQL_KEYWORD('NULL')
                if default_f:
                    alter_colum_drop = 'MODIFY %s %s' % (qn(field_column), col_type)

            # column creation
            if not new_field in db_table_fields:
                sql_output.append("ALTER TABLE %s ADD COLUMN %s" % (qn(db_table), ' '.join(field_sql)))

            if lang == self.default_lang and not was_translatable_before:
                # data copy from old field (only for default language)
                sql_output.append("UPDATE %s SET %s = %s" % (qn(db_table), \
                                    qn(field_column), qn(field_name)))
                if not f.null:
                    # changing to NOT NULL after having data copied
                    sql_output.append("ALTER TABLE %s %s %s" % \
                                    (qn(db_table), alter_colum_set, \
                                    style.SQL_KEYWORD('NOT NULL')))
            elif default_f and not default_f.null:
                if lang == self.default_lang:
                    f_required = self.get_field_required_in_db(db_table,
                                                           field_column,
                                                           value_not_implemented=False)
                    if default_f.name == new_field and default_f_required:
                        continue
                    if not f_required:
                        # data copy from old field (only for default language)
                        sql_output.append(("UPDATE %(db_table)s SET %(f_colum)s = '%(value_default)s' "
                                    "WHERE %(f_colum)s is %(null)s or %(f_colum)s = '' " %  
                                        {'db_table': qn(db_table),
                                        'f_colum': qn(field_column),
                                        'value_default': self.get_value_default(),
                                        'null': style.SQL_KEYWORD('NULL'),
                                        }))
                        # changing to NOT NULL after having data copied
                        sql_output.append("ALTER TABLE %s %s %s" % \
                                        (qn(db_table), alter_colum_set, \
                                        style.SQL_KEYWORD('NOT NULL')))
                else:
                    f_required = self.get_field_required_in_db(db_table,
                                                           field_column,
                                                           value_not_implemented=True)
                    if f_required:
                        sql_output.append(("ALTER TABLE %s %s %s" % 
                                        (qn(db_table), alter_colum_drop, not_null)))

        if not was_translatable_before:
            # we drop field only if field was no translatable before
            sql_output.append("ALTER TABLE %s DROP COLUMN %s" % (qn(db_table), qn(field_name)))
        return sql_output
def get_sync_sql(self, field_name, db_change_langs, model, db_table_fields):
        """ returns SQL needed for sync schema for a new translatable field """
        qn = connection.ops.quote_name
        style = no_style()
        sql_output = []
        db_table = model._meta.db_table
        was_translatable_before = self.was_translatable_before(field_name, db_table_fields)
        default_f = self.get_default_field(field_name, model)
        default_f_required = default_f and self.get_field_required_in_db(db_table,
                                                    default_f.name,
                                                    value_not_implemented=False)
        for lang in db_change_langs:
            new_field = get_real_fieldname(field_name, lang)
            try:
                f = model._meta.get_field(new_field)
                col_type = self.get_type_of_db_field(field_name, model)
                field_column = f.column
            except FieldDoesNotExist:  # columns in db, removed the settings.LANGUGES
                field_column = new_field
                col_type = self.get_type_of_db_field(field_name, model)
            field_sql = [style.SQL_FIELD(qn(field_column)), style.SQL_COLTYPE(col_type)]

            alter_colum_set = 'ALTER COLUMN %s SET' % qn(field_column)
            if default_f:
                alter_colum_drop = 'ALTER COLUMN %s DROP' % qn(field_column)
            not_null = style.SQL_KEYWORD('NOT NULL')

            if 'mysql' in backend.__name__:
                alter_colum_set = 'MODIFY %s %s' % (qn(field_column), col_type)
                not_null = style.SQL_KEYWORD('NULL')
                if default_f:
                    alter_colum_drop = 'MODIFY %s %s' % (qn(field_column), col_type)

            # column creation
            if not new_field in db_table_fields:
                sql_output.append("ALTER TABLE %s ADD COLUMN %s" % (qn(db_table), ' '.join(field_sql)))

            if lang == self.default_lang and not was_translatable_before:
                # data copy from old field (only for default language)
                sql_output.append("UPDATE %s SET %s = %s" % (qn(db_table), \
                                    qn(field_column), qn(field_name)))
                if not f.null:
                    # changing to NOT NULL after having data copied
                    sql_output.append("ALTER TABLE %s %s %s" % \
                                    (qn(db_table), alter_colum_set, \
                                    style.SQL_KEYWORD('NOT NULL')))
            elif default_f and not default_f.null:
                if lang == self.default_lang:
                    f_required = self.get_field_required_in_db(db_table,
                                                           field_column,
                                                           value_not_implemented=False)
                    if default_f.name == new_field and default_f_required:
                        continue
                    if not f_required:
                        # data copy from old field (only for default language)
                        sql_output.append(("UPDATE %(db_table)s SET %(f_colum)s = '%(value_default)s' "
                                    "WHERE %(f_colum)s is %(null)s or %(f_colum)s = '' " %  
                                        {'db_table': qn(db_table),
                                        'f_colum': qn(field_column),
                                        'value_default': self.get_value_default(),
                                        'null': style.SQL_KEYWORD('NULL'),
                                        }))
                        # changing to NOT NULL after having data copied
                        sql_output.append("ALTER TABLE %s %s %s" % \
                                        (qn(db_table), alter_colum_set, \
                                        style.SQL_KEYWORD('NOT NULL')))
                else:
                    f_required = self.get_field_required_in_db(db_table,
                                                           field_column,
                                                           value_not_implemented=True)
                    if f_required:
                        sql_output.append(("ALTER TABLE %s %s %s" % 
                                        (qn(db_table), alter_colum_drop, not_null)))

        if not was_translatable_before:
            # we drop field only if field was no translatable before
            sql_output.append("ALTER TABLE %s DROP COLUMN %s" % (qn(db_table), qn(field_name)))
        return sql_output
def get_sync_sql ( self , field_name , db_change_langs , model , db_table_fields ) : qn = connection . ops . quote_name style = no_style ( ) sql_output = [ ] db_table = model . _meta . db_table was_translatable_before = self . was_translatable_before ( field_name , db_table_fields ) default_f = self . get_default_field ( field_name , model ) default_f_required = default_f and self . get_field_required_in_db ( db_table , default_f . name , value_not_implemented = False ) for lang in db_change_langs : new_field = get_real_fieldname ( field_name , lang ) try : f = model . _meta . get_field ( new_field ) col_type = self . get_type_of_db_field ( field_name , model ) field_column = f . column except FieldDoesNotExist : # columns in db, removed the settings.LANGUGES field_column = new_field col_type = self . get_type_of_db_field ( field_name , model ) field_sql = [ style . SQL_FIELD ( qn ( field_column ) ) , style . SQL_COLTYPE ( col_type ) ] alter_colum_set = 'ALTER COLUMN %s SET' % qn ( field_column ) if default_f : alter_colum_drop = 'ALTER COLUMN %s DROP' % qn ( field_column ) not_null = style . SQL_KEYWORD ( 'NOT NULL' ) if 'mysql' in backend . __name__ : alter_colum_set = 'MODIFY %s %s' % ( qn ( field_column ) , col_type ) not_null = style . SQL_KEYWORD ( 'NULL' ) if default_f : alter_colum_drop = 'MODIFY %s %s' % ( qn ( field_column ) , col_type ) # column creation if not new_field in db_table_fields : sql_output . append ( "ALTER TABLE %s ADD COLUMN %s" % ( qn ( db_table ) , ' ' . join ( field_sql ) ) ) if lang == self . default_lang and not was_translatable_before : # data copy from old field (only for default language) sql_output . append ( "UPDATE %s SET %s = %s" % ( qn ( db_table ) , qn ( field_column ) , qn ( field_name ) ) ) if not f . null : # changing to NOT NULL after having data copied sql_output . append ( "ALTER TABLE %s %s %s" % ( qn ( db_table ) , alter_colum_set , style . SQL_KEYWORD ( 'NOT NULL' ) ) ) elif default_f and not default_f . null : if lang == self . default_lang : f_required = self . get_field_required_in_db ( db_table , field_column , value_not_implemented = False ) if default_f . name == new_field and default_f_required : continue if not f_required : # data copy from old field (only for default language) sql_output . append ( ( "UPDATE %(db_table)s SET %(f_colum)s = '%(value_default)s' " "WHERE %(f_colum)s is %(null)s or %(f_colum)s = '' " % { 'db_table' : qn ( db_table ) , 'f_colum' : qn ( field_column ) , 'value_default' : self . get_value_default ( ) , 'null' : style . SQL_KEYWORD ( 'NULL' ) , } ) ) # changing to NOT NULL after having data copied sql_output . append ( "ALTER TABLE %s %s %s" % ( qn ( db_table ) , alter_colum_set , style . SQL_KEYWORD ( 'NOT NULL' ) ) ) else : f_required = self . get_field_required_in_db ( db_table , field_column , value_not_implemented = True ) if f_required : sql_output . append ( ( "ALTER TABLE %s %s %s" % ( qn ( db_table ) , alter_colum_drop , not_null ) ) ) if not was_translatable_before : # we drop field only if field was no translatable before sql_output . append ( "ALTER TABLE %s DROP COLUMN %s" % ( qn ( db_table ) , qn ( field_name ) ) ) return sql_output
returns SQL needed for sync schema for a new translatable field
returns SQL needed for sync schema for a new translatable field