realm-swift icon indicating copy to clipboard operation
realm-swift copied to clipboard

Support renaming tables in migration

Open jpsim opened this issue 10 years ago • 12 comments
trafficstars

As originally requested here: https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/realm-cocoa/sLd7S-jSX40/FeQSRPW0BwAJ

This could be exposed as -[RLMMigration renameModelFrom:(NSString *)oldName to:(NSString *)newName], which would use core's Group::rename_table() function.

jpsim avatar Sep 07 '15 18:09 jpsim

Is there a workaround to rename a class that works with the current version of RealmSwift?

Based on the ObjC example, I've created this configuration:

    let config = Realm.Configuration(
      schemaVersion: 2,

      migrationBlock: { migration, oldSchemaVersion in
        if (oldSchemaVersion < 2) {
          migration.enumerate("Person", { (oldObject, newObject) in
            if let oldObject = oldObject {
              print(oldObject)
              migration.create("Employee", value: oldObject)
            }
          })
        }
    })

This does print the first oldObject, but it crashes on the migration.create with the unhelpful error message: libc++abi.dylib: terminating with uncaught exception of type NSException

Update: I've noticed that the crash is due to one of fields being an (optional) RealmObject. If I nil that out, it doesn't crash.

tomlokhorst avatar Nov 04 '16 10:11 tomlokhorst

it crashes on the migration.create with the unhelpful error message: libc++abi.dylib: terminating with uncaught exception of type NSException

This is only unhelpful if you choose to ignore the exception message. From your update to your comment, it seems like your two classes may have different schemas?

jpsim avatar Nov 08 '16 19:11 jpsim

Ah, I didn't know you could catch NSExceptions in Swift, but I got it to work.

This is the error that occurs: Error Domain=RLMException Code=0 "(null)" UserInfo={RLMRealmVersion=2.0.3, RLMRealmCoreVersion=unknown}

The only change I made to the schema is renaming the Object. Person to Employee, in this example.

tomlokhorst avatar Nov 08 '16 20:11 tomlokhorst

Any plans to support this on the roadmap?

marchy avatar Aug 25 '17 21:08 marchy

@marchy #2499 has been closed. I think this has been delayed.

ricardopereira avatar Apr 11 '18 12:04 ricardopereira

It's been already implemented.. So simple to be added, but still not merged. :(

aleksey-ho avatar Jan 11 '19 14:01 aleksey-ho

TOP

mefilt avatar Oct 28 '20 16:10 mefilt

So is this available yet or still not merged in? Been a few years now...

marchy avatar Oct 28 '20 22:10 marchy

Another 4 years have passed, is there any update on this feature?

It's as easy in Android as: schema.rename("A", "B")

Very frustrating we cant do this for iOS

mattlewer avatar Nov 04 '24 10:11 mattlewer

@mattlewer Realm is now dead, there won't be such a feature unless you contribute it yourself

aehlke avatar Nov 06 '24 08:11 aehlke

@aehlke How do you mean it's dead, last release was 2 weeks ago?

mattlewer avatar Nov 06 '24 12:11 mattlewer

@mattlewer it's recently deprecated and will not receive any more commits or releases from parent company later next year. it's transitioning to an open source framework without any parent company supporting it. there's been very little community contribution until now so I don't expect it to become very active after Mongo step down.

aehlke avatar Nov 21 '24 01:11 aehlke