From fc63adf55849f748a37cef4c425a19a1c0f0e76e Mon Sep 17 00:00:00 2001 From: hanemile Date: Sun, 19 Jul 2020 01:56:09 +0200 Subject: initial commit --- src/db/schema.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/db/schema.go (limited to 'src/db/schema.go') diff --git a/src/db/schema.go b/src/db/schema.go new file mode 100644 index 0000000..9d7e31e --- /dev/null +++ b/src/db/schema.go @@ -0,0 +1,51 @@ +package db + +import ( + "git.darknebu.la/chaosdorf/freitagsfoo/src/structs" + pg "github.com/go-pg/pg/v9" + orm "github.com/go-pg/pg/v9/orm" + "github.com/sirupsen/logrus" +) + +// CreateSchema creates the defined schemas in the database +func CreateSchema(db *pg.DB) error { + // define the models + models := []interface{}{ + (*structs.Talk)(nil), + } + + // create a table for each model defined above + for i, model := range models { + + err := db.CreateTable(model, &orm.CreateTableOptions{ + IfNotExists: true, + }) + if err != nil { + return err + } + logrus.Tracef("\t Created the %T table", models[i]) + } + + return nil +} + +// DeleteAllTables deletes all tables in the given database +func DeleteAllTables(db *pg.DB) error { + // define the models + models := []interface{}{ + (*structs.Talk)(nil), + } + + // delete all models + for i, model := range models { + err := db.DropTable(model, &orm.DropTableOptions{ + IfExists: true, + }) + if err != nil { + return err + } + logrus.Tracef("\t Deleted the %T table", models[i]) + } + + return nil +} -- cgit 1.4.1