Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement migrate:fresh #861

Merged
merged 3 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
41 changes: 41 additions & 0 deletions src/masoniteorm/commands/MigrateFreshCommand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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}
{--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()

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
21 changes: 21 additions & 0 deletions src/masoniteorm/migrations/Migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,24 @@ def reset(self, migration="all"):
def refresh(self, migration="all"):
self.reset(migration)
self.migrate(migration)

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

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

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

def fresh(self, migration="all"):
self.drop_all_tables()
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 @@ -291,6 +291,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