Skip to content

Commit

Permalink
Merge branch 'main' into chore/stable-migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm authored Oct 23, 2023
2 parents b947e72 + 5e36583 commit 5912b5b
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 35 deletions.
28 changes: 1 addition & 27 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,9 @@ jobs:
- name: run tests
run: go test -v ./...
working-directory: ./server
test_migrations:
runs-on: ubuntu-latest
services:
mariadb:
image: bitnami/mariadb:latest
ports:
- 3306:3306
env:
MARIADB_ROOT_PASSWORD: super_secret_passw0rd
MARIADB_DATABASE: campus_db
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: '1.21'
cache-dependency-path: |
server/go.sum
- name: wait for db
run: sleep 20 && docker logs $(docker ps -qa)
- name: run migration
run: go run main.go
working-directory: ./server
env:
CI_EXIT_AFTER_MIGRATION: "true"
DB_DSN: root:super_secret_passw0rd@tcp(localhost:3306)/campus_db?charset=utf8mb4&parseTime=True&loc=Local
ENVIRONMENT: dev
build:
runs-on: ubuntu-latest
needs: [test, test_migrations]
needs: [test]
steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand Down
91 changes: 91 additions & 0 deletions .github/workflows/test_migration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: Migration Test
on:
pull_request:
branches: [ main ]
concurrency:
group: ${{ github.head_ref }}
cancel-in-progress: true
jobs:
test_migrations:
runs-on: ubuntu-latest
services:
auto_mariadb:
image: bitnami/mariadb:latest
ports:
- 3306:3306
env:
MARIADB_ROOT_PASSWORD: super_secret_passw0rd
MARIADB_DATABASE: campus_db
manual_mariadb:
image: bitnami/mariadb:latest
ports:
- 3300:3306
env:
MARIADB_ROOT_PASSWORD: super_secret_passw0rd
MARIADB_DATABASE: campus_db
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: '1.21'
cache-dependency-path: |
server/go.sum
- name: run manual migrations
run: go run main.go
working-directory: ./server
env:
CI_EXIT_AFTER_MIGRATION: "true"
CI_AUTO_MIGRATION: "false"
DB_DSN: root:super_secret_passw0rd@tcp(localhost:3306)/campus_db?charset=utf8mb4&parseTime=True&loc=Local
ENVIRONMENT: dev
- name: run auto migrations
run: go run main.go
working-directory: ./server
env:
CI_EXIT_AFTER_MIGRATION: "true"
CI_AUTO_MIGRATION: "true"
DB_DSN: root:super_secret_passw0rd@tcp(localhost:3300)/campus_db?charset=utf8mb4&parseTime=True&loc=Local
ENVIRONMENT: dev
- uses: ariga/setup-atlas@master
- name: export diff the migrations
id: diff_migrations
run: |
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64) &&
echo "local_to_auto<<$EOF" >> $GITHUB_OUTPUT &&
atlas schema diff --from "maria://root:super_secret_passw0rd@localhost:3306/campus_db" --to "maria://root:super_secret_passw0rd@localhost:3300/campus_db" --format '{{ sql . " " }}' >> $GITHUB_OUTPUT &&
echo "$EOF" >> $GITHUB_OUTPUT
echo "auto_to_local<<$EOF" >> $GITHUB_OUTPUT &&
atlas schema diff --from "maria://root:super_secret_passw0rd@localhost:3306/campus_db" --to "maria://root:super_secret_passw0rd@localhost:3300/campus_db" --format '{{ sql . " " }}' >> $GITHUB_OUTPUT &&
echo "$EOF" >> $GITHUB_OUTPUT
- name: Find Comment
uses: peter-evans/find-comment@v2
id: fc
with:
issue-number: "${{ github.event.number }}"
body-includes: Found the following differences in the sql schema
comment-author: github-actions[bot]
- name: Create comment
uses: peter-evans/create-or-update-comment@v3
with:
issue-number: "${{ github.event.number }}"
comment-id: "${{ steps.fc.outputs.comment-id }}"
body: |
:eyes: Found the following differences in the sql schema:
<details>
<summary>Needed get from <b>local</b> to <b>auto</b> migration state</summary>
```sql
${{ steps.diff_migrations.outputs.local_to_auto }}
```
</details>
<details>
<summary>Needed from <b>auto</b> to <b>local</b> migration state</summary>
```sql
${{ steps.diff_migrations.outputs.auto_to_local }}
```
</details>
edit-mode: replace
42 changes: 39 additions & 3 deletions server/backend/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,48 @@ import (

func autoMigrate(db *gorm.DB) error {
err := db.AutoMigrate(
&model.Cafeteria{},
&model.CafeteriaRating{},
&model.CafeteriaRatingAverage{},
&model.CafeteriaRatingTag{},
&model.CafeteriaRatingTagAverage{},
&model.CafeteriaRatingTagOption{},
&model.CanteenHeadCount{},
&model.Crontab{},
&model.Device{},
&model.Dish{},
&model.DishNameTag{},
&model.DishNameTagAverage{},
&model.DishNameTagOption{},
&model.DishNameTagOptionExcluded{},
&model.DishNameTagOptionIncluded{},
&model.DishRating{},
&model.DishRatingAverage{},
&model.DishRatingTag{},
&model.DishRatingTagAverage{},
&model.DishRatingTagOption{},
&model.DishToDishNameTag{},
&model.DishesOfTheWeek{},
&model.PublishedExamResult{},
&model.Feedback{},
&model.File{},
&model.NewsSource{},
&model.NewsAlert{},
&model.IOSDevice{},
//&model.IOSDeviceLastUpdated{}, -- not a gorm model
&model.IOSDeviceRequestLog{},
&model.IOSDevicesActivityReset{},
//&model.IOSGrade{}, -- not a gorm model
//&model.IOSRemoteNotification...{}, -- wtf???
&model.IOSScheduledUpdateLog{},
&model.IOSSchedulingPriority{},
&model.Kino{},
&model.NewExamResultsSubscriber{},
&model.News{},
&model.CanteenHeadCount{},
&model.NewsAlert{},
&model.NewsSource{},
&model.Notification{},
&model.NotificationConfirmation{},
&model.NotificationType{},
&model.UpdateNote{},
)
return err
}
Expand Down
6 changes: 3 additions & 3 deletions server/model/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ type Device struct {
Member null.Int `gorm:"column:member;type:int;" json:"member"`
UUID string `gorm:"column:uuid;type:varchar(50);" json:"uuid"`
Created null.Time `gorm:"column:created;type:timestamp;" json:"created"`
LastAccess time.Time `gorm:"column:lastAccess;type:timestamp;default:0000-00-00 00:00:00;" json:"last_access"`
LastAccess time.Time `gorm:"column:lastAccess;type:timestamp;default:'0000-00-00 00:00:00';" json:"last_access"`
LastAPI string `gorm:"column:lastApi;type:text;size:16777215;" json:"last_api"`
Developer string `gorm:"column:developer;type:char;size:5;default:false;" json:"developer"`
Developer string `gorm:"column:developer;type:enum('true','false');default:'false';" json:"developer"`
OsVersion string `gorm:"column:osVersion;type:text;size:16777215;" json:"os_version"`
AppVersion string `gorm:"column:appVersion;type:text;size:16777215;" json:"app_version"`
Counter int32 `gorm:"column:counter;type:int;default:0;" json:"counter"`
Pk null.String `gorm:"column:pk;type:text;size:4294967295;" json:"pk"`
PkActive string `gorm:"column:pkActive;type:char;size:5;default:false;" json:"pk_active"`
PkActive string `gorm:"column:pkActive;type:enum('true', 'false');default:'false';" json:"pk_active"`
GcmToken null.String `gorm:"column:gcmToken;type:text;size:65535;" json:"gcm_token"`
GcmStatus null.String `gorm:"column:gcmStatus;type:varchar(200);" json:"gcm_status"`
ConfirmationKey null.String `gorm:"column:confirmationKey;type:varchar(35);" json:"confirmation_key"`
Expand Down
2 changes: 1 addition & 1 deletion server/model/notification_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ var (
type NotificationType struct {
Type int64 `gorm:"primary_key;AUTO_INCREMENT;column:type;type:int;" json:"type"`
Name string `gorm:"column:name;type:text;size:65535;" json:"name"`
Confirmation string `gorm:"column:confirmation;type:char;size:5;default:false;" json:"confirmation"`
Confirmation string `gorm:"column:confirmation;type:enum('true', 'false');default:'false';" json:"confirmation"`
}
2 changes: 1 addition & 1 deletion server/utils/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func SetupDB() *gorm.DB {

// Migrate the schema
// currently not activated as
if err := migration.Migrate(db, false); err != nil {
if err := migration.Migrate(db, os.Getenv("CI_AUTO_MIGRATION") == "true"); err != nil {
log.WithError(err).Fatal("Failed to migrate database")
}

Expand Down

0 comments on commit 5912b5b

Please sign in to comment.