Skip to content

Commit

Permalink
feat: added flag migrations as yaml
Browse files Browse the repository at this point in the history
flag migrations as yaml
  • Loading branch information
Drew Foehn committed Mar 30, 2024
1 parent 770461b commit 7d0a23d
Show file tree
Hide file tree
Showing 14 changed files with 518 additions and 2 deletions.
10 changes: 10 additions & 0 deletions contributes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"contributes": {
"jsonValidation": [
{
"fileMatch": "migrations/*.yaml",
"url": "migrations/.schema.json"
}
]
}
}
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [Debug Console](flagr_debugging.md)
- Server Configuration
- [Env](flagr_env.md)
- [Migrations](flagr_migrations.md)
- Client SDKs
- [Ruby SDK 🔗](https://github.com/openflagr/rbflagr)
- [Go SDK 🔗](https://github.com/openflagr/goflagr)
Expand Down
53 changes: 53 additions & 0 deletions docs/flagr_migrations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Migrations

Users can create flag collections as yaml files within a migration directory and run flagr to insert/modify flags.
This allows for developers to create flags as deployment assets and allows for migrating flags through various environments whilst keeping keys stable.

Each found flag is upserted into the database. Then the flag has all segments, variants and tags removed then replaced with the new flag properties.

As an example, create a file named `migrations/202403030000.yaml` with the following content:
```yaml
---
# this is a basic flag
- key: SIMPLE-FLAG-1
description: a toggle for just one user
enabled: true
segments:
- description: flag for just for one email test@test.com
rank: 0
rolloutPercent: 100
constraints:
- property: email
operator: EQ
value: '"test@test.com"'
distributions:
- variantKey: "on"
percent: 100
- rank: 1
rolloutPercent: 100
constraints: []
distributions:
- variantKey: "off"
percent: 100
variants:
- key: "off"
attachment: {}
- key: "on"
attachment: {}
entityType: User
dataRecordsEnabled: true

```

```shell
$ flagr -m
INFO[0146] 1 new migrations completed (1 total)
```
Once the application is ran, flagr will scan the migration files, insert them into the db and shut down.

### Config
Location of yaml configs can be set with either argument or env var.
```
FLAGR_MIGRATION_PATH=./migrations/ ./flagr -m
./flagr -m --migrationPath=`pwd`/migrations
```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,5 @@ require (
modernc.org/mathutil v1.5.0 // indirect
modernc.org/memory v1.5.0 // indirect
modernc.org/sqlite v1.23.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -714,3 +714,5 @@ modernc.org/strutil v1.1.3/go.mod h1:MEHNA7PdEnEwLvspRMtWTNnp2nnyvMfkimT1NKNAGbw
modernc.org/tcl v1.15.0/go.mod h1:xRoGotBZ6dU+Zo2tca+2EqVEeMmOUBzHnhIwq4YrVnE=
modernc.org/token v1.0.1/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
modernc.org/z v1.7.0/go.mod h1:hVdgNMh8ggTuRG1rGU8x+xGRFfiQUIAw0ZqlPy8+HyQ=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
125 changes: 125 additions & 0 deletions migrations/.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"key": {
"type": "string"
},
"description": {
"type": "string"
},
"enabled": {
"type": "boolean"
},
"segments": {
"type": "array",
"items": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"rank": {
"type": "integer"
},
"rolloutPercent": {
"type": "integer"
},
"constraints": {
"type": "array",
"items": {
"type": "object",
"properties": {
"property": {
"type": "string"
},
"operator": {
"type": "string"
},
"value": {
"type": "string"
}
},
"required": [
"property",
"operator",
"value"
]
}
},
"distributions": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"variantKey": {
"type": "string"
},
"percent": {
"type": "integer"
}
},
"required": [
"variantKey",
"percent"
]
}
]
}
},
"required": [
"rank",
"rolloutPercent",
"distributions"
]
}
},
"variants": {
"type": "array",
"items": {
"type": "object",
"properties": {
"key": {
"type": "string"
},
"attachment": {
"type": "object"
}
},
"required": [
"key"
]
}
},
"entityType": {
"type": "string"
},
"dataRecordsEnabled": {
"type": "boolean"
},
"tags": {
"type": "array",
"items":
{
"type": "object",
"properties": {
"value": {
"type": "string"
}
},
"required": [
"value"
]
}
}
},
"required": [
"key",
"description",
"enabled"
]
}
}
1 change: 1 addition & 0 deletions pkg/entity/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var AutoMigrateTables = []interface{}{
Variant{},
Tag{},
FlagEntityType{},
FlagMigration{},
}

func connectDB() (db *gorm.DB, err error) {
Expand Down
9 changes: 9 additions & 0 deletions pkg/entity/flag_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package entity

import "gorm.io/gorm"

type FlagMigration struct {
gorm.Model

Name string `gorm:"type:varchar(64);uniqueIndex:idx_flag_migration_name"`
}
Loading

0 comments on commit 7d0a23d

Please sign in to comment.