Best Testkube code snippet using migrator.Add
tables.go
Source:tables.go
2import (3 "fmt"4 "github.com/grafana/grafana/pkg/services/sqlstore/migrator"5)6// AddMigration defines database migrations.7func AddTablesMigrations(mg *migrator.Migrator) {8 AddAlertDefinitionMigrations(mg, 60)9 AddAlertDefinitionVersionMigrations(mg)10 // Create alert_instance table11 AlertInstanceMigration(mg)12 // Create alert_rule13 AddAlertRuleMigrations(mg, 60)14 AddAlertRuleVersionMigrations(mg)15 // Create Alertmanager configurations16 AddAlertmanagerConfigMigrations(mg)17 // Create Admin Configuration18 AddAlertAdminConfigMigrations(mg)19}20// AddAlertDefinitionMigrations should not be modified.21func AddAlertDefinitionMigrations(mg *migrator.Migrator, defaultIntervalSeconds int64) {22 mg.AddMigration("delete alert_definition table", migrator.NewDropTableMigration("alert_definition"))23 alertDefinition := migrator.Table{24 Name: "alert_definition",25 Columns: []*migrator.Column{26 {Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},27 {Name: "org_id", Type: migrator.DB_BigInt, Nullable: false},28 {Name: "title", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},29 {Name: "condition", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},30 {Name: "data", Type: migrator.DB_Text, Nullable: false},31 {Name: "updated", Type: migrator.DB_DateTime, Nullable: false},32 {Name: "interval_seconds", Type: migrator.DB_BigInt, Nullable: false, Default: fmt.Sprintf("%d", defaultIntervalSeconds)},33 {Name: "version", Type: migrator.DB_Int, Nullable: false, Default: "0"},34 {Name: "uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false, Default: "0"},35 },36 Indices: []*migrator.Index{37 {Cols: []string{"org_id", "title"}, Type: migrator.IndexType},38 {Cols: []string{"org_id", "uid"}, Type: migrator.IndexType},39 },40 }41 // create table42 mg.AddMigration("recreate alert_definition table", migrator.NewAddTableMigration(alertDefinition))43 // create indices44 mg.AddMigration("add index in alert_definition on org_id and title columns", migrator.NewAddIndexMigration(alertDefinition, alertDefinition.Indices[0]))45 mg.AddMigration("add index in alert_definition on org_id and uid columns", migrator.NewAddIndexMigration(alertDefinition, alertDefinition.Indices[1]))46 mg.AddMigration("alter alert_definition table data column to mediumtext in mysql", migrator.NewRawSQLMigration("").47 Mysql("ALTER TABLE alert_definition MODIFY data MEDIUMTEXT;"))48 mg.AddMigration("drop index in alert_definition on org_id and title columns", migrator.NewDropIndexMigration(alertDefinition, alertDefinition.Indices[0]))49 mg.AddMigration("drop index in alert_definition on org_id and uid columns", migrator.NewDropIndexMigration(alertDefinition, alertDefinition.Indices[1]))50 uniqueIndices := []*migrator.Index{51 {Cols: []string{"org_id", "title"}, Type: migrator.UniqueIndex},52 {Cols: []string{"org_id", "uid"}, Type: migrator.UniqueIndex},53 }54 mg.AddMigration("add unique index in alert_definition on org_id and title columns", migrator.NewAddIndexMigration(alertDefinition, uniqueIndices[0]))55 mg.AddMigration("add unique index in alert_definition on org_id and uid columns", migrator.NewAddIndexMigration(alertDefinition, uniqueIndices[1]))56 mg.AddMigration("Add column paused in alert_definition", migrator.NewAddColumnMigration(alertDefinition, &migrator.Column{57 Name: "paused", Type: migrator.DB_Bool, Nullable: false, Default: "0",58 }))59 mg.AddMigration("drop alert_definition table", migrator.NewDropTableMigration("alert_definition"))60}61// AddAlertDefinitionMigrations should not be modified.62func AddAlertDefinitionVersionMigrations(mg *migrator.Migrator) {63 mg.AddMigration("delete alert_definition_version table", migrator.NewDropTableMigration("alert_definition_version"))64 alertDefinitionVersion := migrator.Table{65 Name: "alert_definition_version",66 Columns: []*migrator.Column{67 {Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},68 {Name: "alert_definition_id", Type: migrator.DB_BigInt},69 {Name: "alert_definition_uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false, Default: "0"},70 {Name: "parent_version", Type: migrator.DB_Int, Nullable: false},71 {Name: "restored_from", Type: migrator.DB_Int, Nullable: false},72 {Name: "version", Type: migrator.DB_Int, Nullable: false},73 {Name: "created", Type: migrator.DB_DateTime, Nullable: false},74 {Name: "title", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},75 {Name: "condition", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},76 {Name: "data", Type: migrator.DB_Text, Nullable: false},77 {Name: "interval_seconds", Type: migrator.DB_BigInt, Nullable: false},78 },79 Indices: []*migrator.Index{80 {Cols: []string{"alert_definition_id", "version"}, Type: migrator.UniqueIndex},81 {Cols: []string{"alert_definition_uid", "version"}, Type: migrator.UniqueIndex},82 },83 }84 mg.AddMigration("recreate alert_definition_version table", migrator.NewAddTableMigration(alertDefinitionVersion))85 mg.AddMigration("add index in alert_definition_version table on alert_definition_id and version columns", migrator.NewAddIndexMigration(alertDefinitionVersion, alertDefinitionVersion.Indices[0]))86 mg.AddMigration("add index in alert_definition_version table on alert_definition_uid and version columns", migrator.NewAddIndexMigration(alertDefinitionVersion, alertDefinitionVersion.Indices[1]))87 mg.AddMigration("alter alert_definition_version table data column to mediumtext in mysql", migrator.NewRawSQLMigration("").88 Mysql("ALTER TABLE alert_definition_version MODIFY data MEDIUMTEXT;"))89 mg.AddMigration("drop alert_definition_version table", migrator.NewDropTableMigration("alert_definition_version"))90}91func AlertInstanceMigration(mg *migrator.Migrator) {92 alertInstance := migrator.Table{93 Name: "alert_instance",94 Columns: []*migrator.Column{95 {Name: "def_org_id", Type: migrator.DB_BigInt, Nullable: false},96 {Name: "def_uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false, Default: "0"},97 {Name: "labels", Type: migrator.DB_Text, Nullable: false},98 {Name: "labels_hash", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},99 {Name: "current_state", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},100 {Name: "current_state_since", Type: migrator.DB_BigInt, Nullable: false},101 {Name: "last_eval_time", Type: migrator.DB_BigInt, Nullable: false},102 },103 PrimaryKeys: []string{"def_org_id", "def_uid", "labels_hash"},104 Indices: []*migrator.Index{105 {Cols: []string{"def_org_id", "def_uid", "current_state"}, Type: migrator.IndexType},106 {Cols: []string{"def_org_id", "current_state"}, Type: migrator.IndexType},107 },108 }109 // create table110 mg.AddMigration("create alert_instance table", migrator.NewAddTableMigration(alertInstance))111 mg.AddMigration("add index in alert_instance table on def_org_id, def_uid and current_state columns", migrator.NewAddIndexMigration(alertInstance, alertInstance.Indices[0]))112 mg.AddMigration("add index in alert_instance table on def_org_id, current_state columns", migrator.NewAddIndexMigration(alertInstance, alertInstance.Indices[1]))113 mg.AddMigration("add column current_state_end to alert_instance", migrator.NewAddColumnMigration(alertInstance, &migrator.Column{114 Name: "current_state_end", Type: migrator.DB_BigInt, Nullable: false, Default: "0",115 }))116 mg.AddMigration("remove index def_org_id, def_uid, current_state on alert_instance", migrator.NewDropIndexMigration(alertInstance, alertInstance.Indices[0]))117 mg.AddMigration("remove index def_org_id, current_state on alert_instance", migrator.NewDropIndexMigration(alertInstance, alertInstance.Indices[1]))118 mg.AddMigration("rename def_org_id to rule_org_id in alert_instance", migrator.NewRawSQLMigration("").119 Default("ALTER TABLE alert_instance RENAME COLUMN def_org_id TO rule_org_id;").120 Mysql("ALTER TABLE alert_instance CHANGE def_org_id rule_org_id BIGINT;"))121 mg.AddMigration("rename def_uid to rule_uid in alert_instance", migrator.NewRawSQLMigration("").122 Default("ALTER TABLE alert_instance RENAME COLUMN def_uid TO rule_uid;").123 Mysql("ALTER TABLE alert_instance CHANGE def_uid rule_uid VARCHAR(40);"))124 mg.AddMigration("add index rule_org_id, rule_uid, current_state on alert_instance", migrator.NewAddIndexMigration(alertInstance, &migrator.Index{125 Cols: []string{"rule_org_id", "rule_uid", "current_state"}, Type: migrator.IndexType,126 }))127 mg.AddMigration("add index rule_org_id, current_state on alert_instance", migrator.NewAddIndexMigration(alertInstance, &migrator.Index{128 Cols: []string{"rule_org_id", "current_state"}, Type: migrator.IndexType,129 }))130}131func AddAlertRuleMigrations(mg *migrator.Migrator, defaultIntervalSeconds int64) {132 alertRule := migrator.Table{133 Name: "alert_rule",134 Columns: []*migrator.Column{135 {Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},136 {Name: "org_id", Type: migrator.DB_BigInt, Nullable: false},137 {Name: "title", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},138 {Name: "condition", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},139 {Name: "data", Type: migrator.DB_Text, Nullable: false},140 {Name: "updated", Type: migrator.DB_DateTime, Nullable: false},141 {Name: "interval_seconds", Type: migrator.DB_BigInt, Nullable: false, Default: fmt.Sprintf("%d", defaultIntervalSeconds)},142 {Name: "version", Type: migrator.DB_Int, Nullable: false, Default: "0"},143 {Name: "uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false, Default: "0"},144 // the following fields will correspond to a dashboard (or folder) UIID145 {Name: "namespace_uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false},146 {Name: "rule_group", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},147 {Name: "no_data_state", Type: migrator.DB_NVarchar, Length: 15, Nullable: false, Default: "'NoData'"},148 {Name: "exec_err_state", Type: migrator.DB_NVarchar, Length: 15, Nullable: false, Default: "'Alerting'"},149 },150 Indices: []*migrator.Index{151 {Cols: []string{"org_id", "title"}, Type: migrator.UniqueIndex},152 {Cols: []string{"org_id", "uid"}, Type: migrator.UniqueIndex},153 {Cols: []string{"org_id", "namespace_uid", "rule_group"}, Type: migrator.IndexType},154 },155 }156 // create table157 mg.AddMigration("create alert_rule table", migrator.NewAddTableMigration(alertRule))158 // create indices159 mg.AddMigration("add index in alert_rule on org_id and title columns", migrator.NewAddIndexMigration(alertRule, alertRule.Indices[0]))160 mg.AddMigration("add index in alert_rule on org_id and uid columns", migrator.NewAddIndexMigration(alertRule, alertRule.Indices[1]))161 mg.AddMigration("add index in alert_rule on org_id, namespace_uid, group_uid columns", migrator.NewAddIndexMigration(alertRule, alertRule.Indices[2]))162 mg.AddMigration("alter alert_rule table data column to mediumtext in mysql", migrator.NewRawSQLMigration("").163 Mysql("ALTER TABLE alert_rule MODIFY data MEDIUMTEXT;"))164 // add for column165 mg.AddMigration("add column for to alert_rule", migrator.NewAddColumnMigration(alertRule, &migrator.Column{Name: "for", Type: migrator.DB_BigInt, Nullable: false, Default: "0"}))166 // add annotations column167 mg.AddMigration("add column annotations to alert_rule", migrator.NewAddColumnMigration(alertRule, &migrator.Column{Name: "annotations", Type: migrator.DB_Text, Nullable: true}))168 // add labels column169 mg.AddMigration("add column labels to alert_rule", migrator.NewAddColumnMigration(alertRule, &migrator.Column{Name: "labels", Type: migrator.DB_Text, Nullable: true}))170 mg.AddMigration("remove unique index from alert_rule on org_id, title columns", migrator.NewDropIndexMigration(alertRule, &migrator.Index{171 Cols: []string{"org_id", "title"}, Type: migrator.UniqueIndex,172 }))173 mg.AddMigration("add index in alert_rule on org_id, namespase_uid and title columns", migrator.NewAddIndexMigration(alertRule, &migrator.Index{174 Cols: []string{"org_id", "namespace_uid", "title"}, Type: migrator.UniqueIndex,175 }))176 mg.AddMigration("add dashboard_uid column to alert_rule", migrator.NewAddColumnMigration(177 migrator.Table{Name: "alert_rule"},178 &migrator.Column{179 Name: "dashboard_uid",180 Type: migrator.DB_NVarchar,181 Length: 40,182 Nullable: true,183 },184 ))185 mg.AddMigration("add panel_id column to alert_rule", migrator.NewAddColumnMigration(186 migrator.Table{Name: "alert_rule"},187 &migrator.Column{188 Name: "panel_id",189 Type: migrator.DB_BigInt,190 Nullable: true,191 },192 ))193 mg.AddMigration("add index in alert_rule on org_id, dashboard_uid and panel_id columns", migrator.NewAddIndexMigration(194 migrator.Table{Name: "alert_rule"},195 &migrator.Index{196 Name: "IDX_alert_rule_org_id_dashboard_uid_panel_id",197 Cols: []string{"org_id", "dashboard_uid", "panel_id"},198 },199 ))200}201func AddAlertRuleVersionMigrations(mg *migrator.Migrator) {202 alertRuleVersion := migrator.Table{203 Name: "alert_rule_version",204 Columns: []*migrator.Column{205 {Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},206 {Name: "rule_org_id", Type: migrator.DB_BigInt},207 {Name: "rule_uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false, Default: "0"},208 // the following fields will correspond to a dashboard (or folder) UID209 {Name: "rule_namespace_uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false},210 {Name: "rule_group", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},211 {Name: "parent_version", Type: migrator.DB_Int, Nullable: false},212 {Name: "restored_from", Type: migrator.DB_Int, Nullable: false},213 {Name: "version", Type: migrator.DB_Int, Nullable: false},214 {Name: "created", Type: migrator.DB_DateTime, Nullable: false},215 {Name: "title", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},216 {Name: "condition", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},217 {Name: "data", Type: migrator.DB_Text, Nullable: false},218 {Name: "interval_seconds", Type: migrator.DB_BigInt, Nullable: false},219 {Name: "no_data_state", Type: migrator.DB_NVarchar, Length: 15, Nullable: false, Default: "'NoData'"},220 {Name: "exec_err_state", Type: migrator.DB_NVarchar, Length: 15, Nullable: false, Default: "'Alerting'"},221 },222 Indices: []*migrator.Index{223 {Cols: []string{"rule_org_id", "rule_uid", "version"}, Type: migrator.UniqueIndex},224 {Cols: []string{"rule_org_id", "rule_namespace_uid", "rule_group"}, Type: migrator.IndexType},225 },226 }227 mg.AddMigration("create alert_rule_version table", migrator.NewAddTableMigration(alertRuleVersion))228 mg.AddMigration("add index in alert_rule_version table on rule_org_id, rule_uid and version columns", migrator.NewAddIndexMigration(alertRuleVersion, alertRuleVersion.Indices[0]))229 mg.AddMigration("add index in alert_rule_version table on rule_org_id, rule_namespace_uid and rule_group columns", migrator.NewAddIndexMigration(alertRuleVersion, alertRuleVersion.Indices[1]))230 mg.AddMigration("alter alert_rule_version table data column to mediumtext in mysql", migrator.NewRawSQLMigration("").231 Mysql("ALTER TABLE alert_rule_version MODIFY data MEDIUMTEXT;"))232 // add for column233 mg.AddMigration("add column for to alert_rule_version", migrator.NewAddColumnMigration(alertRuleVersion, &migrator.Column{Name: "for", Type: migrator.DB_BigInt, Nullable: false, Default: "0"}))234 // add annotations column235 mg.AddMigration("add column annotations to alert_rule_version", migrator.NewAddColumnMigration(alertRuleVersion, &migrator.Column{Name: "annotations", Type: migrator.DB_Text, Nullable: true}))236 // add labels column237 mg.AddMigration("add column labels to alert_rule_version", migrator.NewAddColumnMigration(alertRuleVersion, &migrator.Column{Name: "labels", Type: migrator.DB_Text, Nullable: true}))238}239func AddAlertmanagerConfigMigrations(mg *migrator.Migrator) {240 alertConfiguration := migrator.Table{241 Name: "alert_configuration",242 Columns: []*migrator.Column{243 {Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},244 {Name: "alertmanager_configuration", Type: migrator.DB_Text, Nullable: false},245 {Name: "configuration_version", Type: migrator.DB_NVarchar, Length: 3}, // In a format of vXX e.g. v1, v2, v10, etc246 {Name: "created_at", Type: migrator.DB_Int, Nullable: false},247 },248 }249 mg.AddMigration("create_alert_configuration_table", migrator.NewAddTableMigration(alertConfiguration))250 mg.AddMigration("Add column default in alert_configuration", migrator.NewAddColumnMigration(alertConfiguration, &migrator.Column{251 Name: "default", Type: migrator.DB_Bool, Nullable: false, Default: "0",252 }))253 mg.AddMigration("alert alert_configuration alertmanager_configuration column from TEXT to MEDIUMTEXT if mysql", migrator.NewRawSQLMigration("").254 Mysql("ALTER TABLE alert_configuration MODIFY alertmanager_configuration MEDIUMTEXT;"))255 mg.AddMigration("add column org_id in alert_configuration", migrator.NewAddColumnMigration(alertConfiguration, &migrator.Column{256 Name: "org_id", Type: migrator.DB_BigInt, Nullable: false, Default: "0",257 }))258 // add index on org_id259 mg.AddMigration("add index in alert_configuration table on org_id column", migrator.NewAddIndexMigration(alertConfiguration, &migrator.Index{260 Cols: []string{"org_id"},261 }))262}263func AddAlertAdminConfigMigrations(mg *migrator.Migrator) {264 adminConfiguration := migrator.Table{265 Name: "ngalert_configuration",266 Columns: []*migrator.Column{267 {Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},268 {Name: "org_id", Type: migrator.DB_BigInt, Nullable: false},269 {Name: "alertmanagers", Type: migrator.DB_Text, Nullable: true},270 {Name: "created_at", Type: migrator.DB_Int, Nullable: false},271 {Name: "updated_at", Type: migrator.DB_Int, Nullable: false},272 },273 Indices: []*migrator.Index{274 {Cols: []string{"org_id"}, Type: migrator.UniqueIndex},275 },276 }277 mg.AddMigration("create_ngalert_configuration_table", migrator.NewAddTableMigration(adminConfiguration))278 mg.AddMigration("add index in ngalert_configuration on org_id column", migrator.NewAddIndexMigration(adminConfiguration, adminConfiguration.Indices[0]))279 mg.AddMigration("add column send_alerts_to in ngalert_configuration", migrator.NewAddColumnMigration(adminConfiguration, &migrator.Column{280 Name: "send_alerts_to", Type: migrator.DB_SmallInt, Nullable: false, Default: "0",281 }))282}...
migrations.go
Source:migrations.go
1package accesscontrol2import "github.com/grafana/grafana/pkg/services/sqlstore/migrator"3func AddMigration(mg *migrator.Migrator) {4 permissionV1 := migrator.Table{5 Name: "permission",6 Columns: []*migrator.Column{7 {Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},8 {Name: "role_id", Type: migrator.DB_BigInt},9 {Name: "action", Type: migrator.DB_Varchar, Length: 190, Nullable: false},10 {Name: "scope", Type: migrator.DB_Varchar, Length: 190, Nullable: false},11 {Name: "created", Type: migrator.DB_DateTime, Nullable: false},12 {Name: "updated", Type: migrator.DB_DateTime, Nullable: false},13 },14 Indices: []*migrator.Index{15 {Cols: []string{"role_id"}},16 {Cols: []string{"role_id", "action", "scope"}, Type: migrator.UniqueIndex},17 },18 }19 mg.AddMigration("create permission table", migrator.NewAddTableMigration(permissionV1))20 //------- indexes ------------------21 mg.AddMigration("add unique index permission.role_id", migrator.NewAddIndexMigration(permissionV1, permissionV1.Indices[0]))22 mg.AddMigration("add unique index role_id_action_scope", migrator.NewAddIndexMigration(permissionV1, permissionV1.Indices[1]))23 roleV1 := migrator.Table{24 Name: "role",25 Columns: []*migrator.Column{26 {Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},27 {Name: "name", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},28 {Name: "description", Type: migrator.DB_Text, Nullable: true},29 {Name: "version", Type: migrator.DB_BigInt, Nullable: false},30 {Name: "org_id", Type: migrator.DB_BigInt},31 {Name: "uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false},32 {Name: "created", Type: migrator.DB_DateTime, Nullable: false},33 {Name: "updated", Type: migrator.DB_DateTime, Nullable: false},34 },35 Indices: []*migrator.Index{36 {Cols: []string{"org_id"}},37 {Cols: []string{"org_id", "name"}, Type: migrator.UniqueIndex},38 {Cols: []string{"org_id", "uid"}, Type: migrator.UniqueIndex},39 },40 }41 mg.AddMigration("create role table", migrator.NewAddTableMigration(roleV1))42 mg.AddMigration("add column display_name", migrator.NewAddColumnMigration(roleV1, &migrator.Column{43 Name: "display_name", Type: migrator.DB_NVarchar, Length: 190, Nullable: true,44 }))45 mg.AddMigration("add column group_name", migrator.NewAddColumnMigration(roleV1, &migrator.Column{46 Name: "group_name", Type: migrator.DB_NVarchar, Length: 190, Nullable: true,47 }))48 //------- indexes ------------------49 mg.AddMigration("add index role.org_id", migrator.NewAddIndexMigration(roleV1, roleV1.Indices[0]))50 mg.AddMigration("add unique index role_org_id_name", migrator.NewAddIndexMigration(roleV1, roleV1.Indices[1]))51 mg.AddMigration("add index role_org_id_uid", migrator.NewAddIndexMigration(roleV1, roleV1.Indices[2]))52 teamRoleV1 := migrator.Table{53 Name: "team_role",54 Columns: []*migrator.Column{55 {Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},56 {Name: "org_id", Type: migrator.DB_BigInt},57 {Name: "team_id", Type: migrator.DB_BigInt},58 {Name: "role_id", Type: migrator.DB_BigInt},59 {Name: "created", Type: migrator.DB_DateTime, Nullable: false},60 },61 Indices: []*migrator.Index{62 {Cols: []string{"org_id"}},63 {Cols: []string{"org_id", "team_id", "role_id"}, Type: migrator.UniqueIndex},64 {Cols: []string{"team_id"}},65 },66 }67 mg.AddMigration("create team role table", migrator.NewAddTableMigration(teamRoleV1))68 //------- indexes ------------------69 mg.AddMigration("add index team_role.org_id", migrator.NewAddIndexMigration(teamRoleV1, teamRoleV1.Indices[0]))70 mg.AddMigration("add unique index team_role_org_id_team_id_role_id", migrator.NewAddIndexMigration(teamRoleV1, teamRoleV1.Indices[1]))71 mg.AddMigration("add index team_role.team_id", migrator.NewAddIndexMigration(teamRoleV1, teamRoleV1.Indices[2]))72 userRoleV1 := migrator.Table{73 Name: "user_role",74 Columns: []*migrator.Column{75 {Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},76 {Name: "org_id", Type: migrator.DB_BigInt},77 {Name: "user_id", Type: migrator.DB_BigInt},78 {Name: "role_id", Type: migrator.DB_BigInt},79 {Name: "created", Type: migrator.DB_DateTime, Nullable: false},80 },81 Indices: []*migrator.Index{82 {Cols: []string{"org_id"}},83 {Cols: []string{"org_id", "user_id", "role_id"}, Type: migrator.UniqueIndex},84 {Cols: []string{"user_id"}},85 },86 }87 mg.AddMigration("create user role table", migrator.NewAddTableMigration(userRoleV1))88 //------- indexes ------------------89 mg.AddMigration("add index user_role.org_id", migrator.NewAddIndexMigration(userRoleV1, userRoleV1.Indices[0]))90 mg.AddMigration("add unique index user_role_org_id_user_id_role_id", migrator.NewAddIndexMigration(userRoleV1, userRoleV1.Indices[1]))91 mg.AddMigration("add index user_role.user_id", migrator.NewAddIndexMigration(userRoleV1, userRoleV1.Indices[2]))92 builtinRoleV1 := migrator.Table{93 Name: "builtin_role",94 Columns: []*migrator.Column{95 {Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},96 {Name: "role", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},97 {Name: "role_id", Type: migrator.DB_BigInt},98 {Name: "created", Type: migrator.DB_DateTime, Nullable: false},99 {Name: "updated", Type: migrator.DB_DateTime, Nullable: false},100 },101 Indices: []*migrator.Index{102 {Cols: []string{"role_id"}},103 {Cols: []string{"role"}},104 },105 }106 mg.AddMigration("create builtin role table", migrator.NewAddTableMigration(builtinRoleV1))107 //------- indexes ------------------108 mg.AddMigration("add index builtin_role.role_id", migrator.NewAddIndexMigration(builtinRoleV1, builtinRoleV1.Indices[0]))109 mg.AddMigration("add index builtin_role.name", migrator.NewAddIndexMigration(builtinRoleV1, builtinRoleV1.Indices[1]))110 // Add org_id column to the builtin_role table111 mg.AddMigration("Add column org_id to builtin_role table", migrator.NewAddColumnMigration(builtinRoleV1, &migrator.Column{112 Name: "org_id", Type: migrator.DB_BigInt, Default: "0",113 }))114 mg.AddMigration("add index builtin_role.org_id", migrator.NewAddIndexMigration(builtinRoleV1, &migrator.Index{115 Cols: []string{"org_id"},116 }))117 mg.AddMigration("add unique index builtin_role_org_id_role_id_role", migrator.NewAddIndexMigration(builtinRoleV1, &migrator.Index{118 Cols: []string{"org_id", "role_id", "role"}, Type: migrator.UniqueIndex,119 }))120 // Make role.uid unique across Grafana instance121 mg.AddMigration("Remove unique index role_org_id_uid", migrator.NewDropIndexMigration(roleV1, &migrator.Index{122 Cols: []string{"org_id", "uid"}, Type: migrator.UniqueIndex,123 }))124 mg.AddMigration("add unique index role.uid", migrator.NewAddIndexMigration(roleV1, &migrator.Index{125 Cols: []string{"uid"}, Type: migrator.UniqueIndex,126 }))127 seedAssignmentV1 := migrator.Table{128 Name: "seed_assignment",129 Columns: []*migrator.Column{130 {Name: "builtin_role", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},131 {Name: "role_name", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},132 },133 Indices: []*migrator.Index{134 {Cols: []string{"builtin_role", "role_name"}, Type: migrator.UniqueIndex},135 },136 }137 mg.AddMigration("create seed assignment table", migrator.NewAddTableMigration(seedAssignmentV1))138 //------- indexes ------------------139 mg.AddMigration("add unique index builtin_role_role_name", migrator.NewAddIndexMigration(seedAssignmentV1, seedAssignmentV1.Indices[0]))140}...
database_mig.go
Source:database_mig.go
...3 "fmt"4 "github.com/grafana/grafana/pkg/services/sqlstore/migrator"5)6func addAlertDefinitionMigrations(mg *migrator.Migrator) {7 mg.AddMigration("delete alert_definition table", migrator.NewDropTableMigration("alert_definition"))8 alertDefinition := migrator.Table{9 Name: "alert_definition",10 Columns: []*migrator.Column{11 {Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},12 {Name: "org_id", Type: migrator.DB_BigInt, Nullable: false},13 {Name: "title", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},14 {Name: "condition", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},15 {Name: "data", Type: migrator.DB_Text, Nullable: false},16 {Name: "updated", Type: migrator.DB_DateTime, Nullable: false},17 {Name: "interval_seconds", Type: migrator.DB_BigInt, Nullable: false, Default: fmt.Sprintf("%d", defaultIntervalSeconds)},18 {Name: "version", Type: migrator.DB_Int, Nullable: false, Default: "0"},19 {Name: "uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false, Default: "0"},20 },21 Indices: []*migrator.Index{22 {Cols: []string{"org_id", "title"}, Type: migrator.IndexType},23 {Cols: []string{"org_id", "uid"}, Type: migrator.IndexType},24 },25 }26 // create table27 mg.AddMigration("recreate alert_definition table", migrator.NewAddTableMigration(alertDefinition))28 // create indices29 mg.AddMigration("add index in alert_definition on org_id and title columns", migrator.NewAddIndexMigration(alertDefinition, alertDefinition.Indices[0]))30 mg.AddMigration("add index in alert_definition on org_id and uid columns", migrator.NewAddIndexMigration(alertDefinition, alertDefinition.Indices[1]))31 mg.AddMigration("alter alert_definition table data column to mediumtext in mysql", migrator.NewRawSQLMigration("").32 Mysql("ALTER TABLE alert_definition MODIFY data MEDIUMTEXT;"))33 mg.AddMigration("drop index in alert_definition on org_id and title columns", migrator.NewDropIndexMigration(alertDefinition, alertDefinition.Indices[0]))34 mg.AddMigration("drop index in alert_definition on org_id and uid columns", migrator.NewDropIndexMigration(alertDefinition, alertDefinition.Indices[1]))35 uniqueIndices := []*migrator.Index{36 {Cols: []string{"org_id", "title"}, Type: migrator.UniqueIndex},37 {Cols: []string{"org_id", "uid"}, Type: migrator.UniqueIndex},38 }39 mg.AddMigration("add unique index in alert_definition on org_id and title columns", migrator.NewAddIndexMigration(alertDefinition, uniqueIndices[0]))40 mg.AddMigration("add unique index in alert_definition on org_id and uid columns", migrator.NewAddIndexMigration(alertDefinition, uniqueIndices[1]))41 mg.AddMigration("Add column paused in alert_definition", migrator.NewAddColumnMigration(alertDefinition, &migrator.Column{42 Name: "paused", Type: migrator.DB_Bool, Nullable: false, Default: "0",43 }))44}45func addAlertDefinitionVersionMigrations(mg *migrator.Migrator) {46 mg.AddMigration("delete alert_definition_version table", migrator.NewDropTableMigration("alert_definition_version"))47 alertDefinitionVersion := migrator.Table{48 Name: "alert_definition_version",49 Columns: []*migrator.Column{50 {Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},51 {Name: "alert_definition_id", Type: migrator.DB_BigInt},52 {Name: "alert_definition_uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false, Default: "0"},53 {Name: "parent_version", Type: migrator.DB_Int, Nullable: false},54 {Name: "restored_from", Type: migrator.DB_Int, Nullable: false},55 {Name: "version", Type: migrator.DB_Int, Nullable: false},56 {Name: "created", Type: migrator.DB_DateTime, Nullable: false},57 {Name: "title", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},58 {Name: "condition", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},59 {Name: "data", Type: migrator.DB_Text, Nullable: false},60 {Name: "interval_seconds", Type: migrator.DB_BigInt, Nullable: false},61 },62 Indices: []*migrator.Index{63 {Cols: []string{"alert_definition_id", "version"}, Type: migrator.UniqueIndex},64 {Cols: []string{"alert_definition_uid", "version"}, Type: migrator.UniqueIndex},65 },66 }67 mg.AddMigration("recreate alert_definition_version table", migrator.NewAddTableMigration(alertDefinitionVersion))68 mg.AddMigration("add index in alert_definition_version table on alert_definition_id and version columns", migrator.NewAddIndexMigration(alertDefinitionVersion, alertDefinitionVersion.Indices[0]))69 mg.AddMigration("add index in alert_definition_version table on alert_definition_uid and version columns", migrator.NewAddIndexMigration(alertDefinitionVersion, alertDefinitionVersion.Indices[1]))70 mg.AddMigration("alter alert_definition_version table data column to mediumtext in mysql", migrator.NewRawSQLMigration("").71 Mysql("ALTER TABLE alert_definition_version MODIFY data MEDIUMTEXT;"))72}73func alertInstanceMigration(mg *migrator.Migrator) {74 alertInstance := migrator.Table{75 Name: "alert_instance",76 Columns: []*migrator.Column{77 {Name: "def_org_id", Type: migrator.DB_BigInt, Nullable: false},78 {Name: "def_uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false, Default: "0"},79 {Name: "labels", Type: migrator.DB_Text, Nullable: false},80 {Name: "labels_hash", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},81 {Name: "current_state", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},82 {Name: "current_state_since", Type: migrator.DB_BigInt, Nullable: false},83 {Name: "last_eval_time", Type: migrator.DB_BigInt, Nullable: false},84 },85 PrimaryKeys: []string{"def_org_id", "def_uid", "labels_hash"},86 Indices: []*migrator.Index{87 {Cols: []string{"def_org_id", "def_uid", "current_state"}, Type: migrator.IndexType},88 {Cols: []string{"def_org_id", "current_state"}, Type: migrator.IndexType},89 },90 }91 // create table92 mg.AddMigration("create alert_instance table", migrator.NewAddTableMigration(alertInstance))93 mg.AddMigration("add index in alert_instance table on def_org_id, def_uid and current_state columns", migrator.NewAddIndexMigration(alertInstance, alertInstance.Indices[0]))94 mg.AddMigration("add index in alert_instance table on def_org_id, current_state columns", migrator.NewAddIndexMigration(alertInstance, alertInstance.Indices[1]))95}...
Add
Using AI Code Generation
1import (2func main() {3 db, err := gorm.Open("sqlite3", "test.db")4 if err != nil {5 panic("failed to connect database")6 }7 defer db.Close()8 db.AutoMigrate(&Product{})9 db.Create(&Product{Code: "L1212", Price: 1000})10 db.Model(&product).Update("Price", 2000)11 db.Delete(&product)12}13type Product struct {14}15import (16func main() {17 db, err := gorm.Open("sqlite3", "test.db")18 if err != nil {19 panic("failed to connect database")20 }21 defer db.Close()22 db.AutoMigrate(&Product{})23 db.Create(&Product{Code: "L1212", Price: 1000})24 db.Model(&product).Update("Price", 2000)25 db.Delete(&product)26}27type Product struct {28}29import (
Add
Using AI Code Generation
1import (2type User struct {3}4func main() {5 db, err := gorm.Open("mysql", "root:@/gorm?charset=utf8&parseTime=True&loc=Local")6 if err != nil {7 panic("failed to connect database")8 }9 defer db.Close()10 migrator := db.Migrator()11 migrator.AddConstraint(&User{}, "unique_name", "unique(name)")12}13mysql> desc users;14| id | int(10) unsigned | NO | PRI | NULL | auto_increment |15| name | varchar(255) | YES | UNI | NULL | |16| age | int(11) | YES | | NULL | |17| unique_name_index | int(11) | YES | UNI | NULL | |187 rows in set (0.00 sec)19mysql> show index from users;
Add
Using AI Code Generation
1import (2type User struct {3}4func main() {5 db, err := gorm.Open("mysql", "root:root@/gorm?charset=utf8&parseTime=True&loc=Local")6 if err != nil {7 log.Fatal(err)8 }9 defer db.Close()10 db.AutoMigrate(&User{})11 db.Create(&User{Name: "Jinzhu", Email: "
Add
Using AI Code Generation
1migrator.Add("2.go")2migrator.Add("3.go")3migrator.Add("4.go")4migrator.Add("5.go")5migrator.Add("6.go")6migrator.Add("7.go")7migrator.Add("8.go")8migrator.Add("9.go")9migrator.Add("10.go")10migrator.Add("11.go")11migrator.Add("12.go")12migrator.Add("13.go")13migrator.Add("14.go")14migrator.Add("15.go")15migrator.Add("16.go")
Add
Using AI Code Generation
1migrator.Add("add_column", "table_name", "column_name", "datatype")2migrator.Drop("drop_column", "table_name", "column_name")3migrator.Change("change_column", "table_name", "column_name", "datatype")4migrator.Rename("rename_column", "table_name", "old_column_name", "new_column_name")5migrator.AddIndex("add_index", "table_name", "column_name")6migrator.RemoveIndex("remove_index", "table_name", "column_name")7migrator.AddForeignKey("add_foreign_key", "table_name", "column_name", "reference_table_name", "reference_column_name")8migrator.RemoveForeignKey("remove_foreign_key", "table_name", "column_name")9migrator.CreateTable("create_table", "table_name")10migrator.DropTable("drop_table", "table_name")11migrator.RenameTable("rename_table", "table_name", "new_table_name")12migrator.AddColumn("add_column", "table_name", "column_name", "datatype")13migrator.DropColumn("drop_column", "table_name", "column_name")14migrator.RenameColumn("rename_column", "table_name", "old_column_name", "new_column_name")15migrator.ChangeColumn("change_column", "table_name", "column_name", "datatype")16migrator.AddIndex("add_index", "table_name", "column_name")
Add
Using AI Code Generation
1migrator.Add("users", "name", "string", "unique:true")2migrator.DropColumn("users", "name")3migrator.RenameColumn("users", "name", "username")4migrator.RenameTable("users", "accounts")5migrator.DropTable("users")6migrator.CreateTable("users")7migrator.AddIndex("users", "name", "unique:true")8migrator.RemoveIndex("users", "name")9migrator.AddForeignKey("users", "account_id", "accounts(id)", "cascade", "cascade")10migrator.RemoveForeignKey("users", "account_id")11migrator.DropTableIfExists("users")12migrator.HasTable("users")13migrator.HasColumn("users", "name")14migrator.HasIndex("users", "name")15migrator.HasForeignKey("users", "account_id")16migrator.Create("users", func(table *Table) {17 table.Increments("id")18 table.String("name")19})20migrator.AlterTable("users", func(table *Table) {21 table.RenameColumn("name", "username")22})23migrator.DropTable("users")24migrator.CreateTable("users")
Add
Using AI Code Generation
1migrator.Add("CreateUser", "create table user (id int, name varchar(20))")2migrator.Add("CreatePost", "create table post (id int, title varchar(20))")3migrator.Add("CreateComment", "create table comment (id int, content varchar(20))")4migrator.Migrate()5migrator.Add("AddIndexToUser", "create index idx_user_name on user(name)")6migrator.Migrate()7migrator.Add("AddIndexToPost", "create index idx_post_title on post(title)")8migrator.Migrate()9migrator.Add("AddIndexToComment", "create index idx_comment_content on comment(content)")10migrator.Migrate()11migrator.Rollback()12migrator.Rollback()13migrator.Rollback()14migrator.Rollback()15migrator.Rollback()16migrator.Rollback()17migrator.Rollback()
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!