How to use init method of migrations Package

Best Testkube code snippet using migrations.init

gormigrate.go

Source:gormigrate.go Github

copy

Full Screen

...6 "gorm.io/gorm"7 "gorm.io/gorm/logger"8)9const (10 initSchemaMigrationID = "SCHEMA_INIT"11)12// MigrateFunc is the func signature for migrating.13type MigrateFunc func(*gorm.DB) error14// RollbackFunc is the func signature for rollbacking.15type RollbackFunc func(*gorm.DB) error16// InitSchemaFunc is the func signature for initializing the schema.17type InitSchemaFunc func(*gorm.DB) error18// Options define options for all migrations.19type Options struct {20 // TableName is the migration table.21 TableName string22 // IDColumnName is the name of column where the migration id will be stored.23 IDColumnName string24 // IDColumnSize is the length of the migration id column25 IDColumnSize int26 // UseTransaction makes Gormigrate execute migrations inside a single transaction.27 // Keep in mind that not all databases support DDL commands inside transactions.28 UseTransaction bool29 // ValidateUnknownMigrations will cause migrate to fail if there's unknown migration30 // IDs in the database31 ValidateUnknownMigrations bool32 // Logger is used for reporting high-level progress.33 Logger logger.Interface34}35// Migration represents a database migration (a modification to be made on the database).36type Migration struct {37 // ID is the migration identifier. Usually a timestamp like "201601021504".38 ID string39 // Migrate is a function that will br executed while running this migration.40 Migrate MigrateFunc41 // Rollback will be executed on rollback. Can be nil.42 Rollback RollbackFunc43}44// Gormigrate represents a collection of all migrations of a database schema.45type Gormigrate struct {46 db *gorm.DB47 tx *gorm.DB48 options *Options49 migrations []*Migration50 initSchema InitSchemaFunc51}52// ReservedIDError is returned when a migration is using a reserved ID53type ReservedIDError struct {54 ID string55}56func (e *ReservedIDError) Error() string {57 return fmt.Sprintf(`gormigrate: Reserved migration ID: "%s"`, e.ID)58}59// DuplicatedIDError is returned when more than one migration have the same ID60type DuplicatedIDError struct {61 ID string62}63func (e *DuplicatedIDError) Error() string {64 return fmt.Sprintf(`gormigrate: Duplicated migration ID: "%s"`, e.ID)65}66var (67 // DefaultOptions can be used if you don't want to think about options.68 DefaultOptions = &Options{69 TableName: "migrations",70 IDColumnName: "id",71 IDColumnSize: 255,72 UseTransaction: false,73 ValidateUnknownMigrations: false,74 }75 // ErrRollbackImpossible is returned when trying to rollback a migration76 // that has no rollback function.77 ErrRollbackImpossible = errors.New("gormigrate: It's impossible to rollback this migration")78 // ErrNoMigrationDefined is returned when no migration is defined.79 ErrNoMigrationDefined = errors.New("gormigrate: No migration defined")80 // ErrMissingID is returned when the ID od migration is equal to ""81 ErrMissingID = errors.New("gormigrate: Missing ID in migration")82 // ErrNoRunMigration is returned when any run migration was found while83 // running RollbackLast84 ErrNoRunMigration = errors.New("gormigrate: Could not find last run migration")85 // ErrMigrationIDDoesNotExist is returned when migrating or rolling back to a migration ID that86 // does not exist in the list of migrations87 ErrMigrationIDDoesNotExist = errors.New("gormigrate: Tried to migrate to an ID that doesn't exist")88 // ErrUnknownPastMigration is returned if a migration exists in the DB that doesn't exist in the code89 ErrUnknownPastMigration = errors.New("gormigrate: Found migration in DB that does not exist in code")90)91// New returns a new Gormigrate.92func New(db *gorm.DB, options *Options, migrations []*Migration) *Gormigrate {93 if options.TableName == "" {94 options.TableName = DefaultOptions.TableName95 }96 if options.IDColumnName == "" {97 options.IDColumnName = DefaultOptions.IDColumnName98 }99 if options.IDColumnSize == 0 {100 options.IDColumnSize = DefaultOptions.IDColumnSize101 }102 if options.Logger == nil {103 options.Logger = logger.Default.LogMode(logger.Info)104 }105 return &Gormigrate{106 db: db,107 options: options,108 migrations: migrations,109 }110}111// InitSchema sets a function that is run if no migration is found.112// The idea is preventing to run all migrations when a new clean database113// is being migrating. In this function you should create all tables and114// foreign key necessary to your application.115func (g *Gormigrate) InitSchema(initSchema InitSchemaFunc) {116 g.initSchema = initSchema117}118// Migrate executes all migrations that did not run yet.119func (g *Gormigrate) Migrate() error {120 if !g.hasMigrations() {121 return ErrNoMigrationDefined122 }123 var targetMigrationID string124 if len(g.migrations) > 0 {125 targetMigrationID = g.migrations[len(g.migrations)-1].ID126 }127 return g.migrate(targetMigrationID)128}129// MigrateTo executes all migrations that did not run yet up to the migration that matches `migrationID`.130func (g *Gormigrate) MigrateTo(migrationID string) error {131 if err := g.checkIDExist(migrationID); err != nil {132 return err133 }134 return g.migrate(migrationID)135}136func (g *Gormigrate) migrate(migrationID string) error {137 if !g.hasMigrations() {138 return ErrNoMigrationDefined139 }140 if err := g.checkReservedID(); err != nil {141 return err142 }143 if err := g.checkDuplicatedID(); err != nil {144 return err145 }146 g.begin()147 defer g.rollback()148 if err := g.createMigrationTableIfNotExists(); err != nil {149 return err150 }151 if g.options.ValidateUnknownMigrations {152 unknownMigrations, err := g.unknownMigrationsHaveHappened()153 if err != nil {154 return err155 }156 if unknownMigrations {157 return ErrUnknownPastMigration158 }159 }160 if g.initSchema != nil {161 canInitializeSchema, err := g.canInitializeSchema()162 if err != nil {163 return err164 }165 if canInitializeSchema {166 if err := g.runInitSchema(); err != nil {167 return err168 }169 return g.commit()170 }171 }172 for _, migration := range g.migrations {173 if err := g.runMigration(migration); err != nil {174 return err175 }176 if migrationID != "" && migration.ID == migrationID {177 break178 }179 }180 return g.commit()181}182// There are migrations to apply if either there's a defined183// initSchema function or if the list of migrations is not empty.184func (g *Gormigrate) hasMigrations() bool {185 return g.initSchema != nil || len(g.migrations) > 0186}187// Check whether any migration is using a reserved ID.188// For now there's only have one reserved ID, but there may be more in the future.189func (g *Gormigrate) checkReservedID() error {190 for _, m := range g.migrations {191 if m.ID == initSchemaMigrationID {192 return &ReservedIDError{ID: m.ID}193 }194 }195 return nil196}197func (g *Gormigrate) checkDuplicatedID() error {198 lookup := make(map[string]struct{}, len(g.migrations))199 for _, m := range g.migrations {200 if _, ok := lookup[m.ID]; ok {201 return &DuplicatedIDError{ID: m.ID}202 }203 lookup[m.ID] = struct{}{}204 }205 return nil206}207func (g *Gormigrate) checkIDExist(migrationID string) error {208 for _, migrate := range g.migrations {209 if migrate.ID == migrationID {210 return nil211 }212 }213 return ErrMigrationIDDoesNotExist214}215// RollbackLast undo the last migration216func (g *Gormigrate) RollbackLast() error {217 if len(g.migrations) == 0 {218 return ErrNoMigrationDefined219 }220 g.begin()221 defer g.rollback()222 lastRunMigration, err := g.getLastRunMigration()223 if err != nil {224 return err225 }226 if err := g.rollbackMigration(lastRunMigration); err != nil {227 return err228 }229 return g.commit()230}231// RollbackTo undoes migrations up to the given migration that matches the `migrationID`.232// Migration with the matching `migrationID` is not rolled back.233func (g *Gormigrate) RollbackTo(migrationID string) error {234 if len(g.migrations) == 0 {235 return ErrNoMigrationDefined236 }237 if err := g.checkIDExist(migrationID); err != nil {238 return err239 }240 g.begin()241 defer g.rollback()242 for i := len(g.migrations) - 1; i >= 0; i-- {243 migration := g.migrations[i]244 if migration.ID == migrationID {245 break246 }247 migrationRan, err := g.migrationRan(migration)248 if err != nil {249 return err250 }251 if migrationRan {252 if err := g.rollbackMigration(migration); err != nil {253 return err254 }255 }256 }257 return g.commit()258}259func (g *Gormigrate) getLastRunMigration() (*Migration, error) {260 for i := len(g.migrations) - 1; i >= 0; i-- {261 migration := g.migrations[i]262 migrationRan, err := g.migrationRan(migration)263 if err != nil {264 return nil, err265 }266 if migrationRan {267 return migration, nil268 }269 }270 return nil, ErrNoRunMigration271}272// RollbackMigration undo a migration.273func (g *Gormigrate) RollbackMigration(m *Migration) error {274 g.begin()275 defer g.rollback()276 if err := g.rollbackMigration(m); err != nil {277 return err278 }279 return g.commit()280}281func (g *Gormigrate) rollbackMigration(m *Migration) error {282 g.logInfo("Rolling back migration: %s", m.ID)283 if m.Rollback == nil {284 return ErrRollbackImpossible285 }286 if err := m.Rollback(g.tx); err != nil {287 return err288 }289 sql := fmt.Sprintf("DELETE FROM %s WHERE %s = ?", g.options.TableName, g.options.IDColumnName)290 return g.tx.Exec(sql, m.ID).Error291}292func (g *Gormigrate) runInitSchema() error {293 g.logInfo("Initializing schema")294 if err := g.initSchema(g.tx); err != nil {295 return err296 }297 if err := g.insertMigration(initSchemaMigrationID); err != nil {298 return err299 }300 for _, migration := range g.migrations {301 if err := g.insertMigration(migration.ID); err != nil {302 return err303 }304 }305 return nil306}307func (g *Gormigrate) runMigration(migration *Migration) error {308 if len(migration.ID) == 0 {309 return ErrMissingID310 }311 migrationRan, err := g.migrationRan(migration)312 if err != nil {313 return err314 }315 if !migrationRan {316 g.logInfo("Running migration: %s", migration.ID)317 if err := migration.Migrate(g.tx); err != nil {318 return err319 }320 if err := g.insertMigration(migration.ID); err != nil {321 return err322 }323 }324 return nil325}326func (g *Gormigrate) createMigrationTableIfNotExists() error {327 g.logInfo("Ensuring migration table exists")328 if g.tx.Migrator().HasTable(g.options.TableName) {329 return nil330 }331 sql := fmt.Sprintf("CREATE TABLE %s (%s VARCHAR(%d) PRIMARY KEY)", g.options.TableName, g.options.IDColumnName, g.options.IDColumnSize)332 if err := g.tx.Exec(sql).Error; err != nil {333 return fmt.Errorf("error creating migration table: %w", err)334 }335 return nil336}337func (g *Gormigrate) migrationRan(m *Migration) (bool, error) {338 var count int64339 err := g.tx.340 Table(g.options.TableName).341 Where(fmt.Sprintf("%s = ?", g.options.IDColumnName), m.ID).342 Count(&count).343 Error344 return count > 0, err345}346// The schema can be initialised only if it hasn't been initialised yet347// and no other migration has been applied already.348func (g *Gormigrate) canInitializeSchema() (bool, error) {349 migrationRan, err := g.migrationRan(&Migration{ID: initSchemaMigrationID})350 if err != nil {351 return false, err352 }353 if migrationRan {354 return false, nil355 }356 // If the ID doesn't exist, we also want the list of migrations to be empty357 var count int64358 err = g.tx.359 Table(g.options.TableName).360 Count(&count).361 Error362 return count == 0, err363}364func (g *Gormigrate) unknownMigrationsHaveHappened() (bool, error) {365 sql := fmt.Sprintf("SELECT %s FROM %s", g.options.IDColumnName, g.options.TableName)366 rows, err := g.tx.Raw(sql).Rows()367 if err != nil {368 return false, err369 }370 defer rows.Close()371 validIDSet := make(map[string]struct{}, len(g.migrations)+1)372 validIDSet[initSchemaMigrationID] = struct{}{}373 for _, migration := range g.migrations {374 validIDSet[migration.ID] = struct{}{}375 }376 for rows.Next() {377 var pastMigrationID string378 if err := rows.Scan(&pastMigrationID); err != nil {379 return false, err380 }381 if _, ok := validIDSet[pastMigrationID]; !ok {382 return true, nil383 }384 }385 return false, nil386}...

