35 lines
		
	
	
	
		
			933 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			35 lines
		
	
	
	
		
			933 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| package migrate
 | |
| 
 | |
| import "database/sql"
 | |
| 
 | |
| func createSchemaIfNotExists(tx *sql.Tx, schema string) error {
 | |
| 	row := tx.QueryRow(`SELECT 1 FROM information_schema.schemata WHERE schema_name = '` + schema + `'`)
 | |
| 	err := row.Scan(new(int))
 | |
| 	if err == sql.ErrNoRows {
 | |
| 		_, err = tx.Exec(`CREATE SCHEMA ` + schema)
 | |
| 		if err != nil {
 | |
| 			return err
 | |
| 		}
 | |
| 	}
 | |
| 	return err
 | |
| }
 | |
| 
 | |
| func createTableIfNotExists(tx *sql.Tx, schema, table string) (string, error) {
 | |
| 	versionTable := table
 | |
| 	var schemaCond string
 | |
| 
 | |
| 	if schema != `` {
 | |
| 		versionTable = schema + `.` + table
 | |
| 		schemaCond = ` AND table_schema = '` + schema + `'`
 | |
| 	}
 | |
| 
 | |
| 	row := tx.QueryRow(`SELECT 1 FROM information_schema.tables WHERE table_name = '` + table + `'` + schemaCond)
 | |
| 	err := row.Scan(new(int))
 | |
| 	if err == sql.ErrNoRows {
 | |
| 		_, err = tx.Exec(`CREATE TABLE ` + versionTable + ` (version integer NOT NULL PRIMARY KEY)`)
 | |
| 		if err != nil {
 | |
| 			return ``, err
 | |
| 		}
 | |
| 	}
 | |
| 	return versionTable, err
 | |
| }
 |