Skip to content

Commit 744c1c6

Browse files
committed
change type of migration type
1 parent 00eea54 commit 744c1c6

File tree

3 files changed

+48
-19
lines changed

3 files changed

+48
-19
lines changed

Migration.go

+8-18
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,27 @@ package exodus
22

33
import "fmt"
44

5-
// Migration is a complete, raw SQL command that can be ran
6-
// against a database.
7-
type Migration struct {
8-
SQL string
9-
}
10-
11-
func (m Migration) String() string {
12-
return string(m.SQL)
13-
}
5+
// Migration is a fully-formed SQL command that can be ran against
6+
// a database connection.
7+
type Migration string
148

159
// MigrationInterface ...
1610
type MigrationInterface interface {
1711
Up() Migration
1812
Down() Migration
1913
}
2014

21-
// Create generates a Migration that contains SQL to create
22-
// the schema against the table name provided.
15+
// Create generates an SQL command to create a table using the
16+
// schema provided.
2317
func Create(table string, schema Schema) Migration {
2418
sql := parseColumns(loadColumnSQL(schema))
2519

26-
return Migration{
27-
SQL: fmt.Sprintf("CREATE TABLE %s ( %s );", table, sql),
28-
}
20+
return Migration(fmt.Sprintf("CREATE TABLE %s ( %s );", table, sql))
2921
}
3022

31-
// Drop generates an SQL command to drop a table.
23+
// Drop generates an SQL command to drop the given table.
3224
func Drop(table string) Migration {
33-
return Migration{
34-
SQL: fmt.Sprintf("DROP TABLE %s", table),
35-
}
25+
return Migration(fmt.Sprintf("DROP TABLE %s", table))
3626
}
3727

3828
// loadColumnSQL iterates through the Columns defined in the

Migrator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (m *Migrator) Run(migrations ...MigrationInterface) error {
135135
batch := m.nextBatchNumber()
136136

137137
for _, migration := range migrations {
138-
if _, err := m.DB.Exec(migration.Up().String()); err != nil {
138+
if _, err := m.DB.Exec(string(migration.Up())); err != nil {
139139
return err
140140
}
141141

column/UniqueSet.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package column
2+
3+
import "fmt"
4+
5+
type Constraint struct {
6+
Name string
7+
Columns []string
8+
}
9+
10+
// UniqueSet ...
11+
func UniqueSet(name string, columns ...string) *Constraint {
12+
return &Constraint{
13+
Name: name,
14+
Columns: columns,
15+
}
16+
}
17+
18+
func (us *Constraint) ToSQL() string {
19+
sql := fmt.Sprintf(
20+
"CONSTRAINT %s UNIQUE ( %s )",
21+
us.Name,
22+
us.getSet(),
23+
)
24+
25+
fmt.Println(sql)
26+
27+
return sql
28+
}
29+
30+
func (us *Constraint) getSet() (inline string) {
31+
for i, column := range us.Columns {
32+
inline = inline + column
33+
if i < len(us.Columns)-1 {
34+
inline = inline + ","
35+
}
36+
}
37+
38+
return
39+
}

0 commit comments

Comments
 (0)