Single migrations for the world DB

Report a typo

Suppose you have a simple database, world. Below is the database diagram:

world database diagram

The songs database has the following relationship between tables:

  • countries — stores the name and capital of all countries in the world;
  • country_stats — stores the statistics of each country, each country only has one description of statistics;
  • continents — stores the name of the continents, each continent has one or many countries;

Your task is to run independent (single) migrations using the db.Migrator() interface to propagate the following changes to the world database:

  1. Rename the country_stats table to country_statistics;
  2. Add the gdp column to the country_statistics table;
Write a program in Go
package main

import (
"fmt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"log"
)

// DO NOT modify the model declarations below!
type Continent struct {
gorm.Model
Name string
Countries []Country
}

type Country struct {
gorm.Model
Name string
Capital string
ContinentID uint
}

type CountryStats struct {
gorm.Model
CountryID uint
Country Country
Population int
}

func main() {
// DO NOT modify the gorm.Open code block, it connects to an in-memory database 'world':
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
must(err)
// DO NOT delete this code block! It creates the in-memory 'world' DB schema:
must(db.AutoMigrate(&Continent{}, &Country{}, &CountryStats{}))
___

Create a free account to access the full topic