Example #1
0
func TestCreateTable(t *testing.T) {
	assert := assert.New(t)
	tx, err := spiffy.DefaultDb().Begin()
	assert.Nil(err)
	defer tx.Rollback()

	tableName := util.String.RandomString(12)
	err = createTestTable(tableName, nil)
	assert.Nil(err)

	exists, err := tableExists(spiffy.DefaultDb(), nil, tableName)
	assert.Nil(err)
	assert.True(exists, "table does not exist")
}
Example #2
0
func TestCreateRole(t *testing.T) {
	assert := assert.New(t)
	tx, err := spiffy.DefaultDb().Begin()
	assert.Nil(err)
	defer tx.Rollback()

	roleName := util.String.RandomString(32)
	err = createTestRole(roleName, tx)
	assert.Nil(err)

	exists, err := roleExists(spiffy.DefaultDb(), tx, roleName)
	assert.Nil(err)
	assert.True(exists, "role does not exist")
}
Example #3
0
func TestCreateIndex(t *testing.T) {
	assert := assert.New(t)
	tx, err := spiffy.DefaultDb().Begin()
	assert.Nil(err)
	defer tx.Rollback()

	tableName := util.String.RandomString(12)
	err = createTestTable(tableName, tx)
	assert.Nil(err)

	indexName := fmt.Sprintf("ix_%s_%s", tableName, util.String.RandomString(12))
	err = createTestIndex(tableName, indexName, tx)
	assert.Nil(err)

	exists, err := indexExists(spiffy.DefaultDb(), tx, tableName, indexName)
	assert.Nil(err)
	assert.True(exists, "constraint does not exist")
}
Example #4
0
func TestCreateColumn(t *testing.T) {
	assert := assert.New(t)
	tx, err := spiffy.DefaultDb().Begin()
	assert.Nil(err)
	defer tx.Rollback()

	tableName := util.String.RandomString(12)
	err = createTestTable(tableName, tx)
	assert.Nil(err)

	columnName := util.String.RandomString(12)
	err = createTestColumn(tableName, columnName, tx)
	assert.Nil(err)

	exists, err := columnExists(spiffy.DefaultDb(), tx, tableName, columnName)
	assert.Nil(err)
	assert.True(exists, "column does not exist on table")
}
Example #5
0
func seedObjects(count int) error {
	var err error
	for x := 0; x < count; x++ {
		err = spiffy.DefaultDb().Create(newTestObject())
		if err != nil {
			return err
		}
	}
	return nil
}
Example #6
0
func createTable() error {
	m := migration.New(
		"create `test_object` table",
		migration.Step(
			migration.AlterTable,
			migration.Body(
				`DROP TABLE IF EXISTS test_object`,
			),
			"test_object",
		),
		migration.Step(
			migration.CreateTable,
			migration.Body("CREATE TABLE test_object (id serial not null, uuid varchar(64) not null, created_utc timestamp not null, updated_utc timestamp, active boolean, name varchar(64), variance float)"),
			"test_object",
		),
	)
	return m.Apply(spiffy.DefaultDb())
}
Example #7
0
func main() {
	err := spiffy.SetDefaultDb(spiffy.NewDbConnectionFromEnvironment())
	if err != nil {
		log.Fatal(err)
	}

	m := migration.New(
		"create & fill `test_vocab`",
		migration.New(
			"create `test_vocab` table",
			migration.Step(
				migration.CreateTable,
				migration.Body(
					"CREATE TABLE test_vocab (id serial not null, word varchar(32) not null);",
					"ALTER TABLE test_vocab ADD CONSTRAINT pk_test_vocab_id PRIMARY KEY(id);",
				),
				"test_vocab",
			),
		),
		migration.New(
			"fill `test_vocab`",
			migration.ReadDataFile("data.sql"),
		),
		migration.New(
			"drop `test_vocab` table",
			migration.Step(
				migration.AlterTable,
				migration.Body(
					"DROP TABLE test_vocab;",
				),
				"test_vocab",
			),
		),
	)
	m.SetLogger(migration.NewLogger())
	m.SetShouldAbortOnError(true)
	err = m.Apply(spiffy.DefaultDb())
	if err != nil {
		log.Fatal(err)
	}
}
Example #8
0
func createTestRole(roleName string, tx *sql.Tx) error {
	body := fmt.Sprintf("CREATE ROLE %s;", roleName)
	step := Step(CreateRole, Body(body), roleName)
	return step.Apply(spiffy.DefaultDb(), tx)
}
Example #9
0
func createTestIndex(tableName, indexName string, tx *sql.Tx) error {
	body := fmt.Sprintf("CREATE INDEX %s ON %s (name);", indexName, tableName)
	step := Step(CreateIndex, Body(body), tableName, indexName)
	return step.Apply(spiffy.DefaultDb(), tx)
}
Example #10
0
func createTestConstraint(tableName, constraintName string, tx *sql.Tx) error {
	body := fmt.Sprintf("ALTER TABLE %s ADD CONSTRAINT %s UNIQUE (name);", tableName, constraintName)
	step := Step(CreateColumn, Body(body), tableName, constraintName)
	return step.Apply(spiffy.DefaultDb(), tx)
}
Example #11
0
func createTestColumn(tableName, columnName string, tx *sql.Tx) error {
	body := fmt.Sprintf("ALTER TABLE %s ADD %s varchar(32);", tableName, columnName)
	step := Step(CreateColumn, Body(body), tableName, columnName)
	return step.Apply(spiffy.DefaultDb(), tx)
}
Example #12
0
func createTestTable(tableName string, tx *sql.Tx) error {
	body := fmt.Sprintf("CREATE TABLE %s (id int, name varchar(32));", tableName)
	step := Step(CreateTable, Body(body), tableName)
	return step.Apply(spiffy.DefaultDb(), tx)
}
Example #13
0
func dropTable() error {
	return spiffy.DefaultDb().Exec("DROP TABLE IF EXISTS test_object")
}