Skip to content

Commit

Permalink
Merge pull request #861 from eaguad1337/feat/860
Browse files Browse the repository at this point in the history
implement migrate:fresh
  • Loading branch information
josephmancuso authored Dec 6, 2023
2 parents 946a474 + dbbdbc7 commit 0c455d0
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions orm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ from src.masoniteorm.commands import (
MigrateCommand,
MigrateRollbackCommand,
MigrateRefreshCommand,
MigrateFreshCommand,
MakeMigrationCommand,
MakeObserverCommand,
MakeModelCommand,
Expand All @@ -25,6 +26,7 @@ application = Application("ORM Version:", 0.1)
application.add(MigrateCommand())
application.add(MigrateRollbackCommand())
application.add(MigrateRefreshCommand())
application.add(MigrateFreshCommand())
application.add(MakeMigrationCommand())
application.add(MakeModelCommand())
application.add(MakeModelDocstringCommand())
Expand Down
2 changes: 2 additions & 0 deletions src/masoniteorm/commands/Entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
MigrateCommand,
MigrateRollbackCommand,
MigrateRefreshCommand,
MigrateFreshCommand,
MakeMigrationCommand,
MakeModelCommand,
MakeModelDocstringCommand,
Expand All @@ -26,6 +27,7 @@
application.add(MigrateCommand())
application.add(MigrateRollbackCommand())
application.add(MigrateRefreshCommand())
application.add(MigrateFreshCommand())
application.add(MakeMigrationCommand())
application.add(MakeModelCommand())
application.add(MakeModelDocstringCommand())
Expand Down
42 changes: 42 additions & 0 deletions src/masoniteorm/commands/MigrateFreshCommand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from ..migrations import Migration

from .Command import Command


class MigrateFreshCommand(Command):
"""
Drops all tables and migrates them again.
migrate:fresh
{--c|connection=default : The connection you want to run migrations on}
{--d|directory=databases/migrations : The location of the migration directory}
{--f|ignore-fk=? : The connection you want to run migrations on}
{--s|seed=? : Seed database after fresh. 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.}
"""

def handle(self):
migration = Migration(
command_class=self,
connection=self.option("connection"),
migration_directory=self.option("directory"),
config_path=self.option("config"),
schema=self.option("schema"),
)

migration.fresh(ignore_fk=self.option("ignore-fk"))

self.line("")

if self.option("seed") == "null":
self.call(
"seed:run",
f"None --directory {self.option('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')}",
)
1 change: 1 addition & 0 deletions src/masoniteorm/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .MigrateCommand import MigrateCommand
from .MigrateRollbackCommand import MigrateRollbackCommand
from .MigrateRefreshCommand import MigrateRefreshCommand
from .MigrateFreshCommand import MigrateFreshCommand
from .MigrateResetCommand import MigrateResetCommand
from .MakeModelCommand import MakeModelCommand
from .MakeModelDocstringCommand import MakeModelDocstringCommand
Expand Down
27 changes: 27 additions & 0 deletions src/masoniteorm/migrations/Migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,30 @@ def reset(self, migration="all"):
def refresh(self, migration="all"):
self.reset(migration)
self.migrate(migration)

def drop_all_tables(self, ignore_fk=False):
if self.command_class:
self.command_class.line("<comment>Dropping all tables</comment>")

if ignore_fk:
self.schema.disable_foreign_key_constraints()

for table in self.schema.get_all_tables():
self.schema.drop(table)

if ignore_fk:
self.schema.enable_foreign_key_constraints()

if self.command_class:
self.command_class.line("<info>All tables dropped</info>")

def fresh(self, ignore_fk=False, migration="all"):
self.drop_all_tables(ignore_fk=ignore_fk)
self.create_table_if_not_exists()

if not self.get_unran_migrations():
if self.command_class:
self.command_class.line("<comment>Nothing to migrate</comment>")
return

self.migrate(migration)
19 changes: 19 additions & 0 deletions src/masoniteorm/schema/Schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,25 @@ def get_schema(self):
"schema"
)

def get_all_tables(self):
"""Gets all tables in the database"""
sql = self.platform().compile_get_all_tables(
database=self.get_connection_information().get("database"),
schema=self.get_schema(),
)

if self._dry:
self._sql = sql
return sql

result = self.new_connection().query(sql, ())

return (
list(map(lambda t: list(t.values())[0], result))
if result
else []
)

def has_table(self, table, query_only=False):
"""Checks if the a database has a specific table
Arguments:
Expand Down
3 changes: 3 additions & 0 deletions src/masoniteorm/schema/platforms/MSSQLPlatform.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ def compile_drop_table(self, table):
def compile_column_exists(self, table, column):
return f"SELECT 1 FROM sys.columns WHERE Name = N'{column}' AND Object_ID = Object_ID(N'{table}')"

def compile_get_all_tables(self, database, schema=None):
return f"SELECT name FROM {database}.sys.tables"

def get_current_schema(self, connection, table_name, schema=None):
return Table(table_name)

Expand Down
3 changes: 3 additions & 0 deletions src/masoniteorm/schema/platforms/MySQLPlatform.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ def compile_drop_table(self, table):
def compile_column_exists(self, table, column):
return f"SELECT column_name FROM information_schema.columns WHERE table_name='{table}' and column_name='{column}'"

def compile_get_all_tables(self, database, schema=None):
return f"SELECT table_name FROM information_schema.tables WHERE table_schema = '{database}'"

def get_current_schema(self, connection, table_name, schema=None):
table = Table(table_name)
sql = f"DESCRIBE {table_name}"
Expand Down
3 changes: 3 additions & 0 deletions src/masoniteorm/schema/platforms/PostgresPlatform.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,9 @@ def compile_drop_table(self, table):
def compile_column_exists(self, table, column):
return f"SELECT column_name FROM information_schema.columns WHERE table_name='{table}' and column_name='{column}'"

def compile_get_all_tables(self, database=None, schema=None):
return f"SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_catalog = '{database}'"

def get_current_schema(self, connection, table_name, schema=None):
sql = self.table_information_string().format(
table=table_name, schema=schema or "public"
Expand Down
3 changes: 3 additions & 0 deletions src/masoniteorm/schema/platforms/SQLitePlatform.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,9 @@ def compile_table_exists(self, table, database=None, schema=None):
def compile_column_exists(self, table, column):
return f"SELECT column_name FROM information_schema.columns WHERE table_name='{table}' and column_name='{column}'"

def compile_get_all_tables(self, database, schema=None):
return "SELECT name FROM sqlite_master WHERE type='table'"

def compile_truncate(self, table, foreign_keys=False):
if not foreign_keys:
return f"DELETE FROM {self.wrap_table(table)}"
Expand Down

0 comments on commit 0c455d0

Please sign in to comment.