Skip to content
Merged
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
16 changes: 14 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Change Log


## [Unreleased]

#### Breaking Changes

- Using `*increments()` for a primary key definition now requires adding `.primary()` to the column definition. `id()` keeps creating the primary key automatically.

### Fixed

- `*increments()` could not be used for non primary key columns
- Schema honors explicitly provided connection details instead of always reloading them from the config file

## [3.0.0] - 2025-03-30

### Changed
Expand All @@ -16,9 +27,10 @@

### Fixed

- QueryBuilder requires `database.py` even when passing connection detain in directly
- Several classes would import DB (ConnectionResolver) from config even if inline connection details are provided
- Model `update` and `delete` not casting passed values


## [2.24.0] - 2025-01-23

### Added
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': 'joe@email.com',
'password': 'secret'
})
User.on(self.connection).set_schema(self.schema_name).create(
{"name": "Joe", "email": "joe@email.com", "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
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 @@ -88,8 +89,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 self.connection_details:
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
14 changes: 10 additions & 4 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,17 @@ 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",
# The PRIMARY KEY constraint for these is added in columnize() after
# the nullability clause: T-SQL expects IDENTITY before any column
# constraints ([name] INT IDENTITY NOT NULL PRIMARY KEY).
"tiny_increments_primary": "TINYINT IDENTITY",
"increments_primary": "INT IDENTITY",
"big_increments_primary": "BIGINT IDENTITY",
}

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

constraint = ""
column_constraint = ""
if column.primary:
if column.column_type.endswith("increments_primary"):
constraint = " PRIMARY KEY"

if column.column_type == "enum":
Expand Down
14 changes: 8 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,15 @@ class MySQLPlatform(Platform):
"date": "DATE",
"year": "YEAR",
"datetime": "DATETIME",
"tiny_increments": "TINYINT AUTO_INCREMENT",
"unsigned": "INT UNSIGNED",
# MySQL requires an AUTO_INCREMENT column to be defined as a key
# (error 1075), so non-primary *_increments columns carry UNIQUE.
"tiny_increments": "TINYINT UNSIGNED AUTO_INCREMENT UNIQUE",
"increments": "INT UNSIGNED AUTO_INCREMENT UNIQUE",
"big_increments": "BIGINT UNSIGNED AUTO_INCREMENT UNIQUE",
"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 +93,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
Loading