Example #1
0
// CreateTable get a CreateTable task
// IMPORTANT: only support hash type yet!!!
func (d *Manage) CreateTable(user, db, sql string, table *meta.Table) (id string, rows uint64, err error) {
	// build create table task
	// get all subdb
	var backends []*meta.Backend
	if backends, err = d.info.GetDBs(user, db); err != nil {
		glog.Warningf("GetDBs get an error:%v", err)
	}
	subs := make([]*SubPlan, 0, len(backends))
	for _, backend := range backends {
		subs = append(subs, &SubPlan{
			SQL:         sql,
			IsDB:        false,
			SubDatabase: backend,
		})
	}
	var t = &Task{
		Plan: &Plan{
			DBName:      db,
			UserName:    user,
			Table:       table,
			SubPlans:    subs,
			LockKey:     path.Join(user, db, table.Name),
			ID:          uuid.Get(),
			LockVersion: 0,
		},
		Type: CreateTable,
		c:    make(chan *Result, 1),
	}
	glog.Infof("DDL: create table task:%v", t)
	rows, err = d.handleTask(t)
	return t.Plan.ID, rows, err
}
Example #2
0
// CreateDatabase will create a new database for the uname's user
func (d *Manage) CreateDatabase(uname, database string, num int) (string, uint64, error) {
	// build the ddl plan
	nodes, err := d.info.GetMysqlNodes()
	if err != nil {
		return "", 0, err
	}
	// get users backend
	if num == 0 {
		num = len(nodes)
	}
	glog.Infof("Create Database sub db num is: %v", num)
	subs := make([]*SubPlan, 0, num)
	for i := 0; i < num; i++ {
		dbName := fmt.Sprintf("%s_%s_%s", uname, database, strconv.Itoa(i))
		createDB := fmt.Sprintf("CREATE DATABASE `%s` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci", dbName)
		index := i % len(nodes)
		node := nodes[index]
		subs = append(subs, &SubPlan{
			SQL:  createDB,
			IsDB: true,
			SubDatabase: &meta.Backend{
				Seq:        i,
				UserName:   node.UserName,
				Password:   node.Password,
				Host:       node.Host,
				Name:       dbName,
				ParentNode: node,
			},
		})
	}
	var t = &Task{
		Plan: &Plan{
			DBName:      database,
			UserName:    uname,
			SubPlans:    subs,
			LockKey:     path.Join(uname, database),
			ID:          uuid.Get(),
			LockVersion: 0,
		},
		Type: CreateDB,
		c:    make(chan *Result, 1),
	}
	glog.Infof("DDL: create database task:%+v", t)
	rows, err := d.handleTask(t)
	return t.Plan.ID, rows, err
}