Giter Site home page Giter Site logo

foundation's Introduction

Foundation

Foundation is a Laravel Eloquent inspired sql migration handle(?) made for fun.

Documentation

To create a migration (file: migration.go):

package main

import (
    . "foundation/table"
    . "foundation/schema"
)

func main() {
    RunMigrations(Credentials{
        Username: "root",
        Password: "pw",
        Ip: "127.0.0.1",
        Port: 3306,
        Name: "go_db_test",
    }, up, down);

    // Credentials expand to root:pw@tcp(127.0.0.1:3306)/go_db_test
}

func up(schema *Schema) {
    schema.CreateTable("companies", func (table *Table) {
        table.ID()
        table.String("name")
        table.String("address")
    })

    schema.CreateTable("users", func (table *Table) {
        table.ID()
        table.String("email")
        table.Int("age").Nullable()
        table.Enum("roles", []string{"Owner", "Maintainer", "Developer", "Guest"})
        table.ForeignID("company_id", "companies", "id")
        table.Bool("is_male").Default(true)
        table.Timestamps()
    })

    schema.AlterTable("users", func(table *Table) {
        // Creates new field called "gender"
        table.Enum("gender", []string{"Male", "Female"}).Default("Female")

        // Sets the "email" field to unique
        table.String("email").Unique().Alter()

        // Drops "is_male"
        table.DropColumn("is_male")
    })

}

func down(schema *Schema) {
    schema.DropTableIfExists("users")
    schema.DropTableIfExists("companies")
}

Then to run up

go run migration.go up

Then to run down

go run migration.go down

Columns

  • String
  • DateTime
  • Int
  • Text
  • ForeignID
  • Enum
  • Bool

String

Takes in a column name

Translates to a varchar

Methods:

  • Length - Sets the length of the string
  • Nullable - Removes NOT NULL from final string
  • Unique - Adds UNIQUE to final string
  • Primary - Adds PRIMARY KEY to final string
  • Default - Adds a default value when no value is inserted in the database
  • OnUpdate - Sql on update
  • OnDelete - Sql on delete

DateTime

Takes in a column name

Translates to a datetime

Methods:

  • Nullable - Removes NOT NULL from final string
  • Unique - Adds UNIQUE to final string
  • Primary - Adds PRIMARY KEY to final string
  • Default - Adds a default value when no value is inserted in the database
  • OnUpdate - Sql on update
  • OnDelete - Sql on delete

Int

Takes in a column name

Translates to a Int or TINYINT, SMALLINT, MEDIUMINT, BIGINT depending

Methods:

  • Length - Sets the length of the int
  • Tiny - Makes it a TINYINT
  • Small - Makes it a SMALLINT
  • Medium - Makes it a MEDIUMINT
  • Big - Makes it a BIGINT
  • Unsigned - Makes it unsigned
  • Nullable - Removes NOT NULL from final string
  • Unique - Adds UNIQUE to final string
  • Primary - Adds PRIMARY KEY to final string
  • AutoIncrement - Adds AUTO_INCREMENT to final string
  • Default - Adds a default value when no value is inserted in the database
  • OnUpdate - Sql on update
  • OnDelete - Sql on delete

Text

Takes in a column name

Translates to a text or SMALLTEXT, MEDIUMTEXT, LONGTEXT depending

Methods:

  • Length - Sets the length of the string
  • Small - Makes it a SMALLTEXT
  • Medium - Makes it a MEDIUMTEXT
  • Long - Makes it a LONGTEXT
  • Nullable - Removes NOT NULL from final string
  • Unique - Adds UNIQUE to final string
  • Primary - Adds PRIMARY KEY to final string
  • Default - Adds a default value when no value is inserted in the database
  • OnUpdate - Sql on update
  • OnDelete - Sql on delete

ForeignID

Takes in a column name, what it references(table) and on what (field)

Translates to a Int or TINYINT, SMALLINT, MEDIUMINT, BIGINT depending

Automatically set to UNSIGNED BIGINT

Methods:

  • Length - Sets the length of the int
  • Tiny - Makes it a TINYINT
  • Small - Makes it a SMALLINT
  • Medium - Makes it a MEDIUMINT
  • Big - Makes it a BIGINT
  • Nullable - Removes NOT NULL from final string
  • Unsigned - Makes it unsigned
  • Unique - Adds UNIQUE to final string
  • Primary - Adds PRIMARY KEY to final string
  • Default - Adds a default value when no value is inserted in the database
  • OnUpdate - Sql on update
  • OnDelete - Sql on delete

Enum

Takes in a column name and a string array of values

Translates to a ENUM

Methods:

  • Nullable - Removes NOT NULL from final string
  • Unique - Adds UNIQUE to final string
  • Primary - Adds PRIMARY KEY to final string
  • Default - Adds a default value when no value is inserted in the database
  • OnUpdate - Sql on update
  • OnDelete - Sql on delete

Bool

Takes in a column name and a string array of values

Translates to a Bool

Methods:

  • Nullable - Removes NOT NULL from final string
  • Unique - Adds UNIQUE to final string
  • Primary - Adds PRIMARY KEY to final string
  • Default - Adds a default value when no value is inserted in the database
  • OnUpdate - Sql on update
  • OnDelete - Sql on delete

DropColumn

Drops the given column

Helper methods

table.ID()

// Same as
table.Int("id").AutoIncrement().Primary().Unsigned().Big()
table.Timestamps()

// Same as
table.DateTime("created_at").Default("NOW()")
table.DateTime("updated_at").Default("NOW()").OnUpdate("NOW()")
table.TinyText(name)

// Same as
table.Text(name).Tiny()
table.MediumText(name)

// Same as
table.Text(name).Medium()
table.LongText(name)

// Same as
table.Text(name).Long()
table.TinyInt(name)

// Same as
table.Int(name).Tiny()
table.SmallInt(name)

// Same as
table.Int(name).Small()
table.MediumInt(name)

// Same as
table.Int(name).Medium()
table.BigInt(name)

// Same as
table.Int(name).Big()

foundation's People

Contributors

mkanilsson avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.