Skip to content

Commit

Permalink
Merge pull request #752 from maicol07/2.0
Browse files Browse the repository at this point in the history
Added new methods to builder (#748)
  • Loading branch information
josephmancuso authored Jul 19, 2022
2 parents 1e01c76 + 1b236af commit 8cfc1c8
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 5 deletions.
22 changes: 21 additions & 1 deletion src/masoniteorm/models/Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
from decimal import Decimal

from inflection import tableize
from inflection import tableize, underscore
import inspect

import pendulum
Expand Down Expand Up @@ -143,6 +143,7 @@ class Model(TimeStampsMixin, ObservesEvents, metaclass=ModelMeta):
_booted = False
_scopes = {}
__primary_key__ = "id"
__primary_key_type__ = "int"
__casts__ = {}
__dates__ = []
__hidden__ = []
Expand Down Expand Up @@ -287,6 +288,14 @@ def get_primary_key(self):
"""
return self.__primary_key__

def get_primary_key_type(self):
"""Gets the primary key column type
Returns:
mixed
"""
return self.__primary_key_type__

def get_primary_key_value(self):
"""Gets the primary key value.
Expand All @@ -305,6 +314,17 @@ def get_primary_key_value(self):
f"class '{name}' has no attribute {self.get_primary_key()}. Did you set the primary key correctly on the model using the __primary_key__ attribute?"
)

def get_foreign_key(self):
"""Gets the foreign key based on this model name.
Args:
relationship (str): The relationship name.
Returns:
str
"""
return underscore(self.__class__.__name__ + "_" + self.get_primary_key())

def query(self):
return self.get_builder()

Expand Down
63 changes: 63 additions & 0 deletions src/masoniteorm/schema/Blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ def big_integer(self, column, length=32, nullable=False):
)
return self

def unsigned_big_integer(self, column, length=32, nullable=False):
"""Sets a column to be the unsigned big_integer representation for the table.
Arguments:
column {string} -- The column name.
Keyword Arguments:
length {int} -- The length of the column. (default: {32})
nullable {bool} -- Whether the column is nullable (default: {False})
Returns:
self
"""
return self.big_integer(column, length=length, nullable=nullable).unsigned()

def _compile_create(self):
return self.grammar(creates=self._columns, table=self.table)._compile_create()

Expand Down Expand Up @@ -177,6 +192,17 @@ def tiny_increments(self, column, nullable=False):
self.primary(column)
return self

def id(self, column="id"):
"""Sets a column to be the auto-incrementing big integer (8-byte) primary key representation for the table.
Arguments:
column {string} -- The column name. Defaults to "id".
Returns:
self
"""
return self.big_increments(column)

def uuid(self, column, nullable=False, length=36):
"""Sets a column to be the UUID4 representation for the table.
Expand Down Expand Up @@ -841,6 +867,43 @@ def foreign(self, column, name=None):
)
return self

def foreign_id(self, column):
"""Sets a column to be a unsigned big integer (8-byte) representation for a foreign ID.
Arguments:
column {string} -- The name of the column to reference.
Returns:
self
"""
return self.unsigned_big_integer(column).foreign(column)

def foreign_uuid(self, column):
"""Sets a column to be a UUID representation for a foreign UUID.
Arguments:
column {string} -- The name of the column to reference.
Returns:
self
"""
return self.uuid(column).foreign(column)

def foreign_id_for(self, model, column=None):
"""Sets a column to be a unsigned big integer (8-byte) representation for a foreign ID.
Arguments:
model {Model} -- The model to reference.
Returns:
self
"""
clm = column if column else model.get_foreign_key()

return self.foreign_id(clm)\
if model.get_primary_key_type() == 'int'\
else self.foreign_uuid(column)

def references(self, column):
"""Sets the other column on the foreign table that the local column will use to reference.
Expand Down
24 changes: 20 additions & 4 deletions tests/mysql/schema/test_mysql_schema_builder.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import os
import unittest

from masoniteorm import Model
from tests.integrations.config.database import DATABASES
from src.masoniteorm.connections import MySQLConnection
from src.masoniteorm.schema import Schema
from src.masoniteorm.schema.platforms import MySQLPlatform


class Discussion(Model):
pass


class TestMySQLSchemaBuilder(unittest.TestCase):
maxDiff = None

