Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Change Log

## [3.0.0] - 2025-03-30
## [3.0.0] - 2025-12-05

### Changed

Expand All @@ -9,15 +9,20 @@

#### Breaking Changes

- Using `*increments()` for primary key definition now requires adding `.primay()` to the column definition.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be primary()

- Changed raw expressions placeholder from requiringing explicit quoring per grammar (like this '?') to automaic (like this ?)
- Changed `update` and `delete` methods to return the affected rows instead of the model
- Seeding depencies are now in a separate`[seeder]` extension
- `Factory` class must now be imported from the sub-package `masoniteorm.factories`

### Fixed

- Model `update` and `delete` not casting passed values
- QueryBuilder does not require `database.py` when passing connection detain in directly
- Import DB (ConnectionResolver) from config even if inline connection details are provided
- `*increments()` can be used fir non primary key columns if supported by the platform
- `*increments()` can be used fir non primary key columns if supported by the platform
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lines 22 y 23 are duplicated

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, fir -> for?


- Model `update` and `delete` not casting passed values

## [2.24.0] - 2025-01-23

Expand Down
26 changes: 12 additions & 14 deletions databases/migrations/2018_01_09_043202_create_users_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@ class CreateUsersTable(Migration):

def up(self):
"""Run the migrations."""
with self.schema.create('users') as table:
table.increments('id')
table.string('name')
table.string('email').unique()
table.string('password')
table.string('second_password').nullable()
table.string('remember_token').nullable()
table.timestamp('verified_at').nullable()
with self.schema.create("users") as table:
table.increments("id").primary()
table.string("name")
table.string("email").unique()
table.string("password")
table.string("second_password").nullable()
table.string("remember_token").nullable()
table.timestamp("verified_at").nullable()
table.timestamps()

if not self.schema._dry:
User.on(self.connection).set_schema(self.schema_name).create({
'name': 'Joe',
'email': '[email protected]',
'password': 'secret'
})
User.on(self.connection).set_schema(self.schema_name).create(
{"name": "Joe", "email": "[email protected]", "password": "secret"}
)

def down(self):
"""Revert the migrations."""
self.schema.drop('users')
self.schema.drop("users")
13 changes: 7 additions & 6 deletions databases/migrations/2020_04_17_000000_create_friends_table.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
from src.masoniteorm.migrations.Migration import Migration


class CreateFriendsTable(Migration):

def up(self):
def up(self):
"""
Run the migrations.
"""

with self.schema.create('friends') as table:
table.increments('id')
table.string('name')
table.integer('age')
with self.schema.create("friends") as table:
table.increments("id").primary()
table.string("name")
table.integer("age")

def down(self):
"""
Revert the migrations.
"""
self.schema.drop('friends')
self.schema.drop("friends")
13 changes: 7 additions & 6 deletions databases/migrations/2020_04_17_00000_create_articles_table.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
from src.masoniteorm.migrations.Migration import Migration


class CreateArticlesTable(Migration):

def up(self):
def up(self):
"""
Run the migrations.
"""
with self.schema.create('fans') as table:
table.increments('id')
table.string('name')
table.integer('age')
with self.schema.create("fans") as table:
table.increments("id").primary()
table.string("name")
table.integer("age")

