mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
* App ID * Clean-up * Use ID or name to reference apps * Can use app by name or ID * Get rid of AppName for routes API and model routes API is completely backwards-compatible routes API accepts both app ID and name * Get rid of AppName from calls API and model * Fixing tests * Get rid of AppName from logs API and model * Restrict API to work with app names only * Addressing review comments * Fix for hybrid mode * Fix rebase problems * Addressing review comments * Addressing review comments pt.2 * Fixing test issue * Addressing review comments pt.3 * Updated docstring * Adjust UpdateApp SQL implementation to work with app IDs instead of names * Fixing tests * fmt after rebase * Make tests green again! * Use GetAppByID wherever it is necessary - adding new v2 endpoints to keep hybrid api/runner mode working - extract CallBase from Call object to expose that to a user (it doesn't include any app reference, as we do for all other API objects) * Get rid of GetAppByName * Adjusting server router setup * Make hybrid work again * Fix datastore tests * Fixing tests * Do not ignore app_id * Resolve issues after rebase * Updating test to make it work as it was * Tabula rasa for migrations * Adding calls API test - we need to ensure we give "App not found" for the missing app and missing call in first place - making previous test work (request missing call for the existing app) * Make datastore tests work fine with correctly applied migrations * Make CallFunction middleware work again had to adjust its implementation to set app ID before proceeding * The biggest rebase ever made * Fix 8's migration * Fix tests * Fix hybrid client * Fix tests problem * Increment app ID migration version * Fixing TestAppUpdate * Fix rebase issues * Addressing review comments * Renew vendor * Updated swagger doc per recommendations
160 lines
3.4 KiB
Go
160 lines
3.4 KiB
Go
package migrations
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"github.com/fnproject/fn/api/datastore/sql/migratex"
|
|
"github.com/fnproject/fn/api/id"
|
|
"github.com/fnproject/fn/api/models"
|
|
"github.com/jmoiron/sqlx"
|
|
)
|
|
|
|
func up10(ctx context.Context, tx *sqlx.Tx) error {
|
|
addAppIDStatements := []string{
|
|
"ALTER TABLE apps ADD id VARCHAR(256);",
|
|
"ALTER TABLE calls ADD app_id VARCHAR(256);",
|
|
"ALTER TABLE logs ADD app_id VARCHAR(256);",
|
|
"ALTER TABLE routes ADD app_id VARCHAR(256);",
|
|
}
|
|
for _, statement := range addAppIDStatements {
|
|
_, err := tx.ExecContext(ctx, statement)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
rows, err := tx.QueryxContext(ctx, "SELECT DISTINCT name FROM apps;")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
res := []*models.App{}
|
|
for rows.Next() {
|
|
|
|
var app models.App
|
|
err := rows.StructScan(&app)
|
|
if err != nil {
|
|
if err == sql.ErrNoRows {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
app.ID = id.New().String()
|
|
res = append(res, &app)
|
|
}
|
|
err = rows.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// it is required for some reason, can't do this within the rows iteration.
|
|
for _, app := range res {
|
|
query := tx.Rebind(`UPDATE apps SET id=:id WHERE name=:name`)
|
|
_, err = tx.NamedExecContext(ctx, query, app)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, t := range []string{"routes", "calls", "logs"} {
|
|
q := fmt.Sprintf(`UPDATE %s SET app_id=:id WHERE app_name=:name;`, t)
|
|
_, err = tx.NamedExecContext(ctx, tx.Rebind(q), app)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
dropAppNameStatements := []string{
|
|
"ALTER TABLE routes DROP COLUMN app_name;",
|
|
"ALTER TABLE calls DROP COLUMN app_name;",
|
|
"ALTER TABLE logs DROP COLUMN app_name;",
|
|
}
|
|
for _, statement := range dropAppNameStatements {
|
|
_, err := tx.ExecContext(ctx, statement)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func down10(ctx context.Context, tx *sqlx.Tx) error {
|
|
|
|
addAppNameStatements := []string{
|
|
"ALTER TABLE calls ADD app_name VARCHAR(256);",
|
|
"ALTER TABLE logs ADD app_name VARCHAR(256);",
|
|
"ALTER TABLE routes ADD app_name VARCHAR(256);",
|
|
}
|
|
for _, statement := range addAppNameStatements {
|
|
_, err := tx.ExecContext(ctx, statement)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
rows, err := tx.QueryxContext(ctx, "SELECT DISTINCT id, name FROM apps;")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
res := []*models.App{}
|
|
for rows.Next() {
|
|
var app models.App
|
|
err := rows.StructScan(&app)
|
|
if err != nil {
|
|
if err == sql.ErrNoRows {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
res = append(res, &app)
|
|
}
|
|
err = rows.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// it is required for some reason, can't do this within the rows iteration.
|
|
for _, app := range res {
|
|
for _, t := range []string{"routes", "calls", "logs"} {
|
|
q := "UPDATE " + t + " SET app_name=:name WHERE app_id=:id;"
|
|
_, err = tx.NamedExecContext(ctx, tx.Rebind(q), app)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
removeAppIDStatements := []string{
|
|
"ALTER TABLE logs DROP COLUMN app_id;",
|
|
"ALTER TABLE calls DROP COLUMN app_id;",
|
|
"ALTER TABLE routes DROP COLUMN app_id;",
|
|
"ALTER TABLE apps DROP COLUMN id;",
|
|
}
|
|
for _, statement := range removeAppIDStatements {
|
|
_, err := tx.ExecContext(ctx, statement)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func init() {
|
|
Migrations = append(Migrations, &migratex.MigFields{
|
|
VersionFunc: vfunc(10),
|
|
UpFunc: up10,
|
|
DownFunc: down10,
|
|
})
|
|
}
|