0

I have a table defined as:

@Table(name = "Rooms")
@Entity
interface IRooms : Persistable {
    @get:Column(nullable = false)
    @get:Key
    var roomJid: String

    @get:Column(name = "gameId", nullable = false)
    @get:ManyToOne
    var game: IGames

    @get:Column(name = "creatorJid", nullable = false)
    @get:ManyToOne
    var creator: IProfiles

    @get:Column(nullable = false)
    @get:OneToMany(cascade = arrayOf(CascadeAction.DELETE, CascadeAction.SAVE))
    val players: MutableList<IPlayers>

    @get:JunctionTable
    @get:ManyToMany(cascade = arrayOf(CascadeAction.DELETE, CascadeAction.SAVE))
    val invitedContacts: MutableList<IInvitedContacts>

    @get:Column(nullable = false)
    var lastModified: Long

    @get:Column(nullable = false)
    var isLocked: Boolean

    var nextPlayerJid: String?

    @get:Column(nullable = false)
    var hasGameEnded: Boolean

    @get:Column(nullable = false)
    var isSoloRoom: Boolean
}

What I want to do is to delete isSoloRoom column and add another column matchType defined as:

@get:Convert(MatchType.RequeryConverter::class)
var matchType: MatchType?

Now, I want to write migration script to remove isSoloRoom column and if isSoloRoom value is True set matchType to SOLO else null.

What I tried:

  1. I removed isSoloRoom column from the Schema above and in onUpgrade method I wrote:

db?.execSQL("UPDATE Rooms SET ('matchType') = ('SOLO') WHERE soloRoom = 1")

The matchType column is successfully added with desired effect but isSoloRoom is not removed.

What should I do in order to remove the isSoloRoom column and add matchType column. Should I remove it from Schema too and from onUpgrade script too via sqlite (which would be very hectic to write entire create table) or does requery provides some alternative to it?

LS_ᴅᴇᴠ
  • 10,823
  • 1
  • 23
  • 46
Embydextrous
  • 1,611
  • 1
  • 12
  • 20
  • I don't know why you are programming like this. It looks REALLY old school. Change your style grandpa lol (joke). I'd suggest you use the Android SQLite "norm" .... Create you `contract` class ; update it at will... Create EVERY SINGLE `create` table statement.. I know it's painful but you know what !? ... When you'll encounter situations like yours, you'll love to just change DB_VERSION so that `onUpgrade` does the job – Jason Krs Jul 28 '17 at 11:34
  • That seriously isn't cool and does not answer the problem. – Embydextrous Jul 28 '17 at 19:11
  • oooo I'm sorry. Did not mean to hurt you... Actually, you can't drop a column with SQLite https://stackoverflow.com/a/8442173/6266949 No matter what method you think of, if you want to remove a column in SQLite, just backup what ever you want, Then drop the table. Recreate it with a new structure and restore backup information...Sorry again.. – Jason Krs Jul 28 '17 at 22:16

0 Answers0