rel icon indicating copy to clipboard operation
rel copied to clipboard

Extend Adapter interface to include function to return adapter name

Open lafriks opened this issue 2 years ago • 6 comments

Would be useful to allow check what adapter is used currently so that database type specific code could be executed only on right adapters.

func Name() string

or

func Type() string

that would return mysql, postgres, sqlite3 etc

lafriks avatar Sep 28 '23 12:09 lafriks

could you give examples?

and is the database type specific code will be in sql package?

Fs02 avatar Sep 28 '23 23:09 Fs02

no it's for usage in user code later on, for one I would want this to use in migrator package for such code to be able to lock down migrations so that if multiple instances are started at same time (for example in containers) they would not run same migrations:

	if m.typ == DatabaseSQLite3 {
		if err := m.sync(ctx); err != nil {
			cancelUpdate(err)
		}
		return func(ctx context.Context, vers int64) {
			if vers == 0 {
				return
			}
			if err := m.repo.Insert(ctx, &version{Version: vers}); err != nil {
				cancelUpdate(err)
			}
		}, func() {}
	}

	updZeroVersion := "INSERT INTO versions(version, created_at) VALUES (0, current_timestamp) ON CONFLICT(version) DO UPDATE SET updated_at = current_timestamp"
	if m.typ == DatabaseMySQL {
		updZeroVersion = "INSERT INTO versions(version, created_at) VALUES (0, current_timestamp) ON DUPLICATE KEY UPDATE updated_at = current_timestamp"
	}

currently because of that I have to copy over migrator code and can not really implement it upstream as instead of go-rel/migration package: https://github.com/go-rel/migration/blob/dbef2632dc4efecbb107b4c61648a0d0d255c0ac/migration.go#L170 I use:

// New migratorr.
func New(typ string, repo rel.Repository) *Migrator {
	return &Migrator{
		typ:  typ,
		repo: repo,
	}
}

instead I could use for db specific code something like m.repo.Adapter().Type() == "sqlite3" instead of having to keep around current database type in code and pass it all around for database type specific SQLs etc

lafriks avatar Sep 29 '23 10:09 lafriks

got it, I think it's should be okay to add some metadata information like this

I prefer func Name() string, and maybe have a constant (ex: Name) in each adapter package so comparison can be done like: m.repo.Adapter().Type() == sqlite3.Name

Fs02 avatar Oct 01 '23 11:10 Fs02

Ok, I will create PR

lafriks avatar Oct 01 '23 13:10 lafriks

@Fs02 please review: https://github.com/go-rel/rel/pull/346 https://github.com/go-rel/sql/pull/59 https://github.com/go-rel/sqlite3/pull/53

If naming/implementation looks good I will create PRs for other adapters

lafriks avatar Oct 03 '23 14:10 lafriks

@lafriks need to be updated too https://github.com/go-rel/reltest/blob/main/nop_adapter.go https://github.com/go-rel/mssql

Fs02 avatar Oct 14 '23 11:10 Fs02