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

Generator: add an option to skip partitions #437

Open
MassimilianoPoggiQonto opened this issue Dec 27, 2024 · 4 comments
Open

Generator: add an option to skip partitions #437

MassimilianoPoggiQonto opened this issue Dec 27, 2024 · 4 comments
Labels

Comments

@MassimilianoPoggiQonto
Copy link

Currently if you have partitioned tables, with partitions residing on the same schema as your tables, jet will generate schemas for each partition individually.

It would be great if an --ignore-partitions flag could be added so that partitions are not included for generation.

@go-jet
Copy link
Owner

go-jet commented Jan 7, 2025

Different developers use various naming templates for partitions, so there's no single universal solution to exclude partition tables from generation.
You can customize generator to skip unwanted tables.

@MassimilianoPoggiQonto
Copy link
Author

MassimilianoPoggiQonto commented Jan 8, 2025

MSSql can rely on sys.partitions and Postgres can rely on pg_class.relispartition to identify partitions.

@go-jet
Copy link
Owner

go-jet commented Jan 10, 2025

Yeah, it seems you're right. We could add generator flag to handle this use case.

@go-jet go-jet added the good first issue Good for newcomers label Jan 10, 2025
@lkzcover
Copy link

I’m trying to do it in the custom postgres.GenerateDSN() method. It works for the model package, but I haven’t understood how to skip partitions in the table package yet, because I can’t find a way to filter schemaMetadata in template.ProcessSchema yet.

Example:

func isPartitionTable(db *sql.DB, tableName string) bool {
	var count int
	err := db.QueryRow(`
		SELECT COUNT(*)
		FROM pg_inherits
		JOIN pg_class c ON pg_inherits.inhrelid = c.oid
		WHERE c.relname = $1`, tableName).Scan(&count)

	if err != nil {
		log.Fatal(err.Error())
	}
	return count > 0
}

err = postgres.GenerateDSN(
	dsn,
	schema,
	path,
	template.Default(postgres2.Dialect).
		UseSchema(func(schemaMetaData metadata.Schema) template.Schema {
			return template.DefaultSchema(schemaMetaData).
				UseModel(template.DefaultModel().
					UseTable(func(table metadata.Table) template.TableModel {
						if isPartitionTable(db, table.Name) {
							return template.TableModel{Skip: true}
						}
						...
					}),
				)
		}),
)

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

No branches or pull requests

3 participants