From 78bdac34950b4eb1746cc164ee14341b6b432735 Mon Sep 17 00:00:00 2001 From: raj <106125091@nitt.edu> Date: Sat, 25 Apr 2026 16:15:40 +0530 Subject: [PATCH] Change default folder #867 --- src/masoniteorm/commands/Command.py | 40 ++++++++- .../commands/MakeMigrationCommand.py | 7 +- src/masoniteorm/commands/MakeModelCommand.py | 18 +++- src/masoniteorm/commands/MakeSeedCommand.py | 4 +- src/masoniteorm/commands/MigrateCommand.py | 7 +- .../commands/MigrateFreshCommand.py | 11 ++- .../commands/MigrateResetCommand.py | 7 +- .../commands/MigrateRollbackCommand.py | 7 +- .../commands/MigrateStatusCommand.py | 7 +- src/masoniteorm/commands/SeedRunCommand.py | 7 +- tests/commands/test_directory_defaults.py | 82 +++++++++++++++++++ tests/integrations/config/folder_defaults.py | 19 +++++ 12 files changed, 202 insertions(+), 14 deletions(-) create mode 100644 tests/commands/test_directory_defaults.py create mode 100644 tests/integrations/config/folder_defaults.py diff --git a/src/masoniteorm/commands/Command.py b/src/masoniteorm/commands/Command.py index a4d13a105..fc16c74dd 100644 --- a/src/masoniteorm/commands/Command.py +++ b/src/masoniteorm/commands/Command.py @@ -1,6 +1,44 @@ +from ..config import load_config +from ..exceptions import ConfigurationNotFound +from clikit.api.args.exceptions import NoSuchOptionException from .CanOverrideConfig import CanOverrideConfig from .CanOverrideOptionsDefault import CanOverrideOptionsDefault class Command(CanOverrideOptionsDefault, CanOverrideConfig): - pass + def _get_config(self): + if not hasattr(self, "_orm_config"): + try: + self._orm_config = load_config(self._get_option_value("config")) + except ConfigurationNotFound: + self._orm_config = None + + return self._orm_config + + def _get_option_value(self, option_name): + try: + return self.option(option_name) + except NoSuchOptionException: + option = self.config.options.get(option_name) + if option: + return option.default + + return None + + def option_or_config(self, option_name, default, *config_names): + value = self._get_option_value(option_name) + + if value != default: + return value + + config = self._get_config() + if not config: + return default + + names = config_names or (option_name.replace("-", "_"),) + for name in names: + for config_name in (name, name.upper()): + if hasattr(config, config_name): + return getattr(config, config_name) + + return default diff --git a/src/masoniteorm/commands/MakeMigrationCommand.py b/src/masoniteorm/commands/MakeMigrationCommand.py index f62aea426..edfaf30d2 100644 --- a/src/masoniteorm/commands/MakeMigrationCommand.py +++ b/src/masoniteorm/commands/MakeMigrationCommand.py @@ -33,7 +33,12 @@ def handle(self): table = tableize(name.replace("create_", "").replace("_table", "")) stub_file = "create_migration" - migration_directory = self.option("directory") + migration_directory = self.option_or_config( + "directory", + "databases/migrations", + "MIGRATIONS_DIRECTORY", + "migrations_directory", + ) with open( os.path.join( diff --git a/src/masoniteorm/commands/MakeModelCommand.py b/src/masoniteorm/commands/MakeModelCommand.py index 685024050..241f76333 100644 --- a/src/masoniteorm/commands/MakeModelCommand.py +++ b/src/masoniteorm/commands/MakeModelCommand.py @@ -25,7 +25,9 @@ class MakeModelCommand(Command): def handle(self): name = self.argument("name") - model_directory = self.option("directory") + model_directory = self.option_or_config( + "directory", "app", "MODELS_DIRECTORY", "models_directory" + ) with open( os.path.join(pathlib.Path(__file__).parent.absolute(), "stubs/model.stub") @@ -53,7 +55,12 @@ def handle(self): self.info(f"Model created: {os.path.join(model_directory, file_name)}") if self.option("migration"): - migrations_directory = self.option("migrations-directory") + migrations_directory = self.option_or_config( + "migrations-directory", + "databases/migrations", + "MIGRATIONS_DIRECTORY", + "migrations_directory", + ) if self.option("table"): self.call( "migration", @@ -66,5 +73,10 @@ def handle(self): ) if self.option("seeder"): - directory = self.option("seeders-directory") + directory = self.option_or_config( + "seeders-directory", + "databases/seeds", + "SEEDERS_DIRECTORY", + "seeders_directory", + ) self.call("seed", f"{self.argument('name')} --directory {directory}") diff --git a/src/masoniteorm/commands/MakeSeedCommand.py b/src/masoniteorm/commands/MakeSeedCommand.py index 680386a89..02edcc277 100644 --- a/src/masoniteorm/commands/MakeSeedCommand.py +++ b/src/masoniteorm/commands/MakeSeedCommand.py @@ -20,7 +20,9 @@ def handle(self): # replace the placeholders of a stub file # output the content to a file location name = self.argument("name") + "TableSeeder" - seed_directory = self.option("directory") + seed_directory = self.option_or_config( + "directory", "databases/seeds", "SEEDS_DIRECTORY", "seed_directory" + ) file_name = underscore(name) stub_file = "create_seed" diff --git a/src/masoniteorm/commands/MigrateCommand.py b/src/masoniteorm/commands/MigrateCommand.py index d86dea59c..dd6551ea0 100644 --- a/src/masoniteorm/commands/MigrateCommand.py +++ b/src/masoniteorm/commands/MigrateCommand.py @@ -31,7 +31,12 @@ def handle(self): migration = Migration( command_class=self, connection=self.option("connection"), - migration_directory=self.option("directory"), + migration_directory=self.option_or_config( + "directory", + "databases/migrations", + "MIGRATIONS_DIRECTORY", + "migrations_directory", + ), config_path=self.option("config"), schema=self.option("schema"), ) diff --git a/src/masoniteorm/commands/MigrateFreshCommand.py b/src/masoniteorm/commands/MigrateFreshCommand.py index fb90f52ab..1408f38ba 100644 --- a/src/masoniteorm/commands/MigrateFreshCommand.py +++ b/src/masoniteorm/commands/MigrateFreshCommand.py @@ -20,7 +20,12 @@ def handle(self): migration = Migration( command_class=self, connection=self.option("connection"), - migration_directory=self.option("directory"), + migration_directory=self.option_or_config( + "directory", + "databases/migrations", + "MIGRATIONS_DIRECTORY", + "migrations_directory", + ), config_path=self.option("config"), schema=self.option("schema"), ) @@ -32,11 +37,11 @@ def handle(self): if self.option("seed") == "null": self.call( "seed:run", - f"None --directory {self.option('seed-directory')} --connection {self.option('connection')}", + f"None --directory {self.option_or_config('seed-directory', 'databases/seeds', 'SEEDS_DIRECTORY', 'seed_directory')} --connection {self.option('connection')}", ) elif self.option("seed"): self.call( "seed:run", - f"{self.option('seed')} --directory {self.option('seed-directory')} --connection {self.option('connection')}", + f"{self.option('seed')} --directory {self.option_or_config('seed-directory', 'databases/seeds', 'SEEDS_DIRECTORY', 'seed_directory')} --connection {self.option('connection')}", ) diff --git a/src/masoniteorm/commands/MigrateResetCommand.py b/src/masoniteorm/commands/MigrateResetCommand.py index 6ca176e8d..3fad60904 100644 --- a/src/masoniteorm/commands/MigrateResetCommand.py +++ b/src/masoniteorm/commands/MigrateResetCommand.py @@ -17,7 +17,12 @@ def handle(self): migration = Migration( command_class=self, connection=self.option("connection"), - migration_directory=self.option("directory"), + migration_directory=self.option_or_config( + "directory", + "databases/migrations", + "MIGRATIONS_DIRECTORY", + "migrations_directory", + ), config_path=self.option("config"), schema=self.option("schema"), ) diff --git a/src/masoniteorm/commands/MigrateRollbackCommand.py b/src/masoniteorm/commands/MigrateRollbackCommand.py index 10cdd4a19..731cbd3e2 100644 --- a/src/masoniteorm/commands/MigrateRollbackCommand.py +++ b/src/masoniteorm/commands/MigrateRollbackCommand.py @@ -18,7 +18,12 @@ def handle(self): Migration( command_class=self, connection=self.option("connection"), - migration_directory=self.option("directory"), + migration_directory=self.option_or_config( + "directory", + "databases/migrations", + "MIGRATIONS_DIRECTORY", + "migrations_directory", + ), config_path=self.option("config"), schema=self.option("schema"), ).rollback(migration=self.option("migration"), output=self.option("show")) diff --git a/src/masoniteorm/commands/MigrateStatusCommand.py b/src/masoniteorm/commands/MigrateStatusCommand.py index 7ddbfdd32..4eb42f609 100644 --- a/src/masoniteorm/commands/MigrateStatusCommand.py +++ b/src/masoniteorm/commands/MigrateStatusCommand.py @@ -16,7 +16,12 @@ def handle(self): migration = Migration( command_class=self, connection=self.option("connection"), - migration_directory=self.option("directory"), + migration_directory=self.option_or_config( + "directory", + "databases/migrations", + "MIGRATIONS_DIRECTORY", + "migrations_directory", + ), config_path=self.option("config"), schema=self.option("schema"), ) diff --git a/src/masoniteorm/commands/SeedRunCommand.py b/src/masoniteorm/commands/SeedRunCommand.py index 89e90359d..ffa125e44 100644 --- a/src/masoniteorm/commands/SeedRunCommand.py +++ b/src/masoniteorm/commands/SeedRunCommand.py @@ -18,7 +18,12 @@ class SeedRunCommand(Command): def handle(self): seeder = Seeder( dry=self.option("dry"), - seed_path=self.option("directory"), + seed_path=self.option_or_config( + "directory", + "databases/seeds", + "SEEDS_DIRECTORY", + "seed_directory", + ), connection=self.option("connection"), ) diff --git a/tests/commands/test_directory_defaults.py b/tests/commands/test_directory_defaults.py new file mode 100644 index 000000000..3baacec37 --- /dev/null +++ b/tests/commands/test_directory_defaults.py @@ -0,0 +1,82 @@ +import os +import unittest + +from src.masoniteorm.commands import ( + MakeMigrationCommand, + MakeModelCommand, + MakeSeedCommand, + MigrateCommand, + SeedRunCommand, +) + + +class TestDirectoryDefaults(unittest.TestCase): + def setUp(self): + self.original_db_config_path = os.getenv("DB_CONFIG_PATH") + os.environ["DB_CONFIG_PATH"] = "tests/integrations/config/folder_defaults" + + def tearDown(self): + if self.original_db_config_path is None: + os.environ.pop("DB_CONFIG_PATH", None) + else: + os.environ["DB_CONFIG_PATH"] = self.original_db_config_path + + def test_make_model_uses_configured_defaults(self): + command = MakeModelCommand() + + self.assertEqual( + command.option_or_config( + "directory", "app", "MODELS_DIRECTORY", "models_directory" + ), + "custom-app", + ) + self.assertEqual( + command.option_or_config( + "migrations-directory", + "databases/migrations", + "MIGRATIONS_DIRECTORY", + "migrations_directory", + ), + "custom-databases/migrations", + ) + self.assertEqual( + command.option_or_config( + "seeders-directory", + "databases/seeds", + "SEEDERS_DIRECTORY", + "seeders_directory", + ), + "custom-databases/seeds", + ) + + def test_migration_and_seed_commands_use_configured_defaults(self): + self.assertEqual( + MigrateCommand().option_or_config( + "directory", + "databases/migrations", + "MIGRATIONS_DIRECTORY", + "migrations_directory", + ), + "custom-databases/migrations", + ) + self.assertEqual( + MakeMigrationCommand().option_or_config( + "directory", + "databases/migrations", + "MIGRATIONS_DIRECTORY", + "migrations_directory", + ), + "custom-databases/migrations", + ) + self.assertEqual( + MakeSeedCommand().option_or_config( + "directory", "databases/seeds", "SEEDS_DIRECTORY", "seed_directory" + ), + "custom-databases/seeds", + ) + self.assertEqual( + SeedRunCommand().option_or_config( + "directory", "databases/seeds", "SEEDS_DIRECTORY", "seed_directory" + ), + "custom-databases/seeds", + ) diff --git a/tests/integrations/config/folder_defaults.py b/tests/integrations/config/folder_defaults.py new file mode 100644 index 000000000..f78660f17 --- /dev/null +++ b/tests/integrations/config/folder_defaults.py @@ -0,0 +1,19 @@ +from src.masoniteorm.connections import ConnectionResolver + + +DATABASES = { + "default": "test", + "test": { + "driver": "sqlite", + "database": "orm.sqlite3", + "prefix": "", + "log_queries": True, + }, +} + +DB = ConnectionResolver().set_connection_details(DATABASES) + +MODELS_DIRECTORY = "custom-app" +MIGRATIONS_DIRECTORY = "custom-databases/migrations" +SEEDS_DIRECTORY = "custom-databases/seeds" +SEEDERS_DIRECTORY = "custom-databases/seeds" \ No newline at end of file