def down(self):
"""
Revert the migrations.
"""
self.schema.drop('fans')
self.schema.drop("fans")
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def up(self):
Run the migrations.
"""
with self.schema.create("table_schema") as table:
table.increments('id')
table.string('name')
table.increments("id").primary()
table.string("name")
table.timestamps()

def down(self):
Expand Down
Binary file modified orm.sqlite3
Binary file not shown.
2 changes: 1 addition & 1 deletion src/masoniteorm/migrations/Migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(
def create_table_if_not_exists(self):
if not self.schema.has_table("migrations"):
with self.schema.create("migrations") as table:
table.increments("migration_id")
table.increments("migration_id").primary()
table.string("migration")
table.integer("batch")

Expand Down
31 changes: 23 additions & 8 deletions src/masoniteorm/schema/Blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def _compile_alter(self):
)._compile_create()

def increments(self, column, nullable=False):
"""Sets a column to be the auto incrementing primary key representation for the table.
"""Sets a column to be the auto-incrementing integer.

Arguments:
column {string} -- The column name.
Expand All @@ -176,11 +176,10 @@ def increments(self, column, nullable=False):
column, "increments", nullable=nullable
)

self.primary(column)
return self

def tiny_increments(self, column, nullable=False):
"""Sets a column to be the auto tiny incrementing primary key representation for the table.
"""Sets a column to an auto-increment tiny integer.

Arguments:
column {string} -- The column name.
Expand All @@ -195,19 +194,18 @@ def tiny_increments(self, column, nullable=False):
column, "tiny_increments", nullable=nullable
)

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.
"""Sets a column to an auto-incrementing big integer.

Arguments:
column {string} -- The column name. Defaults to "id".

Returns:
self
"""
return self.big_increments(column)
return self.big_increments(column).primary()

def uuid(self, column, nullable=False, length=36):
"""Sets a column to be the UUID4 representation for the table.
Expand All @@ -227,7 +225,7 @@ def uuid(self, column, nullable=False, length=36):
return self

def big_increments(self, column, nullable=False):
"""Sets a column to be the the big integer increments representation for the table
"""Sets a column to an auto-incrementing big integer

Arguments:
column {string} -- The column name.
Expand All @@ -242,7 +240,6 @@ def big_increments(self, column, nullable=False):
column, "big_increments", nullable=nullable
)

self.primary(column)
return self

def binary(self, column, nullable=False):
Expand Down Expand Up @@ -905,6 +902,24 @@ def primary(self, column=None, name=None):
column = self._last_column.name

if not isinstance(column, list):
self.table.set_primary_key(column)
check_column = self.table.added_columns.get(column)
if check_column:
check_column.set_as_primary()
if check_column.column_type in [
"tiny_increments",
"increments",
"big_increments",
]:
# use column attributes for primary key auto increment columns
check_column.column_type = (
f"{check_column.column_type}_primary"
)
self.table.added_columns[column] = check_column
return self

self.table.added_columns[column] = check_column

column = [column]

