Browse Source

Ability to skip migration

Volodymyr Tkach 2 years ago
parent
commit
2120f51c80
6 changed files with 36 additions and 20 deletions
  1. 5 3
      gosql/common/common.go
  2. 3 3
      gosql/common/common_test.go
  3. 6 6
      gosql/engine/engine.go
  4. 4 4
      gosql/gosql.go
  5. 17 3
      gosql/gosql_test.go
  6. 1 1
      main.go

+ 5 - 3
gosql/common/common.go

@@ -119,7 +119,7 @@ func ParseUrl(dbURL string) (*url.URL, error) {
 	return databaseURL, nil
 }
 
-func OpenDB(databaseURL *url.URL, migrationsDir string, debug bool) (*sql.DB, error) {
+func OpenDB(databaseURL *url.URL, migrationsDir string, skipMigration bool, debug bool) (*sql.DB, error) {
 	mate := dbmate.New(databaseURL)
 
 	mate.AutoDumpSchema = false
@@ -133,8 +133,10 @@ func OpenDB(databaseURL *url.URL, migrationsDir string, debug bool) (*sql.DB, er
 		return nil, fmt.Errorf("DB get driver error: %w", err)
 	}
 
-	if err := mate.CreateAndMigrate(); err != nil {
-		return nil, fmt.Errorf("DB migration error: %w", err)
+	if !skipMigration {
+		if err := mate.CreateAndMigrate(); err != nil {
+			return nil, fmt.Errorf("DB migration error: %w", err)
+		}
 	}
 
 	var db *sql.DB

+ 3 - 3
gosql/common/common_test.go

@@ -176,7 +176,7 @@ var _ = Describe("common", func() {
 			// 	databaseURL, err := url.Parse("mysql://root:root@127.0.0.1:3306/gosql")
 			// 	Expect(err).To(Succeed())
 
-			// 	db, err := common.OpenDB(databaseURL, migrationsDir, false)
+			// 	db, err := common.OpenDB(databaseURL, migrationsDir, false, false)
 			// 	Expect(err).To(Succeed())
 			// 	Expect(db.Close()).To(Succeed())
 			// })
@@ -186,7 +186,7 @@ var _ = Describe("common", func() {
 			// 	databaseURL, err := url.Parse("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable")
 			// 	Expect(err).To(Succeed())
 
-			// 	db, err := common.OpenDB(databaseURL, migrationsDir, false)
+			// 	db, err := common.OpenDB(databaseURL, migrationsDir, false, false)
 			// 	Expect(err).To(Succeed())
 			// 	Expect(db.Close()).To(Succeed())
 			// })
@@ -199,7 +199,7 @@ var _ = Describe("common", func() {
 				databaseURL, err := url.Parse("sqlite://" + f.Name())
 				Expect(err).To(Succeed())
 
-				db, err := common.OpenDB(databaseURL, migrationsDir, false)
+				db, err := common.OpenDB(databaseURL, migrationsDir, false, false)
 				Expect(err).To(Succeed())
 				Expect(db.Close()).To(Succeed())
 			})

+ 6 - 6
gosql/engine/engine.go

@@ -12,8 +12,8 @@ type mysql struct {
 	*common.DBMethods
 }
 
-func NewMySQL(dbURL *url.URL, migrationsDir string, debug bool) (common.Engine, error) {
-	db, err := common.OpenDB(dbURL, migrationsDir, debug)
+func NewMySQL(dbURL *url.URL, migrationsDir string, skipMigration bool, debug bool) (common.Engine, error) {
+	db, err := common.OpenDB(dbURL, migrationsDir, skipMigration, debug)
 	if err != nil {
 		return nil, err
 	}
@@ -33,8 +33,8 @@ type postgresql struct {
 	*common.DBMethods
 }
 
-func NewPostgreSQL(dbURL *url.URL, migrationsDir string, debug bool) (common.Engine, error) {
-	db, err := common.OpenDB(dbURL, migrationsDir, debug)
+func NewPostgreSQL(dbURL *url.URL, migrationsDir string, skipMigration bool, debug bool) (common.Engine, error) {
+	db, err := common.OpenDB(dbURL, migrationsDir, skipMigration, debug)
 	if err != nil {
 		return nil, err
 	}
@@ -54,8 +54,8 @@ type sqlite struct {
 	*common.DBMethods
 }
 
-func NewSQLite(dbURL *url.URL, migrationsDir string, debug bool) (common.Engine, error) {
-	db, err := common.OpenDB(dbURL, migrationsDir, debug)
+func NewSQLite(dbURL *url.URL, migrationsDir string, skipMigration bool, debug bool) (common.Engine, error) {
+	db, err := common.OpenDB(dbURL, migrationsDir, skipMigration, debug)
 	if err != nil {
 		return nil, err
 	}

+ 4 - 4
gosql/gosql.go

@@ -13,7 +13,7 @@ type Rows = common.Rows
 
 type Tx = common.Tx
 
-func Open(dbURL, migrationsDir string, debug bool) (common.Engine, error) {
+func Open(dbURL, migrationsDir string, skipMigration bool, debug bool) (common.Engine, error) {
 	databaseURL, err := common.ParseUrl(dbURL)
 	if err != nil {
 		return nil, err
@@ -21,11 +21,11 @@ func Open(dbURL, migrationsDir string, debug bool) (common.Engine, error) {
 
 	switch databaseURL.Scheme {
 	case "mysql":
-		return engine.NewMySQL(databaseURL, migrationsDir, debug)
+		return engine.NewMySQL(databaseURL, migrationsDir, skipMigration, debug)
 	case "postgres", "postgresql":
-		return engine.NewPostgreSQL(databaseURL, migrationsDir, debug)
+		return engine.NewPostgreSQL(databaseURL, migrationsDir, skipMigration, debug)
 	case "sqlite", "sqlite3":
-		return engine.NewSQLite(databaseURL, migrationsDir, debug)
+		return engine.NewSQLite(databaseURL, migrationsDir, skipMigration, debug)
 	default:
 		return nil, fmt.Errorf("DB open error")
 	}

+ 17 - 3
gosql/gosql_test.go

@@ -31,7 +31,7 @@ var _ = Describe("gosql", func() {
 		// // Note: you need to up MySQL server for this test case
 		// Context("for MySQL", func() {
 		// 	It("open connection, migrate and select data", func() {
-		// 		db, err := gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", migrationsDir, false)
+		// 		db, err := gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", migrationsDir, false, false)
 		// 		Expect(err).To(Succeed())
 
 		// 		err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
@@ -51,7 +51,7 @@ var _ = Describe("gosql", func() {
 		// // Note: you need to up PostgreSQL server for this test case
 		// Context("for PostgreSQL", func() {
 		// 	It("open connection, migrate and select data", func() {
-		// 		db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false)
+		// 		db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
 		// 		Expect(err).To(Succeed())
 
 		// 		err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
@@ -74,7 +74,7 @@ var _ = Describe("gosql", func() {
 				Expect(err).To(Succeed())
 				f.Close()
 
-				db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false)
+				db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
 				Expect(err).To(Succeed())
 
 				err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
@@ -90,6 +90,20 @@ var _ = Describe("gosql", func() {
 				Expect(db.Close()).To(Succeed())
 			})
 		})
+
+		It("open connection and skip migration", func() {
+			f, err := ioutil.TempFile("", "go-sqlite-test-")
+			Expect(err).To(Succeed())
+			f.Close()
+
+			db, err := gosql.Open("sqlite://"+f.Name(), "", true, false)
+			Expect(err).To(Succeed())
+			Expect(db.Ping(ctx)).To(Succeed())
+
+			var size int
+			err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
+			Expect(err.Error()).To(Equal("no such table: users"))
+		})
 	})
 })
 

+ 1 - 1
main.go

@@ -27,7 +27,7 @@ func main() {
 
 	// Open DB connection, SQLite is used as example
 	// You can use here MySQL or PostgreSQL, just change dbURL
-	db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, true)
+	db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, true)
 	if err != nil {
 		panic(fmt.Sprintf("%s", err))
 	}