mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
ID should be primary key on apps model and name should be unique. This allows for renaming apps in the future. (#1048)
This commit is contained in:
84
api/datastore/sql/migrations/15_alter_apps.go
Normal file
84
api/datastore/sql/migrations/15_alter_apps.go
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
package migrations
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/fnproject/fn/api/datastore/sql/migratex"
|
||||||
|
"github.com/jmoiron/sqlx"
|
||||||
|
)
|
||||||
|
|
||||||
|
func up15(ctx context.Context, tx *sqlx.Tx) error {
|
||||||
|
_, err := tx.ExecContext(ctx, "ALTER TABLE apps RENAME TO old_apps;")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
newTable := `CREATE TABLE apps (
|
||||||
|
id varchar(256) NOT NULL PRIMARY KEY,
|
||||||
|
name varchar(256) NOT NULL UNIQUE,
|
||||||
|
config text NOT NULL,
|
||||||
|
annotations text NOT NULL,
|
||||||
|
created_at varchar(256),
|
||||||
|
updated_at varchar(256),
|
||||||
|
syslog_url text
|
||||||
|
);`
|
||||||
|
_, err = tx.ExecContext(ctx, newTable)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
insertQuery := `INSERT INTO apps(id,name,config,annotations,created_at,updated_at,syslog_url)
|
||||||
|
SELECT id,name,config,annotations,created_at,updated_at,syslog_url FROM old_apps;`
|
||||||
|
|
||||||
|
_, err = tx.ExecContext(ctx, insertQuery)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = tx.ExecContext(ctx, "DROP TABLE old_apps;")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func down15(ctx context.Context, tx *sqlx.Tx) error {
|
||||||
|
_, err := tx.ExecContext(ctx, "ALTER TABLE apps RENAME TO old_apps;")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
newTable := `CREATE TABLE apps (
|
||||||
|
id varchar(256),
|
||||||
|
name varchar(256) NOT NULL PRIMARY KEY,
|
||||||
|
config text NOT NULL,
|
||||||
|
annotations text NOT NULL,
|
||||||
|
created_at varchar(256),
|
||||||
|
updated_at varchar(256),
|
||||||
|
syslog_url text
|
||||||
|
);`
|
||||||
|
_, err = tx.ExecContext(ctx, newTable)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
insertQuery := `INSERT INTO apps(id,name,config,annotations,created_at,updated_at,syslog_url)
|
||||||
|
SELECT id,name,config,annotations,created_at,updated_at,syslog_url FROM old_apps;`
|
||||||
|
|
||||||
|
_, err = tx.ExecContext(ctx, insertQuery)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = tx.ExecContext(ctx, "DROP TABLE old_apps;")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Migrations = append(Migrations, &migratex.MigFields{
|
||||||
|
VersionFunc: vfunc(15),
|
||||||
|
UpFunc: up15,
|
||||||
|
DownFunc: down15,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -59,8 +59,8 @@ var tables = [...]string{`CREATE TABLE IF NOT EXISTS routes (
|
|||||||
);`,
|
);`,
|
||||||
|
|
||||||
`CREATE TABLE IF NOT EXISTS apps (
|
`CREATE TABLE IF NOT EXISTS apps (
|
||||||
id varchar(256),
|
id varchar(256) NOT NULL PRIMARY KEY,
|
||||||
name varchar(256) NOT NULL PRIMARY KEY,
|
name varchar(256) NOT NULL UNIQUE,
|
||||||
config text NOT NULL,
|
config text NOT NULL,
|
||||||
annotations text NOT NULL,
|
annotations text NOT NULL,
|
||||||
syslog_url text,
|
syslog_url text,
|
||||||
|
|||||||
Reference in New Issue
Block a user