Skip to content

Commit 0c455d0

Browse files
Merge pull request #861 from eaguad1337/feat/860
implement migrate:fresh
2 parents 946a474 + dbbdbc7 commit 0c455d0

File tree

10 files changed

+105
-0
lines changed

10 files changed

+105
-0
lines changed

orm

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ from src.masoniteorm.commands import (
1010
MigrateCommand,
1111
MigrateRollbackCommand,
1212
MigrateRefreshCommand,
13+
MigrateFreshCommand,
1314
MakeMigrationCommand,
1415
MakeObserverCommand,
1516
MakeModelCommand,
@@ -25,6 +26,7 @@ application = Application("ORM Version:", 0.1)
2526
application.add(MigrateCommand())
2627
application.add(MigrateRollbackCommand())
2728
application.add(MigrateRefreshCommand())
29+
application.add(MigrateFreshCommand())
2830
application.add(MakeMigrationCommand())
2931
application.add(MakeModelCommand())
3032
application.add(MakeModelDocstringCommand())

src/masoniteorm/commands/Entry.py

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
MigrateCommand,
1111
MigrateRollbackCommand,
1212
MigrateRefreshCommand,
13+
MigrateFreshCommand,
1314
MakeMigrationCommand,
1415
MakeModelCommand,
1516
MakeModelDocstringCommand,
@@ -26,6 +27,7 @@
2627
application.add(MigrateCommand())
2728
application.add(MigrateRollbackCommand())
2829
application.add(MigrateRefreshCommand())
30+
application.add(MigrateFreshCommand())
2931
application.add(MakeMigrationCommand())
3032
application.add(MakeModelCommand())
3133
application.add(MakeModelDocstringCommand())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from ..migrations import Migration
2+
3+
from .Command import Command
4+
5+
6+
class MigrateFreshCommand(Command):
7+
"""
8+
Drops all tables and migrates them again.
9+
10+
migrate:fresh
11+
{--c|connection=default : The connection you want to run migrations on}
12+
{--d|directory=databases/migrations : The location of the migration directory}
13+
{--f|ignore-fk=? : The connection you want to run migrations on}
14+
{--s|seed=? : Seed database after fresh. The seeder to be ran can be provided in argument}
15+
{--schema=? : Sets the schema to be migrated}
16+
{--D|seed-directory=databases/seeds : The location of the seed directory if seed option is used.}
17+
"""
18+
19+
def handle(self):
20+
migration = Migration(
21+
command_class=self,
22+
connection=self.option("connection"),
23+
migration_directory=self.option("directory"),
24+
config_path=self.option("config"),
25+
schema=self.option("schema"),
26+
)
27+
28+
migration.fresh(ignore_fk=self.option("ignore-fk"))
29+
30+
self.line("")
31+
32+
if self.option("seed") == "null":
33+
self.call(
34+
"seed:run",
35+
f"None --directory {self.option('seed-directory')} --connection {self.option('connection')}",
36+
)
37+
38+
elif self.option("seed"):
39+
self.call(
40+
"seed:run",
41+
f"{self.option('seed')} --directory {self.option('seed-directory')} --connection {self.option('connection')}",
42+
)

src/masoniteorm/commands/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .MigrateCommand import MigrateCommand
77
from .MigrateRollbackCommand import MigrateRollbackCommand
88
from .MigrateRefreshCommand import MigrateRefreshCommand
9+
from .MigrateFreshCommand import MigrateFreshCommand
910
from .MigrateResetCommand import MigrateResetCommand
1011
from .MakeModelCommand import MakeModelCommand
1112
from .MakeModelDocstringCommand import MakeModelDocstringCommand

src/masoniteorm/migrations/Migration.py

+27
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,30 @@ def reset(self, migration="all"):
288288
def refresh(self, migration="all"):
289289
self.reset(migration)
290290
self.migrate(migration)
291+
292+
def drop_all_tables(self, ignore_fk=False):
293+
if self.command_class:
294+
self.command_class.line("<comment>Dropping all tables</comment>")
295+
296+
if ignore_fk:
297+
self.schema.disable_foreign_key_constraints()
298+
299+
for table in self.schema.get_all_tables():
300+
self.schema.drop(table)
301+
302+
if ignore_fk:
303+
self.schema.enable_foreign_key_constraints()
304+
305+
if self.command_class:
306+
self.command_class.line("<info>All tables dropped</info>")
307+
308+
def fresh(self, ignore_fk=False, migration="all"):
309+
self.drop_all_tables(ignore_fk=ignore_fk)
310+
self.create_table_if_not_exists()
311+
312+
if not self.get_unran_migrations():
313+
if self.command_class:
314+
self.command_class.line("<comment>Nothing to migrate</comment>")
315+
return
316+
317+
self.migrate(migration)

src/masoniteorm/schema/Schema.py

+19
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,25 @@ def get_schema(self):
293293
"schema"
294294
)
295295

