masonite-framework-orm 3.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- masonite_framework_orm-3.0.1.dist-info/METADATA +87 -0
- masonite_framework_orm-3.0.1.dist-info/RECORD +116 -0
- masonite_framework_orm-3.0.1.dist-info/WHEEL +5 -0
- masonite_framework_orm-3.0.1.dist-info/entry_points.txt +3 -0
- masonite_framework_orm-3.0.1.dist-info/licenses/LICENSE +21 -0
- masonite_framework_orm-3.0.1.dist-info/top_level.txt +1 -0
- masoniteorm/__init__.py +1 -0
- masoniteorm/collection/Collection.py +605 -0
- masoniteorm/collection/__init__.py +1 -0
- masoniteorm/commands/CanOverrideConfig.py +16 -0
- masoniteorm/commands/CanOverrideOptionsDefault.py +22 -0
- masoniteorm/commands/Command.py +6 -0
- masoniteorm/commands/Entry.py +43 -0
- masoniteorm/commands/MakeMigrationCommand.py +57 -0
- masoniteorm/commands/MakeModelCommand.py +78 -0
- masoniteorm/commands/MakeModelDocstringCommand.py +37 -0
- masoniteorm/commands/MakeObserverCommand.py +54 -0
- masoniteorm/commands/MakeSeedCommand.py +54 -0
- masoniteorm/commands/MigrateCommand.py +46 -0
- masoniteorm/commands/MigrateFreshCommand.py +41 -0
- masoniteorm/commands/MigrateRefreshCommand.py +41 -0
- masoniteorm/commands/MigrateResetCommand.py +25 -0
- masoniteorm/commands/MigrateRollbackCommand.py +26 -0
- masoniteorm/commands/MigrateStatusCommand.py +51 -0
- masoniteorm/commands/SeedRunCommand.py +35 -0
- masoniteorm/commands/ShellCommand.py +205 -0
- masoniteorm/commands/__init__.py +18 -0
- masoniteorm/commands/stubs/create_migration.stub +20 -0
- masoniteorm/commands/stubs/create_seed.stub +9 -0
- masoniteorm/commands/stubs/model.stub +9 -0
- masoniteorm/commands/stubs/observer.stub +101 -0
- masoniteorm/commands/stubs/table_migration.stub +19 -0
- masoniteorm/config.py +123 -0
- masoniteorm/connections/BaseConnection.py +101 -0
- masoniteorm/connections/ConnectionFactory.py +59 -0
- masoniteorm/connections/ConnectionResolver.py +132 -0
- masoniteorm/connections/MSSQLConnection.py +176 -0
- masoniteorm/connections/MySQLConnection.py +232 -0
- masoniteorm/connections/PostgresConnection.py +225 -0
- masoniteorm/connections/SQLiteConnection.py +179 -0
- masoniteorm/connections/__init__.py +6 -0
- masoniteorm/exceptions.py +38 -0
- masoniteorm/expressions/__init__.py +1 -0
- masoniteorm/expressions/expressions.py +288 -0
- masoniteorm/factories/Factory.py +112 -0
- masoniteorm/factories/__init__.py +1 -0
- masoniteorm/helpers/__init__.py +0 -0
- masoniteorm/helpers/misc.py +22 -0
- masoniteorm/migrations/Migration.py +330 -0
- masoniteorm/migrations/__init__.py +1 -0
- masoniteorm/models/MigrationModel.py +9 -0
- masoniteorm/models/Model.py +1209 -0
- masoniteorm/models/Model.pyi +1366 -0
- masoniteorm/models/Pivot.py +5 -0
- masoniteorm/models/__init__.py +1 -0
- masoniteorm/observers/ObservesEvents.py +27 -0
- masoniteorm/observers/__init__.py +1 -0
- masoniteorm/pagination/BasePaginator.py +10 -0
- masoniteorm/pagination/LengthAwarePaginator.py +34 -0
- masoniteorm/pagination/SimplePaginator.py +28 -0
- masoniteorm/pagination/__init__.py +2 -0
- masoniteorm/providers/ORMProvider.py +39 -0
- masoniteorm/providers/__init__.py +1 -0
- masoniteorm/query/EagerRelation.py +42 -0
- masoniteorm/query/QueryBuilder.py +2486 -0
- masoniteorm/query/__init__.py +1 -0
- masoniteorm/query/grammars/BaseGrammar.py +1027 -0
- masoniteorm/query/grammars/MSSQLGrammar.py +194 -0
- masoniteorm/query/grammars/MySQLGrammar.py +238 -0
- masoniteorm/query/grammars/PostgresGrammar.py +213 -0
- masoniteorm/query/grammars/SQLiteGrammar.py +228 -0
- masoniteorm/query/grammars/__init__.py +4 -0
- masoniteorm/query/processors/MSSQLPostProcessor.py +58 -0
- masoniteorm/query/processors/MySQLPostProcessor.py +48 -0
- masoniteorm/query/processors/PostgresPostProcessor.py +49 -0
- masoniteorm/query/processors/SQLitePostProcessor.py +49 -0
- masoniteorm/query/processors/__init__.py +4 -0
- masoniteorm/relationships/BaseRelationship.py +161 -0
- masoniteorm/relationships/BelongsTo.py +124 -0
- masoniteorm/relationships/BelongsToMany.py +604 -0
- masoniteorm/relationships/HasMany.py +66 -0
- masoniteorm/relationships/HasManyThrough.py +269 -0
- masoniteorm/relationships/HasOne.py +111 -0
- masoniteorm/relationships/HasOneThrough.py +275 -0
- masoniteorm/relationships/MorphMany.py +152 -0
- masoniteorm/relationships/MorphOne.py +156 -0
- masoniteorm/relationships/MorphTo.py +111 -0
- masoniteorm/relationships/MorphToMany.py +108 -0
- masoniteorm/relationships/__init__.py +10 -0
- masoniteorm/schema/Blueprint.py +1161 -0
- masoniteorm/schema/Column.py +144 -0
- masoniteorm/schema/ColumnDiff.py +0 -0
- masoniteorm/schema/Constraint.py +5 -0
- masoniteorm/schema/ForeignKeyConstraint.py +28 -0
- masoniteorm/schema/Index.py +5 -0
- masoniteorm/schema/Schema.py +359 -0
- masoniteorm/schema/Table.py +94 -0
- masoniteorm/schema/TableDiff.py +86 -0
- masoniteorm/schema/__init__.py +3 -0
- masoniteorm/schema/platforms/MSSQLPlatform.py +367 -0
- masoniteorm/schema/platforms/MySQLPlatform.py +513 -0
- masoniteorm/schema/platforms/Platform.py +97 -0
- masoniteorm/schema/platforms/PostgresPlatform.py +551 -0
- masoniteorm/schema/platforms/SQLitePlatform.py +481 -0
- masoniteorm/schema/platforms/__init__.py +4 -0
- masoniteorm/scopes/BaseScope.py +6 -0
- masoniteorm/scopes/SoftDeleteScope.py +56 -0
- masoniteorm/scopes/SoftDeletesMixin.py +13 -0
- masoniteorm/scopes/TimeStampsMixin.py +12 -0
- masoniteorm/scopes/TimeStampsScope.py +47 -0
- masoniteorm/scopes/UUIDPrimaryKeyMixin.py +8 -0
- masoniteorm/scopes/UUIDPrimaryKeyScope.py +51 -0
- masoniteorm/scopes/__init__.py +8 -0
- masoniteorm/scopes/scope.py +15 -0
- masoniteorm/seeds/Seeder.py +42 -0
- masoniteorm/seeds/__init__.py +1 -0
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from os import listdir
|
|
3
|
+
from os.path import isfile, join
|
|
4
|
+
from pydoc import locate
|
|
5
|
+
from timeit import default_timer as timer
|
|
6
|
+
|
|
7
|
+
from inflection import camelize
|
|
8
|
+
|
|
9
|
+
from ..config import load_config
|
|
10
|
+
from ..models.MigrationModel import MigrationModel
|
|
11
|
+
from ..schema import Schema
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Migration:
|
|
15
|
+
def __init__(
|
|
16
|
+
self,
|
|
17
|
+
connection="default",
|
|
18
|
+
dry=False,
|
|
19
|
+
command_class=None,
|
|
20
|
+
migration_directory="databases/migrations",
|
|
21
|
+
config_path=None,
|
|
22
|
+
schema=None,
|
|
23
|
+
):
|
|
24
|
+
self.connection = connection
|
|
25
|
+
self.migration_directory = migration_directory
|
|
26
|
+
self.last_migrations_ran = []
|
|
27
|
+
self.command_class = command_class
|
|
28
|
+
|
|
29
|
+
self.schema_name = schema
|
|
30
|
+
|
|
31
|
+
DB = load_config(config_path).DB
|
|
32
|
+
|
|
33
|
+
DATABASES = DB.get_connection_details()
|
|
34
|
+
|
|
35
|
+
self.schema = Schema(
|
|
36
|
+
connection=connection,
|
|
37
|
+
connection_details=DATABASES,
|
|
38
|
+
dry=dry,
|
|
39
|
+
schema=self.schema_name,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
self.migration_model = MigrationModel.on(self.connection)
|
|
43
|
+
if self.schema_name:
|
|
44
|
+
self.migration_model.set_schema(self.schema_name)
|
|
45
|
+
|
|
46
|
+
def create_table_if_not_exists(self):
|
|
47
|
+
if not self.schema.has_table("migrations"):
|
|
48
|
+
with self.schema.create("migrations") as table:
|
|
49
|
+
table.increments("migration_id")
|
|
50
|
+
table.string("migration")
|
|
51
|
+
table.integer("batch")
|
|
52
|
+
|
|
53
|
+
return True
|
|
54
|
+
|
|
55
|
+
return False
|
|
56
|
+
|
|
57
|
+
def get_unran_migrations(self):
|
|
58
|
+
directory_path = os.path.join(os.getcwd(), self.migration_directory)
|
|
59
|
+
all_migrations = [
|
|
60
|
+
f.replace(".py", "")
|
|
61
|
+
for f in listdir(directory_path)
|
|
62
|
+
if isfile(join(directory_path, f))
|
|
63
|
+
and f != "__init__.py"
|
|
64
|
+
and not f.startswith(".")
|
|
65
|
+
]
|
|
66
|
+
all_migrations.sort()
|
|
67
|
+
unran_migrations = []
|
|
68
|
+
database_migrations = self.migration_model.all()
|
|
69
|
+
for migration in all_migrations:
|
|
70
|
+
if migration not in database_migrations.pluck("migration"):
|
|
71
|
+
unran_migrations.append(migration)
|
|
72
|
+
return unran_migrations
|
|
73
|
+
|
|
74
|
+
def get_rollback_migrations(self):
|
|
75
|
+
return (
|
|
76
|
+
self.migration_model.where(
|
|
77
|
+
"batch", self.migration_model.all().max("batch")
|
|
78
|
+
)
|
|
79
|
+
.order_by("migration_id", "desc")
|
|
80
|
+
.get()
|
|
81
|
+
.pluck("migration")
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
def get_all_migrations(self, reverse=False):
|
|
85
|
+
if reverse:
|
|
86
|
+
return (
|
|
87
|
+
self.migration_model.order_by("migration_id", "desc")
|
|
88
|
+
.get()
|
|
89
|
+
.pluck("migration")
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
return self.migration_model.all().pluck("migration")
|
|
93
|
+
|
|
94
|
+
def get_last_batch_number(self):
|
|
95
|
+
return self.migration_model.select("batch").get().max("batch")
|
|
96
|
+
|
|
97
|
+
def delete_migration(self, file_path):
|
|
98
|
+
return self.migration_model.where("migration", file_path).delete()
|
|
99
|
+
|
|
100
|
+
def locate(self, file_name):
|
|
101
|
+
migration_name = camelize(
|
|
102
|
+
"_".join(file_name.split("_")[4:]).replace(".py", "")
|
|
103
|
+
)
|
|
104
|
+
file_name = file_name.replace(".py", "")
|
|
105
|
+
migration_directory = self.migration_directory.replace(
|
|
106
|
+
"/", "."
|
|
107
|
+
).replace("\\", ".")
|
|
108
|
+
return locate(f"{migration_directory}.{file_name}.{migration_name}")
|
|
109
|
+
|
|
110
|
+
def get_ran_migrations(self):
|
|
111
|
+
directory_path = os.path.join(os.getcwd(), self.migration_directory)
|
|
112
|
+
all_migrations = [
|
|
113
|
+
f.replace(".py", "")
|
|
114
|
+
for f in listdir(directory_path)
|
|
115
|
+
if isfile(join(directory_path, f))
|
|
116
|
+
and f != "__init__.py"
|
|
117
|
+
and not f.startswith(".")
|
|
118
|
+
]
|
|
119
|
+
all_migrations.sort()
|
|
120
|
+
ran = []
|
|
121
|
+
|
|
122
|
+
database_migrations = self.migration_model.all()
|
|
123
|
+
for migration in all_migrations:
|
|
124
|
+
matched_migration = database_migrations.where(
|
|
125
|
+
"migration", migration
|
|
126
|
+
).first()
|
|
127
|
+
if matched_migration:
|
|
128
|
+
ran.append(
|
|
129
|
+
{
|
|
130
|
+
"migration_file": matched_migration.migration,
|
|
131
|
+
"batch": matched_migration.batch,
|
|
132
|
+
}
|
|
133
|
+
)
|
|
134
|
+
return ran
|
|
135
|
+
|
|
136
|
+
def migrate(self, migration="all", output=False):
|
|
137
|
+
default_migrations = self.get_unran_migrations()
|
|
138
|
+
migrations = default_migrations if migration == "all" else [migration]
|
|
139
|
+
|
|
140
|
+
batch = self.get_last_batch_number() + 1
|
|
141
|
+
|
|
142
|
+
for migration in migrations:
|
|
143
|
+
try:
|
|
144
|
+
migration_class = self.locate(migration)
|
|
145
|
+
|
|
146
|
+
except TypeError:
|
|
147
|
+
self.command_class.line(
|
|
148
|
+
f"<error>Not Found: {migration}</error>"
|
|
149
|
+
)
|
|
150
|
+
continue
|
|
151
|
+
|
|
152
|
+
self.last_migrations_ran.append(migration)
|
|
153
|
+
if self.command_class:
|
|
154
|
+
self.command_class.line(
|
|
155
|
+
f"<comment>Migrating:</comment> <question>{migration}</question>"
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
migration_class = migration_class(
|
|
159
|
+
connection=self.connection, schema=self.schema_name
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
if output:
|
|
163
|
+
migration_class.schema.dry()
|
|
164
|
+
start = timer()
|
|
165
|
+
migration_class.up()
|
|
166
|
+
duration = "{:.2f}".format(timer() - start)
|
|
167
|
+
|
|
168
|
+
if output:
|
|
169
|
+
if self.command_class:
|
|
170
|
+
table = self.command_class.table()
|
|
171
|
+
table.set_header_row(["SQL"])
|
|
172
|
+
sql = migration_class.schema._blueprint.to_sql()
|
|
173
|
+
if isinstance(sql, list):
|
|
174
|
+
sql = ",".join(sql)
|
|
175
|
+
table.set_rows([[sql]])
|
|
176
|
+
table.render(self.command_class.io)
|
|
177
|
+
continue
|
|
178
|
+
else:
|
|
179
|
+
print(migration_class.schema._blueprint.to_sql())
|
|
180
|
+
|
|
181
|
+
if self.command_class:
|
|
182
|
+
self.command_class.line(
|
|
183
|
+
f"<info>Migrated:</info> <question>{migration}</question> ({duration}s)"
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
self.migration_model.create(
|
|
187
|
+
{"batch": batch, "migration": migration.replace(".py", "")}
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
def rollback(self, migration="all", output=False):
|
|
191
|
+
default_migrations = self.get_rollback_migrations()
|
|
192
|
+
migrations = default_migrations if migration == "all" else [migration]
|
|
193
|
+
|
|
194
|
+
for migration in migrations:
|
|
195
|
+
if migration.endswith(".py"):
|
|
196
|
+
migration = migration.replace(".py", "")
|
|
197
|
+
|
|
198
|
+
if self.command_class:
|
|
199
|
+
self.command_class.line(
|
|
200
|
+
f"<comment>Rolling back:</comment> <question>{migration}</question>"
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
try:
|
|
204
|
+
migration_class = self.locate(migration)
|
|
205
|
+
except TypeError:
|
|
206
|
+
self.command_class.line(
|
|
207
|
+
f"<error>Not Found: {migration}</error>"
|
|
208
|
+
)
|
|
209
|
+
continue
|
|
210
|
+
|
|
211
|
+
migration_class = migration_class(
|
|
212
|
+
connection=self.connection, schema=self.schema_name
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
if output:
|
|
216
|
+
migration_class.schema.dry()
|
|
217
|
+
|
|
218
|
+
start = timer()
|
|
219
|
+
migration_class.down()
|
|
220
|
+
duration = "{:.2f}".format(timer() - start)
|
|
221
|
+
|
|
222
|
+
if output:
|
|
223
|
+
if self.command_class:
|
|
224
|
+
table = self.command_class.table()
|
|
225
|
+
table.set_header_row(["SQL"])
|
|
226
|
+
if (
|
|
227
|
+
hasattr(migration_class.schema, "_blueprint")
|
|
228
|
+
and migration_class.schema._blueprint
|
|
229
|
+
):
|
|
230
|
+
sql = migration_class.schema._blueprint.to_sql()
|
|
231
|
+
if isinstance(sql, list):
|
|
232
|
+
sql = ",".join(sql)
|
|
233
|
+
|
|
234
|
+
table.set_rows([[sql]])
|
|
235
|
+
elif migration_class.schema._sql:
|
|
236
|
+
table.set_rows([[migration_class.schema._sql]])
|
|
237
|
+
table.render(self.command_class.io)
|
|
238
|
+
continue
|
|
239
|
+
else:
|
|
240
|
+
print(migration_class.schema._blueprint.to_sql())
|
|
241
|
+
|
|
242
|
+
self.delete_migration(migration)
|
|
243
|
+
|
|
244
|
+
if self.command_class:
|
|
245
|
+
self.command_class.line(
|
|
246
|
+
f"<info>Rolled back:</info> <question>{migration}</question> ({duration}s)"
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
def delete_migrations(self, migrations=None):
|
|
250
|
+
return self.migration_model.where_in(
|
|
251
|
+
"migration", migrations or []
|
|
252
|
+
).delete()
|
|
253
|
+
|
|
254
|
+
def delete_last_batch(self):
|
|
255
|
+
return self.migration_model.where(
|
|
256
|
+
"batch", self.get_last_batch_number()
|
|
257
|
+
).delete()
|
|
258
|
+
|
|
259
|
+
def reset(self, migration="all"):
|
|
260
|
+
default_migrations = self.get_all_migrations(reverse=True)
|
|
261
|
+
migrations = default_migrations if migration == "all" else [migration]
|
|
262
|
+
|
|
263
|
+
if not len(migrations):
|
|
264
|
+
if self.command_class:
|
|
265
|
+
self.command_class.line("<info>Nothing to reset</info>")
|
|
266
|
+
else:
|
|
267
|
+
print("Nothing to reset")
|
|
268
|
+
|
|
269
|
+
for migration in migrations:
|
|
270
|
+
if self.command_class:
|
|
271
|
+
self.command_class.line(
|
|
272
|
+
f"<comment>Rolling back:</comment> <question>{migration}</question>"
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
try:
|
|
276
|
+
self.locate(migration)(
|
|
277
|
+
connection=self.connection, schema=self.schema_name
|
|
278
|
+
).down()
|
|
279
|
+
except TypeError:
|
|
280
|
+
self.command_class.line(
|
|
281
|
+
f"<error>Not Found: {migration}</error>"
|
|
282
|
+
)
|
|
283
|
+
continue
|
|
284
|
+
|
|
285
|
+
# raise MigrationNotFound(f"Could not find {migration}")
|
|
286
|
+
|
|
287
|
+
self.delete_migration(migration)
|
|
288
|
+
|
|
289
|
+
if self.command_class:
|
|
290
|
+
self.command_class.line(
|
|
291
|
+
f"<info>Rolled back:</info> <question>{migration}</question>"
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
self.delete_migrations([migration])
|
|
295
|
+
|
|
296
|
+
if self.command_class:
|
|
297
|
+
self.command_class.line("")
|
|
298
|
+
|
|
299
|
+
def refresh(self, migration="all"):
|
|
300
|
+
self.reset(migration)
|
|
301
|
+
self.migrate(migration)
|
|
302
|
+
|
|
303
|
+
def drop_all_tables(self, ignore_fk=False):
|
|
304
|
+
if self.command_class:
|
|
305
|
+
self.command_class.line("<comment>Dropping all tables</comment>")
|
|
306
|
+
|
|
307
|
+
if ignore_fk:
|
|
308
|
+
self.schema.disable_foreign_key_constraints()
|
|
309
|
+
|
|
310
|
+
for table in self.schema.get_all_tables():
|
|
311
|
+
self.schema.drop(table)
|
|
312
|
+
|
|
313
|
+
if ignore_fk:
|
|
314
|
+
self.schema.enable_foreign_key_constraints()
|
|
315
|
+
|
|
316
|
+
if self.command_class:
|
|
317
|
+
self.command_class.line("<info>All tables dropped</info>")
|
|
318
|
+
|
|
319
|
+
def fresh(self, ignore_fk=False, migration="all"):
|
|
320
|
+
self.drop_all_tables(ignore_fk=ignore_fk)
|
|
321
|
+
self.create_table_if_not_exists()
|
|
322
|
+
|
|
323
|
+
if not self.get_unran_migrations():
|
|
324
|
+
if self.command_class:
|
|
325
|
+
self.command_class.line(
|
|
326
|
+
"<comment>Nothing to migrate</comment>"
|
|
327
|
+
)
|
|
328
|
+
return
|
|
329
|
+
|
|
330
|
+
self.migrate(migration)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .Migration import Migration
|