Compare commits

..

No commits in common. "402c51497085474456eda1d870d8f66c40bbb4ca" and "2b46385126a5297933868d190087c355ba3c74da" have entirely different histories.

3 changed files with 23 additions and 54 deletions

View File

@ -36,64 +36,38 @@ func Migrate(app *app.App, anyStruct interface{}) error {
// createTable creates a table with the given name if it doesn't exist, it is assumed that id will be the primary key
func createTable(app *app.App, tableName string) error {
// Check to see if the table already exists
var tableExists bool
err := app.Db.QueryRow("SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname ~ $1 AND pg_catalog.pg_table_is_visible(c.oid))", "^"+tableName+"$").Scan(&tableExists)
sanitizedTableQuery := fmt.Sprintf("CREATE TABLE IF NOT EXISTS \"%s\" (\"Id\" serial primary key)", tableName)
_, err := app.Db.Query(sanitizedTableQuery)
if err != nil {
log.Println("Error checking if table exists: " + tableName)
log.Println("Error creating table: " + tableName)
return err
}
if tableExists {
log.Println("Table already exists: " + tableName)
return nil
} else {
sanitizedTableQuery := fmt.Sprintf("CREATE TABLE IF NOT EXISTS \"%s\" (\"Id\" serial primary key)", tableName)
_, err := app.Db.Query(sanitizedTableQuery)
if err != nil {
log.Println("Error creating table: " + tableName)
return err
}
log.Println("Table created successfully: " + tableName)
return nil
}
log.Println("Table created successfully (or already exists): " + tableName)
return nil
}
// createColumn creates a column with the given name and type if it doesn't exist
func createColumn(app *app.App, tableName, columnName, columnType string) error {
// Check to see if the column already exists
var columnExists bool
err := app.Db.QueryRow("SELECT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = $1 AND column_name = $2)", tableName, columnName).Scan(&columnExists)
postgresType, err := getPostgresType(columnType)
if err != nil {
log.Println("Error checking if column exists: " + columnName + " in table: " + tableName)
log.Println("Error creating column: " + columnName + " in table: " + tableName + " with type: " + postgresType)
return err
}
if columnExists {
log.Println("Column already exists: " + columnName + " in table: " + tableName)
return nil
} else {
postgresType, err := getPostgresType(columnType)
if err != nil {
log.Println("Error creating column: " + columnName + " in table: " + tableName + " with type: " + postgresType)
return err
}
sanitizedTableName := pq.QuoteIdentifier(tableName)
query := fmt.Sprintf("ALTER TABLE %s ADD COLUMN IF NOT EXISTS \"%s\" %s", sanitizedTableName, columnName, postgresType)
sanitizedTableName := pq.QuoteIdentifier(tableName)
query := fmt.Sprintf("ALTER TABLE %s ADD COLUMN IF NOT EXISTS \"%s\" %s", sanitizedTableName, columnName, postgresType)
_, err = app.Db.Query(query)
if err != nil {
log.Println("Error creating column: " + columnName + " in table: " + tableName + " with type: " + postgresType)
return err
}
log.Println("Column created successfully:", columnName)
return nil
_, err = app.Db.Query(query)
if err != nil {
log.Println("Error creating column: " + columnName + " in table: " + tableName + " with type: " + postgresType)
return err
}
log.Println("Column created successfully (or already exists):", columnName)
return nil
}
// Given a type in Go, return the corresponding type in Postgres

View File

@ -3,7 +3,6 @@ package models
import (
"GoWeb/app"
"GoWeb/database"
"time"
)
// RunAllMigrations defines the structs that should be represented in the database
@ -14,13 +13,9 @@ func RunAllMigrations(app *app.App) error {
Username: "migrate",
Password: "migrate",
AuthToken: "migrate",
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
err := database.Migrate(app, user)
if err != nil {
return err
CreatedAt: "2021-01-01 00:00:00",
UpdatedAt: "2021-01-01 00:00:00",
}
return nil
return database.Migrate(app, user)
}

View File

@ -19,8 +19,8 @@ type User struct {
Username string
Password string
AuthToken string
CreatedAt time.Time
UpdatedAt time.Time
CreatedAt string
UpdatedAt string
}
// GetCurrentUser finds the currently logged-in user by session cookie