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

feature: create reflect source #1139

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jdudmesh
Copy link

Create a source which supplies migrations from a struct which can be read at runtime using reflection.

This driver allows you to define your up/down statements in a struct (even an anonymous struct). The driver uses reflection to examine the fields of the struct and return the relevant statements at runtime.

Struct fields must end with '_up' or '_down' and the driver pairs matching fields automatically. The order of execution is the same as the order of definition. There doesn't need to be a matching down for each up statement, orphaned down statements are ignored.

The migration version is calculated automatically by default but if you want to, you can specify it manually by adding a migrate tag to each field of the struct. See example #2.

Example 1 - auto version

	migrations := &struct {
		Users_up   string
		Users_down string
		Folders_up string
		Posts_up   string
		Posts_down string
	}{

		Users_up: `
			create table users (
			id int not null primary key,
			created_at timestamp not null,
			email text not null,
			password text not null);`,

		Users_down: `drop table users;`,

		Folders_up: `create table folders (
			id int not null primary key,
			created_at timestamp not null,
			label text not null);`,

		Posts_up: `
			create table posts (
			id int not null primary key,
			created_at timestamp not null,
			user_id int not null,
			body text not null);`,

		Posts_down: `drop table posts;`,
	}

	driver, err := New(migrations)
	if err != nil {
		log.Fatal(err)
	}

	driver, err = driver.Open("")
	if err != nil {
		log.Fatal(err)
	}
  ...

Example 2 - struct tags

	migrations := &struct {
		Table1_up   string `migrate:"1"`
		Table1_down string `migrate:"1"`
		Table2_up   string `migrate:"3"`
		Table3_up   string `migrate:"4"`
		Table3_down string `migrate:"4"`
		Table4_down string `migrate:"5"`
		Table5_down string `migrate:"7"`
		Table5_up   string `migrate:"7"`
	}{
		Table1_up:   `test statement 1`,
		Table1_down: `test statement 2`,
		Table2_up:   `test statement 3`,
		Table3_up:   `test statement 4`,
		Table3_down: `test statement 5`,
		Table4_down: `test statement 6`,
		Table5_up:   `test statement 7`,
		Table5_down: `test statement 8`,
	}

Create a source which supplies migrations from a struct which can be read at runtime using reflection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant