Browse Source

account -> org table migration is starting to work, need to test mysql and postgres

Torkel Ödegaard 10 years ago
parent
commit
f3f79792ab

+ 26 - 17
pkg/services/sqlstore/migrations/migrations.go

@@ -18,25 +18,34 @@ func AddMigrations(mg *Migrator) {
 }
 
 func addMigrationLogMigrations(mg *Migrator) {
-	mg.AddMigration("create migration_log table", new(AddTableMigration).
-		Name("migration_log").WithColumns(
-		&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
-		&Column{Name: "migration_id", Type: DB_NVarchar, Length: 255},
-		&Column{Name: "sql", Type: DB_Text},
-		&Column{Name: "success", Type: DB_Bool},
-		&Column{Name: "error", Type: DB_Text},
-		&Column{Name: "timestamp", Type: DB_DateTime},
-	))
+	migrationLogV1 := Table{
+		Name: "migration_log",
+		Columns: []*Column{
+			&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
+			&Column{Name: "migration_id", Type: DB_NVarchar, Length: 255},
+			&Column{Name: "sql", Type: DB_Text},
+			&Column{Name: "success", Type: DB_Bool},
+			&Column{Name: "error", Type: DB_Text},
+			&Column{Name: "timestamp", Type: DB_DateTime},
+		},
+	}
+
+	mg.AddMigration("create migration_log table", NewAddTableMigration(migrationLogV1))
 }
 
 func addStarMigrations(mg *Migrator) {
-	mg.AddMigration("create star table", new(AddTableMigration).
-		Name("star").WithColumns(
-		&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
-		&Column{Name: "user_id", Type: DB_BigInt, Nullable: false},
-		&Column{Name: "dashboard_id", Type: DB_BigInt, Nullable: false},
-	))
+	starV1 := Table{
+		Name: "star",
+		Columns: []*Column{
+			&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
+			&Column{Name: "user_id", Type: DB_BigInt, Nullable: false},
+			&Column{Name: "dashboard_id", Type: DB_BigInt, Nullable: false},
+		},
+		Indices: []*Index{
+			&Index{Cols: []string{"user_id", "dashboard_id"}, Type: UniqueIndex},
+		},
+	}
 
-	mg.AddMigration("add unique index star.user_id_dashboard_id", new(AddIndexMigration).
-		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]))
 }

+ 1 - 1
pkg/services/sqlstore/migrations/migrations_test.go

@@ -32,7 +32,7 @@ func TestMigrations(t *testing.T) {
 
 			mg := NewMigrator(x)
 			mg.LogLevel = log.DEBUG
-			addMigrations(mg)
+			AddMigrations(mg)
 
 			err = mg.Start()
 			So(err, ShouldBeNil)

+ 2 - 2
pkg/services/sqlstore/migrations/org_mig.go

@@ -55,7 +55,7 @@ func addOrgMigrations(mg *Migrator) {
 		"name":    "name",
 		"created": "created",
 		"updated": "updated",
-	}))
+	}).IfTableExists("account"))
 
 	mg.AddMigration("copy data account_user to org_user", NewCopyTableDataMigration("org_user", "account_user", map[string]string{
 		"id":      "id",
@@ -64,7 +64,7 @@ func addOrgMigrations(mg *Migrator) {
 		"role":    "role",
 		"created": "created",
 		"updated": "updated",
-	}))
+	}).IfTableExists("account_user"))
 
 	mg.AddMigration("Drop old table account", NewDropTableMigration("account"))
 	mg.AddMigration("Drop old table account_user", NewDropTableMigration("account_user"))

+ 4 - 13
pkg/services/sqlstore/migrations/user_mig.go

@@ -26,20 +26,11 @@ func addUserMigrations(mg *Migrator) {
 		},
 	}
 
+	// create table
 	mg.AddMigration("create user table", NewAddTableMigration(userV1))
-
-	mg.AddMigration("Add email_verified flag", new(AddColumnMigration).
-		Table("user").Column(&Column{Name: "email_verified", Type: DB_Bool, Nullable: true}))
-
-	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())
+	// add indices
+	mg.AddMigration("add unique index user.login", NewAddIndexMigration(userV1, userV1.Indices[0]))
+	mg.AddMigration("add unique index user.email", NewAddIndexMigration(userV1, userV1.Indices[1]))
 
 	// ---------------------
 	// account -> org changes

+ 5 - 55
pkg/services/sqlstore/migrator/migrations.go

@@ -85,17 +85,6 @@ func (m *AddIndexMigration) Table(tableName string) *AddIndexMigration {
 	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 {
 	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}
 }
 
-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 {
 	if m.index.Name == "" {
 		m.index.Name = fmt.Sprintf("%s", strings.Join(m.index.Cols, "_"))
@@ -139,39 +112,16 @@ type AddTableMigration struct {
 }
 
 func NewAddTableMigration(table Table) *AddTableMigration {
-	return &AddTableMigration{table: table}
-}
-
-func (m *AddTableMigration) Sql(d Dialect) string {
-	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)
+	for _, col := range table.Columns {
 		if col.IsPrimaryKey {
-			m.table.PrimaryKeys = append(m.table.PrimaryKeys, col.Name)
+			table.PrimaryKeys = append(table.PrimaryKeys, col.Name)
 		}
 	}
-	return m
+	return &AddTableMigration{table: table}
 }
 
-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
+func (m *AddTableMigration) Sql(d Dialect) string {
+	return d.CreateTableSql(&m.table)
 }
 
 type DropTableMigration struct {

+ 1 - 1
pkg/services/sqlstore/org_users.go

@@ -35,7 +35,7 @@ func AddOrgUser(cmd *m.AddOrgUserCommand) error {
 func GetOrgUsers(query *m.GetOrgUsersQuery) error {
 	query.Result = make([]*m.OrgUserDTO, 0)
 	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.Cols("org_user.org_id", "org_user.user_id", "user.email", "user.login", "org_user.role")
 	sess.Asc("user.email", "user.login")

+ 1 - 1
pkg/services/sqlstore/user.go

@@ -234,7 +234,7 @@ func GetUserOrgList(query *m.GetUserOrgListQuery) error {
 	sess := x.Table("org_user")
 	sess.Join("INNER", "org", "org_user.org_id=org.id")
 	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)
 	return err
 }