Expand Down Expand Up @@ -91,6 +96,8 @@ def test_can_add_columns_with_foreign_key_constaint(self):
blueprint.integer("age")
blueprint.integer("profile_id")
blueprint.foreign("profile_id").references("id").on("profiles")
blueprint.foreign_id("post_id").references("id").on("posts")
blueprint.foreign_id_for(Discussion).references("id").on("discussions")

self.assertEqual(len(blueprint.table.added_columns), 3)
self.assertEqual(
Expand All @@ -99,8 +106,11 @@ def test_can_add_columns_with_foreign_key_constaint(self):
"CREATE TABLE `users` (`name` VARCHAR(255) NOT NULL, "
"`age` INT(11) NOT NULL, "
"`profile_id` INT(11) NOT NULL, "
"`post_id` BIGINT UNSIGNED NOT NULL, "
"CONSTRAINT users_name_unique UNIQUE (name), "
"CONSTRAINT users_profile_id_foreign FOREIGN KEY (`profile_id`) REFERENCES `profiles`(`id`))"
"CONSTRAINT users_profile_id_foreign FOREIGN KEY (`profile_id`) REFERENCES `profiles`(`id`), "
"CONSTRAINT users_profile_id_foreign FOREIGN KEY (`post_id`) REFERENCES `posts`(`id`)), "
"CONSTRAINT users_discussions_id_foreign FOREIGN KEY (`discussion_id`) REFERENCES `posts`(`id`))"
],
)

Expand All @@ -126,6 +136,7 @@ def test_can_add_columns_with_foreign_key_constaint(self):
def test_can_advanced_table_creation(self):
with self.schema.create("users") as blueprint:
blueprint.increments("id")
blueprint.id("id2")
blueprint.string("name")
blueprint.tiny_integer("active")
blueprint.string("email").unique()
Expand All @@ -138,15 +149,16 @@ def test_can_advanced_table_creation(self):
blueprint.timestamp("verified_at").nullable()
blueprint.timestamps()

self.assertEqual(len(blueprint.table.added_columns), 13)
self.assertEqual(len(blueprint.table.added_columns), 14)
self.assertEqual(
blueprint.to_sql(),
[
"CREATE TABLE `users` (`id` INT UNSIGNED AUTO_INCREMENT NOT NULL, "
"`id2` BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, "
"`name` VARCHAR(255) NOT NULL, `active` TINYINT(1) NOT NULL, `email` VARCHAR(255) NOT NULL, `gender` ENUM('male', 'female') NOT NULL, "
"`password` VARCHAR(255) NOT NULL, `money` DECIMAL(17, 6) NOT NULL, "
"`admin` INT(11) NOT NULL DEFAULT 0, `option` VARCHAR(255) NOT NULL DEFAULT 'ADMIN', `remember_token` VARCHAR(255) NULL, `verified_at` TIMESTAMP NULL, "
"`created_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT users_id_primary PRIMARY KEY (id), CONSTRAINT users_email_unique UNIQUE (email))"
"`created_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` DATETIME NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT users_id_primary PRIMARY KEY (id), CONSTRAINT users_id2_primary PRIMARY KEY (id2), CONSTRAINT users_email_unique UNIQUE (email))"
],
)

Expand Down Expand Up @@ -259,6 +271,8 @@ def test_can_have_unsigned_columns(self):
blueprint.tiny_integer("tiny_profile_id").unsigned()
blueprint.small_integer("small_profile_id").unsigned()
blueprint.medium_integer("medium_profile_id").unsigned()
blueprint.unsigned_integer("unsigned_profile_id")
blueprint.unsigned_big_integer("unsigned_big_profile_id")

self.assertEqual(
blueprint.to_sql(),
Expand All @@ -268,7 +282,9 @@ def test_can_have_unsigned_columns(self):
"`big_profile_id` BIGINT UNSIGNED NOT NULL, "
"`tiny_profile_id` TINYINT UNSIGNED NOT NULL, "
"`small_profile_id` SMALLINT UNSIGNED NOT NULL, "
"`medium_profile_id` MEDIUMINT UNSIGNED NOT NULL)"
"`medium_profile_id` MEDIUMINT UNSIGNED NOT NULL, "
"`unsigned_profile_id` INT UNSIGNED NOT NULL, "
"`unsigned_big_profile_id` BIGINT UNSIGNED NOT NULL)"
],
)

Expand Down

0 comments on commit 8cfc1c8

Please sign in to comment.