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

Apply migrations when tink-server boots #313

Merged
merged 1 commit into from
Oct 1, 2020
Merged

Apply migrations when tink-server boots #313

merged 1 commit into from
Oct 1, 2020

Conversation

gianarb
Copy link
Contributor

@gianarb gianarb commented Oct 1, 2020

Description

It will be nice to have the tink-server capable of applying migrations
if necessary. The idea to self-apply migrations will may look unusual
but it is an easy way to distribute software capable of migrating
database schema without having to run any external procedures.

$ docker run -d -it -e  POSTGRES_PASSWORD=tinkerbell -e POSTGRES_USER=tinkerbell -p 5432:5432 postgres
# PGPASSWORD=tinkerbell PGUSER=tinkerbell PGSSLMODE=disable PACKET_VERSION=v1 PACKET_ENV=ga ROLLBAR_TOKEN=ignored go run cmd/tink-server/main.go
{"level":"info","ts":1600772721.3009,"caller":"tink-server/main.go:30","msg":"starting version devel","service":"github.com/tinkerbell/tink"}
migrations  1
records  0
{"level":"info","ts":1600772721.309925,"caller":"tink-server/main.go:54","msg":"Your database schema is not up to date. Please apply migrations running tink-server with env var ONLY_MIGRATION set.","service":"github.com/tinkerbell/tink"}
{"level":"info","ts":1600772721.310011,"caller":"metrics/metrics.go:58","msg":"initializing label values","service":"github.com/tinkerbell/tink"}

As suggested you should run tink-server with ONLY_MIGRATION=true

$ ONLY_MIGRATION=true PGPASSWORD=tinkerbell PGUSER=tinkerbell PGSSLMODE=disable PACKET_VERSION=v1 PACKET_ENV=ga ROLLBAR_TOKEN=ignored go run cmd/tink-server/main.go
{"level":"info","ts":1600772853.779405,"caller":"tink-server/main.go:30","msg":"starting version devel","service":"github.com/tinkerbell/tink"}
{"level":"info","ts":1600772853.779805,"caller":"tink-server/main.go:40","msg":"Applying migrations. This process will end when migrations will take place.","service":"github.com/tinkerbell/tink"}
{"level":"info","ts":1600772853.819854,"caller":"tink-server/main.go:45","msg":"Running migrations if necessary","service":"github.com/tinkerbell/tink","num_applied_migrations":1}

This is the logs from docker-compose up, as you can see there is a new service that runs before tink-server and it applies the migrations.

registry_1               | 127.0.0.1 - - [01/Oct/2020:08:58:07 +0000] "GET / HTTP/2.0" 200 0 "" "curl/7.67.0"
db_1                     | 2020-10-01 08:58:04.165 UTC [1] LOG:  database system is ready to accept connections
registry_1               | 127.0.0.1 - - [01/Oct/2020:08:58:12 +0000] "GET / HTTP/2.0" 200 0 "" "curl/7.67.0"
hegel_1                  | 2020/10/01 08:58:05 fetch cert: Get http://127.0.0.1:42114/cert: dial tcp 127.0.0.1:42114: connect: connection refused
tink-server-migration_1  | {"level":"info","ts":1601542686.5914426,"caller":"tink-server/main.go:29","msg":"starting version 81ae632","service":"github.com/tinkerbell/tink"}
tink-server-migration_1  | {"level":"info","ts":1601542686.591473,"caller":"tink-server/main.go:39","msg":"Applying migrations. This process will end when migrations will take place.","service":"github.com/tinkerbell/tink"}
tink-server-migration_1  | {"level":"info","ts":1601542686.6131954,"caller":"tink-server/main.go:45","msg":"Migrations applied successfully","service":"github.com/tinkerbell/tink","num_applied_migrations":0}
deploy_tink-server-migration_1 exited with code 0
tink-server_1            | {"level":"info","ts":1601542687.7541816,"caller":"tink-server/main.go:29","msg":"starting version 81ae632","service":"github.com/tinkerbell/tink"}
tink-server_1            | {"level":"info","ts":1601542687.7602122,"caller":"metrics/metrics.go:58","msg":"initializing label values","service":"github.com/tinkerbell/tink"}
tink-server_1            | {"level":"info","ts":1601542687.7629616,"caller":"grpc-server/grpc_server.go:76","msg":"serving grpc","service":"github.com/tinkerbell/tink"}
tink-server_1            | {"level":"info","ts":1601542687.7635539,"caller":"http-server/http_server.go:90","msg":"serving http","service":"github.com/tinkerbell/tink"}

Why is this needed

Fixes: #295

How Has This Been Tested?

With docker-compose and vagrant.

How are existing users impacted? What migration steps/scripts do we need?

no impact, the workflow is well managed by docker-compose, transparently.

They are not impacted by this change.

Checklist:

I have:

  • updated the documentation and/or roadmap (if required)
  • added unit or e2e tests
  • provided instructions on how to upgrade

@codecov
Copy link

codecov bot commented Oct 1, 2020

Codecov Report

Merging #313 into master will increase coverage by 0.01%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #313      +/-   ##
==========================================
+ Coverage   22.65%   22.67%   +0.01%     
==========================================
  Files          15       15              
  Lines        1271     1270       -1     
==========================================
  Hits          288      288              
+ Misses        964      963       -1     
  Partials       19       19              
Impacted Files Coverage Δ
grpc-server/grpc_server.go 0.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 08601fe...c5dcc6b. Read the comment docs.

@gianarb gianarb requested a review from mmlb October 1, 2020 09:01
It will be nice to have the tink-server capable of applying migrations
if necessary. The idea to self-apply migrations will may look unusual
but it is an easy way to distribute software capable of migrating
database schema without having to run any external procedures.

```console
$ docker run -d -it -e  POSTGRES_PASSWORD=tinkerbell -e POSTGRES_USER=tinkerbell -p 5432:5432 postgres
$ PGPASSWORD=tinkerbell PGUSER=tinkerbell PGSSLMODE=disable PACKET_VERSION=v1 PACKET_ENV=ga ROLLBAR_TOKEN=ignored go run cmd/tink-server/main.go
{"level":"info","ts":1600349232.375571,"caller":"tink-server/main.go:31","msg":"starting version devel","service":"github.com/tinkerbell/tink"}
{"level":"info","ts":1600349232.419096,"caller":"tink-server/main.go:42","msg":"Running migrations if necessary","service":"github.com/tinkerbell/tink","num_applied_migrations":1}
```

Second time you run it:

```
$ PGPASSWORD=tinkerbell PGUSER=tinkerbell PGSSLMODE=disable PACKET_VERSION=v1 PACKET_ENV=ga ROLLBAR_TOKEN=ignored go run cmd/tink-server/main.go                                                                                   [12/78]{"level":"info","ts":1600349264.877263,"caller":"tink-server/main.go:31","msg":"starting version devel","service":"github.com/tinkerbell/tink"}
{"level":"info","ts":1600349264.8883042,"caller":"tink-server/main.go:42","msg":"Running migrations if necessary","service":"github.com/tinkerbell/tink","num_applied_migrations":0}
```

Signed-off-by: Gianluca Arbezzano <gianarb92@gmail.com>
@gianarb gianarb added the ready-to-merge Signal to Mergify to merge the PR. label Oct 1, 2020
@mergify mergify bot merged commit a686336 into tinkerbell:master Oct 1, 2020
@mmlb mmlb removed the ready-to-merge Signal to Mergify to merge the PR. label Jan 25, 2021
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.

Integrate a migration tool in tink
2 participants