Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions examples/real-project/starter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ import (

"github.com/Liphium/magic/v2"
"github.com/Liphium/magic/v2/mconfig"
"github.com/Liphium/magic/v2/mrunner/databases"
"github.com/Liphium/magic/v2/scripting"
)

func BuildMagicConfig() magic.Config {
return magic.Config{
AppName: "magic-example-real-project",
PlanDeployment: func(ctx *mconfig.Context) {
// Create a PostgreSQL database for the posts service
postsDB := ctx.NewPostgresDatabase("posts")

// Create a new driver for PostgreSQL databases
driver := databases.NewLegacyPostgresDriver("postgres:17").
// Create a PostgreSQL database for the posts service (the driver supports a builder pattern with this method)
NewDatabase("posts")

// Make sure to register the driver in the context
ctx.Register(driver)

// Allocate a new port for the service. This makes it possible to run multiple instances of this app
// locally, without weird configuration hell. Magic will pick a port in case the preferred one is taken.
Expand All @@ -22,11 +29,11 @@ func BuildMagicConfig() magic.Config {
// Set up environment variables for the application
ctx.WithEnvironment(mconfig.Environment{
// Database connection environment variables
"DB_HOST": postsDB.Host(ctx),
"DB_PORT": postsDB.Port(ctx),
"DB_USER": postsDB.Username(),
"DB_PASSWORD": postsDB.Password(),
"DB_DATABASE": postsDB.DatabaseName(ctx),
"DB_HOST": driver.Host(ctx),
"DB_PORT": driver.Port(ctx),
"DB_USER": driver.Username(),
"DB_PASSWORD": driver.Password(),
"DB_DATABASE": mconfig.ValueStatic("posts"),

// Make the server listen on localhost using the port allocated by Magic
"LISTEN": mconfig.ValueWithBase([]mconfig.EnvironmentValue{port}, func(s []string) string {
Expand All @@ -41,7 +48,8 @@ func BuildMagicConfig() magic.Config {
StartFunction: Start,
Scripts: []scripting.Script{
// Scripts to deal with the database, can always come in handy
scripting.CreateScript("db-reset", "Reset the database by dropping and recreating all tables", ResetDatabase),
scripting.CreateScript("db-reset", "Reset the database by dropping all tables", ResetDatabase),
scripting.CreateScript("db-clear", "Clear the database by truncating all tables", ClearDatabases),
scripting.CreateScript("db-seed", "Seed the database with sample posts", SeedDatabase),

// Scripts to call endpoints, really useful for tests and development
Expand Down
23 changes: 20 additions & 3 deletions examples/real-project/starter/scripts_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,31 @@ import (
"github.com/Liphium/magic/v2/mrunner"
)

// Script to reset the database by dropping and recreating all tables
// Script to clear all database tables content, but not fully delete them.
//
// Here we just use any to ignore the argument. This can be useful for scripts such as this one.
func ClearDatabases(runner *mrunner.Runner) error {
log.Println("Clearing database...")

// Magic can clear all databases for you, don't worry, only data will be deleted meaning your schema is still all good :D
if err := runner.ClearTables(); err != nil {
log.Fatalln("Couldn't clear database tables:", err)
}

log.Println("Database clear completed successfully!")
return nil
}

// Script to reset the database by dropping all tables.
//
// Here we just use any to ignore the argument. This can be useful for scripts such as this one.
func ResetDatabase(runner *mrunner.Runner) error {
log.Println("Resetting database...")

// Magic can clear all databases for you, don't worry, only data will be deleted meaning your schema is still all good :D
runner.ClearDatabases()
// Magic can drop all databases for you as well, this means that all the tables are actually gone
if err := runner.DropTables(); err != nil {
log.Fatalln("Couldn't reset database tables:", err)
}

log.Println("Database reset completed successfully!")
return nil
Expand Down
2 changes: 1 addition & 1 deletion examples/real-project/starter/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestApp(t *testing.T) {
defer client.Close()

// You can clear databases here, but if you don't rely on an empty database for a test, just not doing it is fine, too.
magic.GetTestRunner().ClearDatabases()
assert.Nil(t, magic.GetTestRunner().ClearTables())

// Yes, you can call scripts in here to make your life a little easier.
if err := starter.SeedDatabase(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func createFactory() (Factory, error) {
return Factory{}, err
}

for i := 0; i < maxRecursiveTries; i++ {
for range maxRecursiveTries {
modPath := filepath.Join(dir, "go.mod")
if _, err := os.Stat(modPath); err == nil {
return Factory{projectDir: dir}, nil
Expand Down Expand Up @@ -68,7 +68,7 @@ func (f *Factory) LockFile(profile string) string {

// Get the location of the plan file for a profile
func (f *Factory) PlanFile(profile string) string {
return filepath.Join(f.MagicDirectory(), fmt.Sprintf("%s.mplan", profile))
return filepath.Join(f.MagicDirectory(), fmt.Sprintf("%s.json", profile))
}

// Check if a profile is locked (a magic instance is running)
Expand Down
16 changes: 9 additions & 7 deletions initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ func prepare(config Config, testProfile string) (*Factory, *mrunner.Runner) {
if isTestRunner {
currentProfile = "test-" + testProfile
}
ctx := mconfig.DefaultContext(config.AppName, currentProfile)

// Check if all scripts should be listed
if *scriptsFlag && !isTestRunner {
listScripts(config)
return nil, nil
}

// Create a factory for initializing everything
factory, err := createFactory()
Expand All @@ -79,6 +72,15 @@ func prepare(config Config, testProfile string) (*Factory, *mrunner.Runner) {
}
factory.WarnIfNotIgnored()

// Create the context for Magic config generation
ctx := mconfig.DefaultContext(config.AppName, currentProfile, factory.projectDir)

// Check if all scripts should be listed
if *scriptsFlag && !isTestRunner {
listScripts(config)
return nil, nil
}

// Check if a script should be run
script := *runFlag
if script != "" && !isTestRunner {
Expand Down
4 changes: 0 additions & 4 deletions integration/constants.go

This file was deleted.

91 changes: 0 additions & 91 deletions integration/directory.go

This file was deleted.

89 changes: 0 additions & 89 deletions integration/execute_command.go

This file was deleted.

31 changes: 0 additions & 31 deletions integration/file.go

This file was deleted.

21 changes: 0 additions & 21 deletions integration/formatting.go

This file was deleted.

Loading