self.table.add_constraint(
Expand Down
11 changes: 9 additions & 2 deletions src/masoniteorm/schema/Schema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ..config import load_config
from ..connections import ConnectionResolver
from ..exceptions import ConnectionNotRegistered
from .Blueprint import Blueprint
from .Table import Table
Expand Down Expand Up @@ -87,8 +88,14 @@ def on(self, connection_key):
Returns:
cls
"""
resolver = load_config(config_path=self.config_path).DB
self.connection_details = resolver.get_connection_details()
if not self.connection_details:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be: If connection_details is not provided, load from config. If provided, use it directly.

resolver = ConnectionResolver(
connection_details=self.connection_details
)
else:
resolver = load_config(config_path=self.config_path).DB
self.connection_details = resolver.get_connection_details()

if connection_key == "default":
self.connection = self.connection_details.get("default")
else:
Expand Down
12 changes: 6 additions & 6 deletions src/masoniteorm/schema/platforms/MSSQLPlatform.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class MSSQLPlatform(Platform):
type_map = {
"string": "VARCHAR",
"char": "CHAR",
"big_increments": "BIGINT IDENTITY",
"integer": "INT",
"big_integer": "BIGINT",
"tiny_integer": "TINYINT",
Expand All @@ -25,7 +24,6 @@ class MSSQLPlatform(Platform):
"tiny_integer_unsigned": "TINYINT",
"small_integer_unsigned": "SMALLINT",
"medium_integer_unsigned": "MEDIUMINT",
"increments": "INT IDENTITY",
"uuid": "CHAR",
"binary": "LONGBLOB",
"boolean": "BOOLEAN",
Expand All @@ -48,9 +46,14 @@ class MSSQLPlatform(Platform):
"date": "DATE",
"year": "YEAR",
"datetime": "DATETIME",
"tiny_increments": "TINYINT IDENTITY",
"unsigned": "INT",
"unsigned_integer": "INT",
"tiny_increments": "TINYINT IDENTITY",
"increments": "INT IDENTITY",
"big_increments": "BIGINT IDENTITY",
"tiny_increments_primary": "TINYINT PRIMARY KEY IDENTITY",
"increments_primary": "INT PRIMARY KEY IDENTITY",
"big_increments_primary": "BIGINT PRIMARY KEY IDENTITY",
}

premapped_nulls = {True: "NULL", False: "NOT NULL"}
Expand Down Expand Up @@ -261,9 +264,6 @@ def columnize(self, columns):

constraint = ""
column_constraint = ""
if column.primary:
constraint = " PRIMARY KEY"

if column.column_type == "enum":
values = ", ".join(f"'{x}'" for x in column.values)
column_constraint = f" CHECK([{column.name}] IN ({values}))"
Expand Down
12 changes: 6 additions & 6 deletions src/masoniteorm/schema/platforms/MySQLPlatform.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class MySQLPlatform(Platform):
"tiny_integer_unsigned": "TINYINT UNSIGNED",
"small_integer_unsigned": "SMALLINT UNSIGNED",
"medium_integer_unsigned": "MEDIUMINT UNSIGNED",
"big_increments": "BIGINT UNSIGNED AUTO_INCREMENT",
"increments": "INT UNSIGNED AUTO_INCREMENT",
"uuid": "CHAR",
"binary": "LONGBLOB",
"boolean": "BOOLEAN",
Expand All @@ -45,8 +43,13 @@ class MySQLPlatform(Platform):
"date": "DATE",
"year": "YEAR",
"datetime": "DATETIME",
"tiny_increments": "TINYINT AUTO_INCREMENT",
"unsigned": "INT UNSIGNED",
"tiny_increments": "TINYINT UNSIGNED AUTO_INCREMENT",
"increments": "INT UNSIGNED AUTO_INCREMENT",
"big_increments": "BIGINT UNSIGNED AUTO_INCREMENT",
"tiny_increments_primary": "TINYINT UNSIGNED PRIMARY KEY AUTO_INCREMENT",
"increments_primary": "INT UNSIGNED PRIMARY KEY AUTO_INCREMENT",
"big_increments_primary": "BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT",
}

premapped_nulls = {True: "NULL", False: "NOT NULL"}
Expand Down Expand Up @@ -88,9 +91,6 @@ def columnize(self, columns):

constraint = ""
column_constraint = ""
if column.primary:
constraint = "PRIMARY KEY"

if column.column_type == "enum":
values = ", ".join(f"'{x}'" for x in column.values)
column_constraint = f"({values})"
Expand Down
40 changes: 1 addition & 39 deletions src/masoniteorm/schema/platforms/Platform.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class Platform:
foreign_key_actions = {
"cascade": "CASCADE",
"set null": "SET NULL",
"cascade": "CASCADE",
"restrict": "RESTRICT",
Expand All @@ -11,44 +10,7 @@ class Platform:
signed = {"signed": "SIGNED", "unsigned": "UNSIGNED"}

def columnize(self, columns):
sql = []
for name, column in columns.items():
if column.length:
length = self.create_column_length(column.column_type).format(
length=column.length
)
else:
length = ""

if column.default in (0,):
default = f" DEFAULT {column.default}"
elif column.default in self.premapped_defaults.keys():
default = self.premapped_defaults.get(column.default)
elif column.default:
if (
isinstance(column.default, (str,))
and not column.default_is_raw
):
default = f" DEFAULT '{column.default}'"
else:
default = f" DEFAULT {column.default}"
else:
default = ""

sql.append(
self.columnize_string()
.format(
name=column.name,
data_type=self.type_map.get(column.column_type, ""),
length=length,
constraint="PRIMARY KEY" if column.primary else "",
nullable=self.premapped_nulls.get(column.is_null) or "",
default=default,
)
.strip()
)

return sql
raise NotImplementedError

def columnize_string(self):
raise NotImplementedError
Expand Down
Loading