Skip to content

Commit

Permalink
Merge pull request #867 from openziti/no_auto_migration
Browse files Browse the repository at this point in the history
Disable Auto-Migration (#866)
  • Loading branch information
michaelquigley authored Feb 12, 2025
2 parents 93dd468 + e2448c6 commit 2b1c765
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## v0.4.48

FEATURE: The controller configuration now supports a `disable_auto_migration` boolean in the `store` stanza. When set to `true`, the controller will not attempt to auto-migrate (or otherwise validate the migration state) of the underlying database. Leaving `disable_auto_migration` out, or setting it to false will retain the default behavior of auto-migrating when starting the zrok controller. The `zrok admin migrate` command will still perform a migration regardless of how this setting is configured in the controller configuration (https://github.com/openziti/zrok/issues/866)

FIX: the Python SDK erroneously assumed the enabled zrok environment contained a config.json file, and was changed to only load it if the file was present (https://github.com/openziti/zrok/pull/853/).

## v0.4.47
Expand Down
5 changes: 5 additions & 0 deletions cmd/zrok/adminMigrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ func (cmd *adminMigrate) run(_ *cobra.Command, args []string) {
if err != nil {
panic(err)
}

logrus.Info(cf.Dump(inCfg, cf.DefaultOptions()))

// override the 'disable_auto_migration' setting... the user is requesting a migration here.
inCfg.Store.DisableAutoMigration = false

if _, err := store.Open(inCfg.Store); err != nil {
panic(err)
}
Expand Down
21 changes: 14 additions & 7 deletions controller/store/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,39 +42,44 @@ func (str *Store) CreateGlobalFrontend(f *Frontend, tx *sqlx.Tx) (int, error) {
}

func (str *Store) GetFrontend(id int, tx *sqlx.Tx) (*Frontend, error) {
utrx := tx.Unsafe()
i := &Frontend{}
if err := tx.QueryRowx("select * from frontends where id = $1", id).StructScan(i); err != nil {
if err := utrx.QueryRowx("select * from frontends where id = $1", id).StructScan(i); err != nil {
return nil, errors.Wrap(err, "error selecting frontend by id")
}
return i, nil
}

func (str *Store) FindFrontendWithToken(token string, tx *sqlx.Tx) (*Frontend, error) {
utrx := tx.Unsafe()
i := &Frontend{}
if err := tx.QueryRowx("select frontends.* from frontends where token = $1 and not deleted", token).StructScan(i); err != nil {
if err := utrx.QueryRowx("select frontends.* from frontends where token = $1 and not deleted", token).StructScan(i); err != nil {
return nil, errors.Wrap(err, "error selecting frontend by name")
}
return i, nil
}

func (str *Store) FindFrontendWithZId(zId string, tx *sqlx.Tx) (*Frontend, error) {
utrx := tx.Unsafe()
i := &Frontend{}
if err := tx.QueryRowx("select frontends.* from frontends where z_id = $1 and not deleted", zId).StructScan(i); err != nil {
if err := utrx.QueryRowx("select frontends.* from frontends where z_id = $1 and not deleted", zId).StructScan(i); err != nil {
return nil, errors.Wrap(err, "error selecting frontend by ziti id")
}
return i, nil
}

func (str *Store) FindFrontendPubliclyNamed(publicName string, tx *sqlx.Tx) (*Frontend, error) {
utrx := tx.Unsafe()
i := &Frontend{}
if err := tx.QueryRowx("select frontends.* from frontends where public_name = $1 and not deleted", publicName).StructScan(i); err != nil {
if err := utrx.QueryRowx("select frontends.* from frontends where public_name = $1 and not deleted", publicName).StructScan(i); err != nil {
return nil, errors.Wrap(err, "error selecting frontend by public_name")
}
return i, nil
}

func (str *Store) FindFrontendsForEnvironment(envId int, tx *sqlx.Tx) ([]*Frontend, error) {
rows, err := tx.Queryx("select frontends.* from frontends where environment_id = $1 and not deleted", envId)
utrx := tx.Unsafe()
rows, err := utrx.Queryx("select frontends.* from frontends where environment_id = $1 and not deleted", envId)
if err != nil {
return nil, errors.Wrap(err, "error selecting frontends by environment_id")
}
Expand All @@ -90,7 +95,8 @@ func (str *Store) FindFrontendsForEnvironment(envId int, tx *sqlx.Tx) ([]*Fronte
}

func (str *Store) FindPublicFrontends(tx *sqlx.Tx) ([]*Frontend, error) {
rows, err := tx.Queryx("select frontends.* from frontends where environment_id is null and reserved = true and not deleted")
utrx := tx.Unsafe()
rows, err := utrx.Queryx("select frontends.* from frontends where environment_id is null and reserved = true and not deleted")
if err != nil {
return nil, errors.Wrap(err, "error selecting public frontends")
}
Expand All @@ -106,7 +112,8 @@ func (str *Store) FindPublicFrontends(tx *sqlx.Tx) ([]*Frontend, error) {
}

func (str *Store) FindFrontendsForPrivateShare(shrId int, tx *sqlx.Tx) ([]*Frontend, error) {
rows, err := tx.Queryx("select frontends.* from frontends where private_share_id = $1 and not deleted", shrId)
utrx := tx.Unsafe()
rows, err := utrx.Queryx("select frontends.* from frontends where private_share_id = $1 and not deleted", shrId)
if err != nil {
return nil, errors.Wrap(err, "error selecting frontends by private_share_id")
}
Expand Down
13 changes: 8 additions & 5 deletions controller/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ type Model struct {
}

type Config struct {
Path string `cf:"+secret"`
Type string
EnableLocking bool
Path string `cf:"+secret"`
Type string
EnableLocking bool
DisableAutoMigration bool
}

type Store struct {
Expand Down Expand Up @@ -57,8 +58,10 @@ func Open(cfg *Config) (*Store, error) {
dbx.MapperFunc(strcase.ToSnake)

store := &Store{cfg: cfg, db: dbx}
if err := store.migrate(cfg); err != nil {
return nil, errors.Wrapf(err, "error migrating database '%v'", cfg.Path)
if !cfg.DisableAutoMigration {
if err := store.migrate(cfg); err != nil {
return nil, errors.Wrapf(err, "error migrating database '%v'", cfg.Path)
}
}
return store, nil
}
Expand Down
1 change: 1 addition & 0 deletions etc/ctrl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ reset_password:
#store:
# path: "host=127.0.0.1 user=zrok password=zrok dbname=zrok"
# type: "postgres"
# disable_auto_migration: true
#
store:
path: zrok.db
Expand Down

0 comments on commit 2b1c765

Please # to comment.