-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply migrations when tink-server boots (#296)
## 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. ## Why is this needed Fixes: #295 ## How Has This Been Tested? ```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":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} ``` ## How are existing users impacted? What migration steps/scripts do we need? 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
- Loading branch information
Showing
8 changed files
with
432 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package migration | ||
|
||
import migrate "github.com/rubenv/sql-migrate" | ||
|
||
func Get202009171251() *migrate.Migration { | ||
return &migrate.Migration{ | ||
Id: "202009171251-init-database", | ||
Up: []string{` | ||
SET ROLE tinkerbell; | ||
SELECT 'CREATE DATABASE tinkerbell' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'tinkerbell'); | ||
CREATE TABLE IF NOT EXISTS hardware ( | ||
id UUID UNIQUE | ||
, inserted_at TIMESTAMPTZ | ||
, deleted_at TIMESTAMPTZ | ||
, data JSONB | ||
); | ||
CREATE INDEX IF NOT EXISTS idx_id ON hardware (id); | ||
CREATE INDEX IF NOT EXISTS idx_deleted_at ON hardware (deleted_at NULLS FIRST); | ||
CREATE INDEX IF NOT EXISTS idxgin_type ON hardware USING GIN (data JSONB_PATH_OPS); | ||
CREATE TABLE IF NOT EXISTS template ( | ||
id UUID UNIQUE NOT NULL | ||
, name VARCHAR(200) UNIQUE NOT NULL | ||
, created_at TIMESTAMPTZ | ||
, updated_at TIMESTAMPTZ | ||
, deleted_at TIMESTAMPTZ | ||
, data BYTEA | ||
CONSTRAINT CK_name CHECK (name ~ '^[a-zA-Z0-9_-]*$') | ||
); | ||
CREATE INDEX IF NOT EXISTS idx_tid ON template (id); | ||
CREATE INDEX IF NOT EXISTS idx_tdeleted_at ON template (deleted_at NULLS FIRST); | ||
CREATE TABLE IF NOT EXISTS workflow ( | ||
id UUID UNIQUE NOT NULL | ||
, template UUID NOT NULL | ||
, devices JSONB NOT NULL | ||
, created_at TIMESTAMPTZ | ||
, updated_at TIMESTAMPTZ | ||
, deleted_at TIMESTAMPTZ | ||
); | ||
CREATE INDEX IF NOT EXISTS idx_wid ON workflow (id); | ||
CREATE INDEX IF NOT EXISTS idx_wdeleted_at ON workflow (deleted_at NULLS FIRST); | ||
CREATE TABLE IF NOT EXISTS workflow_state ( | ||
workflow_id UUID UNIQUE NOT NULL | ||
, current_task_name VARCHAR(200) | ||
, current_action_name VARCHAR(200) | ||
, current_action_state SMALLINT | ||
, current_worker VARCHAR(200) | ||
, action_list JSONB | ||
, current_action_index int | ||
, total_number_of_actions INT | ||
); | ||
CREATE INDEX IF NOT EXISTS idx_wfid ON workflow_state (workflow_id); | ||
CREATE TABLE IF NOT EXISTS workflow_event ( | ||
workflow_id UUID NOT NULL | ||
, worker_id UUID NOT NULL | ||
, task_name VARCHAR(200) | ||
, action_name VARCHAR(200) | ||
, execution_time int | ||
, message VARCHAR(200) | ||
, status SMALLINT | ||
, created_at TIMESTAMPTZ | ||
); | ||
CREATE INDEX IF NOT EXISTS idx_event ON workflow_event (created_at); | ||
CREATE TABLE IF NOT EXISTS workflow_worker_map ( | ||
workflow_id UUID NOT NULL | ||
, worker_id UUID NOT NULL | ||
); | ||
CREATE TABLE IF NOT EXISTS workflow_data ( | ||
workflow_id UUID NOT NULL | ||
, version INT | ||
, metadata JSONB | ||
, data JSONB | ||
);`}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package migration | ||
|
||
import migrate "github.com/rubenv/sql-migrate" | ||
|
||
func GetMigrations() *migrate.MemoryMigrationSource { | ||
return &migrate.MemoryMigrationSource{ | ||
Migrations: []*migrate.Migration{ | ||
Get202009171251(), | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.