This repository has been archived by the owner on Aug 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
migrate.go
115 lines (97 loc) · 2.51 KB
/
migrate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package gondolier
import (
"database/sql"
"strings"
)
var (
db *sql.DB
migrator Migrator
naming = NameSchema(&SnakeCase{})
metaModels = make([]MetaModel, 0)
)
// Migrator interface used to migrate a database schema for a specific database.
type Migrator interface {
Migrate([]MetaModel)
DropTable(string)
}
// NameSchema interface used to translate model names to schema names.
type NameSchema interface {
Get(string) string
}
// Use sets the database connection and migrator.
func Use(conn *sql.DB, m Migrator) {
db = conn
migrator = m
}
// Naming sets the naming pattern used for migration. Default is snake case.
//
// Example:
// Naming(SnakeCase)
func Naming(schema NameSchema) {
if schema == nil {
panic("Name schema must not be nil")
}
naming = schema
}
// Model adds one or more objects for migration.
// The objects can be passed as references, values or mixed.
// This function might panic if an invalid model is used.
//
// Example:
// Model(&MyModel{}, AnotherModel{})
func Model(models ...interface{}) {
for _, model := range models {
if !modelExists(model) {
metaModels = append(metaModels, buildMetaModel(model))
}
}
}
// Migrate migrates models added previously using Model().
// The database connection and migrator must be set before by calling Use().
//
// Example:
// Use(Postgres)
// Model(MyModel{}, AnotherModel{})
// Migrate()
func Migrate() {
checkSetup()
migrator.Migrate(metaModels)
reset()
}
// Drop drops tables for given objects if they exist.
// The database connection and migrator must be set before by calling Use().
// The objects can be passed as references, values or mixed.
// This function might panic if an invalid model is used or the tables cannot be dropped.
//
// Example:
// Drop(&MyModel{}, AnotherModel{})
func Drop(models ...interface{}) {
checkSetup()
for _, model := range models {
metaModel := buildMetaModel(model)
migrator.DropTable(metaModel.ModelName)
}
}
func modelExists(model interface{}) bool {
name := strings.ToLower(getModelName(model))
for _, metaModel := range metaModels {
if name == strings.ToLower(metaModel.ModelName) {
return true
}
}
return false
}
func checkSetup() {
if db == nil {
panic("No database connection was set, call Use(connection, migrator) to set one")
}
if migrator == nil {
panic("No migrator was set, call Use(connection, migrator) to select one")
}
if naming == nil {
panic("No naming was set, call Naming(naming) to set one")
}
}
func reset() {
metaModels = make([]MetaModel, 0)
}