Skip to content

Bug: postgres metadata reader; options are assigned twice #358

Description

@Allam76

Consider the following code:

func TestUsqlPostgresOptionsFail(t *testing.T) {
	db, _ := sql.Open("postgres", "postgres://martin:<pass>@localhost:5432/northwind")
	reader := postgres.NewReader()(db,infos.WithDataTypeFormatter(dataTypeFormatter)).(metadata.BasicReader)
	tableSet, err := reader.Tables(metadata.Filter{Schema: "public", Catalog: "northwind"})
	assert.NoError(t, err)
	assert.NotEmpty(t, tableSet)
}

Here I would like to add a custom data type formatter to the postgres metadata reader.
This fails with:
panic: interface conversion: metadata.Reader is *metadata.LoggingReader, not *informationschema.InformationSchema

From reading the offending code:

func NewReader() func(drivers.DB, ...metadata.ReaderOption) metadata.Reader {
	return func(db drivers.DB, opts ...metadata.ReaderOption) metadata.Reader {
		newIS := infos.New(
			infos.WithIndexes(false),
			infos.WithCustomClauses(map[infos.ClauseName]string{
				infos.ColumnsColumnSize:         "COALESCE(character_maximum_length, numeric_precision, datetime_precision, interval_precision, 0)",
				infos.FunctionColumnsColumnSize: "COALESCE(character_maximum_length, numeric_precision, datetime_precision, interval_precision, 0)",
			}),
			infos.WithSystemSchemas([]string{"pg_catalog", "pg_toast", "information_schema"}),
			infos.WithCurrentSchema("CURRENT_SCHEMA"),
			infos.WithDataTypeFormatter(dataTypeFormatter))
		return metadata.NewPluginReader(
			newIS(db, opts...),
			&metaReader{
				LoggingReader: metadata.NewLoggingReader(db, opts...),
			},
		)
	}
}

it is clear that the opts... are assigned twice. Last time to an incompatible reader (LoggingReader).

Maybe also consider putting

type metaReader struct

in uppercase to facilitate extension.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions