Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unnecessary enum type generated for actual schema #2475

Closed
maksim-ramenskiy opened this issue Jul 18, 2023 · 1 comment · Fixed by #2853
Closed

unnecessary enum type generated for actual schema #2475

maksim-ramenskiy opened this issue Jul 18, 2023 · 1 comment · Fixed by #2853
Milestone

Comments

@maksim-ramenskiy
Copy link

Version

1.19.1

What happened?

The code associated with the enum field is generated even if the table is dropped after define this field.

Relevant log output

type StreamSessionState string

const (
	StreamSessionStateACTIVATING                StreamSessionState = "ACTIVATING"
	StreamSessionStateACTIVE                    StreamSessionState = "ACTIVE"
	StreamSessionStateCONNECTED                 StreamSessionState = "CONNECTED"
	StreamSessionStatePENDINGCLIENTRECONNECTION StreamSessionState = "PENDING_CLIENT_RECONNECTION"
	StreamSessionStateTERMINATED                StreamSessionState = "TERMINATED"
	StreamSessionStateERROR                     StreamSessionState = "ERROR"
)

func (e *StreamSessionState) Scan(src interface{}) error {
	switch s := src.(type) {
	case []byte:
		*e = StreamSessionState(s)
	case string:
		*e = StreamSessionState(s)
	default:
		return fmt.Errorf("unsupported scan type for StreamSessionState: %T", src)
	}
	return nil
}

type NullStreamSessionState struct {
	StreamSessionState StreamSessionState
	Valid                      bool // Valid is true if StreamSessionState is not NULL
}

// Scan implements the Scanner interface.
func (ns *NullStreamSessionState) Scan(value interface{}) error {
	if value == nil {
		ns.StreamSessionState, ns.Valid = "", false
		return nil
	}
	ns.Valid = true
	return ns.StreamSessionState.Scan(value)
}

// Value implements the driver Valuer interface.
func (ns NullStreamSessionState) Value() (driver.Value, error) {
	if !ns.Valid {
		return nil, nil
	}
	return string(ns.StreamSessionState), nil
}

Database schema

CREATE TABLE `stream_session` (
  `id` int NOT NULL AUTO_INCREMENT,
  `external_id` varchar(256) DEFAULT NULL,
  `stream_group_id` int NOT NULL,
  `state` enum('ACTIVATING','ACTIVE','CONNECTED','PENDING_CLIENT_RECONNECTION','TERMINATED','ERROR') NOT NULL,
  PRIMARY KEY (`id`),
  KEY `stream_group_id` (`stream_group_id`),
  CONSTRAINT `stream_session_ibfk_1` FOREIGN KEY (`stream_group_id`) REFERENCES `stream_group` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DROP TABLE stream_session;

SQL queries

No response

Configuration

version: "2"
sql:
- schema: "db/schema/DDL"
  queries: "db/query"
  engine: "mysql"
  gen:
    go:
      package: "db"
      out: "db/sqlc"    
      emit_json_tags: true
      emit_interface: true
      emit_empty_slices: true
      emit_exact_table_names: true

Playground URL

No response

What operating system are you using?

macOS

What database engines are you using?

MySQL

What type of code are you generating?

Go

@maksim-ramenskiy maksim-ramenskiy added bug Something isn't working triage New issues that hasn't been reviewed labels Jul 18, 2023
@maksim-ramenskiy maksim-ramenskiy changed the title enum type generated for dropped table unnecessary enum type generated for actual schema Jul 19, 2023
@andrewmbenton
Copy link
Collaborator

If you use the config option omit_unused_structs I think sqlc won't generate that enum. But it does seem like this is a bug.

@kyleconroy kyleconroy added 📚 mysql 🔧 golang 💻 darwin and removed triage New issues that hasn't been reviewed labels Oct 2, 2023
@kyleconroy kyleconroy added this to the 1.23.0 milestone Oct 2, 2023
kyleconroy added a commit that referenced this issue Oct 13, 2023
Extends the work done in #2680 to support adding and dropping columns and tables.

Fixes #2475
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants