/
202108020000_add_connector_type_tables.go
61 lines (56 loc) · 2.26 KB
/
202108020000_add_connector_type_tables.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package migrations
// Migrations should NEVER use types from other packages. Types can change
// and then migrations run on a _new_ database will fail or behave unexpectedly.
// Instead of importing types, always re-create the type in the migration, as
// is done here, even though the same type is defined in pkg/api
import (
"github.com/bf2fc6cc711aee1a0c2a/kas-fleet-manager/pkg/api"
"github.com/bf2fc6cc711aee1a0c2a/kas-fleet-manager/pkg/db"
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
"time"
)
func addConnectorTypeTables(migrationId string) *gormigrate.Migration {
type ConnectorChannel struct {
Channel string `gorm:"primaryKey"`
CreatedAt time.Time
UpdatedAt time.Time
// needed for soft delete. See https://gorm.io/docs/delete.html#Soft-Delete
DeletedAt gorm.DeletedAt
}
type ConnectorTypeLabel struct {
ConnectorTypeID string `gorm:"primaryKey"`
Label string `gorm:"primaryKey"`
}
type ConnectorType struct {
api.Meta
Version string
Name string `gorm:"index"`
Description string
// A json schema that can be used to validate a connector's connector_spec field.
JsonSchema api.JSON `gorm:"type:jsonb"`
// Type's channels
Channels []ConnectorChannel `gorm:"many2many:connector_type_channels;"`
// URL to an icon of the connector.
IconHref string
// labels used to categorize the connector
Labels []ConnectorTypeLabel `gorm:"foreignKey:ConnectorTypeID"`
}
type ConnectorShardMetadata struct {
ID int64 `gorm:"primaryKey:autoIncrement"`
ConnectorTypeId string `gorm:"primaryKey"`
Channel string `gorm:"primaryKey"`
ShardMetadata string `gorm:"type:jsonb"`
LatestId *int64
}
return db.CreateMigrationFromActions(migrationId,
db.ExecAction("", "DROP TABLE connector_type_channels"),
db.CreateTableAction(&ConnectorTypeLabel{}),
db.CreateTableAction(&ConnectorChannel{}),
db.CreateTableAction(&ConnectorShardMetadata{}),
db.CreateTableAction(&ConnectorType{}),
db.ExecAction(
"ALTER TABLE connector_shard_metadata ADD CONSTRAINT fk_connector_shard_metadata_connector_channels FOREIGN KEY (channel) REFERENCES connector_channels(channel)",
"ALTER TABLE connector_shard_metadata DROP CONSTRAINT IF EXISTS fk_connector_shard_metadata_connector_channels"),
)
}