diff options
author | hanemile <mail@emile.space> | 2020-07-19 01:56:09 +0200 |
---|---|---|
committer | hanemile <mail@emile.space> | 2020-07-19 01:56:09 +0200 |
commit | fc63adf55849f748a37cef4c425a19a1c0f0e76e (patch) | |
tree | 034c6ab3abb08fe15c702298f294226f1b0071c2 /src/db/schema.go |
initial commit
Diffstat (limited to 'src/db/schema.go')
-rw-r--r-- | src/db/schema.go | 51 |
1 files changed, 51 insertions, 0 deletions
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 +} |