Breaking the database model - how are migrations done?

28 views
Skip to first unread message

Peter Smit

unread,
Dec 31, 2014, 5:32:17 PM12/31/14
to gog...@googlegroups.com
Hi,

I was looking to a nice feature I could implement, e.g. ssh deploy keys, and I notice I will have to make db changes that are more than simply adding tables and columns. 

How does xorm handle index updates and column removals? Is there any way to insert migration code?

To give an example, we have now a PublicKey struct: https://github.com/gogits/gogs/blob/dev/models/publickey.go#L80-L90

type PublicKey struct {
Id                int64
OwnerId           int64     `xorm:"UNIQUE(s) INDEX NOT NULL"`
Name              string    `xorm:"UNIQUE(s) NOT NULL"`
Fingerprint       string    `xorm:"INDEX NOT NULL"`
Content           string    `xorm:"TEXT NOT NULL"`
Created           time.Time `xorm:"CREATED"`
Updated           time.Time
HasRecentActivity bool `xorm:"-"`
HasUsed           bool `xorm:"-"`
}

Lets say we want to remove the OwnerId from this struct and add these two structs

// One to many relationship
type PublicKeyOwner struct {
      Id                int64
OwnerId           int64     `xorm:"INDEX NOT NULL"`
PublicKeyId           int64     `xorm:"UNIQUE INDEX NOT NULL"`
}

// Many-to-many relationship
type PublicKeyRepo struct {
      Id                int64
RepoId           int64     `xorm:"UNIQUE(s) INDEX NOT NULL"`
PublicKeyId           int64     `xorm:"UNIQUE(s) INDEX NOT NULL"`
}

Just coding this will work great for new Gogs installations, but how will migration be done?

Regards,

Peter

xiaol...@gmail.com

unread,
Jan 3, 2015, 9:03:57 AM1/3/15
to gog...@googlegroups.com
I think you have to write some SQL to migrate the table’s data. Xorm don’t provider migrate tool to do that.
And you could use 

    x.Sync2(new(PublicKeyOwner), new(PublicKeyRepo))

to auto create the two tables.
And use Exec() to execute your SQLs.

    x.Exec(sql, param1, param2)


--
You received this message because you are subscribed to the Google Groups "Gogs - Go Git Service" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gogits+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages