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.
Files changed (116) hide show
  1. masonite_framework_orm-3.0.1.dist-info/METADATA +87 -0
  2. masonite_framework_orm-3.0.1.dist-info/RECORD +116 -0
  3. masonite_framework_orm-3.0.1.dist-info/WHEEL +5 -0
  4. masonite_framework_orm-3.0.1.dist-info/entry_points.txt +3 -0
  5. masonite_framework_orm-3.0.1.dist-info/licenses/LICENSE +21 -0
  6. masonite_framework_orm-3.0.1.dist-info/top_level.txt +1 -0
  7. masoniteorm/__init__.py +1 -0
  8. masoniteorm/collection/Collection.py +605 -0
  9. masoniteorm/collection/__init__.py +1 -0
  10. masoniteorm/commands/CanOverrideConfig.py +16 -0
  11. masoniteorm/commands/CanOverrideOptionsDefault.py +22 -0
  12. masoniteorm/commands/Command.py +6 -0
  13. masoniteorm/commands/Entry.py +43 -0
  14. masoniteorm/commands/MakeMigrationCommand.py +57 -0
  15. masoniteorm/commands/MakeModelCommand.py +78 -0
  16. masoniteorm/commands/MakeModelDocstringCommand.py +37 -0
  17. masoniteorm/commands/MakeObserverCommand.py +54 -0
  18. masoniteorm/commands/MakeSeedCommand.py +54 -0
  19. masoniteorm/commands/MigrateCommand.py +46 -0
  20. masoniteorm/commands/MigrateFreshCommand.py +41 -0
  21. masoniteorm/commands/MigrateRefreshCommand.py +41 -0
  22. masoniteorm/commands/MigrateResetCommand.py +25 -0
  23. masoniteorm/commands/MigrateRollbackCommand.py +26 -0
  24. masoniteorm/commands/MigrateStatusCommand.py +51 -0
  25. masoniteorm/commands/SeedRunCommand.py +35 -0
  26. masoniteorm/commands/ShellCommand.py +205 -0
  27. masoniteorm/commands/__init__.py +18 -0
  28. masoniteorm/commands/stubs/create_migration.stub +20 -0
  29. masoniteorm/commands/stubs/create_seed.stub +9 -0
  30. masoniteorm/commands/stubs/model.stub +9 -0
  31. masoniteorm/commands/stubs/observer.stub +101 -0
  32. masoniteorm/commands/stubs/table_migration.stub +19 -0
  33. masoniteorm/config.py +123 -0
  34. masoniteorm/connections/BaseConnection.py +101 -0
  35. masoniteorm/connections/ConnectionFactory.py +59 -0
  36. masoniteorm/connections/ConnectionResolver.py +132 -0
  37. masoniteorm/connections/MSSQLConnection.py +176 -0
  38. masoniteorm/connections/MySQLConnection.py +232 -0
  39. masoniteorm/connections/PostgresConnection.py +225 -0
  40. masoniteorm/connections/SQLiteConnection.py +179 -0
  41. masoniteorm/connections/__init__.py +6 -0
  42. masoniteorm/exceptions.py +38 -0
  43. masoniteorm/expressions/__init__.py +1 -0
  44. masoniteorm/expressions/expressions.py +288 -0
  45. masoniteorm/factories/Factory.py +112 -0
  46. masoniteorm/factories/__init__.py +1 -0
  47. masoniteorm/helpers/__init__.py +0 -0
  48. masoniteorm/helpers/misc.py +22 -0
  49. masoniteorm/migrations/Migration.py +330 -0
  50. masoniteorm/migrations/__init__.py +1 -0
  51. masoniteorm/models/MigrationModel.py +9 -0
  52. masoniteorm/models/Model.py +1209 -0
  53. masoniteorm/models/Model.pyi +1366 -0
  54. masoniteorm/models/Pivot.py +5 -0
  55. masoniteorm/models/__init__.py +1 -0
  56. masoniteorm/observers/ObservesEvents.py +27 -0
  57. masoniteorm/observers/__init__.py +1 -0
  58. masoniteorm/pagination/BasePaginator.py +10 -0
  59. masoniteorm/pagination/LengthAwarePaginator.py +34 -0
  60. masoniteorm/pagination/SimplePaginator.py +28 -0
  61. masoniteorm/pagination/__init__.py +2 -0
  62. masoniteorm/providers/ORMProvider.py +39 -0
  63. masoniteorm/providers/__init__.py +1 -0
  64. masoniteorm/query/EagerRelation.py +42 -0
  65. masoniteorm/query/QueryBuilder.py +2486 -0
  66. masoniteorm/query/__init__.py +1 -0
  67. masoniteorm/query/grammars/BaseGrammar.py +1027 -0
  68. masoniteorm/query/grammars/MSSQLGrammar.py +194 -0
  69. masoniteorm/query/grammars/MySQLGrammar.py +238 -0
  70. masoniteorm/query/grammars/PostgresGrammar.py +213 -0
  71. masoniteorm/query/grammars/SQLiteGrammar.py +228 -0
  72. masoniteorm/query/grammars/__init__.py +4 -0
  73. masoniteorm/query/processors/MSSQLPostProcessor.py +58 -0
  74. masoniteorm/query/processors/MySQLPostProcessor.py +48 -0
  75. masoniteorm/query/processors/PostgresPostProcessor.py +49 -0
  76. masoniteorm/query/processors/SQLitePostProcessor.py +49 -0
  77. masoniteorm/query/processors/__init__.py +4 -0
  78. masoniteorm/relationships/BaseRelationship.py +161 -0
  79. masoniteorm/relationships/BelongsTo.py +124 -0
  80. masoniteorm/relationships/BelongsToMany.py +604 -0
  81. masoniteorm/relationships/HasMany.py +66 -0
  82. masoniteorm/relationships/HasManyThrough.py +269 -0
  83. masoniteorm/relationships/HasOne.py +111 -0
  84. masoniteorm/relationships/HasOneThrough.py +275 -0
  85. masoniteorm/relationships/MorphMany.py +152 -0
  86. masoniteorm/relationships/MorphOne.py +156 -0
  87. masoniteorm/relationships/MorphTo.py +111 -0
  88. masoniteorm/relationships/MorphToMany.py +108 -0
  89. masoniteorm/relationships/__init__.py +10 -0
  90. masoniteorm/schema/Blueprint.py +1161 -0
  91. masoniteorm/schema/Column.py +144 -0
  92. masoniteorm/schema/ColumnDiff.py +0 -0
  93. masoniteorm/schema/Constraint.py +5 -0
  94. masoniteorm/schema/ForeignKeyConstraint.py +28 -0
  95. masoniteorm/schema/Index.py +5 -0
  96. masoniteorm/schema/Schema.py +359 -0
  97. masoniteorm/schema/Table.py +94 -0
  98. masoniteorm/schema/TableDiff.py +86 -0
  99. masoniteorm/schema/__init__.py +3 -0
  100. masoniteorm/schema/platforms/MSSQLPlatform.py +367 -0
  101. masoniteorm/schema/platforms/MySQLPlatform.py +513 -0
  102. masoniteorm/schema/platforms/Platform.py +97 -0
  103. masoniteorm/schema/platforms/PostgresPlatform.py +551 -0
  104. masoniteorm/schema/platforms/SQLitePlatform.py +481 -0
  105. masoniteorm/schema/platforms/__init__.py +4 -0
  106. masoniteorm/scopes/BaseScope.py +6 -0
  107. masoniteorm/scopes/SoftDeleteScope.py +56 -0
  108. masoniteorm/scopes/SoftDeletesMixin.py +13 -0
  109. masoniteorm/scopes/TimeStampsMixin.py +12 -0
  110. masoniteorm/scopes/TimeStampsScope.py +47 -0
  111. masoniteorm/scopes/UUIDPrimaryKeyMixin.py +8 -0
  112. masoniteorm/scopes/UUIDPrimaryKeyScope.py +51 -0
  113. masoniteorm/scopes/__init__.py +8 -0
  114. masoniteorm/scopes/scope.py +15 -0
  115. masoniteorm/seeds/Seeder.py +42 -0
  116. masoniteorm/seeds/__init__.py +1 -0
@@ -0,0 +1,1161 @@
1
+ class Blueprint:
2
+ """Used for building schemas for creating, modifying or altering schema."""
3
+
4
+ def __init__(
5
+ self,
6
+ grammar,
7
+ table="",
8
+ connection=None,
9
+ platform=None,
10
+ schema=None,
11
+ action=None,
12
+ default_string_length=None,
13
+ dry=False,
14
+ ):
15
+ self.grammar = grammar
16
+ self.table = table
17
+ self._last_column = None
18
+ self._default_string_length = default_string_length
19
+ self.platform = platform
20
+ self.schema = schema
21
+ self._dry = dry
22
+ self._action = action
23
+ self.connection = connection
24
+ if not platform:
25
+ self.platform = self.connection.get_default_platform()
26
+
27
+ def string(self, column, length=255, nullable=False):
28
+ """Sets a column to be the string representation for the table.
29
+
30
+ Arguments:
31
+ column {string} -- The column name.
32
+
33
+ Keyword Arguments:
34
+ length {int} -- The length of the column. (default: {255})
35
+ nullable {bool} -- Whether the column is nullable. (default: {False})
36
+
37
+ Returns:
38
+ self
39
+ """
40
+ self._last_column = self.table.add_column(
41
+ column, "string", length=length, nullable=nullable
42
+ )
43
+
44
+ return self
45
+
46
+ def tiny_integer(self, column, length=1, nullable=False):
47
+ """Sets a column to be the tiny_integer representation for the table.
48
+
49
+ Arguments:
50
+ column {string} -- The column name.
51
+
52
+ Keyword Arguments:
53
+ length {int} -- The length of the column. (default: {1})
54
+ nullable {bool} -- Whether the column is nullable (default: {False})
55
+
56
+ Returns:
57
+ self
58
+ """
59
+ self._last_column = self.table.add_column(
60
+ column, "tiny_integer", length=length, nullable=nullable
61
+ )
62
+ return self
63
+
64
+ def small_integer(self, column, length=5, nullable=False):
65
+ """Sets a column to be the small_integer representation for the table.
66
+
67
+ Arguments:
68
+ column {string} -- The column name.
69
+
70
+ Keyword Arguments:
71
+ length {int} -- The length of the column. (default: {5})
72
+ nullable {bool} -- Whether the column is nullable (default: {False})
73
+
74
+ Returns:
75
+ self
76
+ """
77
+ self._last_column = self.table.add_column(
78
+ column, "small_integer", length=length, nullable=nullable
79
+ )
80
+ return self
81
+
82
+ def medium_integer(self, column, length=7, nullable=False):
83
+ """Sets a column to be the medium_integer representation for the table.
84
+
85
+ Arguments:
86
+ column {string} -- The column name.
87
+
88
+ Keyword Arguments:
89
+ length {int} -- The length of the column. (default: {7})
90
+ nullable {bool} -- Whether the column is nullable (default: {False})
91
+
92
+ Returns:
93
+ self
94
+ """
95
+ self._last_column = self.table.add_column(
96
+ column, "medium_integer", length=length, nullable=nullable
97
+ )
98
+ return self
99
+
100
+ def integer(self, column, length=11, nullable=False):
101
+ """Sets a column to be the integer representation for the table.
102
+
103
+ Arguments:
104
+ column {string} -- The column name.
105
+
106
+ Keyword Arguments:
107
+ length {int} -- The length of the column. (default: {11})
108
+ nullable {bool} -- Whether the column is nullable (default: {False})
109
+
110
+ Returns:
111
+ self
112
+ """
113
+ self._last_column = self.table.add_column(
114
+ column, "integer", length=length, nullable=nullable
115
+ )
116
+ return self
117
+
118
+ def big_integer(self, column, length=32, nullable=False):
119
+ """Sets a column to be the big_integer representation for the table.
120
+
121
+ Arguments:
122
+ column {string} -- The column name.
123
+
124
+ Keyword Arguments:
125
+ length {int} -- The length of the column. (default: {32})
126
+ nullable {bool} -- Whether the column is nullable (default: {False})
127
+
128
+ Returns:
129
+ self
130
+ """
131
+ self._last_column = self.table.add_column(
132
+ column, "big_integer", length=length, nullable=nullable
133
+ )
134
+ return self
135
+
136
+ def unsigned_big_integer(self, column, length=32, nullable=False):
137
+ """Sets a column to be the unsigned big_integer representation for the table.
138
+
139
+ Arguments:
140
+ column {string} -- The column name.
141
+
142
+ Keyword Arguments:
143
+ length {int} -- The length of the column. (default: {32})
144
+ nullable {bool} -- Whether the column is nullable (default: {False})
145
+
146
+ Returns:
147
+ self
148
+ """
149
+ return self.big_integer(
150
+ column, length=length, nullable=nullable
151
+ ).unsigned()
152
+
153
+ def _compile_create(self):
154
+ return self.grammar(
155
+ creates=self._columns, table=self.table
156
+ )._compile_create()
157
+
158
+ def _compile_alter(self):
159
+ return self.grammar(
160
+ creates=self._columns, table=self.table
161
+ )._compile_create()
162
+
163
+ def increments(self, column, nullable=False):
164
+ """Sets a column to be the auto incrementing primary key representation for the table.
165
+
166
+ Arguments:
167
+ column {string} -- The column name.
168
+
169
+ Keyword Arguments:
170
+ nullable {bool} -- Whether the column is nullable. (default: {False})
171
+
172
+ Returns:
173
+ self
174
+ """
175
+ self._last_column = self.table.add_column(
176
+ column, "increments", nullable=nullable
177
+ )
178
+
179
+ self.primary(column)
180
+ return self
181
+
182
+ def tiny_increments(self, column, nullable=False):
183
+ """Sets a column to be the auto tiny incrementing primary key representation for the table.
184
+
185
+ Arguments:
186
+ column {string} -- The column name.
187
+
188
+ Keyword Arguments:
189
+ nullable {bool} -- Whether the column is nullable. (default: {False})
190
+
191
+ Returns:
192
+ self
193
+ """
194
+ self._last_column = self.table.add_column(
195
+ column, "tiny_increments", nullable=nullable
196
+ )
197
+
198
+ self.primary(column)
199
+ return self
200
+
201
+ def id(self, column="id"):
202
+ """Sets a column to be the auto-incrementing big integer (8-byte) primary key representation for the table.
203
+
204
+ Arguments:
205
+ column {string} -- The column name. Defaults to "id".
206
+
207
+ Returns:
208
+ self
209
+ """
210
+ return self.big_increments(column)
211
+
212
+ def uuid(self, column, nullable=False, length=36):
213
+ """Sets a column to be the UUID4 representation for the table.
214
+
215
+ Arguments:
216
+ column {string} -- The column name.
217
+
218
+ Keyword Arguments:
219
+ nullable {bool} -- Whether the column is nullable. (default: {False})
220
+
221
+ Returns:
222
+ self
223
+ """
224
+ self._last_column = self.table.add_column(
225
+ column, "uuid", nullable=nullable, length=length
226
+ )
227
+ return self
228
+
229
+ def big_increments(self, column, nullable=False):
230
+ """Sets a column to be the the big integer increments representation for the table
231
+
232
+ Arguments:
233
+ column {string} -- The column name.
234
+
235
+ Keyword Arguments:
236
+ nullable {bool} -- Whether the column is nullable. (default: {False})
237
+
238
+ Returns:
239
+ self
240
+ """
241
+ self._last_column = self.table.add_column(
242
+ column, "big_increments", nullable=nullable
243
+ )
244
+
245
+ self.primary(column)
246
+ return self
247
+
248
+ def binary(self, column, nullable=False):
249
+ """Sets a column to be the binary representation for the table.
250
+
251
+ Arguments:
252
+ column {string} -- The column name.
253
+
254
+ Keyword Arguments:
255
+ nullable {bool} -- Whether the column is nullable. (default: {False})
256
+
257
+ Returns:
258
+ self
259
+ """
260
+ self._last_column = self.table.add_column(
261
+ column, "binary", nullable=nullable
262
+ )
263
+ return self
264
+
265
+ def boolean(self, column, nullable=False):
266
+ """Sets a column to be the boolean representation for the table.
267
+
268
+ Arguments:
269
+ column {string} -- The column name.
270
+
271
+ Keyword Arguments:
272
+ nullable {bool} -- Whether the column is nullable. (default: {False})
273
+
274
+ Returns:
275
+ self
276
+ """
277
+ self._last_column = self.table.add_column(
278
+ column, "boolean", nullable=nullable
279
+ )
280
+ return self
281
+
282
+ def default(self, value, raw=False):
283
+ self._last_column.default = value
284
+ self._last_column.default_is_raw = raw
285
+ return self
286
+
287
+ def default_raw(self, value):
288
+ self.default(value, True)
289
+ return self
290
+
291
+ def char(self, column, length=1, nullable=False):
292
+ """Sets a column to be the char representation for the table.
293
+
294
+ Arguments:
295
+ column {string} -- The column name.
296
+
297
+ Keyword Arguments:
298
+ length {int} -- The length for the column (default: {1})
299
+ nullable {bool} -- Whether the column is nullable. (default: {False})
300
+
301
+ Returns:
302
+ self
303
+ """
304
+ self._last_column = self.table.add_column(
305
+ column, "char", length=length, nullable=nullable
306
+ )
307
+ return self
308
+
309
+ def date(self, column, nullable=False):
310
+ """Sets a column to be the date representation for the table.
311
+
312
+ Arguments:
313
+ column {string} -- The column name.
314
+
315
+ Keyword Arguments:
316
+ nullable {bool} -- Whether the column is nullable. (default: {False})
317
+
318
+ Returns:
319
+ self
320
+ """
321
+ self._last_column = self.table.add_column(
322
+ column, "date", nullable=nullable
323
+ )
324
+ return self
325
+
326
+ def time(self, column, nullable=False):
327
+ """Sets a column to be the time representation for the table.
328
+
329
+ Arguments:
330
+ column {string} -- The column name.
331
+
332
+ Keyword Arguments:
333
+ nullable {bool} -- Whether the column is nullable. (default: {False})
334
+
335
+ Returns:
336
+ self
337
+ """
338
+ self._last_column = self.table.add_column(
339
+ column, "time", nullable=nullable
340
+ )
341
+ return self
342
+
343
+ def datetime(self, column, nullable=False, now=False):
344
+ """Sets a column to be the datetime representation for the table.
345
+
346
+ Arguments:
347
+ column {string} -- The column name.
348
+
349
+
350
+ Keyword Arguments:
351
+ nullable {bool} -- Whether the column is nullable. (default: {False})
352
+ now {bool} -- Whether the default for the column should be the current time. (default: {False})
353
+
354
+ Returns:
355
+ self
356
+ """
357
+ self._last_column = self.table.add_column(
358
+ column, "datetime", nullable=nullable
359
+ )
360
+
361
+ if now:
362
+ self._last_column.use_current()
363
+
364
+ return self
365
+
366
+ def timestamp(self, column, nullable=False, now=False):
367
+ """Sets a column to be the timestamp representation for the table.
368
+
369
+ Arguments:
370
+ column {string} -- The column name.
371
+
372
+ Keyword Arguments:
373
+ nullable {bool} -- Whether the column is nullable. (default: {False})
374
+ now {bool} -- Whether the default for the column should be the current time. (default: {False})
375
+
376
+ Returns:
377
+ self
378
+ """
379
+
380
+ self._last_column = self.table.add_column(
381
+ column, "timestamp", nullable=nullable
382
+ )
383
+
384
+ if now:
385
+ self._last_column.use_current()
386
+
387
+ return self
388
+
389
+ def timestamps(self):
390
+ """Creates `created_at` and `updated_at` timestamp columns.
391
+
392
+ Returns:
393
+ self
394
+ """
395
+ self.datetime("created_at", nullable=True, now=True)
396
+
397
+ self.datetime("updated_at", nullable=True, now=True)
398
+
399
+ return self
400
+
401
+ def decimal(self, column, length=17, precision=6, nullable=False):
402
+ """Sets a column to be the decimal representation for the table.
403
+
404
+ Arguments:
405
+ column {string} -- The name of the column.
406
+
407
+ Keyword Arguments:
408
+ length {int} -- The total length of the decimal number (default: {17})
409
+ precision {int} -- The number of places that should be used for floating numbers. (default: {6})
410
+ nullable {bool} -- Whether the column is nullable (default: {False})
411
+
412
+ Returns:
413
+ self
414
+ """
415
+
416
+ self._last_column = self.table.add_column(
417
+ column,
418
+ "decimal",
419
+ length="{length}, {precision}".format(
420
+ length=length, precision=precision
421
+ ),
422
+ nullable=nullable,
423
+ )
424
+ return self
425
+
426
+ def float(self, column, length=19, precision=4, nullable=False):
427
+ """Sets a column to be the float representation for the table.
428
+
429
+ Arguments:
430
+ column {string} -- The name of the column.
431
+
432
+ Keyword Arguments:
433
+ length {int} -- The total length of the float number (default: {17})
434
+ precision {int} -- The number of places that should be used for floating numbers. (default: {6})
435
+ nullable {bool} -- Whether the column is nullable (default: {False})
436
+
437
+ Returns:
438
+ self
439
+ """
440
+ self._last_column = self.table.add_column(
441
+ column,
442
+ "float",
443
+ length="{length}, {precision}".format(
444
+ length=length, precision=precision
445
+ ),
446
+ nullable=nullable,
447
+ )
448
+ return self
449
+
450
+ def double(self, column, nullable=False):
451
+ """Sets a column to be the the double representation for the table
452
+
453
+ Arguments:
454
+ column {string} -- The column name.
455
+
456
+ Keyword Arguments:
457
+ nullable {bool} -- Whether the column is nullable. (default: {False})
458
+
459
+ Returns:
460
+ self
461
+ """
462
+ self._last_column = self.table.add_column(
463
+ column, "double", nullable=nullable
464
+ )
465
+ return self
466
+
467
+ def enum(self, column, options=None, nullable=False):
468
+ """Sets a column to be the enum representation for the table.
469
+
470
+ Arguments:
471
+ column {string} -- The column name.
472
+
473
+ Keyword Arguments:
474
+ options {list} -- A list of available options for the enum. (default: {False})
475
+ nullable {bool} -- Whether the column is nullable. (default: {False})
476
+
477
+ Returns:
478
+ self
479
+ """
480
+
481
+ options = options or []
482
+ new_options = ""
483
+ for option in options:
484
+ new_options += "'{}',".format(option)
485
+ new_options = new_options.rstrip(",")
486
+ self._last_column = self.table.add_column(
487
+ column, "enum", length="255", values=options, nullable=nullable
488
+ )
489
+ return self
490
+
491
+ def text(self, column, length=None, nullable=False):
492
+ """Sets a column to be the text representation for the table.
493
+
494
+ Arguments:
495
+ column {string} -- The column name.
496
+
497
+ Keyword Arguments:
498
+ length {int} -- The length of the column if any. (default: {False})
499
+ nullable {bool} -- Whether the column is nullable. (default: {False})
500
+
501
+ Returns:
502
+ self
503
+ """
504
+ self._last_column = self.table.add_column(
505
+ column, "text", length=length, nullable=nullable
506
+ )
507
+ return self
508
+
509
+ def tiny_text(self, column, length=None, nullable=False):
510
+ """Sets a column to be the text representation for the table.
511
+
512
+ Arguments:
513
+ column {string} -- The column name.
514
+
515
+ Keyword Arguments:
516
+ length {int} -- The length of the column if any. (default: {False})
517
+ nullable {bool} -- Whether the column is nullable. (default: {False})
518
+
519
+ Returns:
520
+ self
521
+ """
522
+ self._last_column = self.table.add_column(
523
+ column, "tiny_text", length=length, nullable=nullable
524
+ )
525
+ return self
526
+
527
+ def unsigned_decimal(self, column, length=17, precision=6, nullable=False):
528
+ """Sets a column to be the text representation for the table.
529
+
530
+ Arguments:
531
+ column {string} -- The column name.
532
+
533
+ Keyword Arguments:
534
+ length {int} -- The length of the column if any. (default: {False})
535
+ nullable {bool} -- Whether the column is nullable. (default: {False})
536
+
537
+ Returns:
538
+ self
539
+ """
540
+ self._last_column = self.table.add_column(
541
+ column,
542
+ "decimal",
543
+ length="{length}, {precision}".format(
544
+ length=length, precision=precision
545
+ ),
546
+ nullable=nullable,
547
+ ).unsigned()
548
+ return self
549
+ return self
550
+
551
+ def long_text(self, column, length=None, nullable=False):
552
+ """Sets a column to be the long_text representation for the table.
553
+
554
+ Arguments:
555
+ column {string} -- The column name.
556
+
557
+ Keyword Arguments:
558
+ length {int} -- The length of the column if any. (default: {False})
559
+ nullable {bool} -- Whether the column is nullable. (default: {False})
560
+
561
+ Returns:
562
+ self
563
+ """
564
+ self._last_column = self.table.add_column(
565
+ column, "long_text", length=length, nullable=nullable
566
+ )
567
+ return self
568
+
569
+ def json(self, column, nullable=False):
570
+ """Sets a column to be the json representation for the table.
571
+
572
+ Arguments:
573
+ column {string} -- The column name.
574
+
575
+ Keyword Arguments:
576
+ nullable {bool} -- Whether the column is nullable. (default: {False})
577
+
578
+ Returns:
579
+ self
580
+ """
581
+ self._last_column = self.table.add_column(
582
+ column, "json", nullable=nullable
583
+ )
584
+ return self
585
+
586
+ def jsonb(self, column, nullable=False):
587
+ """Sets a column to be the jsonb representation for the table.
588
+
589
+ Arguments:
590
+ column {string} -- The column name.
591
+
592
+ Keyword Arguments:
593
+ nullable {bool} -- Whether the column is nullable. (default: {False})
594
+
595
+ Returns:
596
+ self
597
+ """
598
+ self._last_column = self.table.add_column(
599
+ column, "jsonb", nullable=nullable
600
+ )
601
+ return self
602
+
603
+ def inet(self, column, length=255, nullable=False):
604
+ """Sets a column to be the inet representation for the table.
605
+
606
+ Arguments:
607
+ column {string} -- The column name.
608
+
609
+ Keyword Arguments:
610
+ nullable {bool} -- Whether the column is nullable. (default: {False})
611
+
612
+ Returns:
613
+ self
614
+ """
615
+ self._last_column = self.table.add_column(
616
+ column, "inet", length=255, nullable=nullable
617
+ )
618
+ return self
619
+
620
+ def cidr(self, column, length=255, nullable=False):
621
+ """Sets a column to be the cidr representation for the table.
622
+
623
+ Arguments:
624
+ column {string} -- The column name.
625
+
626
+ Keyword Arguments:
627
+ nullable {bool} -- Whether the column is nullable. (default: {False})
628
+
629
+ Returns:
630
+ self
631
+ """
632
+ self._last_column = self.table.add_column(
633
+ column, "cidr", length=255, nullable=nullable
634
+ )
635
+ return self
636
+
637
+ def macaddr(self, column, length=255, nullable=False):
638
+ """Sets a column to be the macaddr representation for the table.
639
+
640
+ Arguments:
641
+ column {string} -- The column name.
642
+
643
+ Keyword Arguments:
644
+ nullable {bool} -- Whether the column is nullable. (default: {False})
645
+
646
+ Returns:
647
+ self
648
+ """
649
+ self._last_column = self.table.add_column(
650
+ column, "macaddr", length=255, nullable=nullable
651
+ )
652
+ return self
653
+
654
+ def point(self, column, nullable=False):
655
+ """Sets a column to be the point representation for the table.
656
+
657
+ Arguments:
658
+ column {string} -- The column name.
659
+
660
+ Keyword Arguments:
661
+ nullable {bool} -- Whether the column is nullable. (default: {False})
662
+
663
+ Returns:
664
+ self
665
+ """
666
+ self._last_column = self.table.add_column(
667
+ column, "point", nullable=nullable
668
+ )
669
+ return self
670
+
671
+ def geometry(self, column, nullable=False):
672
+ """Sets a column to be the geometry representation for the table.
673
+
674
+ Arguments:
675
+ column {string} -- The column name.
676
+
677
+ Keyword Arguments:
678
+ nullable {bool} -- Whether the column is nullable. (default: {False})
679
+
680
+ Returns:
681
+ self
682
+ """
683
+ self._last_column = self.table.add_column(
684
+ column, "geometry", nullable=nullable
685
+ )
686
+ return self
687
+
688
+ def year(self, column, length=4, default=None, nullable=False):
689
+ """Sets a column to be the year representation for the table.
690
+
691
+ Arguments:
692
+ column {string} -- The column name.
693
+
694
+ Keyword Arguments:
695
+ nullable {bool} -- Whether the column is nullable. (default: {False})
696
+
697
+ Returns:
698
+ self
699
+ """
700
+ self._last_column = self.table.add_column(
701
+ column, "year", length=length, nullable=nullable, default=default
702
+ )
703
+ return self
704
+
705
+ def unsigned(self, column=None, length=None, nullable=False):
706
+ """Sets a column to be the unsigned integer representation for the table.
707
+
708
+ Arguments:
709
+ column {string} -- The column name.
710
+
711
+ Keyword Arguments:
712
+ length {int} -- The length of the column. (default: {False})
713
+ nullable {bool} -- Whether the column is nullable. (default: {False})
714
+
715
+ Returns:
716
+ self
717
+ """
718
+ if not column:
719
+ self._last_column.unsigned()
720
+ return self
721
+
722
+ self._last_column = self.table.add_column(
723
+ column, "unsigned", length=length, nullable=nullable
724
+ ).unsigned()
725
+ return self
726
+
727
+ def unsigned_integer(self, column, nullable=False):
728
+ """Sets a column to be the unsigned integer representation for the table.
729
+
730
+ Arguments:
731
+ column {string} -- The column name.
732
+
733
+ Keyword Arguments:
734
+ nullable {bool} -- Whether the column is nullable. (default: {False})
735
+
736
+ Returns:
737
+ self
738
+ """
739
+ self._last_column = self.table.add_column(
740
+ column, "integer", nullable=nullable
741
+ ).unsigned()
742
+ return self
743
+
744
+ def morphs(self, column, nullable=False, indexes=True):
745
+ """Sets a column to be used in a polymorphic relationship.
746
+
747
+ Arguments:
748
+ column {string} -- The column name.
749
+
750
+ Keyword Arguments:
751
+ nullable {bool} -- Whether the column is nullable. (default: {False})
752
+
753
+ Returns:
754
+ self
755
+ """
756
+ _columns = []
757
+ _columns.append(
758
+ self.table.add_column(
759
+ "{}_id".format(column), "integer", nullable=nullable
760
+ ).unsigned()
761
+ )
762
+ _columns.append(
763
+ self.table.add_column(
764
+ "{}_type".format(column),
765
+ "string",
766
+ nullable=nullable,
767
+ length=self._default_string_length,
768
+ )
769
+ )
770
+
771
+ if indexes:
772
+ for column in _columns:
773
+ self.index(column.name)
774
+
775
+ self._last_column = _columns
776
+ return self
777
+
778
+ def to_sql(self):
779
+ """Compiles the blueprint class into a sql statement.
780
+
781
+ Returns:
782
+ string -- The SQL statement generated.
783
+ """
784
+ if self._action == "create":
785
+ return self.platform().compile_create_sql(self.table)
786
+ elif self._action == "create_table_if_not_exists":
787
+ return self.platform().compile_create_sql(
788
+ self.table, if_not_exists=True
789
+ )
790
+ else:
791
+ if not self._dry:
792
+ # get current table schema
793
+ table = self.platform().get_current_schema(
794
+ self.connection, self.table.name, schema=self.schema
795
+ )
796
+ self.table.from_table = table
797
+
798
+ return self.platform().compile_alter_sql(self.table)
799
+
800
+ def __enter__(self):
801
+ return self
802
+
803
+ def __exit__(self, exc_type, exc_value, exc_traceback):
804
+ if self._dry:
805
+ return
806
+ return self.connection.query(self.to_sql(), ())
807
+
808
+ def nullable(self):
809
+ """Sets the last columns created as nullable
810
+
811
+ Returns:
812
+ self
813
+ """
814
+ _columns = []
815
+ if not isinstance(self._last_column, list):
816
+ _columns = [self._last_column]
817
+
818
+ for column in _columns:
819
+ column.nullable()
820
+ return self
821
+
822
+ def soft_deletes(self, name="deleted_at"):
823
+ return self.datetime(name, nullable=True).nullable()
824
+
825
+ def unique(self, column=None, name=None):
826
+ """Sets the last column to be unique if no column name is passed.
827
+
828
+ If a column name is passed this method will create a new unique column.
829
+
830
+ Keyword Arguments:
831
+ column {string} -- The name of the column. (default: {None})
832
+
833
+ Returns:
834
+ self
835
+ """
836
+ if not column:
837
+ column = self._last_column.name
838
+
839
+ if not isinstance(column, list):
840
+ column = [column]
841
+
842
+ self.table.add_constraint(
843
+ name or f"{self.table.name}_{'_'.join(column)}_unique",
844
+ "unique",
845
+ columns=column,
846
+ )
847
+
848
+ return self
849
+
850
+ def index(self, column=None, name=None):
851
+ """Creates a constraint based on the index constraint representation of the table.
852
+
853
+ Arguments:
854
+ column {string} -- The name of the column to create the index on.
855
+
856
+ Returns:
857
+ self
858
+ """
859
+ if not column:
860
+ column = self._last_column.name
861
+
862
+ if not isinstance(column, list):
863
+ column = [column]
864
+
865
+ self.table.add_index(
866
+ column,
867
+ name or f"{self.table.name}_{'_'.join(column)}_index",
868
+ "index",
869
+ )
870
+
871
+ return self
872
+
873
+ def fulltext(self, column=None, name=None):
874
+ """Creates a constraint based on the full text constraint representation of the table.
875
+
876
+ Arguments:
877
+ column {string} -- The name of the column to create the index on.
878
+
879
+ Returns:
880
+ self
881
+ """
882
+ if not column:
883
+ column = self._last_column.name
884
+
885
+ if not isinstance(column, list):
886
+ column = [column]
887
+
888
+ self.table.add_constraint(
889
+ name or f"{'_'.join(column)}_fulltext", "fulltext", column
890
+ )
891
+
892
+ return self
893
+
894
+ def primary(self, column=None, name=None):
895
+ """Creates a constraint based on the primary key constraint representation of the table.
896
+ Sets the constraint on the last column if no column name is passed.
897
+
898
+ Arguments:
899
+ column {string} -- The name of the column to create the index on. (default: {None})
900
+
901
+ Returns:
902
+ self
903
+ """
904
+ if column is None:
905
+ column = self._last_column.name
906
+
907
+ if not isinstance(column, list):
908
+ column = [column]
909
+
910
+ self.table.add_constraint(
911
+ name or f"{self.table.name}_{'_'.join(column)}_primary",
912
+ "primary_key",
913
+ columns=column,
914
+ )
915
+
916
+ return self
917
+
918
+ def add_foreign(self, columns, name=None):
919
+ """Creates the foreign spliting the foreign name, reference column, and
920
+ reference table.
921
+
922
+ Arguments:
923
+ columns {string} -- The name of the from_column . to_column . table
924
+ """
925
+ if len(columns.split(".")) != 3:
926
+ raise Exception(
927
+ "Wrong add_foreign argument, the struncture is from_column.to_column.table"
928
+ )
929
+ from_column, to_column, table = columns.split(".")
930
+ return (
931
+ self.foreign(from_column, name=name)
932
+ .references(to_column)
933
+ .on(table)
934
+ )
935
+
936
+ def foreign(self, column, name=None):
937
+ """Starts the creation of a foreign key constraint
938
+
939
+ Arguments:
940
+ column {string} -- The name of the column to create the index on.
941
+
942
+ Returns:
943
+ self
944
+ """
945
+ self._last_foreign = self.table.add_foreign_key(
946
+ column, name=name or f"{self.table.name}_{column}_foreign"
947
+ )
948
+ return self
949
+
950
+ def foreign_id(self, column):
951
+ """Sets a column to be a unsigned big integer (8-byte) representation for a foreign ID.
952
+
953
+ Arguments:
954
+ column {string} -- The name of the column to reference.
955
+
956
+ Returns:
957
+ self
958
+ """
959
+ return self.unsigned_big_integer(column).foreign(column)
960
+
961
+ def foreign_uuid(self, column):
962
+ """Sets a column to be a UUID representation for a foreign UUID.
963
+
964
+ Arguments:
965
+ column {string} -- The name of the column to reference.
966
+
967
+ Returns:
968
+ self
969
+ """
970
+ return self.uuid(column).foreign(column)
971
+
972
+ def foreign_id_for(self, model, column=None):
973
+ """Sets a column to be a unsigned big integer (8-byte) representation for a foreign ID.
974
+
975
+ Arguments:
976
+ model {Model} -- The model to reference.
977
+
978
+ Returns:
979
+ self
980
+ """
981
+ clm = column if column else model.get_foreign_key()
982
+
983
+ return (
984
+ self.foreign_id(clm)
985
+ if model.get_primary_key_type() == "int"
986
+ else self.foreign_uuid(column)
987
+ )
988
+
989
+ def references(self, column):
990
+ """Sets the other column on the foreign table that the local column will use to reference.
991
+
992
+ Arguments:
993
+ column {string} -- The name of the column to create the index on.
994
+
995
+ Returns:
996
+ self
997
+ """
998
+ self._last_foreign.references(column)
999
+ return self
1000
+
1001
+ def on(self, table):
1002
+ """Sets the foreign table that the local column will use to reference on.
1003
+
1004
+ Arguments:
1005
+ table {string} -- The foreign table name.
1006
+
1007
+ Returns:
1008
+ self
1009
+ """
1010
+ self._last_foreign.on(table)
1011
+ return self
1012
+
1013
+ def on_delete(self, action):
1014
+ """Sets the last foreign key to a specific on delete action.
1015
+
1016
+ Arguments:
1017
+ action {string} -- The specific action to do on delete.
1018
+
1019
+ Returns:
1020
+ self
1021
+ """
1022
+ self._last_foreign.on_delete(action)
1023
+ return self
1024
+
1025
+ def on_update(self, action):
1026
+ """Sets the last foreign key to a specific on update action.
1027
+
1028
+ Arguments:
1029
+ action {string} -- The specific action to do on update.
1030
+
1031
+ Returns:
1032
+ self
1033
+ """
1034
+ self._last_foreign.on_update(action)
1035
+ return self
1036
+
1037
+ def comment(self, comment):
1038
+ self._last_column.add_comment(comment)
1039
+ return self
1040
+
1041
+ def table_comment(self, comment):
1042
+ self.table.add_comment(comment)
1043
+ return self
1044
+
1045
+ def rename(self, old_column, new_column, data_type, length=None):
1046
+ """Rename a column from the old value to a new value.
1047
+
1048
+ Arguments:
1049
+ old_column {string} -- The name of the original old column name.
1050
+ new_column {string} -- The name of the new column name.
1051
+
1052
+ Returns:
1053
+ self
1054
+ """
1055
+ self.table.rename_column(
1056
+ old_column, new_column, data_type, length=length
1057
+ )
1058
+ return self
1059
+
1060
+ def after(self, old_column):
1061
+ """Sets the column that this new column should be created after.
1062
+
1063
+ This is useful for setting the location of the new column in the table schema.
1064
+
1065
+ Arguments:
1066
+ old_column {string} -- The column that this new column should be created after
1067
+
1068
+ Returns:
1069
+ self
1070
+ """
1071
+ self._last_column.after(old_column)
1072
+ return self
1073
+
1074
+ def drop_column(self, *columns):
1075
+ """Sets columns that should be dropped
1076
+
1077
+ Returns:
1078
+ self
1079
+ """
1080
+ for column in columns:
1081
+ self.table.drop_column(column)
1082
+
1083
+ return self
1084
+
1085
+ def drop_index(self, index):
1086
+ """Specifies indexes that should be dropped.
1087
+
1088
+ Arguments:
1089
+ indexes {list|string} -- Either a list of indexes or a specific index.
1090
+
1091
+ Returns:
1092
+ self
1093
+ """
1094
+ if isinstance(index, list):
1095
+ for column in index:
1096
+ self.table.remove_index(f"{self.table.name}_{column}_index")
1097
+
1098
+ return self
1099
+
1100
+ self.table.remove_index(index)
1101
+
1102
+ return self
1103
+
1104
+ def change(self):
1105
+ self.table.change_column(self._last_column)
1106
+ return self
1107
+
1108
+ def drop_unique(self, index):
1109
+ """Drops a unique index.
1110
+
1111
+ Arguments:
1112
+ indexes {list|string} -- Either a list of indexes or a specific index.
1113
+
1114
+ Returns:
1115
+ self
1116
+ """
1117
+ if isinstance(index, list):
1118
+ for column in index:
1119
+ self.table.remove_unique_index(
1120
+ f"{self.table.name}_{column}_unique"
1121
+ )
1122
+
1123
+ return self
1124
+
1125
+ self.table.remove_unique_index(index)
1126
+
1127
+ def drop_primary(self, index):
1128
+ """Drops a unique index.
1129
+
1130
+ Arguments:
1131
+ indexes {list|string} -- Either a list of indexes or a specific index.
1132
+
1133
+ Returns:
1134
+ self
1135
+ """
1136
+ if isinstance(index, list):
1137
+ for column in index:
1138
+ self.table.drop_primary(f"{self.table.name}_{column}_primary")
1139
+
1140
+ return self
1141
+
1142
+ self.table.drop_primary(index)
1143
+
1144
+ def drop_foreign(self, index):
1145
+ """Drops foreign key indexes.
1146
+
1147
+ Arguments:
1148
+ indexes {list|string} -- Either a list of indexes or a specific index.
1149
+
1150
+ Returns:
1151
+ self
1152
+ """
1153
+ if isinstance(index, list):
1154
+ for column in index:
1155
+ self.table.drop_foreign(f"{self.table.name}_{column}_foreign")
1156
+
1157
+ return self
1158
+
1159
+ self.table.drop_foreign(index)
1160
+
1161
+ return self