A More Secure Internet Connection for Your Home https://fen.gg
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

72 lines
2.1 KiB

  1. package model
  2. //
  3. // Fengg Security Gateway Server Application
  4. // Copyright (C) 2020 Lukas Matt <support@fen.gg>
  5. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. //
  18. import (
  19. "fmt"
  20. "time"
  21. "tea.fen.gg/fengg/server/types"
  22. "github.com/jmoiron/sqlx"
  23. _ "github.com/lib/pq"
  24. )
  25. const (
  26. PortalMigrationQueryTmpl = `SELECT * FROM portal_migrations %s;`
  27. PortalMigrationNamedUpdateTmpl = `UPDATE portal_migrations SET version=:version WHERE %s;`
  28. PortalMigrationNamedInsertTmpl = `
  29. INSERT INTO portal_migrations (type, version) VALUES (:type, :version);`
  30. )
  31. type PortalMigration struct {
  32. Type types.PortalEventType `db:"type" json:"type"`
  33. Version time.Time `db:"version" json:"version"`
  34. }
  35. type PortalMigrations []PortalMigration
  36. func NewPortalMigration() *PortalMigration {
  37. return &PortalMigration{}
  38. }
  39. func (migration *PortalMigration) FindByType(id types.PortalEventType) error {
  40. db, err := sqlx.Connect(dbDriver, dbConnect)
  41. if err != nil {
  42. return err
  43. }
  44. defer db.Close()
  45. return db.Get(migration, fmt.Sprintf(PortalMigrationQueryTmpl, "WHERE type=$1"), id)
  46. }
  47. func (migration *PortalMigration) CreateOrUpdate() error {
  48. db, err := sqlx.Connect(dbDriver, dbConnect)
  49. if err != nil {
  50. return err
  51. }
  52. defer db.Close()
  53. err = db.Get(&PortalMigration{},
  54. `SELECT type FROM portal_migrations WHERE type=$1;`, migration.Type)
  55. if err == nil {
  56. _, err = db.NamedExec(
  57. fmt.Sprintf(PortalMigrationNamedUpdateTmpl, "type=:type"), migration)
  58. } else {
  59. _, err = db.NamedExec(PortalMigrationNamedInsertTmpl, migration)
  60. }
  61. return err
  62. }