mirror of https://github.com/writeas/writefreely
parent
7a0863f71b
commit
bf3b6a5ba0
@ -0,0 +1,43 @@ |
|||||||
|
package writefreely |
||||||
|
|
||||||
|
import ( |
||||||
|
"context" |
||||||
|
"database/sql" |
||||||
|
"github.com/stretchr/testify/assert" |
||||||
|
"testing" |
||||||
|
) |
||||||
|
|
||||||
|
func TestOAuthDatastore(t *testing.T) { |
||||||
|
if !runMySQLTests() { |
||||||
|
t.Skip("skipping mysql tests") |
||||||
|
} |
||||||
|
withTestDB(t, func(db *sql.DB) { |
||||||
|
ctx := context.Background() |
||||||
|
ds := &datastore{ |
||||||
|
DB: db, |
||||||
|
driverName: "", |
||||||
|
} |
||||||
|
|
||||||
|
state, err := ds.GenerateOAuthState(ctx) |
||||||
|
assert.NoError(t, err) |
||||||
|
assert.Len(t, state, 24) |
||||||
|
|
||||||
|
countRows(t, ctx, db, 1, "SELECT COUNT(*) FROM `oauth_client_state` WHERE `state` = ? AND `used` = false", state) |
||||||
|
|
||||||
|
err = ds.ValidateOAuthState(ctx, state) |
||||||
|
assert.NoError(t, err) |
||||||
|
|
||||||
|
countRows(t, ctx, db, 1, "SELECT COUNT(*) FROM `oauth_client_state` WHERE `state` = ? AND `used` = true", state) |
||||||
|
|
||||||
|
var localUserID int64 = 99 |
||||||
|
var remoteUserID int64 = 100 |
||||||
|
err = ds.RecordRemoteUserID(ctx, localUserID, remoteUserID) |
||||||
|
assert.NoError(t, err) |
||||||
|
|
||||||
|
countRows(t, ctx, db, 1, "SELECT COUNT(*) FROM `users_oauth` WHERE `user_id` = ? AND `remote_user_id` = ?", localUserID, remoteUserID) |
||||||
|
|
||||||
|
foundUserID, err := ds.GetIDForRemoteUser(ctx, remoteUserID) |
||||||
|
assert.NoError(t, err) |
||||||
|
assert.Equal(t, localUserID, foundUserID) |
||||||
|
}) |
||||||
|
} |
@ -0,0 +1,271 @@ |
|||||||
|
package db |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"strings" |
||||||
|
) |
||||||
|
|
||||||
|
type DialectType int |
||||||
|
type ColumnType int |
||||||
|
|
||||||
|
type OptionalInt struct { |
||||||
|
Set bool |
||||||
|
Value int |
||||||
|
} |
||||||
|
|
||||||
|
type OptionalString struct { |
||||||
|
Set bool |
||||||
|
Value string |
||||||
|
} |
||||||
|
|
||||||
|
type SQLBuilder interface { |
||||||
|
ToSQL() (string, error) |
||||||
|
} |
||||||
|
|
||||||
|
type Column struct { |
||||||
|
Dialect DialectType |
||||||
|
Name string |
||||||
|
Nullable bool |
||||||
|
Default OptionalString |
||||||
|
Type ColumnType |
||||||
|
Size OptionalInt |
||||||
|
PrimaryKey bool |
||||||
|
} |
||||||
|
|
||||||
|
type CreateTableSqlBuilder struct { |
||||||
|
Dialect DialectType |
||||||
|
Name string |
||||||
|
IfNotExists bool |
||||||
|
ColumnOrder []string |
||||||
|
Columns map[string]*Column |
||||||
|
Constraints []string |
||||||
|
} |
||||||
|
|
||||||
|
const ( |
||||||
|
DialectSQLite DialectType = iota |
||||||
|
DialectMySQL DialectType = iota |
||||||
|
) |
||||||
|
|
||||||
|
const ( |
||||||
|
ColumnTypeBool ColumnType = iota |
||||||
|
ColumnTypeSmallInt ColumnType = iota |
||||||
|
ColumnTypeInteger ColumnType = iota |
||||||
|
ColumnTypeChar ColumnType = iota |
||||||
|
ColumnTypeVarChar ColumnType = iota |
||||||
|
ColumnTypeText ColumnType = iota |
||||||
|
ColumnTypeDateTime ColumnType = iota |
||||||
|
) |
||||||
|
|
||||||
|
var _ SQLBuilder = &CreateTableSqlBuilder{} |
||||||
|
|
||||||
|
var UnsetSize OptionalInt = OptionalInt{Set: false, Value: 0} |
||||||
|
var UnsetDefault OptionalString = OptionalString{Set: false, Value: ""} |
||||||
|
|
||||||
|
func (d DialectType) Column(name string, t ColumnType, size OptionalInt) *Column { |
||||||
|
switch d { |
||||||
|
case DialectSQLite: |
||||||
|
return &Column{Dialect: DialectSQLite, Name: name, Type: t, Size: size} |
||||||
|
case DialectMySQL: |
||||||
|
return &Column{Dialect: DialectMySQL, Name: name, Type: t, Size: size} |
||||||
|
default: |
||||||
|
panic(fmt.Sprintf("unexpected dialect: %d", d)) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func (d DialectType) Table(name string) *CreateTableSqlBuilder { |
||||||
|
switch d { |
||||||
|
case DialectSQLite: |
||||||
|
return &CreateTableSqlBuilder{Dialect: DialectSQLite, Name: name} |
||||||
|
case DialectMySQL: |
||||||
|
return &CreateTableSqlBuilder{Dialect: DialectMySQL, Name: name} |
||||||
|
default: |
||||||
|
panic(fmt.Sprintf("unexpected dialect: %d", d)) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func (d ColumnType) Format(dialect DialectType, size OptionalInt) (string, error) { |
||||||
|
if dialect != DialectMySQL && dialect != DialectSQLite { |
||||||
|
return "", fmt.Errorf("unsupported column type %d for dialect %d and size %v", d, dialect, size) |
||||||
|
} |
||||||
|
switch d { |
||||||
|
case ColumnTypeSmallInt: |
||||||
|
{ |
||||||
|
if dialect == DialectSQLite { |
||||||
|
return "INTEGER", nil |
||||||
|
} |
||||||
|
mod := "" |
||||||
|
if size.Set { |
||||||
|
mod = fmt.Sprintf("(%d)", size.Value) |
||||||
|
} |
||||||
|
return "SMALLINT" + mod, nil |
||||||
|
} |
||||||
|
case ColumnTypeInteger: |
||||||
|
{ |
||||||
|
if dialect == DialectSQLite { |
||||||
|
return "INTEGER", nil |
||||||
|
} |
||||||
|
mod := "" |
||||||
|
if size.Set { |
||||||
|
mod = fmt.Sprintf("(%d)", size.Value) |
||||||
|
} |
||||||
|
return "INT" + mod, nil |
||||||
|
} |
||||||
|
case ColumnTypeChar: |
||||||
|
{ |
||||||
|
if dialect == DialectSQLite { |
||||||
|
return "TEXT", nil |
||||||
|
} |
||||||
|
mod := "" |
||||||
|
if size.Set { |
||||||
|
mod = fmt.Sprintf("(%d)", size.Value) |
||||||
|
} |
||||||
|
return "CHAR" + mod, nil |
||||||
|
} |
||||||
|
case ColumnTypeVarChar: |
||||||
|
{ |
||||||
|
if dialect == DialectSQLite { |
||||||
|
return "TEXT", nil |
||||||
|
} |
||||||
|
mod := "" |
||||||
|
if size.Set { |
||||||
|
mod = fmt.Sprintf("(%d)", size.Value) |
||||||
|
} |
||||||
|
return "VARCHAR" + mod, nil |
||||||
|
} |
||||||
|
case ColumnTypeBool: |
||||||
|
{ |
||||||
|
if dialect == DialectSQLite { |
||||||
|
return "INTEGER", nil |
||||||
|
} |
||||||
|
return "TINYINT(1)", nil |
||||||
|
} |
||||||
|
case ColumnTypeDateTime: |
||||||
|
return "DATETIME", nil |
||||||
|
case ColumnTypeText: |
||||||
|
return "TEXT", nil |
||||||
|
} |
||||||
|
return "", fmt.Errorf("unsupported column type %d for dialect %d and size %v", d, dialect, size) |
||||||
|
} |
||||||
|
|
||||||
|
func (c *Column) SetName(name string) *Column { |
||||||
|
c.Name = name |
||||||
|
return c |
||||||
|
} |
||||||
|
|
||||||
|
func (c *Column) SetNullable(nullable bool) *Column { |
||||||
|
c.Nullable = nullable |
||||||
|
return c |
||||||
|
} |
||||||
|
|
||||||
|
func (c *Column) SetPrimaryKey(pk bool) *Column { |
||||||
|
c.PrimaryKey = pk |
||||||
|
return c |
||||||
|
} |
||||||
|
|
||||||
|
func (c *Column) SetDefault(value string) *Column { |
||||||
|
c.Default = OptionalString{Set: true, Value: value} |
||||||
|
return c |
||||||
|
} |
||||||
|
|
||||||
|
func (c *Column) SetType(t ColumnType) *Column { |
||||||
|
c.Type = t |
||||||
|
return c |
||||||
|
} |
||||||
|
|
||||||
|
func (c *Column) SetSize(size int) *Column { |
||||||
|
c.Size = OptionalInt{Set: true, Value: size} |
||||||
|
return c |
||||||
|
} |
||||||
|
|
||||||
|
func (c *Column) String() (string, error) { |
||||||
|
var str strings.Builder |
||||||
|
|
||||||
|
str.WriteString(c.Name) |
||||||
|
|
||||||
|
str.WriteString(" ") |
||||||
|
typeStr, err := c.Type.Format(c.Dialect, c.Size) |
||||||
|
if err != nil { |
||||||
|
return "", err |
||||||
|
} |
||||||
|
|
||||||
|
str.WriteString(typeStr) |
||||||
|
|
||||||
|
if !c.Nullable { |
||||||
|
str.WriteString(" NOT NULL") |
||||||
|
} |
||||||
|
|
||||||
|
if c.Default.Set { |
||||||
|
str.WriteString(" DEFAULT ") |
||||||
|
str.WriteString(c.Default.Value) |
||||||
|
} |
||||||
|
|
||||||
|
if c.PrimaryKey { |
||||||
|
str.WriteString(" PRIMARY KEY") |
||||||
|
} |
||||||
|
|
||||||
|
return str.String(), nil |
||||||
|
} |
||||||
|
|
||||||
|
func (b *CreateTableSqlBuilder) Column(column *Column) *CreateTableSqlBuilder { |
||||||
|
if b.Columns == nil { |
||||||
|
b.Columns = make(map[string]*Column) |
||||||
|
} |
||||||
|
b.Columns[column.Name] = column |
||||||
|
b.ColumnOrder = append(b.ColumnOrder, column.Name) |
||||||
|
return b |
||||||
|
} |
||||||
|
|
||||||
|
func (b *CreateTableSqlBuilder) UniqueConstraint(columns ...string) *CreateTableSqlBuilder { |
||||||
|
for _, column := range columns { |
||||||
|
if _, ok := b.Columns[column]; !ok { |
||||||
|
// This fails silently.
|
||||||
|
return b |
||||||
|
} |
||||||
|
} |
||||||
|
b.Constraints = append(b.Constraints, fmt.Sprintf("UNIQUE(%s)", strings.Join(columns, ","))) |
||||||
|
return b |
||||||
|
} |
||||||
|
|
||||||
|
func (b *CreateTableSqlBuilder) SetIfNotExists(ine bool) *CreateTableSqlBuilder { |
||||||
|
b.IfNotExists = ine |
||||||
|
return b |
||||||
|
} |
||||||
|
|
||||||
|
func (b *CreateTableSqlBuilder) ToSQL() (string, error) { |
||||||
|
var str strings.Builder |
||||||
|
|
||||||
|
str.WriteString("CREATE TABLE ") |
||||||
|
if b.IfNotExists { |
||||||
|
str.WriteString("IF NOT EXISTS ") |
||||||
|
} |
||||||
|
str.WriteString(b.Name) |
||||||
|
|
||||||
|
var things []string |
||||||
|
for _, columnName := range b.ColumnOrder { |
||||||
|
column, ok := b.Columns[columnName] |
||||||
|
if !ok { |
||||||
|
return "", fmt.Errorf("column not found: %s", columnName) |
||||||
|
} |
||||||
|
columnStr, err := column.String() |
||||||
|
if err != nil { |
||||||
|
return "", err |
||||||
|
} |
||||||
|
things = append(things, columnStr) |
||||||
|
} |
||||||
|
for _, constraint := range b.Constraints { |
||||||
|
things = append(things, constraint) |
||||||
|
} |
||||||
|
|
||||||
|
if thingLen := len(things); thingLen > 0 { |
||||||
|
str.WriteString(" ( ") |
||||||
|
for i, thing := range things { |
||||||
|
str.WriteString(thing) |
||||||
|
if i < thingLen-1 { |
||||||
|
str.WriteString(", ") |
||||||
|
} |
||||||
|
} |
||||||
|
str.WriteString(" )") |
||||||
|
} |
||||||
|
|
||||||
|
return str.String(), nil |
||||||
|
} |
@ -0,0 +1,146 @@ |
|||||||
|
package db |
||||||
|
|
||||||
|
import ( |
||||||
|
"github.com/stretchr/testify/assert" |
||||||
|
"testing" |
||||||
|
) |
||||||
|
|
||||||
|
func TestDialect_Column(t *testing.T) { |
||||||
|
c1 := DialectSQLite.Column("foo", ColumnTypeBool, UnsetSize) |
||||||
|
assert.Equal(t, DialectSQLite, c1.Dialect) |
||||||
|
c2 := DialectMySQL.Column("foo", ColumnTypeBool, UnsetSize) |
||||||
|
assert.Equal(t, DialectMySQL, c2.Dialect) |
||||||
|
} |
||||||
|
|
||||||
|
func TestColumnType_Format(t *testing.T) { |
||||||
|
type args struct { |
||||||
|
dialect DialectType |
||||||
|
size OptionalInt |
||||||
|
} |
||||||
|
tests := []struct { |
||||||
|
name string |
||||||
|
d ColumnType |
||||||
|
args args |
||||||
|
want string |
||||||
|
wantErr bool |
||||||
|
}{ |
||||||
|
{"Sqlite bool", ColumnTypeBool, args{dialect: DialectSQLite}, "INTEGER", false}, |
||||||
|
{"Sqlite small int", ColumnTypeSmallInt, args{dialect: DialectSQLite}, "INTEGER", false}, |
||||||
|
{"Sqlite int", ColumnTypeInteger, args{dialect: DialectSQLite}, "INTEGER", false}, |
||||||
|
{"Sqlite char", ColumnTypeChar, args{dialect: DialectSQLite}, "TEXT", false}, |
||||||
|
{"Sqlite varchar", ColumnTypeVarChar, args{dialect: DialectSQLite}, "TEXT", false}, |
||||||
|
{"Sqlite text", ColumnTypeText, args{dialect: DialectSQLite}, "TEXT", false}, |
||||||
|
{"Sqlite datetime", ColumnTypeDateTime, args{dialect: DialectSQLite}, "DATETIME", false}, |
||||||
|
|
||||||
|
{"MySQL bool", ColumnTypeBool, args{dialect: DialectMySQL}, "TINYINT(1)", false}, |
||||||
|
{"MySQL small int", ColumnTypeSmallInt, args{dialect: DialectMySQL}, "SMALLINT", false}, |
||||||
|
{"MySQL small int with param", ColumnTypeSmallInt, args{dialect: DialectMySQL, size: OptionalInt{true, 3}}, "SMALLINT(3)", false}, |
||||||
|
{"MySQL int", ColumnTypeInteger, args{dialect: DialectMySQL}, "INT", false}, |
||||||
|
{"MySQL int with param", ColumnTypeInteger, args{dialect: DialectMySQL, size: OptionalInt{true, 11}}, "INT(11)", false}, |
||||||
|
{"MySQL char", ColumnTypeChar, args{dialect: DialectMySQL}, "CHAR", false}, |
||||||
|
{"MySQL char with param", ColumnTypeChar, args{dialect: DialectMySQL, size: OptionalInt{true, 4}}, "CHAR(4)", false}, |
||||||
|
{"MySQL varchar", ColumnTypeVarChar, args{dialect: DialectMySQL}, "VARCHAR", false}, |
||||||
|
{"MySQL varchar with param", ColumnTypeVarChar, args{dialect: DialectMySQL, size: OptionalInt{true, 25}}, "VARCHAR(25)", false}, |
||||||
|
{"MySQL text", ColumnTypeText, args{dialect: DialectMySQL}, "TEXT", false}, |
||||||
|
{"MySQL datetime", ColumnTypeDateTime, args{dialect: DialectMySQL}, "DATETIME", false}, |
||||||
|
|
||||||
|
{"invalid column type", 10000, args{dialect: DialectMySQL}, "", true}, |
||||||
|
{"invalid dialect", ColumnTypeBool, args{dialect: 10000}, "", true}, |
||||||
|
} |
||||||
|
for _, tt := range tests { |
||||||
|
t.Run(tt.name, func(t *testing.T) { |
||||||
|
got, err := tt.d.Format(tt.args.dialect, tt.args.size) |
||||||
|
if (err != nil) != tt.wantErr { |
||||||
|
t.Errorf("Format() error = %v, wantErr %v", err, tt.wantErr) |
||||||
|
return |
||||||
|
} |
||||||
|
if got != tt.want { |
||||||
|
t.Errorf("Format() got = %v, want %v", got, tt.want) |
||||||
|
} |
||||||
|
}) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func TestColumn_Build(t *testing.T) { |
||||||
|
type fields struct { |
||||||
|
Dialect DialectType |
||||||
|
Name string |
||||||
|
Nullable bool |
||||||
|
Default OptionalString |
||||||
|
Type ColumnType |
||||||
|
Size OptionalInt |
||||||
|
PrimaryKey bool |
||||||
|
} |
||||||
|
tests := []struct { |
||||||
|
name string |
||||||
|
fields fields |
||||||
|
want string |
||||||
|
wantErr bool |
||||||
|
}{ |
||||||
|
{"Sqlite bool", fields{DialectSQLite, "foo", false, UnsetDefault, ColumnTypeBool, UnsetSize, false}, "foo INTEGER NOT NULL", false}, |
||||||
|
{"Sqlite bool nullable", fields{DialectSQLite, "foo", true, UnsetDefault, ColumnTypeBool, UnsetSize, false}, "foo INTEGER", false}, |
||||||
|
{"Sqlite small int", fields{DialectSQLite, "foo", false, UnsetDefault, ColumnTypeSmallInt, UnsetSize, true}, "foo INTEGER NOT NULL PRIMARY KEY", false}, |
||||||
|
{"Sqlite small int nullable", fields{DialectSQLite, "foo", true, UnsetDefault, ColumnTypeSmallInt, UnsetSize, false}, "foo INTEGER", false}, |
||||||
|
{"Sqlite int", fields{DialectSQLite, "foo", false, UnsetDefault, ColumnTypeInteger, UnsetSize, false}, "foo INTEGER NOT NULL", false}, |
||||||
|
{"Sqlite int nullable", fields{DialectSQLite, "foo", true, UnsetDefault, ColumnTypeInteger, UnsetSize, false}, "foo INTEGER", false}, |
||||||
|
{"Sqlite char", fields{DialectSQLite, "foo", false, UnsetDefault, ColumnTypeChar, UnsetSize, false}, "foo TEXT NOT NULL", false}, |
||||||
|
{"Sqlite char nullable", fields{DialectSQLite, "foo", true, UnsetDefault, ColumnTypeChar, UnsetSize, false}, "foo TEXT", false}, |
||||||
|
{"Sqlite varchar", fields{DialectSQLite, "foo", false, UnsetDefault, ColumnTypeVarChar, UnsetSize, false}, "foo TEXT NOT NULL", false}, |
||||||
|
{"Sqlite varchar nullable", fields{DialectSQLite, "foo", true, UnsetDefault, ColumnTypeVarChar, UnsetSize, false}, "foo TEXT", false}, |
||||||
|
{"Sqlite text", fields{DialectSQLite, "foo", false, UnsetDefault, ColumnTypeText, UnsetSize, false}, "foo TEXT NOT NULL", false}, |
||||||
|
{"Sqlite text nullable", fields{DialectSQLite, "foo", true, UnsetDefault, ColumnTypeText, UnsetSize, false}, "foo TEXT", false}, |
||||||
|
{"Sqlite datetime", fields{DialectSQLite, "foo", false, UnsetDefault, ColumnTypeDateTime, UnsetSize, false}, "foo DATETIME NOT NULL", false}, |
||||||
|
{"Sqlite datetime nullable", fields{DialectSQLite, "foo", true, UnsetDefault, ColumnTypeDateTime, UnsetSize, false}, "foo DATETIME", false}, |
||||||
|
|
||||||
|
{"MySQL bool", fields{DialectMySQL, "foo", false, UnsetDefault, ColumnTypeBool, UnsetSize, false}, "foo TINYINT(1) NOT NULL", false}, |
||||||
|
{"MySQL bool nullable", fields{DialectMySQL, "foo", true, UnsetDefault, ColumnTypeBool, UnsetSize, false}, "foo TINYINT(1)", false}, |
||||||
|
{"MySQL small int", fields{DialectMySQL, "foo", false, UnsetDefault, ColumnTypeSmallInt, UnsetSize, true}, "foo SMALLINT NOT NULL PRIMARY KEY", false}, |
||||||
|
{"MySQL small int nullable", fields{DialectMySQL, "foo", true, UnsetDefault, ColumnTypeSmallInt, UnsetSize, false}, "foo SMALLINT", false}, |
||||||
|
{"MySQL int", fields{DialectMySQL, "foo", false, UnsetDefault, ColumnTypeInteger, UnsetSize, false}, "foo INT NOT NULL", false}, |
||||||
|
{"MySQL int nullable", fields{DialectMySQL, "foo", true, UnsetDefault, ColumnTypeInteger, UnsetSize, false}, "foo INT", false}, |
||||||
|
{"MySQL char", fields{DialectMySQL, "foo", false, UnsetDefault, ColumnTypeChar, UnsetSize, false}, "foo CHAR NOT NULL", false}, |
||||||
|
{"MySQL char nullable", fields{DialectMySQL, "foo", true, UnsetDefault, ColumnTypeChar, UnsetSize, false}, "foo CHAR", false}, |
||||||
|
{"MySQL varchar", fields{DialectMySQL, "foo", false, UnsetDefault, ColumnTypeVarChar, UnsetSize, false}, "foo VARCHAR NOT NULL", false}, |
||||||
|
{"MySQL varchar nullable", fields{DialectMySQL, "foo", true, UnsetDefault, ColumnTypeVarChar, UnsetSize, false}, "foo VARCHAR", false}, |
||||||
|
{"MySQL text", fields{DialectMySQL, "foo", false, UnsetDefault, ColumnTypeText, UnsetSize, false}, "foo TEXT NOT NULL", false}, |
||||||
|
{"MySQL text nullable", fields{DialectMySQL, "foo", true, UnsetDefault, ColumnTypeText, UnsetSize, false}, "foo TEXT", false}, |
||||||
|
{"MySQL datetime", fields{DialectMySQL, "foo", false, UnsetDefault, ColumnTypeDateTime, UnsetSize, false}, "foo DATETIME NOT NULL", false}, |
||||||
|
{"MySQL datetime nullable", fields{DialectMySQL, "foo", true, UnsetDefault, ColumnTypeDateTime, UnsetSize, false}, "foo DATETIME", false}, |
||||||
|
} |
||||||
|
for _, tt := range tests { |
||||||
|
t.Run(tt.name, func(t *testing.T) { |
||||||
|
c := &Column{ |
||||||
|
Dialect: tt.fields.Dialect, |
||||||
|
Name: tt.fields.Name, |
||||||
|
Nullable: tt.fields.Nullable, |
||||||
|
Default: tt.fields.Default, |
||||||
|
Type: tt.fields.Type, |
||||||
|
Size: tt.fields.Size, |
||||||
|
PrimaryKey: tt.fields.PrimaryKey, |
||||||
|
} |
||||||
|
if got, err := c.String(); got != tt.want { |
||||||
|
if (err != nil) != tt.wantErr { |
||||||
|
t.Errorf("String() error = %v, wantErr %v", err, tt.wantErr) |
||||||
|
return |
||||||
|
} |
||||||
|
if got != tt.want { |
||||||
|
t.Errorf("String() got = %v, want %v", got, tt.want) |
||||||
|
} |
||||||
|
} |
||||||
|
}) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func TestCreateTableSqlBuilder_ToSQL(t *testing.T) { |
||||||
|
sql, err := DialectMySQL. |
||||||
|
Table("foo"). |
||||||
|
SetIfNotExists(true). |
||||||
|
Column(DialectMySQL.Column("bar", ColumnTypeInteger, UnsetSize).SetPrimaryKey(true)). |
||||||
|
Column(DialectMySQL.Column("baz", ColumnTypeText, UnsetSize)). |
||||||
|
Column(DialectMySQL.Column("qux", ColumnTypeDateTime, UnsetSize).SetDefault("NOW()")). |
||||||
|
UniqueConstraint("bar"). |
||||||
|
UniqueConstraint("bar", "baz"). |
||||||
|
ToSQL() |
||||||
|
assert.NoError(t, err) |
||||||
|
assert.Equal(t, "CREATE TABLE IF NOT EXISTS foo ( bar INT NOT NULL PRIMARY KEY, baz TEXT NOT NULL, qux DATETIME NOT NULL DEFAULT NOW(), UNIQUE(bar), UNIQUE(bar,baz) )", sql) |
||||||
|
} |
@ -0,0 +1,26 @@ |
|||||||
|
package db |
||||||
|
|
||||||
|
import ( |
||||||
|
"context" |
||||||
|
"database/sql" |
||||||
|
) |
||||||
|
|
||||||
|
// TransactionScopedWork describes code executed within a database transaction.
|
||||||
|
type TransactionScopedWork func(ctx context.Context, db *sql.Tx) error |
||||||
|
|
||||||
|
// RunTransactionWithOptions executes a block of code within a database transaction.
|
||||||
|
func RunTransactionWithOptions(ctx context.Context, db *sql.DB, txOpts *sql.TxOptions, txWork TransactionScopedWork) error { |
||||||
|
tx, err := db.BeginTx(ctx, txOpts) |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
if err = txWork(ctx, tx); err != nil { |
||||||
|
if txErr := tx.Rollback(); txErr != nil { |
||||||
|
return txErr |
||||||
|
} |
||||||
|
return err |
||||||
|
} |
||||||
|
return tx.Commit() |
||||||
|
} |
||||||
|
|
@ -1,18 +1,153 @@ |
|||||||
package writefreely |
package writefreely |
||||||
|
|
||||||
import ( |
import ( |
||||||
|
"context" |
||||||
|
"database/sql" |
||||||
"encoding/gob" |
"encoding/gob" |
||||||
|
"errors" |
||||||
|
"fmt" |
||||||
|
uuid "github.com/nu7hatch/gouuid" |
||||||
|
"github.com/stretchr/testify/assert" |
||||||
"math/rand" |
"math/rand" |
||||||
"os" |
"os" |
||||||
|
"strings" |
||||||
"testing" |
"testing" |
||||||
"time" |
"time" |
||||||
) |
) |
||||||
|
|
||||||
|
var testDB *sql.DB |
||||||
|
|
||||||
|
type ScopedTestBody func(*sql.DB) |
||||||
|
|
||||||
// TestMain provides testing infrastructure within this package.
|
// TestMain provides testing infrastructure within this package.
|
||||||
func TestMain(m *testing.M) { |
func TestMain(m *testing.M) { |
||||||
rand.Seed(time.Now().UTC().UnixNano()) |
rand.Seed(time.Now().UTC().UnixNano()) |
||||||
|
|
||||||
gob.Register(&User{}) |
gob.Register(&User{}) |
||||||
|
|
||||||
os.Exit(m.Run()) |
if runMySQLTests() { |
||||||
|
var err error |
||||||
|
|
||||||
|
testDB, err = initMySQL(os.Getenv("WF_USER"), os.Getenv("WF_PASSWORD"), os.Getenv("WF_DB"), os.Getenv("WF_HOST")) |
||||||
|
if err != nil { |
||||||
|
fmt.Println(err) |
||||||
|
return |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
code := m.Run() |
||||||
|
if runMySQLTests() { |
||||||
|
if closeErr := testDB.Close(); closeErr != nil { |
||||||
|
fmt.Println(closeErr) |
||||||
|
} |
||||||
|
} |
||||||
|
os.Exit(code) |
||||||
|
} |
||||||
|
|
||||||
|
func runMySQLTests() bool { |
||||||
|
return len(os.Getenv("TEST_MYSQL")) > 0 |
||||||
|
} |
||||||
|
|
||||||
|
func initMySQL(dbUser, dbPassword, dbName, dbHost string) (*sql.DB, error) { |
||||||
|
if dbUser == "" || dbPassword == "" { |
||||||
|
return nil, errors.New("database user or password not set") |
||||||
|
} |
||||||
|
if dbHost == "" { |
||||||
|
dbHost = "localhost" |
||||||
|
} |
||||||
|
if dbName == "" { |
||||||
|
dbName = "writefreely" |
||||||
|
} |
||||||
|
|
||||||
|
dsn := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8mb4&parseTime=true", dbUser, dbPassword, dbHost, dbName) |
||||||
|
db, err := sql.Open("mysql", dsn) |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
if err := ensureMySQL(db); err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
return db, nil |
||||||
|
} |
||||||
|
|
||||||
|
func ensureMySQL(db *sql.DB) error { |
||||||
|
if err := db.Ping(); err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
db.SetMaxOpenConns(250) |
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
// withTestDB provides a scoped database connection.
|
||||||
|
func withTestDB(t *testing.T, testBody ScopedTestBody) { |
||||||
|
db, cleanup, err := newTestDatabase(testDB, |
||||||
|
os.Getenv("WF_USER"), |
||||||
|
os.Getenv("WF_PASSWORD"), |
||||||
|
os.Getenv("WF_DB"), |
||||||
|
os.Getenv("WF_HOST"), |
||||||
|
) |
||||||
|
assert.NoError(t, err) |
||||||
|
defer func() { |
||||||
|
assert.NoError(t, cleanup()) |
||||||
|
}() |
||||||
|
|
||||||
|
testBody(db) |
||||||
|
} |
||||||
|
|
||||||
|
// newTestDatabase creates a new temporary test database. When a test
|
||||||
|
// database connection is returned, it will have created a new database and
|
||||||
|
// initialized it with tables from a reference database.
|
||||||
|
func newTestDatabase(base *sql.DB, dbUser, dbPassword, dbName, dbHost string) (*sql.DB, func() error, error) { |
||||||
|
var err error |
||||||
|
var baseName = dbName |
||||||
|
|
||||||
|
if baseName == "" { |
||||||
|
row := base.QueryRow("SELECT DATABASE()") |
||||||
|
err := row.Scan(&baseName) |
||||||
|
if err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
} |
||||||
|
tUUID, _ := uuid.NewV4() |
||||||
|
suffix := strings.Replace(tUUID.String(), "-", "_", -1) |
||||||
|
newDBName := baseName + suffix |
||||||
|
_, err = base.Exec("CREATE DATABASE " + newDBName) |
||||||
|
if err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
newDB, err := initMySQL(dbUser, dbPassword, newDBName, dbHost) |
||||||
|
if err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
|
||||||
|
rows, err := base.Query("SHOW TABLES IN " + baseName) |
||||||
|
if err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
for rows.Next() { |
||||||
|
var tableName string |
||||||
|
if err := rows.Scan(&tableName); err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
query := fmt.Sprintf("CREATE TABLE %s LIKE %s.%s", tableName, baseName, tableName) |
||||||
|
if _, err := newDB.Exec(query); err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
cleanup := func() error { |
||||||
|
if closeErr := newDB.Close(); closeErr != nil { |
||||||
|
fmt.Println(closeErr) |
||||||
|
} |
||||||
|
|
||||||
|
_, err = base.Exec("DROP DATABASE " + newDBName) |
||||||
|
return err |
||||||
|
} |
||||||
|
return newDB, cleanup, nil |
||||||
|
} |
||||||
|
|
||||||
|
func countRows(t *testing.T, ctx context.Context, db *sql.DB, count int, query string, args ...interface{}) { |
||||||
|
var returned int |
||||||
|
err := db.QueryRowContext(ctx, query, args...).Scan(&returned) |
||||||
|
assert.NoError(t, err, "error executing query %s and args %s", query, args) |
||||||
|
assert.Equal(t, count, returned, "unexpected return count %d, expected %d from %s and args %s", returned, count, query, args) |
||||||
} |
} |
@ -0,0 +1,46 @@ |
|||||||
|
package migrations |
||||||
|
|
||||||
|
import ( |
||||||
|
"context" |
||||||
|
"database/sql" |
||||||
|
|
||||||
|
wf_db "github.com/writeas/writefreely/db" |
||||||
|
) |
||||||
|
|
||||||
|
func oauth(db *datastore) error { |
||||||
|
dialect := wf_db.DialectMySQL |
||||||
|
if db.driverName == driverSQLite { |
||||||
|
dialect = wf_db.DialectSQLite |
||||||
|
} |
||||||
|
return wf_db.RunTransactionWithOptions(context.Background(), db.DB, &sql.TxOptions{}, func(ctx context.Context, tx *sql.Tx) error { |
||||||
|
createTableUsersOauth, err := dialect. |
||||||
|
Table("users_oauth"). |
||||||
|
SetIfNotExists(true). |
||||||
|
Column(dialect.Column("user_id", wf_db.ColumnTypeInteger, wf_db.UnsetSize)). |
||||||
|
Column(dialect.Column("remote_user_id", wf_db.ColumnTypeInteger, wf_db.UnsetSize)). |
||||||
|
UniqueConstraint("user_id"). |
||||||
|
UniqueConstraint("remote_user_id"). |
||||||
|
ToSQL() |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
createTableOauthClientState, err := dialect. |
||||||
|
Table("oauth_client_state"). |
||||||
|
SetIfNotExists(true). |
||||||
|
Column(dialect.Column("state", wf_db.ColumnTypeVarChar, wf_db.OptionalInt{Set: true, Value: 255})). |
||||||
|
Column(dialect.Column("used", wf_db.ColumnTypeBool, wf_db.UnsetSize)). |
||||||
|
Column(dialect.Column("created_at", wf_db.ColumnTypeDateTime, wf_db.UnsetSize).SetDefault("NOW()")). |
||||||
|
UniqueConstraint("state"). |
||||||
|
ToSQL() |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
for _, table := range []string{createTableUsersOauth, createTableOauthClientState} { |
||||||
|
if _, err := tx.ExecContext(ctx, table); err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
} |
||||||
|
return nil |
||||||
|
}) |
||||||
|
} |
Loading…
Reference in new issue