Repository: ariga/atlas
Pull request #3339 (2025-01-22 19:26:45)

sql/sqlite: remove query to sqlite_version()
The package is static and updated whenever we update the package


Patch:
diff --git a/sql/sqlite/driver.go b/sql/sqlite/driver.go
index 7925487d..81bee35e 100644
--- a/sql/sqlite/driver.go
+++ b/sql/sqlite/driver.go
@@ -37,8 +37,6 @@ type (
 	conn struct {
 		schema.ExecQuerier
 		url *sqlclient.URL
-		// System variables that are set on `Open`.
-		version string
 	}
 )
 
@@ -116,17 +114,7 @@ func opener(_ context.Context, u *url.URL) (*sqlclient.Client, error) {
 
 // Open opens a new SQLite driver.
 func Open(db schema.ExecQuerier) (migrate.Driver, error) {
-	var (
-		c   = &conn{ExecQuerier: db}
-		ctx = context.Background()
-	)
-	rows, err := db.QueryContext(ctx, "SELECT sqlite_version()")
-	if err != nil {
-		return nil, fmt.Errorf("sqlite: query version pragma: %w", err)
-	}
-	if err := sqlx.ScanOne(rows, &c.version); err != nil {
-		return nil, fmt.Errorf("sqlite: scan version pragma: %w", err)
-	}
+	c := &conn{ExecQuerier: db}
 	return &Driver{
 		conn:        c,
 		Differ:      &sqlx.Diff{DiffDriver: &diff{}},
@@ -204,11 +192,6 @@ func (d *Driver) Lock(_ context.Context, name string, timeout time.Duration) (sc
 	return acquireLock(path, timeout)
 }
 
-// Version returns the version of the connected database.
-func (d *Driver) Version() string {
-	return d.conn.version
-}
-
 // FormatType converts schema type to its column form in the database.
 func (*Driver) FormatType(t schema.Type) (string, error) {
 	return FormatType(t)


Test patch:
diff --git a/sql/sqlite/diff_test.go b/sql/sqlite/diff_test.go
index 959280cb..258050e8 100644
--- a/sql/sqlite/diff_test.go
+++ b/sql/sqlite/diff_test.go
@@ -301,9 +301,8 @@ func TestDiff_TableDiff(t *testing.T) {
 		}(),
 	}
 	for _, tt := range tests {
-		db, m, err := sqlmock.New()
+		db, _, err := sqlmock.New()
 		require.NoError(t, err)
-		mock{m}.systemVars("3.36.0")
 		drv, err := Open(db)
 		require.NoError(t, err)
 		t.Run(tt.name, func(t *testing.T) {
@@ -315,9 +314,8 @@ func TestDiff_TableDiff(t *testing.T) {
 }
 
 func TestDiff_SchemaDiff(t *testing.T) {
-	db, m, err := sqlmock.New()
+	db, _, err := sqlmock.New()
 	require.NoError(t, err)
-	mock{m}.systemVars("3.36.0")
 	drv, err := Open(db)
 	require.NoError(t, err)
 	from := &schema.Schema{
diff --git a/sql/sqlite/driver_test.go b/sql/sqlite/driver_test.go
index fbb280f4..9003595e 100644
--- a/sql/sqlite/driver_test.go
+++ b/sql/sqlite/driver_test.go
@@ -6,7 +6,6 @@ package sqlite
 
 import (
 	"context"
-	"database/sql"
 	"database/sql/driver"
 	"os"
 	"path/filepath"
@@ -16,8 +15,6 @@ import (
 
 	"ariga.io/atlas/sql/migrate"
 	"ariga.io/atlas/sql/schema"
-	"ariga.io/atlas/sql/sqlclient"
-
 	"github.com/DATA-DOG/go-sqlmock"
 	"github.com/stretchr/testify/require"
 )
@@ -36,14 +33,6 @@ func (m *mockDriver) Open(name string) (driver.Conn, error) {
 	return db.Driver().Open(name)
 }
 
-func TestParser_ParseURL(t *testing.T) {
-	drv := &mockDriver{}
-	sql.Register("libsql", drv)
-	_, err := sqlclient.Open(context.Background(), "libsql+wss://example.com/db.sqlite3?_fk=1")
-	require.Error(t, err, "did not mock queries")
-	require.Equal(t, []string{"wss://example.com/db.sqlite3?_fk=1"}, drv.opened)
-}
-
 func TestDriver_LockAcquired(t *testing.T) {
 	drv := &Driver{conn: &conn{}}
 
@@ -102,18 +91,6 @@ func TestDriver_CheckClean(t *testing.T) {
 	require.EqualError(t, err, `sql/migrate: connected database is not clean: found multiple tables: 2`)
 }
 
-func TestDriver_Version(t *testing.T) {
-	db, m, err := sqlmock.New()
-	require.NoError(t, err)
-	mock{m}.systemVars("3.36.0")
-	drv, err := Open(db)
-	require.NoError(t, err)
-
-	type vr interface{ Version() string }
-	require.Implements(t, (*vr)(nil), drv)
-	require.Equal(t, "3.36.0", drv.(vr).Version())
-}
-
 type mockInspector struct {
 	schema.Inspector
 	realm *schema.Realm
diff --git a/sql/sqlite/inspect_test.go b/sql/sqlite/inspect_test.go
index 6bdb1606..01041ddc 100644
--- a/sql/sqlite/inspect_test.go
+++ b/sql/sqlite/inspect_test.go
@@ -284,7 +284,6 @@ CREATE TABLE users(
 			db, m, err := sqlmock.New()
 			require.NoError(t, err)
 			mk := mock{m}
-			mk.systemVars("3.36.0")
 			drv, err := Open(db)
 			require.NoError(t, err)
 			tt.before(mk)
@@ -474,7 +473,6 @@ func TestRegex_Checks(t *testing.T) {
 		db, m, err := sqlmock.New()
 		require.NoError(t, err)
 		mk := mock{m}
-		mk.systemVars("3.36.0")
 		mk.tableExists(name, true, tt.input)
 		mk.noColumns(name)
 		mk.noIndexes(name)
@@ -535,7 +533,6 @@ func TestRegex_GeneratedExpr(t *testing.T) {
 		db, m, err := sqlmock.New()
 		require.NoError(t, err)
 		mk := mock{m}
-		mk.systemVars("3.36.0")
 		mk.tableExists(name, true, tt.input)
 		m.ExpectQuery(sqltest.Escape(fmt.Sprintf(columnsQuery, name))).
 			WillReturnRows(sqltest.Rows(fmt.Sprintf(`
@@ -560,15 +557,6 @@ type mock struct {
 	sqlmock.Sqlmock
 }
 
-func (m mock) systemVars(version string) {
-	m.ExpectQuery(sqltest.Escape("SELECT sqlite_version()")).
-		WillReturnRows(sqltest.Rows(`
-     version    
-----------------
- ` + version + `
-`))
-}
-
 func (m mock) tableExists(table string, exists bool, stmt ...string) {
 	m.ExpectQuery(sqltest.Escape(fmt.Sprintf(databasesQueryArgs, "?"))).
 		WithArgs("main").
diff --git a/sql/sqlite/migrate_test.go b/sql/sqlite/migrate_test.go
index a4c2f402..7c4fca1e 100644
--- a/sql/sqlite/migrate_test.go
+++ b/sql/sqlite/migrate_test.go
@@ -484,7 +484,6 @@ func TestPlanChanges(t *testing.T) {
 			db, mk, err := sqlmock.New()
 			require.NoError(t, err)
 			m := mock{mk}
-			m.systemVars("3.36.0")
 			if tt.mock != nil {
 				tt.mock(m)
 			}
@@ -625,9 +624,8 @@ func TestIndentedPlan(t *testing.T) {
 	}
 	for i, tt := range tests {
 		t.Run(strconv.Itoa(i), func(t *testing.T) {
-			db, mk, err := sqlmock.New()
+			db, _, err := sqlmock.New()
 			require.NoError(t, err)
-			mock{mk}.systemVars("3.36.0")
 			drv, err := Open(db)
 			require.NoError(t, err)
 			plan, err := drv.PlanChanges(context.Background(), "wantPlan", []schema.Change{&schema.AddTable{T: tt.T}}, func(opts *migrate.PlanOptions) {