296+
def get_all_tables(self):
297+
"""Gets all tables in the database"""
298+
sql = self.platform().compile_get_all_tables(
299+
database=self.get_connection_information().get("database"),
300+
schema=self.get_schema(),
301+
)
302+
303+
if self._dry:
304+
self._sql = sql
305+
return sql
306+
307+
result = self.new_connection().query(sql, ())
308+
309+
return (
310+
list(map(lambda t: list(t.values())[0], result))
311+
if result
312+
else []
313+
)
314+
296315
def has_table(self, table, query_only=False):
297316
"""Checks if the a database has a specific table
298317
Arguments:

src/masoniteorm/schema/platforms/MSSQLPlatform.py

+3
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ def compile_drop_table(self, table):
334334
def compile_column_exists(self, table, column):
335335
return f"SELECT 1 FROM sys.columns WHERE Name = N'{column}' AND Object_ID = Object_ID(N'{table}')"
336336

337+
def compile_get_all_tables(self, database, schema=None):
338+
return f"SELECT name FROM {database}.sys.tables"
339+
337340
def get_current_schema(self, connection, table_name, schema=None):
338341
return Table(table_name)
339342

src/masoniteorm/schema/platforms/MySQLPlatform.py

+3
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,9 @@ def compile_drop_table(self, table):
403403
def compile_column_exists(self, table, column):
404404
return f"SELECT column_name FROM information_schema.columns WHERE table_name='{table}' and column_name='{column}'"
405405

406+
def compile_get_all_tables(self, database, schema=None):
407+
return f"SELECT table_name FROM information_schema.tables WHERE table_schema = '{database}'"
408+
406409
def get_current_schema(self, connection, table_name, schema=None):
407410
table = Table(table_name)
408411
sql = f"DESCRIBE {table_name}"

src/masoniteorm/schema/platforms/PostgresPlatform.py

+3
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,9 @@ def compile_drop_table(self, table):
459459
def compile_column_exists(self, table, column):
460460
return f"SELECT column_name FROM information_schema.columns WHERE table_name='{table}' and column_name='{column}'"
461461

462+
def compile_get_all_tables(self, database=None, schema=None):
463+
return f"SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_catalog = '{database}'"
464+
462465
def get_current_schema(self, connection, table_name, schema=None):
463466
sql = self.table_information_string().format(
464467
table=table_name, schema=schema or "public"

src/masoniteorm/schema/platforms/SQLitePlatform.py

+3
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,9 @@ def compile_table_exists(self, table, database=None, schema=None):
401401
def compile_column_exists(self, table, column):
402402
return f"SELECT column_name FROM information_schema.columns WHERE table_name='{table}' and column_name='{column}'"
403403

404+
def compile_get_all_tables(self, database, schema=None):
405+
return "SELECT name FROM sqlite_master WHERE type='table'"
406+
404407
def compile_truncate(self, table, foreign_keys=False):
405408
if not foreign_keys:
406409
return f"DELETE FROM {self.wrap_table(table)}"

0 commit comments

Comments
 (0)