Change PGSQL CortezaID columns datatype to numeric

This commit is contained in:
Mumbi Francis
2023-02-07 14:10:22 +03:00
committed by Mumbi Francis
parent b02dff7b66
commit 3d960a1f18
3 changed files with 48 additions and 2 deletions
@@ -114,10 +114,10 @@ func (postgresDialect) AttributeToColumn(attr *dal.Attribute) (col *ddl.Column,
switch t := attr.Type.(type) {
case *dal.TypeID:
col.Type.Name = "BIGINT"
col.Type.Name = "NUMERIC"
col.Default = ddl.DefaultID(t.HasDefault, t.DefaultValue)
case *dal.TypeRef:
col.Type.Name = "BIGINT"
col.Type.Name = "NUMERIC"
col.Default = ddl.DefaultID(t.HasDefault, t.DefaultValue)
case *dal.TypeTimestamp:
+13
View File
@@ -182,3 +182,16 @@ func renameColumn(ctx context.Context, s *Store, table string, from, to string)
return nil
}
// tableNames returns table names that Corteza creates
func tableNames() (tnames []string) {
cortezaModels := append(systemModels.Models(), composeModels.Models()...)
cortezaModels = append(cortezaModels, automationModels.Models()...)
cortezaModels = append(cortezaModels, federationModels.Models()...)
for _, m := range cortezaModels {
tnames = append(tnames, m.Ident)
}
return tnames
}
@@ -42,6 +42,7 @@ var (
fix_2022_09_00_addRevisionOnComposeRecords,
fix_2022_09_00_addMetaOnComposeRecords,
fix_2022_09_00_addMissingNodeIdOnFederationMapping,
fix_2022_09_07_changePostgresIdColumnsDatatype,
}
)
@@ -429,6 +430,38 @@ func fix_2022_09_00_addMissingNodeIdOnFederationMapping(ctx context.Context, s *
)
}
func fix_2022_09_07_changePostgresIdColumnsDatatype(ctx context.Context, s *Store) (err error) {
var tableName string
if !strings.HasPrefix(s.DB.DriverName(), "postgres") {
return
}
tnames := tableNames()
tnamesQry := `SELECT table_name FROM INFORMATION_SCHEMA.COLUMNS WHERE column_name = 'id' AND
table_name IN('` + strings.Join(tnames, "', '") + `') AND table_name NOT LIKE 'auth_sessions'`
rows, err := s.DB.QueryContext(ctx, tnamesQry)
if err != nil {
return err
}
for rows.Next() {
// Get the table name
err = rows.Scan(&tableName)
if err != nil {
return err
}
query := fmt.Sprintf("ALTER TABLE %s ALTER COLUMN id TYPE NUMERIC USING CAST(id AS NUMERIC)", tableName)
_, err = s.DB.ExecContext(ctx, query)
if err != nil {
return err
}
}
return nil
}
func count(ctx context.Context, s *Store, table string, ee ...goqu.Expression) (count int) {
db := s.DB.(goqu.SQLDatabase)