postgres

package
v0.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2024 License: Apache-2.0 Imports: 1 Imported by: 3

Documentation

Overview

Integrates with migration tools recognizing the standard "migrate up" and "migrate down" annotations, simplifying asset database schema management and rollbacks in Postgres.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Migrations

func Migrations() embed.FS

Migrations returns the migrations for the postgres database.

Example
user := "postgres"
if u, ok := os.LookupEnv("POSTGRES_USER"); ok {
	user = u
}

password := "postgres"
if p, ok := os.LookupEnv("POSTGRES_PASSWORD"); ok {
	password = p
}

dbname := "postgres"
if db, ok := os.LookupEnv("POSTGRES_DB"); ok {
	dbname = db
}

log.Printf("DSN: %s", fmt.Sprintf("host=localhost port=5432 user=%s password=%s dbname=%s", user, password, dbname))

dsn := fmt.Sprintf("host=localhost port=5432 user=%s password=%s dbname=%s", user, password, dbname)
db, err := gorm.Open(pg.Open(dsn), &gorm.Config{})
if err != nil {
	panic("failed to connect database")
}

sqlDb, _ := db.DB()

migrationsSource := migrate.EmbedFileSystemMigrationSource{
	FileSystem: Migrations(),
	Root:       "/",
}

_, err = migrate.Exec(sqlDb, "postgres", migrationsSource, migrate.Up)
if err != nil {
	panic(err)
}

tables := []string{"entities", "entity_tags", "edges", "edge_tags"}
for _, table := range tables {
	fmt.Println(db.Migrator().HasTable(table))
}
Output:

true
true
true
true

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL