File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -88,6 +88,14 @@ func WithDialect(dialect Dialect) MorphOption {
8888 }
8989}
9090
91+ // WithMigrations adds the given migrations to be executed.
92+ func WithMigrations (migrations ... Migration ) MorphOption {
93+ return func (m * Morpher ) error {
94+ m .Migrations = append (m .Migrations , migrations ... )
95+ return nil
96+ }
97+ }
98+
9199// WithLog sets the logger that is to be used. If none is supplied, the default logger
92100// is used instead.
93101func WithLog (log * slog.Logger ) MorphOption {
Original file line number Diff line number Diff line change @@ -100,6 +100,39 @@ func TestMigrationUpdate(t *testing.T) {
100100 assert .NoError (t , runErr , "migrations could not be run" )
101101}
102102
103+ type TestMigrationImpl struct {}
104+
105+ func (m TestMigrationImpl ) Key () string { return "TestMigration" }
106+ func (m TestMigrationImpl ) Migrate (tx * sql.Tx ) error {
107+ _ , err := tx .Exec ("CREATE TABLE t0 (id INTEGER PRIMARY KEY)" )
108+ return err
109+ }
110+
111+ // TestWithMigrations tests the adding of migrations using WithMigrations.
112+ func TestWithMigrations (t * testing.T ) {
113+ dbFile , dbFileErr := prepareDB ()
114+
115+ if dbFileErr != nil {
116+ t .Errorf ("DB file could not be created: %v" , dbFileErr )
117+ } else {
118+ defer func () { _ = os .Remove (dbFile ) }()
119+ }
120+
121+ db , dbErr := sql .Open ("sqlite" , dbFile )
122+
123+ if dbErr != nil {
124+ t .Errorf ("DB file could not be created: %v" , dbErr )
125+ } else {
126+ defer func () { _ = db .Close () }()
127+ }
128+
129+ runErr := Run (db ,
130+ WithDialect (DialectSQLite ()),
131+ WithMigrations (TestMigrationImpl {}))
132+
133+ assert .NoError (t , runErr , "did not expect error" )
134+ }
135+
103136// TestMigrationUnableToCreateMorpher tests to use the Run function without any
104137// useful parameter.
105138func TestMigrationUnableToCreateMorpher (t * testing.T ) {
You can’t perform that action at this time.
0 commit comments