Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1521 delete tables on upgrade #1532

Merged
merged 3 commits into from
Feb 23, 2020

Conversation

absoludity
Copy link
Contributor

@absoludity absoludity commented Feb 21, 2020

Ref: #1521

Following on from #1526, this PR adds a pre-upgrade hook which invalidates the database cache of charts. When postgres is being used, this drops and recreates the tables. For mongodb, it's currently a no-op. I've not added functionality to check for a schema change at the moment, so this will rebuild the cache on every upgrade.

IRL test:
Initial deploy dev environment and verify schema is the original (ie. without namespace field in repos table):

$ k -n kubeapps exec kubeapps-postgresql-0 -- sh -c 'PGPASSWORD=$POSTGRES_PASSWORD psql -d assets -U $POSTGRES_USER -c "\d repos;"'
                                    Table "public.repos"
   Column    |       Type        | Collation | Nullable |              Default
-------------+-------------------+-----------+----------+-----------------------------------
 id          | integer           |           | not null | nextval('repos_id_seq'::regclass)
 name        | character varying |           |          |
 checksum    | character varying |           |          |
 last_update | character varying |           |          |
Indexes:
    "repos_pkey" PRIMARY KEY, btree (id)
    "repos_name_key" UNIQUE CONSTRAINT, btree (name)

Upgrade with the new sync image (which has the new schema):

helm -n kubeapps upgrade --reuse-values kubeapps ./chart/kubeapps/ --set apprepository.syncImage.tag=2 --set apprepository.image.tag=1

See preupgrade job deployed successfully (before I added the hook-delete-policy):

$ k -n kubeapps logs kubeapps-internal-apprepository-jobs-preupgrade-jt9zz
time="2020-02-21T00:58:32Z" level=info msg="Successfully invalidated cache"

Re-check the table and now see it's updated.

  $ k -n kubeapps exec kubeapps-postgresql-0 -- sh -c 'PGPASSWORD=$POSTGRES_PASSWORD psql -d assets -U $POSTGRES_USER -c "\d repos;"'
                                    Table "public.repos"
   Column    |       Type        | Collation | Nullable |              Default
-------------+-------------------+-----------+----------+-----------------------------------
 id          | integer           |           | not null | nextval('repos_id_seq'::regclass)
 namespace   | character varying |           | not null |
 name        | character varying |           | not null |
 checksum    | character varying |           |          |
 last_update | character varying |           |          |
Indexes:
    "repos_pkey" PRIMARY KEY, btree (id)
    "repos_namespace_name_key" UNIQUE CONSTRAINT, btree (namespace, name)
Referenced by:
    TABLE "charts" CONSTRAINT "charts_repository_id_fkey" FOREIGN KEY (repository_id) REFERENCES repos(id)

Verified cache is being repopulated as app repo starts.

@absoludity absoludity changed the base branch from master to 1521-sync-records-namespace February 21, 2020 01:12
@@ -98,10 +98,10 @@ Create name for the apprepository-controller based on the fullname
{{- end -}}

{{/*
Create name for the apprepository bootstrap job
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming this is an old job which no longer exists, as I couldn't see the name being used anywhere.

Copy link
Contributor

@andresmgot andresmgot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

// InvalidateCache for postgresql deletes and re-writes the schema
func (m *postgresAssetManager) InvalidateCache() error {
tables := strings.Join([]string{dbutils.RepositoryTable, dbutils.ChartTable, dbutils.ChartFilesTable}, ",")
_, err := m.DB.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s CASCADE", tables))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the CASCADE necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not here it's not, thanks. I'd originally only deleted the one table and relied on the cascades, but I had to change that as the files table doesn't have a FK in this PR. I've removed the CASCADE and will re-add it when the FK's are in the next PR (I think)>

return err
}

return m.initTables()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not really necessary since it will be executed with syncing but I am okay if you want to leave it to leave the tables ready

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the reason for having it here is so that it is sync'd with the correct code (the new, rather than from the still running older pods). We will probably need a transaction here to avoid race conditions with the existing pods too.

@absoludity absoludity merged commit bc765e1 into 1521-sync-records-namespace Feb 23, 2020
absoludity added a commit that referenced this pull request Feb 23, 2020
* Record the namespace in the assetsvc when syncing chart data.

* Update tests

* Add db test setup and start using for pgutils. Review feedback.

* Use postgres db tests based on ENV var.

* 1521 delete tables on upgrade (#1532)

* Add pre-upgrade hook to invalidate cache.

* Enable hook delete policy.

* Remove currently unnecessary CASCADE
@andresmgot andresmgot deleted the 1521-delete-tables-on-upgrade branch September 8, 2020 08:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants