Example #1
0
// Bootstrap creates "migrations" table
// to keep track of already run database migrations
func Bootstrap(db *gorm.DB) error {
	migrationName := "bootstrap_migrations"

	migration := new(Migration)
	// Using Error instead of RecordNotFound because we want to check
	// if the migrations table exists. This is different from later migrations
	// where we query the already create migrations table.
	exists := nil == db.Where("name = ?", migrationName).First(migration).Error

	if exists {
		logger.Infof("Skipping %s migration", migrationName)
		return nil
	}

	logger.Infof("Running %s migration", migrationName)

	// Create migrations table
	if err := db.CreateTable(new(Migration)).Error; err != nil {
		return fmt.Errorf("Error creating migrations table: %s", db.Error)
	}

	// Save a record to migrations table,
	// so we don't rerun this migration again
	migration.Name = migrationName
	if err := db.Create(migration).Error; err != nil {
		return fmt.Errorf("Error saving record to migrations table: %s", err)
	}

	return nil
}
Example #2
0
func initDb() (l *Logger) {
	exists := false
	var err error
	var db *gorm.DB

	if _, err := os.Stat(dbfile); err == nil {
		log.Println("Database file already exists.")
		exists = true
	}

	db, err = gorm.Open("sqlite3", dbfile)
	if err != nil {
		log.Printf("Unable to open database file: %s\n", err)
		return nil
	}

	if !exists {
		db.CreateTable(&LogMessage{})
	}

	l = &Logger{
		db:     db,
		dbPath: dbfile,
	}

	return l
}
Example #3
0
func initializeDatabase(db *gorm.DB) {
	db.SingularTable(true) // 自動生成されるテーブル名を単数形にする.

	db.CreateTable(&Property{})
	db.CreateTable(&Room{})
	//    db.AutoMigrate(&Family{}, &FamilyCredential{}) // 使いこなせないので当面自動マイグレーションはoff.

	db.LogMode(true)
	defer beginTx(db)()
}
Example #4
0
func deleteAllFromDB(db gorm.DB, selectedType string) error {
	switch selectedType {
	case "jobs":
		db.DropTableIfExists(&application.Job{})
		db.CreateTable(&application.Job{})
	case "freelancers":
		db.DropTableIfExists(&application.Freelancer{})
		db.CreateTable(&application.Freelancer{})
	}

	return nil
}
Example #5
0
func migrateDb(db gorm.DB) {
	// Create table
	db.CreateTable(&model.User{})
	db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&model.User{})

	// Automating Migration
	db.AutoMigrate(&model.User{})
	db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&model.User{})
	// Feel free to change your struct, AutoMigrate will keep your database up-to-date.
	// AutoMigrate will ONLY add *new columns* and *new indexes*,
	// WON'T update current column's type or delete unused columns, to protect your data.
	// If the table is not existing, AutoMigrate will create the table automatically.
}
Example #6
0
func seedDb(db *gorm.DB) {
	db.DropTable(&User{})
	db.CreateTable(&User{})
	user1 := User{Name: "user1", Password: "******"}
	user2 := User{Name: "user2", Password: "******"}
	admin := User{Name: "admin", Password: "******", Admin: true}
	db.Create(&user1)
	db.Create(&user2)
	db.Create(&admin)

	db.DropTable(&PrivateWidget{})
	db.CreateTable(&PrivateWidget{})
	db.Create(&PrivateWidget{Name: "User 1's Widget", UserID: user1.ID})
	db.Create(&PrivateWidget{Name: "User 2's Widget", UserID: user2.ID})
}
Example #7
0
func createTables(db *gorm.DB, values ...interface{}) error {
	for _, value := range values {
		if !db.HasTable(value) {
			err := db.CreateTable(value).Error
			if err != nil {
				errClose := db.Close()
				if errClose != nil {
					fmt.Printf("%s", errClose)
				}
				return err
			}
		}
	}
	return nil
}
Example #8
0
// InitDB initializes database tables.
func InitDB(dbConn *gorm.DB) {
	dbConn = assertOpenConnection(dbConn)

	dbConn.CreateTable(&schemas.WhiteTorrent{})
	dbConn.CreateTable(&schemas.Torrent{})
	dbConn.CreateTable(&schemas.TrackerStats{})
	dbConn.CreateTable(&schemas.PeerStats{})
}
Example #9
0
func initDb(db gorm.DB) {
	defer fmt.Printf("Init DB DONE! \n")

	db.DB().Ping()
	db.DB().SetMaxIdleConns(10)
	db.DB().SetMaxOpenConns(100)

	db.CreateTable(&Erp{})
	db.CreateTable(&Entry{})

	db.CreateTable(&SyncField{})
	db.Model(&Entry{}).Related(&SyncField{}, "EntryId")

	db.CreateTable(&Decorator{})
	db.Model(&SyncField{}).Related(&Decorator{}, "DecoratorId")

	db.CreateTable(&SyncEvent{})

}
func initializeGridPieceTable(db gorm.DB) {
	db.CreateTable(&GridPiece{})
	db.Set("gorm:taple_operations", "ENGINE=InnoDB").CreateTable(&GridPiece{})

	pieces := []GridPiece{
		GridPiece{Type: BlackRook, Y: 1, X: 1},
		GridPiece{Type: BlackKnight, Y: 1, X: 2},
		GridPiece{Type: BlackBishop, Y: 1, X: 3},
		GridPiece{Type: BlackQueen, Y: 1, X: 4},
		GridPiece{Type: BlackKing, Y: 1, X: 5},
		GridPiece{Type: BlackBishop, Y: 1, X: 6},
		GridPiece{Type: BlackKnight, Y: 1, X: 7},
		GridPiece{Type: BlackRook, Y: 1, X: 8},
		GridPiece{Type: BlackPawn, Y: 2, X: 1},
		GridPiece{Type: BlackPawn, Y: 2, X: 2},
		GridPiece{Type: BlackPawn, Y: 2, X: 3},
		GridPiece{Type: BlackPawn, Y: 2, X: 4},
		GridPiece{Type: BlackPawn, Y: 2, X: 5},
		GridPiece{Type: BlackPawn, Y: 2, X: 6},
		GridPiece{Type: BlackPawn, Y: 2, X: 7},
		GridPiece{Type: BlackPawn, Y: 2, X: 8},
		GridPiece{Type: WhiteRook, Y: 8, X: 1},
		GridPiece{Type: WhiteKnight, Y: 8, X: 2},
		GridPiece{Type: WhiteBishop, Y: 8, X: 3},
		GridPiece{Type: WhiteQueen, Y: 8, X: 4},
		GridPiece{Type: WhiteKing, Y: 8, X: 5},
		GridPiece{Type: WhiteBishop, Y: 8, X: 6},
		GridPiece{Type: WhiteKnight, Y: 8, X: 7},
		GridPiece{Type: WhiteRook, Y: 8, X: 8},
		GridPiece{Type: WhitePawn, Y: 7, X: 1},
		GridPiece{Type: WhitePawn, Y: 7, X: 2},
		GridPiece{Type: WhitePawn, Y: 7, X: 3},
		GridPiece{Type: WhitePawn, Y: 7, X: 4},
		GridPiece{Type: WhitePawn, Y: 7, X: 5},
		GridPiece{Type: WhitePawn, Y: 7, X: 6},
		GridPiece{Type: WhitePawn, Y: 7, X: 7},
		GridPiece{Type: WhitePawn, Y: 7, X: 8},
	}
	for _, piece := range pieces {
		db.NewRecord(piece)
		db.Create(&piece)
	}
}
func initializePieceTable(db gorm.DB) {
	db.CreateTable(&Piece{})
	db.Set("gorm:taple_operations", "ENGINE=InnoDB").CreateTable(&Piece{})

	pieces := []Piece{
		Piece{Type: BlackRook, Top: 82, Left: 79},
		Piece{Type: BlackKnight, Top: 82, Left: 154},
		Piece{Type: BlackBishop, Top: 82, Left: 234},
		Piece{Type: BlackQueen, Top: 82, Left: 308},
		Piece{Type: BlackKing, Top: 82, Left: 383},
		Piece{Type: BlackBishop, Top: 82, Left: 458},
		Piece{Type: BlackKnight, Top: 82, Left: 533},
		Piece{Type: BlackRook, Top: 82, Left: 608},
		Piece{Type: BlackPawn, Top: 2, Left: 79},
		Piece{Type: BlackPawn, Top: 160, Left: 154},
		Piece{Type: BlackPawn, Top: 160, Left: 234},
		Piece{Type: BlackPawn, Top: 160, Left: 308},
		Piece{Type: BlackPawn, Top: 160, Left: 383},
		Piece{Type: BlackPawn, Top: 160, Left: 458},
		Piece{Type: BlackPawn, Top: 160, Left: 533},
		Piece{Type: BlackPawn, Top: 160, Left: 608},
		Piece{Type: WhiteRook, Top: 611, Left: 79},
		Piece{Type: WhiteKnight, Top: 611, Left: 154},
		Piece{Type: WhiteBishop, Top: 611, Left: 234},
		Piece{Type: WhiteQueen, Top: 611, Left: 308},
		Piece{Type: WhiteKing, Top: 611, Left: 383},
		Piece{Type: WhiteBishop, Top: 611, Left: 458},
		Piece{Type: WhiteKnight, Top: 611, Left: 533},
		Piece{Type: WhiteRook, Top: 611, Left: 608},
		Piece{Type: WhitePawn, Top: 533, Left: 79},
		Piece{Type: WhitePawn, Top: 533, Left: 154},
		Piece{Type: WhitePawn, Top: 533, Left: 234},
		Piece{Type: WhitePawn, Top: 533, Left: 308},
		Piece{Type: WhitePawn, Top: 533, Left: 383},
		Piece{Type: WhitePawn, Top: 533, Left: 458},
		Piece{Type: WhitePawn, Top: 533, Left: 533},
		Piece{Type: WhitePawn, Top: 533, Left: 608},
	}
	for _, piece := range pieces {
		db.NewRecord(piece)
		db.Create(&piece)
	}
}
Example #12
0
func migrate0001(db *gorm.DB, name string) error {
	// Create tables
	if err := db.CreateTable(new(Client)).Error; err != nil {
		return fmt.Errorf("Error creating oauth_clients table: %s", err)
	}
	if err := db.CreateTable(new(Scope)).Error; err != nil {
		return fmt.Errorf("Error creating oauth_scopes table: %s", err)
	}
	if err := db.CreateTable(new(Role)).Error; err != nil {
		return fmt.Errorf("Error creating oauth_roles table: %s", err)
	}
	if err := db.CreateTable(new(User)).Error; err != nil {
		return fmt.Errorf("Error creating oauth_users table: %s", err)
	}
	if err := db.CreateTable(new(RefreshToken)).Error; err != nil {
		return fmt.Errorf("Error creating oauth_refresh_tokens table: %s", err)
	}
	if err := db.CreateTable(new(AccessToken)).Error; err != nil {
		return fmt.Errorf("Error creating oauth_access_tokens table: %s", err)
	}
	if err := db.CreateTable(new(AuthorizationCode)).Error; err != nil {
		return fmt.Errorf("Error creating oauth_authorization_codes table: %s", err)
	}
	err := db.Model(new(User)).AddForeignKey(
		"role_id", "oauth_roles(id)",
		"RESTRICT", "RESTRICT",
	).Error
	if err != nil {
		return fmt.Errorf("Error creating foreign key on "+
			"oauth_users.role_id for oauth_roles(id): %s", err)
	}
	err = db.Model(new(RefreshToken)).AddForeignKey(
		"client_id", "oauth_clients(id)",
		"RESTRICT", "RESTRICT",
	).Error
	if err != nil {
		return fmt.Errorf("Error creating foreign key on "+
			"oauth_refresh_tokens.client_id for oauth_clients(id): %s", err)
	}
	err = db.Model(new(RefreshToken)).AddForeignKey(
		"user_id", "oauth_users(id)",
		"RESTRICT", "RESTRICT",
	).Error
	if err != nil {
		return fmt.Errorf("Error creating foreign key on "+
			"oauth_refresh_tokens.user_id for oauth_users(id): %s", err)
	}
	err = db.Model(new(AccessToken)).AddForeignKey(
		"client_id", "oauth_clients(id)",
		"RESTRICT", "RESTRICT",
	).Error
	if err != nil {
		return fmt.Errorf("Error creating foreign key on "+
			"oauth_access_tokens.client_id for oauth_clients(id): %s", err)
	}
	err = db.Model(new(AccessToken)).AddForeignKey(
		"user_id", "oauth_users(id)",
		"RESTRICT", "RESTRICT",
	).Error
	if err != nil {
		return fmt.Errorf("Error creating foreign key on "+
			"oauth_access_tokens.user_id for oauth_users(id): %s", err)
	}
	err = db.Model(new(AuthorizationCode)).AddForeignKey(
		"client_id", "oauth_clients(id)",
		"RESTRICT", "RESTRICT",
	).Error
	if err != nil {
		return fmt.Errorf("Error creating foreign key on "+
			"oauth_authorization_codes.client_id for oauth_clients(id): %s", err)
	}
	err = db.Model(new(AuthorizationCode)).AddForeignKey(
		"user_id", "oauth_users(id)",
		"RESTRICT", "RESTRICT",
	).Error
	if err != nil {
		return fmt.Errorf("Error creating foreign key on "+
			"oauth_authorization_codes.user_id for oauth_users(id): %s", err)
	}

	return nil
}
// Migrate0001 creates OAuth 2.0 schema
func migrate0001(db *gorm.DB) error {
	migrationName := "0001_initial"

	migration := new(migrations.Migration)
	found := !db.Where(migrations.Migration{
		Name: migrationName,
	}).First(migration).RecordNotFound()

	if found {
		log.Printf("Skipping %s migration", migrationName)
		return nil
	}

	log.Printf("Running %s migration", migrationName)

	var err error

	// Create clients table
	if err := db.CreateTable(new(Client)).Error; err != nil {
		return fmt.Errorf("Error creating clients table: %s", db.Error)
	}

	// Create scopes table
	if err := db.CreateTable(new(Scope)).Error; err != nil {
		return fmt.Errorf("Error creating scopes table: %s", db.Error)
	}

	// Create users table
	if err := db.CreateTable(new(User)).Error; err != nil {
		return fmt.Errorf("Error creating users table: %s", db.Error)
	}

	// Create refresh_tokens table
	if err := db.CreateTable(new(RefreshToken)).Error; err != nil {
		return fmt.Errorf("Error creating refresh_tokens table: %s", db.Error)
	}

	// Create access_tokens table
	if err := db.CreateTable(new(AccessToken)).Error; err != nil {
		return fmt.Errorf("Error creating access_tokens table: %s", db.Error)
	}

	// Create auth_codes table
	if err := db.CreateTable(new(AuthorizationCode)).Error; err != nil {
		return fmt.Errorf("Error creating authorization_codes table: %s", db.Error)
	}

	// Add foreign key on refresh_tokens.client_id
	err = db.Model(new(RefreshToken)).AddForeignKey(
		"client_id",
		"clients(id)",
		"RESTRICT",
		"RESTRICT",
	).Error
	if err != nil {
		return fmt.Errorf("Error creating foreign key on "+
			"refresh_tokens.client_id for clients(id): %s", db.Error)
	}

	// Add foreign key on refresh_tokens.user_id
	err = db.Model(new(RefreshToken)).AddForeignKey(
		"user_id",
		"users(id)",
		"RESTRICT",
		"RESTRICT",
	).Error
	if err != nil {
		return fmt.Errorf("Error creating foreign key on "+
			"refresh_tokens.user_id for users(id): %s", db.Error)
	}

	// Add foreign key on access_tokens.client_id
	err = db.Model(new(AccessToken)).AddForeignKey(
		"client_id",
		"clients(id)",
		"RESTRICT",
		"RESTRICT",
	).Error
	if err != nil {
		return fmt.Errorf("Error creating foreign key on "+
			"access_tokens.client_id for clients(id): %s", db.Error)
	}

	// Add foreign key on access_tokens.user_id
	err = db.Model(new(AccessToken)).AddForeignKey(
		"user_id",
		"users(id)",
		"RESTRICT",
		"RESTRICT",
	).Error
	if err != nil {
		return fmt.Errorf("Error creating foreign key on "+
			"access_tokens.user_id for users(id): %s", db.Error)
	}

	// Add foreign key on authorization_codes.client_id
	err = db.Model(new(AuthorizationCode)).AddForeignKey(
		"client_id",
		"clients(id)",
		"RESTRICT",
		"RESTRICT",
	).Error
	if err != nil {
		return fmt.Errorf("Error creating foreign key on "+
			"authorization_codes.client_id for clients(id): %s", db.Error)
	}

	// Add foreign key on authorization_codes.user_id
	err = db.Model(new(AuthorizationCode)).AddForeignKey(
		"user_id",
		"users(id)",
		"RESTRICT",
		"RESTRICT",
	).Error
	if err != nil {
		return fmt.Errorf("Error creating foreign key on "+
			"authorization_codes.user_id for users(id): %s", db.Error)
	}

	// Save a record to migrations table,
	// so we don't rerun this migration again
	migration.Name = migrationName
	if err := db.Create(migration).Error; err != nil {
		return fmt.Errorf("Error saving record to migrations table: %s", err)
	}

	return nil
}
func InitOsInstallTables(db *gorm.DB, conf *config.Config) error {
	//db.DropTableIfExists(osInstallTables...)
	db.CreateTable(osInstallTables...)
	return nil
}
Example #15
0
func migrate0001(db *gorm.DB, name string) error {
	// Create tables
	if err := db.CreateTable(new(Account)).Error; err != nil {
		return fmt.Errorf("Error creating account_accounts table: %s", err)
	}
	if err := db.CreateTable(new(User)).Error; err != nil {
		return fmt.Errorf("Error creating account_users table: %s", err)
	}
	if err := db.CreateTable(new(Confirmation)).Error; err != nil {
		return fmt.Errorf("Error creating account_confirmations table: %s", err)
	}
	if err := db.CreateTable(new(Invitation)).Error; err != nil {
		return fmt.Errorf("Error creating account_invitations table: %s", err)
	}
	if err := db.CreateTable(new(PasswordReset)).Error; err != nil {
		return fmt.Errorf("Error creating account_password_resets table: %s", err)
	}

	// Add foreign keys
	err := db.Model(new(Account)).AddForeignKey(
		"oauth_client_id", "oauth_clients(id)",
		"RESTRICT", "RESTRICT",
	).Error
	if err != nil {
		return fmt.Errorf("Error creating foreign key on "+
			"account_accounts.oauth_client_id for oauth_clients(id): %s", err)
	}
	err = db.Model(new(User)).AddForeignKey(
		"account_id", "account_accounts(id)",
		"RESTRICT", "RESTRICT",
	).Error
	if err != nil {
		return fmt.Errorf("Error creating foreign key on "+
			"account_users.account_id for account_accounts(id): %s", err)
	}
	err = db.Model(new(User)).AddForeignKey(
		"oauth_user_id", "oauth_users(id)",
		"RESTRICT", "RESTRICT",
	).Error
	if err != nil {
		return fmt.Errorf("Error creating foreign key on "+
			"account_users.oauth_user_id for oauth_users(id): %s", err)
	}
	err = db.Model(new(Confirmation)).AddForeignKey(
		"user_id", "account_users(id)",
		"RESTRICT", "RESTRICT",
	).Error
	if err != nil {
		return fmt.Errorf("Error creating foreign key on "+
			"account_confirmations.user_id for account_users(id): %s", err)
	}
	err = db.Model(new(Invitation)).AddForeignKey(
		"invited_user_id", "account_users(id)",
		"RESTRICT", "RESTRICT",
	).Error
	if err != nil {
		return fmt.Errorf("Error creating foreign key on "+
			"account_invitations.invited_user_id for account_users(id): %s", err)
	}
	err = db.Model(new(Invitation)).AddForeignKey(
		"invited_by_user_id", "account_users(id)",
		"RESTRICT", "RESTRICT",
	).Error
	if err != nil {
		return fmt.Errorf("Error creating foreign key on "+
			"account_invitations.invited_by_user_id for account_users(id): %s", err)
	}
	err = db.Model(new(PasswordReset)).AddForeignKey(
		"user_id", "account_users(id)",
		"RESTRICT", "RESTRICT",
	).Error
	if err != nil {
		return fmt.Errorf("Error creating foreign key on "+
			"account_password_resets.user_id for account_users(id): %s", err)
	}

	return nil
}
Example #16
0
// InitDB create tables if needed and initialize them
// TODO: SKIP in CLI
// TODO:  check regularly structure & indexes
func InitDB(DB gorm.DB) error {
	var err error
	//users table
	if !DB.HasTable(&User{}) {
		if err = DB.CreateTable(&User{}).Error; err != nil {
			return errors.New("Unable to create table user - " + err.Error())
		}
	}

	// Alias
	if !DB.HasTable(&Alias{}) {
		if err = DB.CreateTable(&Alias{}).Error; err != nil {
			return errors.New("Unable to create table Alias - " + err.Error())
		}
	}

	//rcpthosts table
	if !DB.HasTable(&RcptHost{}) {
		if err = DB.CreateTable(&RcptHost{}).Error; err != nil {
			return errors.New("Unable to create RcptHost - " + err.Error())
		}
		// Index
		if err = DB.Model(&RcptHost{}).AddIndex("idx_rcpthots_hostname", "hostname").Error; err != nil {
			return errors.New("Unable to add index idx_rcpthots_domain on table RcptHost - " + err.Error())
		}
	}

	// mailbox
	if !DB.HasTable(&Mailbox{}) {
		if err = DB.CreateTable(&Mailbox{}).Error; err != nil {
			return errors.New("Unable to create Mailbox - " + err.Error())
		}
		// Index
	}

	//relay_ip_oks table
	if !DB.HasTable(&RelayIpOk{}) {
		if err = DB.CreateTable(&RelayIpOk{}).Error; err != nil {
			return errors.New("Unable to create relay_ok_ips - " + err.Error())
		}
		// Index
		if err = DB.Model(&RelayIpOk{}).AddIndex("idx_relay_ok_ips_ip", "ip").Error; err != nil {
			return errors.New("Unable to add index idx_rcpthots_domain on table relay_ok_ips - " + err.Error())
		}
	}

	//queued_messages table
	if !DB.HasTable(&QMessage{}) {
		if err = DB.CreateTable(&QMessage{}).Error; err != nil {
			return errors.New("Unable to create table queued_messages - " + err.Error())
		}
	}
	// deliverd.route
	if !DB.HasTable(&Route{}) {
		if err = DB.CreateTable(&Route{}).Error; err != nil {
			return errors.New("Unable to create table route - " + err.Error())
		}
		// Index
		if err = DB.Model(&Route{}).AddIndex("idx_route_host", "host").Error; err != nil {
			return errors.New("Unable to add index idx_route_host on table route - " + err.Error())
		}
	}

	if !DB.HasTable(&DkimConfig{}) {
		if err = DB.CreateTable(&DkimConfig{}).Error; err != nil {
			return errors.New("Unable to create table dkim_config - " + err.Error())
		}
		// Index
		if err = DB.Model(&DkimConfig{}).AddIndex("idx_domain", "domain").Error; err != nil {
			return errors.New("Unable to add index idx_domain on table dkim_config - " + err.Error())
		}
	}

	return nil
}