datasource_mig.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package migrations
  2. import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
  3. func addDataSourceMigration(mg *Migrator) {
  4. var tableV1 = Table{
  5. Name: "data_source",
  6. Columns: []*Column{
  7. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  8. {Name: "account_id", Type: DB_BigInt, Nullable: false},
  9. {Name: "version", Type: DB_Int, Nullable: false},
  10. {Name: "type", Type: DB_NVarchar, Length: 255, Nullable: false},
  11. {Name: "name", Type: DB_NVarchar, Length: 190, Nullable: false},
  12. {Name: "access", Type: DB_NVarchar, Length: 255, Nullable: false},
  13. {Name: "url", Type: DB_NVarchar, Length: 255, Nullable: false},
  14. {Name: "password", Type: DB_NVarchar, Length: 255, Nullable: true},
  15. {Name: "user", Type: DB_NVarchar, Length: 255, Nullable: true},
  16. {Name: "database", Type: DB_NVarchar, Length: 255, Nullable: true},
  17. {Name: "basic_auth", Type: DB_Bool, Nullable: false},
  18. {Name: "basic_auth_user", Type: DB_NVarchar, Length: 255, Nullable: true},
  19. {Name: "basic_auth_password", Type: DB_NVarchar, Length: 255, Nullable: true},
  20. {Name: "is_default", Type: DB_Bool, Nullable: false},
  21. {Name: "created", Type: DB_DateTime, Nullable: false},
  22. {Name: "updated", Type: DB_DateTime, Nullable: false},
  23. },
  24. Indices: []*Index{
  25. {Cols: []string{"account_id"}},
  26. {Cols: []string{"account_id", "name"}, Type: UniqueIndex},
  27. },
  28. }
  29. mg.AddMigration("create data_source table", NewAddTableMigration(tableV1))
  30. mg.AddMigration("add index data_source.account_id", NewAddIndexMigration(tableV1, tableV1.Indices[0]))
  31. mg.AddMigration("add unique index data_source.account_id_name", NewAddIndexMigration(tableV1, tableV1.Indices[1]))
  32. // ---------------------
  33. // account -> org changes
  34. // drop v1 indices
  35. addDropAllIndicesMigrations(mg, "v1", tableV1)
  36. // rename table
  37. addTableRenameMigration(mg, "data_source", "data_source_v1", "v1")
  38. // new table
  39. var tableV2 = Table{
  40. Name: "data_source",
  41. Columns: []*Column{
  42. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  43. {Name: "org_id", Type: DB_BigInt, Nullable: false},
  44. {Name: "version", Type: DB_Int, Nullable: false},
  45. {Name: "type", Type: DB_NVarchar, Length: 255, Nullable: false},
  46. {Name: "name", Type: DB_NVarchar, Length: 190, Nullable: false},
  47. {Name: "access", Type: DB_NVarchar, Length: 255, Nullable: false},
  48. {Name: "url", Type: DB_NVarchar, Length: 255, Nullable: false},
  49. {Name: "password", Type: DB_NVarchar, Length: 255, Nullable: true},
  50. {Name: "user", Type: DB_NVarchar, Length: 255, Nullable: true},
  51. {Name: "database", Type: DB_NVarchar, Length: 255, Nullable: true},
  52. {Name: "basic_auth", Type: DB_Bool, Nullable: false},
  53. {Name: "basic_auth_user", Type: DB_NVarchar, Length: 255, Nullable: true},
  54. {Name: "basic_auth_password", Type: DB_NVarchar, Length: 255, Nullable: true},
  55. {Name: "is_default", Type: DB_Bool, Nullable: false},
  56. {Name: "json_data", Type: DB_Text, Nullable: true},
  57. {Name: "created", Type: DB_DateTime, Nullable: false},
  58. {Name: "updated", Type: DB_DateTime, Nullable: false},
  59. },
  60. Indices: []*Index{
  61. {Cols: []string{"org_id"}},
  62. {Cols: []string{"org_id", "name"}, Type: UniqueIndex},
  63. },
  64. }
  65. // create v2 table
  66. mg.AddMigration("create data_source table v2", NewAddTableMigration(tableV2))
  67. // add v2 indíces
  68. addTableIndicesMigrations(mg, "v2", tableV2)
  69. //------- copy data from v1 to v2 -------------------
  70. mg.AddMigration("copy data_source v1 to v2", NewCopyTableDataMigration("data_source", "data_source_v1", map[string]string{
  71. "id": "id",
  72. "org_id": "account_id",
  73. "version": "version",
  74. "type": "type",
  75. "name": "name",
  76. "access": "access",
  77. "url": "url",
  78. "user": "user",
  79. "password": "password",
  80. "database": "database",
  81. "basic_auth": "basic_auth",
  82. "basic_auth_user": "basic_auth_user",
  83. "basic_auth_password": "basic_auth_password",
  84. "is_default": "is_default",
  85. "created": "created",
  86. "updated": "updated",
  87. }))
  88. mg.AddMigration("Drop old table data_source_v1 #2", NewDropTableMigration("data_source_v1"))
  89. // add column to activate withCredentials option
  90. mg.AddMigration("Add column with_credentials", NewAddColumnMigration(tableV2, &Column{
  91. Name: "with_credentials", Type: DB_Bool, Nullable: false, Default: "0",
  92. }))
  93. // add column that can store TLS client auth data
  94. mg.AddMigration("Add secure json data column", NewAddColumnMigration(tableV2, &Column{
  95. Name: "secure_json_data", Type: DB_Text, Nullable: true,
  96. }))
  97. mg.AddMigration("Update data_source table charset", NewTableCharsetMigration(tableV2.Name, []*Column{
  98. {Name: "type", Type: DB_NVarchar, Length: 255, Nullable: false},
  99. {Name: "name", Type: DB_NVarchar, Length: 190, Nullable: false},
  100. {Name: "access", Type: DB_NVarchar, Length: 255, Nullable: false},
  101. {Name: "url", Type: DB_NVarchar, Length: 255, Nullable: false},
  102. {Name: "password", Type: DB_NVarchar, Length: 255, Nullable: true},
  103. {Name: "user", Type: DB_NVarchar, Length: 255, Nullable: true},
  104. {Name: "database", Type: DB_NVarchar, Length: 255, Nullable: true},
  105. {Name: "basic_auth_user", Type: DB_NVarchar, Length: 255, Nullable: true},
  106. {Name: "basic_auth_password", Type: DB_NVarchar, Length: 255, Nullable: true},
  107. {Name: "json_data", Type: DB_Text, Nullable: true},
  108. {Name: "secure_json_data", Type: DB_Text, Nullable: true},
  109. }))
  110. const setVersionToOneWhereZero = `UPDATE data_source SET version = 1 WHERE version = 0`
  111. mg.AddMigration("Update initial version to 1", NewRawSqlMigration(setVersionToOneWhereZero))
  112. mg.AddMigration("Add read_only data column", NewAddColumnMigration(tableV2, &Column{
  113. Name: "read_only", Type: DB_Bool, Nullable: true,
  114. }))
  115. const migrateLoggingToLoki = `UPDATE data_source SET type = 'loki' WHERE type = 'logging'`
  116. mg.AddMigration("Migrate logging ds to loki ds", NewRawSqlMigration(migrateLoggingToLoki))
  117. }