Skip to content

Commit

Permalink
Merge branch '2.0' into 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Jul 4, 2022
2 parents 2345eed + 5dd1b2e commit 5dfc447
Show file tree
Hide file tree
Showing 22 changed files with 274 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def up(self):
table.timestamps()

if not self.schema._dry:
User.on(self.connection).create({
User.on(self.connection).set_schema(self.schema_name).create({
'name': 'Joe',
'email': '[email protected]',
'password': 'secret'
Expand Down
3 changes: 3 additions & 0 deletions orm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ from src.masoniteorm.commands import (
MigrateStatusCommand,
MigrateResetCommand,
MakeSeedCommand,
MakeModelDocstringCommand,
SeedRunCommand,
)

Expand All @@ -26,10 +27,12 @@ application.add(MigrateRollbackCommand())
application.add(MigrateRefreshCommand())
application.add(MakeMigrationCommand())
application.add(MakeModelCommand())
application.add(MakeModelDocstringCommand())
application.add(MakeObserverCommand())
application.add(MigrateResetCommand())
application.add(MigrateStatusCommand())
application.add(MakeSeedCommand())

application.add(SeedRunCommand())

if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# https://packaging.python.org/en/latest/single_source_version.html
version="2.16.0",
version="2.17.0",
package_dir={"": "src"},
description="The Official Masonite ORM",
long_description=long_description,
Expand Down
2 changes: 2 additions & 0 deletions src/masoniteorm/commands/MigrateCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class MigrateCommand(Command):
{--c|connection=default : The connection you want to run migrations on}
{--f|force : Force migrations without prompt in production}
{--s|show : Shows the output of SQL for migrations that would be running}
{--schema=? : Sets the schema to be migrated}
{--d|directory=databases/migrations : The location of the migration directory}
"""

Expand All @@ -32,6 +33,7 @@ def handle(self):
connection=self.option("connection"),
migration_directory=self.option("directory"),
config_path=self.option("config"),
schema=self.option("schema"),
)
migration.create_table_if_not_exists()
if not migration.get_unran_migrations():
Expand Down
3 changes: 3 additions & 0 deletions src/masoniteorm/commands/MigrateRefreshCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class MigrateRefreshCommand(Command):
{--c|connection=default : The connection you want to run migrations on}
{--d|directory=databases/migrations : The location of the migration directory}
{--s|seed=? : Seed database after refresh. The seeder to be ran can be provided in argument}
{--schema=? : Sets the schema to be migrated}
{--D|seed-directory=databases/seeds : The location of the seed directory if seed option is used.}
"""

Expand All @@ -22,6 +23,7 @@ def handle(self):
connection=self.option("connection"),
migration_directory=self.option("directory"),
config_path=self.option("config"),
schema=self.option("schema")
)

migration.refresh(self.option("migration"))
Expand All @@ -33,6 +35,7 @@ def handle(self):
"seed:run",
f"None --directory {self.option('seed-directory')} --connection {self.option('connection', 'default')}",
)

elif self.option("seed"):
self.call(
"seed:run",
Expand Down
3 changes: 3 additions & 0 deletions src/masoniteorm/commands/MigrateResetCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class MigrateResetCommand(Command):
migrate:reset
{--m|migration=all : Migration's name to be rollback}
{--c|connection=default : The connection you want to run migrations on}
{--schema=? : Sets the schema to be migrated}
{--d|directory=databases/migrations : The location of the migration directory}
"""

Expand All @@ -18,5 +19,7 @@ def handle(self):
connection=self.option("connection"),
migration_directory=self.option("directory"),
config_path=self.option("config"),
schema=self.option("schema"),
)

migration.reset(self.option("migration"))
2 changes: 2 additions & 0 deletions src/masoniteorm/commands/MigrateRollbackCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class MigrateRollbackCommand(Command):
{--m|migration=all : Migration's name to be rollback}
{--c|connection=default : The connection you want to run migrations on}
{--s|show : Shows the output of SQL for migrations that would be running}
{--schema=? : Sets the schema to be migrated}
{--d|directory=databases/migrations : The location of the migration directory}
"""

Expand All @@ -19,4 +20,5 @@ def handle(self):
connection=self.option("connection"),
migration_directory=self.option("directory"),
config_path=self.option("config"),
schema=self.option("schema"),
).rollback(migration=self.option("migration"), output=self.option("show"))
2 changes: 2 additions & 0 deletions src/masoniteorm/commands/MigrateStatusCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class MigrateStatusCommand(Command):
migrate:status
{--c|connection=default : The connection you want to run migrations on}
{--schema=? : Sets the schema to be migrated}
{--d|directory=databases/migrations : The location of the migration directory}
"""

Expand All @@ -17,6 +18,7 @@ def handle(self):
connection=self.option("connection"),
migration_directory=self.option("directory"),
config_path=self.option("config"),
schema=self.option("schema"),
)
migration.create_table_if_not_exists()
table = self.table()
Expand Down
4 changes: 4 additions & 0 deletions src/masoniteorm/connections/BaseConnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def dry(self):
self._dry = True
return self

def set_schema(self, schema):
self.schema = schema
return self

def log(
self, query, bindings, query_time=0, logger="masoniteorm.connections.queries"
):
Expand Down
8 changes: 6 additions & 2 deletions src/masoniteorm/connections/ConnectionResolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def set_connection_details(self, connection_details):
def get_connection_details(self):
return self._connection_details

def set_connection_option(self, connection: str, options: dict):
self._connection_details.get(connection).update(options)
return self

def get_global_connections(self):
return self._connections

Expand Down Expand Up @@ -104,11 +108,11 @@ def get_connection_information(self, name):
"full_details": details.get(name, {}),
}

def get_schema_builder(self, connection="default"):
def get_schema_builder(self, connection="default", schema=None):
from ..schema import Schema

return Schema(
connection=connection, connection_details=self.get_connection_details()
connection=connection, connection_details=self.get_connection_details(), schema=schema
)

def get_query_builder(self, connection="default"):
Expand Down
4 changes: 4 additions & 0 deletions src/masoniteorm/connections/PostgresConnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(
self._cursor = None
self.transaction_level = 0
self.open = 0
self.schema = None
if name:
self.name = name

Expand All @@ -56,12 +57,15 @@ def make_connection(self):
if self.has_global_connection():
return self.get_global_connection()

schema = self.schema or self.full_details.get("schema")

self._connection = psycopg2.connect(
database=self.database,
user=self.user,
password=self.password,
host=self.host,
port=self.port,
options=f"-c search_path={schema}" if schema else "",
)

self._connection.autocommit = True
Expand Down
21 changes: 17 additions & 4 deletions src/masoniteorm/migrations/Migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,26 @@ def __init__(
command_class=None,
migration_directory="databases/migrations",
config_path=None,
schema=None,
):
self.connection = connection
self.migration_directory = migration_directory
self.last_migrations_ran = []
self.command_class = command_class

self.schema_name = schema

DB = load_config(config_path).DB

DATABASES = DB.get_connection_details()

self.schema = Schema(
connection=connection, connection_details=DATABASES, dry=dry
connection=connection, connection_details=DATABASES, dry=dry, schema=self.schema_name
)

self.migration_model = MigrationModel.on(self.connection)
if self.schema_name:
self.migration_model.set_schema(self.schema_name)

def create_table_if_not_exists(self):
if not self.schema.has_table("migrations"):
Expand Down Expand Up @@ -132,7 +137,7 @@ def migrate(self, migration="all", output=False):
f"<comment>Migrating:</comment> <question>{migration}</question>"
)

migration_class = migration_class(connection=self.connection)
migration_class = migration_class(connection=self.connection, schema=self.schema_name)

if output:
migration_class.schema.dry()
Expand Down Expand Up @@ -182,7 +187,7 @@ def rollback(self, migration="all", output=False):
self.command_class.line(f"<error>Not Found: {migration}</error>")
continue

migration_class = migration_class(connection=self.connection)
migration_class = migration_class(connection=self.connection, schema=self.schema_name)

if output:
migration_class.schema.dry()
Expand Down Expand Up @@ -230,14 +235,22 @@ def reset(self, migration="all"):
default_migrations = self.get_all_migrations(reverse=True)
migrations = default_migrations if migration == "all" else [migration]

if not len(migrations):
if self.command_class:
self.command_class.line(
"<info>Nothing to reset</info>"
)
else:
print("Nothing to reset")

for migration in migrations:
if self.command_class:
self.command_class.line(
f"<comment>Rolling back:</comment> <question>{migration}</question>"
)

try:
self.locate(migration)(connection=self.connection).down()
self.locate(migration)(connection=self.connection, schema=self.schema_name).down()
except TypeError:
self.command_class.line(f"<error>Not Found: {migration}</error>")
continue
Expand Down
1 change: 1 addition & 0 deletions src/masoniteorm/models/Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class Model(TimeStampsMixin, ObservesEvents, metaclass=ModelMeta):
"select_raw",
"select",
"set_global_scope",
"set_schema",
"shared_lock",
"simple_paginate",
"skip",
Expand Down
Loading

0 comments on commit 5dfc447

Please sign in to comment.