Пример #1
0
// getConfigOrUseTravis returns the parsed db.json if it exists or the
// travisCI config if it does not
func getConfigOrUseTravis() (config.Database, error) {
	conf, err := config.Parse("./db.json")
	if os.IsNotExist(err) {
		return travisCI, nil
	}
	return conf, err
}

// The sql dialect must implement the dialect interface
var _ dialect.Dialect = &PostGres{}

var things = sql.Table("things",
	sql.Column("name", types.Varchar()),
	sql.Column("created_at", Timestamp().Default(Now)),
)

type thing struct {
	Name      string
	CreatedAt time.Time `db:",omitempty"`
}

var meetings = Table("meetings",
	sql.Column("uuid", UUID().NotNull().Unique().Default(GenerateV4)),
	sql.Column("time", TimestampRange()),
)

// TODO custom type for TimestampRange
Пример #2
0
	return user.ID != 0
}

// Name returns the concatenated first and last name
func (user User) Name() string {
	return fmt.Sprintf("%s %s", user.FirstName, user.LastName)
}

// String returns the user id and email
func (user User) String() string {
	return fmt.Sprintf("%d: %s", user.ID, user.Email)
}

// Users is the postgres schema for users
var Users = postgres.Table("users",
	sol.Column("id", postgres.Serial()),
	sol.Column("email", types.Varchar().Limit(256).NotNull()),
	sol.Column("first_name", types.Varchar().Limit(64).NotNull()),
	sol.Column("last_name", types.Varchar().Limit(64).NotNull()),
	sol.Column("about", types.Varchar().Limit(512).NotNull()),
	sol.Column("photo", types.Varchar().Limit(512).NotNull()),
	sol.Column("is_active", types.Boolean().NotNull().Default(true)),
	sol.Column("is_superuser", types.Boolean().NotNull().Default(false)),
	sol.Column("password", types.Varchar().Limit(256).NotNull()),
	sol.Column("token", types.Varchar().Limit(256).NotNull()),
	sol.Column(
		"token_set_at",
		postgres.Timestamp().WithTimezone().NotNull().Default(postgres.Now),
	),
	sol.Column(
		"created_at",
Пример #3
0
// was not deleted from the database. It will panic on any connection error.
func (session Session) Delete() error {
	if !session.Exists() {
		return fmt.Errorf("auth: keyless sessions cannot be deleted")
	}
	return session.manager.Delete(session.Key)
}

// Exists returns true if the session exists
func (session Session) Exists() bool {
	return session.Key != ""
}

// Sessions is the postgres schema for sessions
var Sessions = postgres.Table("sessions",
	sol.Column("key", types.Varchar().NotNull()),
	sol.ForeignKey(
		"user_id",
		Users.C("id"),
		types.Integer().NotNull(),
	).OnDelete(sol.Cascade).OnUpdate(sol.Cascade),
	sol.Column("expires", postgres.Timestamp().WithTimezone()),
	sol.PrimaryKey("key"),
)

// SessionManager is the internal manager of sessions
type SessionManager struct {
	conn    sol.Conn
	cookie  config.Cookie
	keyFunc KeyFunc
	nowFunc func() time.Time
Пример #4
0
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	sql "github.com/aodin/sol"
	"github.com/aodin/sol/dialect"
	"github.com/aodin/sol/types"
)

// The sql dialect must implement the dialect interface
var _ dialect.Dialect = &Sqlite3{}

var things = sql.Table("things",
	sql.Column("name", types.Varchar()),
	sql.Column("created_at", types.Timestamp()), // TODO auto-timestamp?
)

type thing struct {
	Name      string
	CreatedAt *time.Time `db:",omitempty"`
}

// Connect to an in-memory sqlite3 instance and execute some statements.
func TestSqlite3(t *testing.T) {
	conn, err := sql.Open("sqlite3", ":memory:")
	require.Nil(t, err, `Failed to connect to in-memory sqlite3 instance`)
	defer conn.Close()

	require.Nil(t,
Пример #5
0
// was not deleted from the database. It will panic on any connection error.
func (token Token) Delete() error {
	if !token.Exists() {
		return fmt.Errorf("auth: keyless tokens cannot be deleted")
	}
	return token.manager.Delete(token.Key)
}

// Exists returns true if the token exists
func (token Token) Exists() bool {
	return token.Key != ""
}

// Tokens is the postgres schema for user API tokens.
var Tokens = postgres.Table("tokens",
	sol.Column("key", types.Varchar().NotNull()),
	sol.ForeignKey(
		"user_id",
		Users.C("id"),
		types.Integer().NotNull(),
	).OnDelete(sol.Cascade).OnUpdate(sol.Cascade),
	sol.Column("expires", postgres.Timestamp().WithTimezone()),
	sol.Column(
		"created_at",
		postgres.Timestamp().WithTimezone().NotNull().Default(postgres.Now),
	),
	sol.PrimaryKey("key"),
)

// TokenManager is the internal manager of tokens
type TokenManager struct {