Full Screen

Full Screen

files_test.go

Source:files_test.go Github

copy

Full Screen

...36 fs: fstest.MapFS{37 "migrations": {38 Mode: fs.ModeDir,39 },40 "migrations/V20211224081255_initial.up.hmf": {},41 "migrations/V20211224091800_add_users_table.down.hmf": {},42 "migrations/V20211224091800_add_users_table.up.hmf": {},43 },44 expectedMigrations: []migration.Description{45 {Migration: migration.Migration{Version: 20211224081255, Name: "initial"}, CanUndo: false},46 {Migration: migration.Migration{Version: 20211224091800, Name: "add_users_table"}, CanUndo: true},47 },48 },49 /* s2 */ {50 name: "s2: should correctly list migrations in an non-standard directory",51 directory: "tmp/.Xs223xxSCa",52 fs: fstest.MapFS{53 "tmp/.Xs223xxSCa": {54 Mode: fs.ModeDir,55 },56 "tmp/.Xs223xxSCa/V20211224081255_initial.up.hmf": {},57 "tmp/.Xs223xxSCa/V20211224091800_add_users_table.down.hmf": {},58 "tmp/.Xs223xxSCa/V20211224091800_add_users_table.up.hmf": {},59 },60 expectedMigrations: []migration.Description{61 {Migration: migration.Migration{Version: 20211224081255, Name: "initial"}, CanUndo: false},62 {Migration: migration.Migration{Version: 20211224091800, Name: "add_users_table"}, CanUndo: true},63 },64 },65 /* s3 */ {66 name: "s3: should skip on bad version format (too short)",67 directory: "migrations",68 fs: fstest.MapFS{69 "migrations": {70 Mode: fs.ModeDir,71 },72 "migrations/V2021122409180_init.up.hmf": {},73 "migrations/V20211224091800_add_users_table.down.hmf": {},74 "migrations/V20211224091800_add_users_table.up.hmf": {},75 },76 expectedMigrations: []migration.Description{77 {Migration: migration.Migration{Version: 20211224091800, Name: "add_users_table"}, CanUndo: true},78 },79 },80 /* s4 */ {81 name: "s4: should skip on bad version format (does not start with a digit)",82 directory: "migrations",83 fs: fstest.MapFS{84 "migrations": {85 Mode: fs.ModeDir,86 },87 "migrations/V_0211224091800_init.up.hmf": {},88 "migrations/V20211224091800_add_users_table.down.hmf": {},89 "migrations/V20211224091800_add_users_table.up.hmf": {},90 },91 expectedMigrations: []migration.Description{92 {Migration: migration.Migration{Version: 20211224091800, Name: "add_users_table"}, CanUndo: true},93 },94 },95 /* s5 */ {96 name: "s5: should skip on bad version format (does not start with a V)",97 directory: "migrations",98 fs: fstest.MapFS{99 "migrations": {100 Mode: fs.ModeDir,101 },102 "migrations/120211224091800_init.up.hmf": {},103 "migrations/V20211224091800_add_users_table.down.hmf": {},104 "migrations/V20211224091800_add_users_table.up.hmf": {},105 },106 expectedMigrations: []migration.Description{107 {Migration: migration.Migration{Version: 20211224091800, Name: "add_users_table"}, CanUndo: true},108 },109 },110 /* s6 */ {111 name: "s6: should skip on bad migration name (no underscore before name)",112 directory: "migrations",113 fs: fstest.MapFS{114 "migrations": {115 Mode: fs.ModeDir,116 },117 "migrations/V20211224091800init.up.hmf": {},118 "migrations/V20211224091800_add_users_table.down.hmf": {},119 "migrations/V20211224091800_add_users_table.up.hmf": {},120 },121 expectedMigrations: []migration.Description{122 {Migration: migration.Migration{Version: 20211224091800, Name: "add_users_table"}, CanUndo: true},123 },124 },125 /* s7 */ {126 name: "s7: should skip on bad migration name (no name)",127 directory: "migrations",128 fs: fstest.MapFS{129 "migrations": {130 Mode: fs.ModeDir,131 },132 "migrations/V20211224091800.up.hmf": {},133 "migrations/V20211224091800_add_users_table.down.hmf": {},134 "migrations/V20211224091800_add_users_table.up.hmf": {},135 },136 expectedMigrations: []migration.Description{137 {Migration: migration.Migration{Version: 20211224091800, Name: "add_users_table"}, CanUndo: true},138 },139 },140 /* s8 */ {141 name: "s8: should skip on bad migration name (no name but with underscore)",142 directory: "migrations",143 fs: fstest.MapFS{144 "migrations": {145 Mode: fs.ModeDir,146 },147 "migrations/V20211224091800_.up.hmf": {},148 "migrations/V20211224091800_add_users_table.down.hmf": {},149 "migrations/V20211224091800_add_users_table.up.hmf": {},150 },151 expectedMigrations: []migration.Description{152 {Migration: migration.Migration{Version: 20211224091800, Name: "add_users_table"}, CanUndo: true},153 },154 },155 /* s9 */ {156 name: "s9: should skip on bad migration name (bad suffix)",157 directory: "migrations",158 fs: fstest.MapFS{159 "migrations": {160 Mode: fs.ModeDir,161 },162 "migrations/V20211224091800_init..hmf": {},163 "migrations/V20211224091800_init.hmf": {},164 "migrations/V20211224091800_init.up": {},165 "migrations/V20211224091800_init.": {},166 "migrations/V20211224091800_init": {},167 "migrations/V20211224091800_add_users_table.down.hmf": {},168 "migrations/V20211224091800_add_users_table.up.hmf": {},169 },170 expectedMigrations: []migration.Description{171 {Migration: migration.Migration{Version: 20211224091800, Name: "add_users_table"}, CanUndo: true},172 },173 },174 /* s10 */ {175 name: "s10: should not care about other directories",176 directory: "migrations",177 fs: fstest.MapFS{178 "migrations": {179 Mode: fs.ModeDir,180 },181 "V20211224091100_init.up.hmf": {},182 "migrations/subdirectory/V20211224091100_init.up.hmf": {},183 "sibling/V20211224091100_init.up.hmf": {},184 "migrations/V20211224091800_add_users_table.down.hmf": {},185 "migrations/V20211224091800_add_users_table.up.hmf": {},186 },187 expectedMigrations: []migration.Description{188 {Migration: migration.Migration{Version: 20211224091800, Name: "add_users_table"}, CanUndo: true},189 },190 },191 /* s11 */ {192 name: "s11: should skip directories with matching name",193 directory: "migrations",194 fs: fstest.MapFS{195 "migrations": {196 Mode: fs.ModeDir,197 },198 "migrations/V20211224091700_init.up.hmf": {199 Mode: fs.ModeDir,200 },201 "migrations/V20211224091800_add_users_table.down.hmf": {},202 "migrations/V20211224091800_add_users_table.up.hmf": {},203 },204 expectedMigrations: []migration.Description{205 {Migration: migration.Migration{Version: 20211224091800, Name: "add_users_table"}, CanUndo: true},206 },207 },208 // -- error tests --------209 /* e0 */ {210 name: "e0: should fail when directory does not exist",211 directory: "henka",212 fs: fstest.MapFS{213 "migrations": {214 Mode: fs.ModeDir,215 },216 "migrations/V20211224081255_initial.up.hmf": {},217 },218 expectErrorWhenCreating: true,219 },220 /* e1 */ {221 name: "e1: should fail on duplicate migration version",222 directory: "migrations",223 fs: fstest.MapFS{224 "migrations": {225 Mode: fs.ModeDir,226 },227 "migrations/V20211224091800_add_users_table.down.hmf": {},228 "migrations/V20211224091800_add_users_table.up.hmf": {},229 "migrations/V20211224091800_add_users_table_2.down.hmf": {},230 },...

