DB Migrations
Replacing modl’s built-in table creation and removal with manual migrations. Thanks to @DavidHuie for gomigrate!
This commit is contained in:
		
							parent
							
								
									c5f94ab67d
								
							
						
					
					
						commit
						8fa594e3f0
					
				
					 6 changed files with 53 additions and 17 deletions
				
			
		|  | @ -117,8 +117,10 @@ The options are: | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	datastore.Connect() | 	datastore.Connect() | ||||||
|  | 	migrationsPath := "./datastore/migrations" | ||||||
|  | 
 | ||||||
| 	if *drop { | 	if *drop { | ||||||
| 		datastore.Drop() | 		datastore.Drop(migrationsPath) | ||||||
| 	} | 	} | ||||||
| 	datastore.Create() | 	datastore.Create(migrationsPath) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -5,6 +5,7 @@ import ( | ||||||
| 	"os" | 	"os" | ||||||
| 	"sync" | 	"sync" | ||||||
| 
 | 
 | ||||||
|  | 	"github.com/DavidHuie/gomigrate" | ||||||
| 	"github.com/jmoiron/modl" | 	"github.com/jmoiron/modl" | ||||||
| 	"github.com/jmoiron/sqlx" | 	"github.com/jmoiron/sqlx" | ||||||
| 	_ "github.com/lib/pq" | 	_ "github.com/lib/pq" | ||||||
|  | @ -37,21 +38,32 @@ func Connect() { | ||||||
| var createSQL []string | var createSQL []string | ||||||
| 
 | 
 | ||||||
| // Create the database schema. It calls log.Fatal if it encounters an error. | // Create the database schema. It calls log.Fatal if it encounters an error. | ||||||
| func Create() { | func Create(path string) { | ||||||
| 	if err := DB.CreateTablesIfNotExists(); err != nil { | 	migrator, err := gomigrate.NewMigrator(DB.Dbx.DB, gomigrate.Postgres{}, path) | ||||||
| 		log.Fatal("Error creating tables: ", err) | 	if err != nil { | ||||||
| 	} | 		log.Fatal("Error initializing migrations: ", err) | ||||||
| 	for _, query := range createSQL { |  | ||||||
| 		if _, err := DB.Exec(query); err != nil { |  | ||||||
| 			log.Fatalf("Error running query %q: %s", query, err) |  | ||||||
| 	} | 	} | ||||||
|  | 
 | ||||||
|  | 	pwd, err := os.Getwd() | ||||||
|  | 	log.Print("current path: ", pwd) | ||||||
|  | 
 | ||||||
|  | 	err = migrator.Migrate() | ||||||
|  | 	if err != nil { | ||||||
|  | 		log.Fatal("Error applying migrations: ", err) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Drop the database schema | // Drop the database schema | ||||||
| func Drop() { | func Drop(path string) { | ||||||
| 	// TODO(mrd): raise errors. | 	migrator, err := gomigrate.NewMigrator(DB.Dbx.DB, gomigrate.Postgres{}, path) | ||||||
| 	DB.DropTables() | 	if err != nil { | ||||||
|  | 		log.Fatal("Error initializing migrations: ", err) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	err = migrator.RollbackAll() | ||||||
|  | 	if err != nil { | ||||||
|  | 		log.Fatal("Error rolling back migrations: ", err) | ||||||
|  | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // transact calls fn in a DB transaction. If dbh is a transaction, then it just calls | // transact calls fn in a DB transaction. If dbh is a transaction, then it just calls | ||||||
|  |  | ||||||
|  | @ -21,6 +21,7 @@ func init() { | ||||||
| 
 | 
 | ||||||
| 	// Reset DB | 	// Reset DB | ||||||
| 	Connect() | 	Connect() | ||||||
| 	Drop() | 	migrationsPath := "./migrations" | ||||||
| 	Create() | 	Drop(migrationsPath) | ||||||
|  | 	Create(migrationsPath) | ||||||
| } | } | ||||||
|  |  | ||||||
							
								
								
									
										5
									
								
								datastore/migrations/00001_AddUsers_down.sql
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								datastore/migrations/00001_AddUsers_down.sql
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,5 @@ | ||||||
|  | -- bactdb | ||||||
|  | -- Matthew R Dillon | ||||||
|  | 
 | ||||||
|  | DROP TABLE users; | ||||||
|  | 
 | ||||||
							
								
								
									
										19
									
								
								datastore/migrations/00001_AddUsers_up.sql
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								datastore/migrations/00001_AddUsers_up.sql
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,19 @@ | ||||||
|  | -- bactdb | ||||||
|  | -- Matthew R Dillon | ||||||
|  | 
 | ||||||
|  | CREATE TABLE users ( | ||||||
|  |     id BIGSERIAL NOT NULL, | ||||||
|  |     username CHARACTER VARYING(100), | ||||||
|  | 
 | ||||||
|  |     createdat TIMESTAMP WITH TIME ZONE, | ||||||
|  |     updatedat TIMESTAMP WITH TIME ZONE, | ||||||
|  |     deletedat TIMESTAMP WITH TIME ZONE, | ||||||
|  | 
 | ||||||
|  |     CONSTRAINT users_pkey PRIMARY KEY (id) | ||||||
|  | ); | ||||||
|  | 
 | ||||||
|  | CREATE UNIQUE INDEX username_idx | ||||||
|  |     ON users | ||||||
|  |     USING btree | ||||||
|  |     (username COLLATE pg_catalog."default"); | ||||||
|  | 
 | ||||||
|  | @ -12,9 +12,6 @@ import ( | ||||||
| 
 | 
 | ||||||
| func init() { | func init() { | ||||||
| 	DB.AddTableWithName(models.User{}, "users").SetKeys(true, "Id") | 	DB.AddTableWithName(models.User{}, "users").SetKeys(true, "Id") | ||||||
| 	createSQL = append(createSQL, |  | ||||||
| 		`CREATE UNIQUE INDEX username_idx ON users (username);`, |  | ||||||
| 	) |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| type usersStore struct { | type usersStore struct { | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Matthew Dillon
						Matthew Dillon