Monday, 2 December 2024

GO LAN GIN migrations

migration: http://gin.io/docs/migrations.html

model: http://gin.io/docs/models.html

MySQL Connection and models

To create a MySQL connection and model with ORM functionalities, we first need to define a new MySQL database in the file ./db/mysql.lua, by requiring Gin's SQL helper and by specifying the mysql adapter like so:

local SqlDatabase = require 'gin.db.sql'
local Gin = require 'gin.core.gin'

-- First, specify the environment settings for this database, for instance:
local DbSettings = {
    development = {
        adapter = 'mysql',
        host = "127.0.0.1",
        port = 3306,
        database = "demo_development",
        user = "root",
        password = "",
        pool = 5
    },

    test = {
        adapter = 'mysql',
        host = "127.0.0.1",
        port = 3306,
        database = "demo_test",
        user = "root",
        password = "",
        pool = 5
    },

    production = {
        adapter = 'mysql',
        host = "127.0.0.1",
        port = 3306,
        database = "demo_production",
        user = "root",
        password = "",
        pool = 5
    }
}

-- Then initialize and return your database:
local MySql = SqlDatabase.new(DbSettings[Gin.env])

return MySql

Migrations

Gin implements a migration engine to help you handle the evolution of your databases. It currently only supports the SQL databases available in Gin (see models).

To create a new migration:

$ gin generate migration
Created new migration file
  db/migrations/20131107134407.lua

The migration name is just the timestamp of the moment the migration file was created. Open up the newly generated migration file:

local SqlMigration = {}

-- specify the database used in this migration (needed by the Gin migration engine)
-- SqlMigration.db = require 'db.mysql'

function SqlMigration.up()
    -- Run your migration
end

function SqlMigration.down()
    -- Run your rollback
end

return SqlMigration

To set up a migration, you need to define:

  • the SqlMigration.db, which must be one of the available databases, for example the MySql database specified in ./db/mysql.lua (see application files for more info).
  • the SqlMigration.up() function, where you basically execute a SQL statement on the specified db. This function will be called by the Gin migration engine when running the migration.
  • the SqlMigration.down() function, where you execute a SQL statement on the specified db. This function will be called by the Gin migration engine when rolling back the migration.

The following example migration file shows the creation of a users table, and the rollback of the migration which drops the table:

local SqlMigration = {}

SqlMigration.db = require 'db.mysql'

function SqlMigration.up()
    SqlMigration.db:execute([[
        CREATE TABLE users (
            id int NOT NULL AUTO_INCREMENT,
            first_name varchar(255) NOT NULL,
            last_name varchar(255),
            PRIMARY KEY (id)
        );
    ]])
end

function SqlMigration.down()
    SqlMigration.db:execute([[
        DROP TABLE users;
    ]])
end

return SqlMigration

No comments:

Post a Comment