Full Screen

Full Screen

migrate.go

Source:migrate.go Github

copy

Full Screen

...7// MigrateFunc is the func signature for migrating.8type MigrateFunc func(*xorm.Engine) error9// RollbackFunc is the func signature for rollbacking.10type RollbackFunc func(*xorm.Engine) error11// InitSchemaFunc is the func signature for initializing the schemas.12type InitSchemaFunc func(*xorm.Engine) error13// Options define options for all migrations.14type Options struct {15 // TableName is the migration table.16 TableName string17 // IDColumnName is the name of column where the migration id will be stored.18 IDColumnName string19}20// Migration represents a database migration (a modification to be made on the database).21type Migration struct {22 // ID is the migration identifier. Usually a timestamp like "201601021504".23 ID string24 // Migrate is a function that will br executed while running this migration.25 Migrate MigrateFunc26 // Rollback will be executed on rollback. Can be nil.27 Rollback RollbackFunc28}29// Migrate represents a collection of all migrations of a database schemas.30type Migrate struct {31 db *xorm.Engine32 options *Options33 migrations []*Migration34 initSchema InitSchemaFunc35}36var (37 // DefaultOptions can be used if you don't want to think about options.38 DefaultOptions = &Options{39 TableName: "migrations",40 IDColumnName: "id",41 }42 // ErrRollbackImpossible is returned when trying to rollback a migration43 // that has no rollback function.44 ErrRollbackImpossible = errors.New("It's impossible to rollback this migration")45 // ErrNoMigrationDefined is returned when no migration is defined.46 ErrNoMigrationDefined = errors.New("No migration defined")47 // ErrMissingID is returned when the ID od migration is equal to ""48 ErrMissingID = errors.New("Missing ID in migration")49 // ErrNoRunnedMigration is returned when any runned migration was found while50 // running RollbackLast51 ErrNoRunnedMigration = errors.New("Could not find last runned migration")52)53// New returns a new Gormigrate.54func New(db *xorm.Engine, options *Options, migrations []*Migration) *Migrate {55 return &Migrate{56 db: db,57 options: options,58 migrations: migrations,59 }60}61// InitSchema sets a function that is run if no migration is found.62// The idea is preventing to run all migrations when a new clean database63// is being migrating. In this function you should create all tables and64// foreign key necessary to your application.65func (m *Migrate) InitSchema(initSchema InitSchemaFunc) {66 m.initSchema = initSchema67}68// Migrate executes all migrations that did not run yet.69func (m *Migrate) Migrate() error {70 if err := m.createMigrationTableIfNotExists(); err != nil {71 return err72 }73 if m.initSchema != nil && m.isFirstRun() {74 return m.runInitSchema()75 }76 for _, migration := range m.migrations {77 if err := m.runMigration(migration); err != nil {78 return err79 }80 }81 return nil82}83// RollbackLast undo the last migration84func (m *Migrate) RollbackLast() error {85 if len(m.migrations) == 0 {86 return ErrNoMigrationDefined87 }88 lastRunnedMigration, err := m.getLastRunnedMigration()89 if err != nil {90 return err91 }92 if err := m.RollbackMigration(lastRunnedMigration); err != nil {93 return err94 }95 return nil96}97func (m *Migrate) getLastRunnedMigration() (*Migration, error) {98 for i := len(m.migrations) - 1; i >= 0; i-- {99 migration := m.migrations[i]100 run, err := m.migrationDidRun(migration)101 if err != nil {102 return nil, err103 } else if run {104 return migration, nil105 }106 }107 return nil, ErrNoRunnedMigration108}109// RollbackMigration undo a migration.110func (m *Migrate) RollbackMigration(mig *Migration) error {111 if mig.Rollback == nil {112 return ErrRollbackImpossible113 }114 if err := mig.Rollback(m.db); err != nil {115 return err116 }117 sql := fmt.Sprintf("DELETE FROM %s WHERE %s = ?", m.options.TableName, m.options.IDColumnName)118 if _, err := m.db.Exec(sql, mig.ID); err != nil {119 return err120 }121 return nil122}123func (m *Migrate) runInitSchema() error {124 if err := m.initSchema(m.db); err != nil {125 return err126 }127 for _, migration := range m.migrations {128 if err := m.insertMigration(migration.ID); err != nil {129 return err130 }131 }132 return nil133}134func (m *Migrate) runMigration(migration *Migration) error {135 if len(migration.ID) == 0 {136 return ErrMissingID137 }138 run, err := m.migrationDidRun(migration)...

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1migrations.Init()2migrations.Init()3migrations.Init()4migrations.Init()5migrations.Init()6migrations.Init()7migrations.Init()8migrations.Init()9migrations.Init()10migrations.Init()11migrations.Init()12migrations.Init()13migrations.Init()14migrations.Init()15migrations.Init()16migrations.Init()17migrations.Init()18migrations.Init()19migrations.Init()20migrations.Init()21migrations.Init()22migrations.Init()23migrations.Init()24migrations.Init()

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1if err := migrations.Migrate(); err != nil {2 log.Fatal(err)3}4if err := migrations.Migrate(); err != nil {5 log.Fatal(err)6}7if err := migrations.Migrate(); err != nil {8 log.Fatal(err)9}10if err := migrations.Migrate(); err != nil {11 log.Fatal(err)12}13if err := migrations.Migrate(); err != nil {14 log.Fatal(err)15}16if err := migrations.Migrate(); err != nil {17 log.Fatal(err)18}19if err := migrations.Migrate(); err != nil {20 log.Fatal(err)21}22if err := migrations.Migrate(); err != nil {23 log.Fatal(err)24}25if err := migrations.Migrate(); err != nil {26 log.Fatal(err)27}28if err := migrations.Migrate(); err != nil {29 log.Fatal(err)30}31if err := migrations.Migrate(); err != nil {32 log.Fatal(err)33}34if err := migrations.Migrate(); err != nil {35 log.Fatal(err)36}37if err := migrations.Migrate(); err != nil {38 log.Fatal(err)39}40if err := migrations.Migrate(); err != nil {41 log.Fatal(err)42}43if err := migrations.Migrate(); err != nil {44 log.Fatal(err)45}

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 db, err := gorm.Open("sqlite3", "/tmp/gorm.db")4 if err != nil {5 log.Println(err)6 }7 defer db.Close()8 migrator.Migrate(db)9}10import (11type User struct {12}13func init() {14 migrator.RegisterMigration("20170310102000", "CreateUsers", func(db *gorm.DB) error {15 type User struct {16 }17 if err := db.CreateTable(&User{}).Error; err != nil {18 }19 if err := db.Model(&User{}).AddIndex("idx_user_name", "name").Error; err != nil {20 }21 })22}23import (24type Post struct {25}26func init() {27 migrator.RegisterMigration("20170310103000", "CreatePosts", func(db *gorm.DB) error {28 type Post struct {29 }30 if err := db.CreateTable(&Post{}).Error; err != nil {31 }32 if err := db.Model(&Post{}).AddIndex("idx_post_title", "title").Error; err != nil {

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1migrations.Init()2migrations.Init()3migrations.Init()4migrations.Init()5migrations.Init()6migrations.Init()7migrations.Init()8migrations.Init()9migrations.Init()10migrations.Init()

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 migrations.Init()4 fmt.Println("Hello World")5}6import (7func main() {8 migrations.Init()9 fmt.Println("Hello World")10}11import (12func Init() {13 fmt.Println("Migration Init")14}

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 dir, err := filepath.Abs(filepath.Dir(os.Args[0]))4 if err != nil {5 log.Fatal(err)6 }7 db, err := sql.Open("sqlite3", dir+"/db.sqlite3")8 if err != nil {9 log.Fatal(err)10 }11 defer db.Close()12 files, err := filepath.Glob(dir + "/migrations/*.sql")13 if err != nil {14 log.Fatal(err)15 }16 _, err = db.Exec("CREATE TABLE IF NOT EXISTS migrations (id INTEGER PRIMARY KEY, name TEXT);")17 if err != nil {18 log.Fatal(err)19 }20 rows, err := db.Query("SELECT name FROM migrations;")21 if err != nil {22 log.Fatal(err)23 }24 defer rows.Close()25 for rows.Next() {26 err = rows.Scan(&name)27 if err != nil {28 log.Fatal(err)29 }30 appliedMigrations = append(appliedMigrations, name)31 }32 for _, file := range files {33 name := strings.TrimSuffix(filepath.Base(file), filepath.Ext(file))34 for _, appliedMigration := range appliedMigrations {35 if name == appliedMigration {36 }37 }38 if !found {39 migration, err := os.Open(file)40 if err != nil {41 log.Fatal(err)42 }43 defer migration.Close()44 migrationContents := make([]byte, 100000)45 migration.Read(migrationContents)46 _, err = db.Exec(string(migration

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testkube automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful