2014-08-26 18:46:41 -07:00
package cassandra
import (
"net/url"
2015-10-15 12:49:43 -04:00
"os"
2014-08-26 18:46:41 -07:00
"testing"
"time"
"github.com/gocql/gocql"
2016-08-04 15:52:31 +03:00
"github.com/dimag-jfrog/migrate/file"
"github.com/dimag-jfrog/migrate/migrate/direction"
pipep "github.com/dimag-jfrog/migrate/pipe"
2014-08-26 18:46:41 -07:00
)
func TestMigrate ( t * testing . T ) {
var session * gocql . Session
2015-10-15 12:49:43 -04:00
host := os . Getenv ( "CASSANDRA_PORT_9042_TCP_ADDR" )
port := os . Getenv ( "CASSANDRA_PORT_9042_TCP_PORT" )
driverUrl := "cassandra://" + host + ":" + port + "/system"
2014-08-26 18:46:41 -07:00
// prepare a clean test database
u , err := url . Parse ( driverUrl )
if err != nil {
t . Fatal ( err )
}
cluster := gocql . NewCluster ( u . Host )
cluster . Keyspace = u . Path [ 1 : len ( u . Path ) ]
cluster . Consistency = gocql . All
cluster . Timeout = 1 * time . Minute
session , err = cluster . CreateSession ( )
if err != nil {
t . Fatal ( err )
}
2015-10-15 12:49:43 -04:00
if err := session . Query ( ` CREATE KEYSPACE IF NOT EXISTS migrate WITH REPLICATION = { 'class': 'SimpleStrategy', 'replication_factor': 1}; ` ) . Exec ( ) ; err != nil {
2014-08-26 18:46:41 -07:00
t . Fatal ( err )
}
2015-10-15 12:49:43 -04:00
cluster . Keyspace = "migrate"
session , err = cluster . CreateSession ( )
driverUrl = "cassandra://" + host + ":" + port + "/migrate"
2014-08-26 18:46:41 -07:00
d := & Driver { }
if err := d . Initialize ( driverUrl ) ; err != nil {
t . Fatal ( err )
}
files := [ ] file . File {
{
Path : "/foobar" ,
FileName : "001_foobar.up.sql" ,
Version : 1 ,
Name : "foobar" ,
Direction : direction . Up ,
Content : [ ] byte ( `
CREATE TABLE yolo (
2014-11-17 11:54:21 +01:00
id varint primary key ,
msg text
2014-08-26 18:46:41 -07:00
) ;
2014-11-17 11:54:21 +01:00
CREATE INDEX ON yolo ( msg ) ;
2014-08-26 18:46:41 -07:00
` ) ,
} ,
{
Path : "/foobar" ,
FileName : "002_foobar.down.sql" ,
Version : 1 ,
Name : "foobar" ,
Direction : direction . Down ,
Content : [ ] byte ( `
DROP TABLE yolo ;
` ) ,
} ,
{
Path : "/foobar" ,
FileName : "002_foobar.up.sql" ,
Version : 2 ,
Name : "foobar" ,
Direction : direction . Up ,
Content : [ ] byte ( `
CREATE TABLE error (
id THIS WILL CAUSE AN ERROR
)
` ) ,
} ,
}
pipe := pipep . New ( )
go d . Migrate ( files [ 0 ] , pipe )
errs := pipep . ReadErrors ( pipe )
if len ( errs ) > 0 {
t . Fatal ( errs )
}
pipe = pipep . New ( )
go d . Migrate ( files [ 1 ] , pipe )
errs = pipep . ReadErrors ( pipe )
if len ( errs ) > 0 {
t . Fatal ( errs )
}
pipe = pipep . New ( )
go d . Migrate ( files [ 2 ] , pipe )
errs = pipep . ReadErrors ( pipe )
if len ( errs ) == 0 {
t . Error ( "Expected test case to fail" )
}
if err := d . Close ( ) ; err != nil {
t . Fatal ( err )
}
}