Пример #1
0
func TestMigrations(t *testing.T) {
	//log.NewLogger(0, "console", `{"level": 0}`)

	testDBs := []sqlutil.TestDB{
		sqlutil.TestDB_Sqlite3,
	}

	for _, testDB := range testDBs {

		Convey("Initial "+testDB.DriverName+" migration", t, func() {
			x, err := xorm.NewEngine(testDB.DriverName, testDB.ConnStr)
			So(err, ShouldBeNil)

			sqlutil.CleanDB(x)

			mg := NewMigrator(x)
			mg.Logger.SetHandler(log15.DiscardHandler())
			AddMigrations(mg)

			err = mg.Start()
			So(err, ShouldBeNil)

			// tables, err := x.DBMetas()
			// So(err, ShouldBeNil)
			//
			// fmt.Printf("\nDB Schema after migration: table count: %v\n", len(tables))
			//
			// for _, table := range tables {
			// 	fmt.Printf("\nTable: %v \n", table.Name)
			// 	for _, column := range table.Columns() {
			// 		fmt.Printf("\t %v \n", column.String(x.Dialect()))
			// 	}
			//
			// 	if len(table.Indexes) > 0 {
			// 		fmt.Printf("\n\tIndexes:\n")
			// 		for _, index := range table.Indexes {
			// 			fmt.Printf("\t %v (%v) %v \n", index.Name, strings.Join(index.Cols, ","), indexTypes[index.Type])
			// 		}
			// 	}
			// }
		})
	}
}
Пример #2
0
func init() {
	loggersToClose = make([]DisposableHandler, 0)
	Root = log15.Root()
	Root.SetHandler(log15.DiscardHandler())
}
Пример #3
0
// NewContext creates a new Context from the specified Config. This also
// creates a Logger.
func NewContext(config *Config) (*Context, error) {
	var context = Context{
		Config: *config,
	}

	// Logging
	context.Log = log15.New()
	if context.Config.Logging.File == "/dev/null" {
		context.handler = log15.DiscardHandler()
	} else if context.Config.Logging.File == "stderr" {
		context.handler = log15.StderrHandler
	} else {
		handler, err := log15.FileHandler(
			context.Config.Logging.File,
			log15.LogfmtFormat(),
		)
		if err != nil {
			return nil, err
		}
		context.handler = handler
	}
	level, err := log15.LvlFromString(context.Config.Logging.Level)
	if err != nil {
		return nil, err
	}
	context.handler = log15.LvlFilterHandler(level, context.handler)
	context.Log.SetHandler(context.handler)

	// Tracing
	if context.Config.Tracing.Enabled {
		s, err := os.Stat(context.Config.Tracing.File)
		if os.IsNotExist(err) || s.Size() == 0 {
			context.tracingFd, err = os.Create(context.Config.Tracing.File)
			if err != nil {
				return nil, err
			}
			context.EventCollector, err = NewWriterEventCollector(
				context.tracingFd,
				false,
			)
			if err != nil {
				return nil, err
			}
		} else {
			context.tracingFd, err = os.OpenFile(
				context.Config.Tracing.File,
				os.O_WRONLY|os.O_APPEND,
				0664,
			)
			if err != nil {
				return nil, err
			}
			context.EventCollector, err = NewWriterEventCollector(
				context.tracingFd,
				true,
			)
			if err != nil {
				return nil, err
			}
		}
	} else {
		context.EventCollector = &NullEventCollector{}
	}
	hostname, err := os.Hostname()
	if err != nil {
		hostname = "main"
	}
	context.EventFactory = NewEventFactory(hostname, "main")
	context.EventFactory.Register(context.EventCollector)

	return &context, nil
}