mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
account -> org table migration is starting to work, need to test mysql and postgres
This commit is contained in:
parent
ed68a4bb9a
commit
f3f79792ab
@ -18,25 +18,34 @@ func AddMigrations(mg *Migrator) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func addMigrationLogMigrations(mg *Migrator) {
|
func addMigrationLogMigrations(mg *Migrator) {
|
||||||
mg.AddMigration("create migration_log table", new(AddTableMigration).
|
migrationLogV1 := Table{
|
||||||
Name("migration_log").WithColumns(
|
Name: "migration_log",
|
||||||
|
Columns: []*Column{
|
||||||
&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
||||||
&Column{Name: "migration_id", Type: DB_NVarchar, Length: 255},
|
&Column{Name: "migration_id", Type: DB_NVarchar, Length: 255},
|
||||||
&Column{Name: "sql", Type: DB_Text},
|
&Column{Name: "sql", Type: DB_Text},
|
||||||
&Column{Name: "success", Type: DB_Bool},
|
&Column{Name: "success", Type: DB_Bool},
|
||||||
&Column{Name: "error", Type: DB_Text},
|
&Column{Name: "error", Type: DB_Text},
|
||||||
&Column{Name: "timestamp", Type: DB_DateTime},
|
&Column{Name: "timestamp", Type: DB_DateTime},
|
||||||
))
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
mg.AddMigration("create migration_log table", NewAddTableMigration(migrationLogV1))
|
||||||
}
|
}
|
||||||
|
|
||||||
func addStarMigrations(mg *Migrator) {
|
func addStarMigrations(mg *Migrator) {
|
||||||
mg.AddMigration("create star table", new(AddTableMigration).
|
starV1 := Table{
|
||||||
Name("star").WithColumns(
|
Name: "star",
|
||||||
|
Columns: []*Column{
|
||||||
&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
||||||
&Column{Name: "user_id", Type: DB_BigInt, Nullable: false},
|
&Column{Name: "user_id", Type: DB_BigInt, Nullable: false},
|
||||||
&Column{Name: "dashboard_id", Type: DB_BigInt, Nullable: false},
|
&Column{Name: "dashboard_id", Type: DB_BigInt, Nullable: false},
|
||||||
))
|
},
|
||||||
|
Indices: []*Index{
|
||||||
mg.AddMigration("add unique index star.user_id_dashboard_id", new(AddIndexMigration).
|
&Index{Cols: []string{"user_id", "dashboard_id"}, Type: UniqueIndex},
|
||||||
Table("star").Columns("user_id", "dashboard_id").Unique())
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
mg.AddMigration("create star table", NewAddTableMigration(starV1))
|
||||||
|
mg.AddMigration("add unique index star.user_id_dashboard_id", NewAddIndexMigration(starV1, starV1.Indices[0]))
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ func TestMigrations(t *testing.T) {
|
|||||||
|
|
||||||
mg := NewMigrator(x)
|
mg := NewMigrator(x)
|
||||||
mg.LogLevel = log.DEBUG
|
mg.LogLevel = log.DEBUG
|
||||||
addMigrations(mg)
|
AddMigrations(mg)
|
||||||
|
|
||||||
err = mg.Start()
|
err = mg.Start()
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
@ -55,7 +55,7 @@ func addOrgMigrations(mg *Migrator) {
|
|||||||
"name": "name",
|
"name": "name",
|
||||||
"created": "created",
|
"created": "created",
|
||||||
"updated": "updated",
|
"updated": "updated",
|
||||||
}))
|
}).IfTableExists("account"))
|
||||||
|
|
||||||
mg.AddMigration("copy data account_user to org_user", NewCopyTableDataMigration("org_user", "account_user", map[string]string{
|
mg.AddMigration("copy data account_user to org_user", NewCopyTableDataMigration("org_user", "account_user", map[string]string{
|
||||||
"id": "id",
|
"id": "id",
|
||||||
@ -64,7 +64,7 @@ func addOrgMigrations(mg *Migrator) {
|
|||||||
"role": "role",
|
"role": "role",
|
||||||
"created": "created",
|
"created": "created",
|
||||||
"updated": "updated",
|
"updated": "updated",
|
||||||
}))
|
}).IfTableExists("account_user"))
|
||||||
|
|
||||||
mg.AddMigration("Drop old table account", NewDropTableMigration("account"))
|
mg.AddMigration("Drop old table account", NewDropTableMigration("account"))
|
||||||
mg.AddMigration("Drop old table account_user", NewDropTableMigration("account_user"))
|
mg.AddMigration("Drop old table account_user", NewDropTableMigration("account_user"))
|
||||||
|
@ -26,20 +26,11 @@ func addUserMigrations(mg *Migrator) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// create table
|
||||||
mg.AddMigration("create user table", NewAddTableMigration(userV1))
|
mg.AddMigration("create user table", NewAddTableMigration(userV1))
|
||||||
|
// add indices
|
||||||
mg.AddMigration("Add email_verified flag", new(AddColumnMigration).
|
mg.AddMigration("add unique index user.login", NewAddIndexMigration(userV1, userV1.Indices[0]))
|
||||||
Table("user").Column(&Column{Name: "email_verified", Type: DB_Bool, Nullable: true}))
|
mg.AddMigration("add unique index user.email", NewAddIndexMigration(userV1, userV1.Indices[1]))
|
||||||
|
|
||||||
mg.AddMigration("Add user.theme column", new(AddColumnMigration).
|
|
||||||
Table("user").Column(&Column{Name: "theme", Type: DB_Varchar, Nullable: true, Length: 20}))
|
|
||||||
|
|
||||||
//------- user table indexes ------------------
|
|
||||||
mg.AddMigration("add unique index user.login", new(AddIndexMigration).
|
|
||||||
Table("user").Columns("login").Unique())
|
|
||||||
|
|
||||||
mg.AddMigration("add unique index user.email", new(AddIndexMigration).
|
|
||||||
Table("user").Columns("email").Unique())
|
|
||||||
|
|
||||||
// ---------------------
|
// ---------------------
|
||||||
// account -> org changes
|
// account -> org changes
|
||||||
|
@ -85,17 +85,6 @@ func (m *AddIndexMigration) Table(tableName string) *AddIndexMigration {
|
|||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *AddIndexMigration) Unique() *AddIndexMigration {
|
|
||||||
m.index.Type = UniqueIndex
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *AddIndexMigration) Columns(columns ...string) *AddIndexMigration {
|
|
||||||
m.index = &Index{}
|
|
||||||
m.index.Cols = columns
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *AddIndexMigration) Sql(dialect Dialect) string {
|
func (m *AddIndexMigration) Sql(dialect Dialect) string {
|
||||||
return dialect.CreateIndexSql(m.tableName, m.index)
|
return dialect.CreateIndexSql(m.tableName, m.index)
|
||||||
}
|
}
|
||||||
@ -110,22 +99,6 @@ func NewDropIndexMigration(table Table, index *Index) *DropIndexMigration {
|
|||||||
return &DropIndexMigration{tableName: table.Name, index: index}
|
return &DropIndexMigration{tableName: table.Name, index: index}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *DropIndexMigration) Table(tableName string) *DropIndexMigration {
|
|
||||||
m.tableName = tableName
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *DropIndexMigration) Unique() *DropIndexMigration {
|
|
||||||
m.index.Type = UniqueIndex
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *DropIndexMigration) Columns(columns ...string) *DropIndexMigration {
|
|
||||||
m.index = &Index{}
|
|
||||||
m.index.Cols = columns
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *DropIndexMigration) Sql(dialect Dialect) string {
|
func (m *DropIndexMigration) Sql(dialect Dialect) string {
|
||||||
if m.index.Name == "" {
|
if m.index.Name == "" {
|
||||||
m.index.Name = fmt.Sprintf("%s", strings.Join(m.index.Cols, "_"))
|
m.index.Name = fmt.Sprintf("%s", strings.Join(m.index.Cols, "_"))
|
||||||
@ -139,6 +112,11 @@ type AddTableMigration struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewAddTableMigration(table Table) *AddTableMigration {
|
func NewAddTableMigration(table Table) *AddTableMigration {
|
||||||
|
for _, col := range table.Columns {
|
||||||
|
if col.IsPrimaryKey {
|
||||||
|
table.PrimaryKeys = append(table.PrimaryKeys, col.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
return &AddTableMigration{table: table}
|
return &AddTableMigration{table: table}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,34 +124,6 @@ func (m *AddTableMigration) Sql(d Dialect) string {
|
|||||||
return d.CreateTableSql(&m.table)
|
return d.CreateTableSql(&m.table)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *AddTableMigration) Table(table Table) *AddTableMigration {
|
|
||||||
m.table = table
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *AddTableMigration) Name(name string) *AddTableMigration {
|
|
||||||
m.table.Name = name
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *AddTableMigration) WithColumns(columns ...*Column) *AddTableMigration {
|
|
||||||
for _, col := range columns {
|
|
||||||
m.table.Columns = append(m.table.Columns, col)
|
|
||||||
if col.IsPrimaryKey {
|
|
||||||
m.table.PrimaryKeys = append(m.table.PrimaryKeys, col.Name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *AddTableMigration) WithColumn(col *Column) *AddTableMigration {
|
|
||||||
m.table.Columns = append(m.table.Columns, col)
|
|
||||||
if col.IsPrimaryKey {
|
|
||||||
m.table.PrimaryKeys = append(m.table.PrimaryKeys, col.Name)
|
|
||||||
}
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
type DropTableMigration struct {
|
type DropTableMigration struct {
|
||||||
MigrationBase
|
MigrationBase
|
||||||
tableName string
|
tableName string
|
||||||
|
@ -35,7 +35,7 @@ func AddOrgUser(cmd *m.AddOrgUserCommand) error {
|
|||||||
func GetOrgUsers(query *m.GetOrgUsersQuery) error {
|
func GetOrgUsers(query *m.GetOrgUsersQuery) error {
|
||||||
query.Result = make([]*m.OrgUserDTO, 0)
|
query.Result = make([]*m.OrgUserDTO, 0)
|
||||||
sess := x.Table("org_user")
|
sess := x.Table("org_user")
|
||||||
sess.Join("INNER", "user", fmt.Sprintf("account_user.user_id=%s.id", x.Dialect().Quote("user")))
|
sess.Join("INNER", "user", fmt.Sprintf("org_user.user_id=%s.id", x.Dialect().Quote("user")))
|
||||||
sess.Where("org_user.org_id=?", query.OrgId)
|
sess.Where("org_user.org_id=?", query.OrgId)
|
||||||
sess.Cols("org_user.org_id", "org_user.user_id", "user.email", "user.login", "org_user.role")
|
sess.Cols("org_user.org_id", "org_user.user_id", "user.email", "user.login", "org_user.role")
|
||||||
sess.Asc("user.email", "user.login")
|
sess.Asc("user.email", "user.login")
|
||||||
|
@ -234,7 +234,7 @@ func GetUserOrgList(query *m.GetUserOrgListQuery) error {
|
|||||||
sess := x.Table("org_user")
|
sess := x.Table("org_user")
|
||||||
sess.Join("INNER", "org", "org_user.org_id=org.id")
|
sess.Join("INNER", "org", "org_user.org_id=org.id")
|
||||||
sess.Where("org_user.user_id=?", query.UserId)
|
sess.Where("org_user.user_id=?", query.UserId)
|
||||||
sess.Cols("org.name", "org_user.role", "org_user.account_id")
|
sess.Cols("org.name", "org_user.role", "org_user.org_id")
|
||||||
err := sess.Find(&query.Result)
|
err := sess.Find(&query.Result)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user