Skip to content

Commit

Permalink
Merge pull request #374 from MasoniteFramework/feature/371
Browse files Browse the repository at this point in the history
Feature/371
  • Loading branch information
josephmancuso authored Feb 5, 2021
2 parents b071349 + c71058d commit c70129f
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 73 deletions.
7 changes: 6 additions & 1 deletion src/masoniteorm/connections/SQLiteConnection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sqlite3
from ..query.grammars import SQLiteGrammar
from .BaseConnection import BaseConnection
from ..schema.platforms import SQLitePlatform
Expand Down Expand Up @@ -46,6 +45,12 @@ def __init__(

def make_connection(self):
"""This sets the connection on the connection class"""
try:
import sqlite3
except ModuleNotFoundError:
raise DriverNotFound(
"You must have the 'sqlite3' package installed to make a connection to MySQL. Please install it using 'pip install pymysql'"
)

if self.has_global_connection():
return self.get_global_connection()
Expand Down
1 change: 1 addition & 0 deletions src/masoniteorm/models/Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class Model(TimeStampsMixin, ObservesEvents, metaclass=ModelMeta):
"where_like",
"where_not_like",
"where_null",
"where_raw",
"where",
"with_",
]
Expand Down
6 changes: 3 additions & 3 deletions src/masoniteorm/schema/Blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def increments(self, column, nullable=False):
self
"""
self._last_column = self.table.add_column(
column, "increments", nullable=nullable
column, "increments", nullable=nullable, primary=True
)
return self

Expand All @@ -170,7 +170,7 @@ def tiny_increments(self, column, nullable=False):
self
"""
self._last_column = self.table.add_column(
column, "tiny_increments", nullable=nullable
column, "tiny_increments", nullable=nullable, primary=True
)
return self

Expand Down Expand Up @@ -204,7 +204,7 @@ def big_increments(self, column, nullable=False):
self
"""
self._last_column = self.table.add_column(
column, "big_increments", nullable=nullable
column, "big_increments", nullable=nullable, primary=True
)
return self

Expand Down
3 changes: 3 additions & 0 deletions src/masoniteorm/schema/Table.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def add_column(
values=None,
nullable=False,
default=None,
primary=False,
):
column = Column(
name,
Expand All @@ -33,6 +34,8 @@ def add_column(
values=values or [],
default=default,
)
if primary:
column.set_as_primary()
self.added_columns.update({name: column})
return column

Expand Down
6 changes: 3 additions & 3 deletions src/masoniteorm/schema/platforms/MSSQLPlatform.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class MSSQLPlatform(Platform):
"integer": "INT",
"big_integer": "BIGINT",
"tiny_integer": "TINYINT",
"big_increments": "BIGINT IDENTITY PRIMARY KEY",
"big_increments": "BIGINT IDENTITY",
"small_integer": "SMALLINT",
"medium_integer": "MEDIUMINT",
"increments": "INT IDENTITY PRIMARY KEY",
"increments": "INT IDENTITY",
"uuid": "CHAR",
"binary": "LONGBLOB",
"boolean": "BOOLEAN",
Expand Down Expand Up @@ -192,7 +192,7 @@ def columnize(self, columns):
constraint = ""
column_constraint = ""
if column.primary:
constraint = "PRIMARY KEY"
constraint = " PRIMARY KEY"

if column.column_type == "enum":
values = ", ".join(f"'{x}'" for x in column.values)
Expand Down
26 changes: 19 additions & 7 deletions src/masoniteorm/schema/platforms/MySQLPlatform.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class MySQLPlatform(Platform):
"integer": "INT",
"big_integer": "BIGINT",
"tiny_integer": "TINYINT",
"big_increments": "BIGINT AUTO_INCREMENT",
"big_increments": "BIGINT UNSIGNED AUTO_INCREMENT",
"small_integer": "SMALLINT",
"medium_integer": "MEDIUMINT",
"increments": "INT UNSIGNED AUTO_INCREMENT PRIMARY KEY",
"increments": "INT UNSIGNED AUTO_INCREMENT",
"uuid": "CHAR",
"binary": "LONGBLOB",
"boolean": "BOOLEAN",
Expand Down Expand Up @@ -80,7 +80,7 @@ def columnize(self, columns):
sql.append(
self.columnize_string()
.format(
name=column.name,
name=self.get_column_string().format(column=column.name),
data_type=self.type_map.get(column.column_type, ""),
column_constraint=column_constraint,
length=length,
Expand All @@ -98,7 +98,7 @@ def compile_create_sql(self, table):

sql.append(
self.create_format().format(
table=table.name,
table=self.get_table_string().format(table=table.name),
columns=", ".join(self.columnize(table.get_added_columns())).strip(),
constraints=", "
+ ", ".join(self.constraintize(table.get_added_constraints(), table))
Expand Down Expand Up @@ -132,7 +132,7 @@ def compile_alter_sql(self, table):
add_columns.append(
self.add_column_string()
.format(
name=column.name,
name=self.get_column_string().format(column=column.name),
data_type=self.type_map.get(column.column_type, ""),
length=length,
constraint="PRIMARY KEY" if column.primary else "",
Expand Down Expand Up @@ -160,7 +160,12 @@ def compile_alter_sql(self, table):
length = ""

renamed_sql.append(
self.rename_column_string().format(to=column.name, old=name).strip()
self.rename_column_string()
.format(
to=self.get_column_string().format(column=column.name),
old=self.get_column_string().format(column=name),
)
.strip()
)

sql.append(
Expand All @@ -184,7 +189,11 @@ def compile_alter_sql(self, table):
dropped_sql = []

for name in table.get_dropped_columns():
dropped_sql.append(self.drop_column_string().format(name=name).strip())
dropped_sql.append(
self.drop_column_string()
.format(name=self.get_column_string().format(column=name))
.strip()
)

sql.append(
self.alter_format().format(
Expand Down Expand Up @@ -264,6 +273,9 @@ def constraintize(self, constraints, table):
def get_table_string(self):
return "`{table}`"

def get_column_string(self):
return "`{column}`"

def create_format(self):
return "CREATE TABLE {table} ({columns}{constraints}{foreign_keys})"

Expand Down
2 changes: 1 addition & 1 deletion src/masoniteorm/schema/platforms/PostgresPlatform.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class PostgresPlatform(Platform):
"integer": "INTEGER",
"big_integer": "BIGINT",
"tiny_integer": "TINYINT",
"big_increments": "SERIAL UNIQUE",
"big_increments": "BIGSERIAL UNIQUE",
"small_integer": "SMALLINT",
"medium_integer": "MEDIUMINT",
"increments": "SERIAL UNIQUE",
Expand Down
2 changes: 1 addition & 1 deletion src/masoniteorm/schema/platforms/SQLitePlatform.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SQLitePlatform(Platform):
"big_increments": "BIGINT",
"small_integer": "SMALLINT",
"medium_integer": "MEDIUMINT",
"increments": "INTEGER PRIMARY KEY",
"increments": "INTEGER",
"uuid": "CHAR",
"binary": "LONGBLOB",
"boolean": "BOOLEAN",
Expand Down
4 changes: 2 additions & 2 deletions tests/mssql/schema/test_mssql_schema_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test_can_advanced_table_creation(self):
self.assertEqual(
blueprint.to_sql(),
(
"CREATE TABLE [users] ([id] INT IDENTITY PRIMARY KEY NOT NULL, [name] VARCHAR(255) NOT NULL, [email] VARCHAR(255) NOT NULL, "
"CREATE TABLE [users] ([id] INT IDENTITY NOT NULL PRIMARY KEY, [name] VARCHAR(255) NOT NULL, [email] VARCHAR(255) NOT NULL, "
"[password] VARCHAR(255) NOT NULL, [admin] INT NOT NULL DEFAULT 0, [remember_token] VARCHAR(255) NULL, "
"[verified_at] DATETIME NULL DEFAULT CURRENT_TIMESTAMP, [created_at] DATETIME NULL DEFAULT CURRENT_TIMESTAMP, "
"[updated_at] DATETIME NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT users_email_unique UNIQUE (email))"
Expand Down Expand Up @@ -102,7 +102,7 @@ def test_can_advanced_table_creation2(self):
self.assertEqual(
blueprint.to_sql(),
(
"CREATE TABLE [users] ([id] INT IDENTITY PRIMARY KEY NOT NULL, [gender] VARCHAR(255) NOT NULL CHECK([gender] IN ('male', 'female')), [name] VARCHAR(255) NOT NULL, [duration] VARCHAR(255) NOT NULL, "
"CREATE TABLE [users] ([id] INT IDENTITY NOT NULL PRIMARY KEY, [gender] VARCHAR(255) NOT NULL CHECK([gender] IN ('male', 'female')), [name] VARCHAR(255) NOT NULL, [duration] VARCHAR(255) NOT NULL, "
"[url] VARCHAR(255) NOT NULL, [published_at] DATETIME NOT NULL, [thumbnail] VARCHAR(255) NULL, [premium] INT NOT NULL, "
"[author_id] INT NULL, [description] TEXT NOT NULL, [created_at] DATETIME NULL DEFAULT CURRENT_TIMESTAMP, "
"[updated_at] DATETIME NULL DEFAULT CURRENT_TIMESTAMP, "
Expand Down
76 changes: 39 additions & 37 deletions tests/mysql/schema/test_mysql_schema_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_can_add_columns1(self):
self.assertEqual(len(blueprint.table.added_columns), 2)
self.assertEqual(
blueprint.to_sql(),
"CREATE TABLE users (name VARCHAR(255) NOT NULL, age INT(11) NOT NULL)",
"CREATE TABLE `users` (`name` VARCHAR(255) NOT NULL, `age` INT(11) NOT NULL)",
)

def test_can_add_columns_with_constaint(self):
Expand All @@ -39,7 +39,7 @@ def test_can_add_columns_with_constaint(self):
self.assertEqual(len(blueprint.table.added_columns), 2)
self.assertEqual(
blueprint.to_sql(),
"CREATE TABLE users (name VARCHAR(255) NOT NULL, age INT(11) NOT NULL, CONSTRAINT users_name_unique UNIQUE (name))",
"CREATE TABLE `users` (`name` VARCHAR(255) NOT NULL, `age` INT(11) NOT NULL, CONSTRAINT users_name_unique UNIQUE (name))",
)

def test_can_add_columns_with_foreign_key_constaint(self):
Expand All @@ -52,9 +52,9 @@ def test_can_add_columns_with_foreign_key_constaint(self):
self.assertEqual(len(blueprint.table.added_columns), 3)
self.assertEqual(
blueprint.to_sql(),
"CREATE TABLE users (name VARCHAR(255) NOT NULL, "
"age INT(11) NOT NULL, "
"profile_id INT(11) NOT NULL, "
"CREATE TABLE `users` (`name` VARCHAR(255) NOT NULL, "
"`age` INT(11) NOT NULL, "
"`profile_id` INT(11) NOT NULL, "
"CONSTRAINT users_name_unique UNIQUE (name), "
"CONSTRAINT users_profile_id_foreign FOREIGN KEY (profile_id) REFERENCES profiles(id))",
)
Expand All @@ -75,14 +75,15 @@ def test_can_advanced_table_creation(self):
blueprint.timestamps()

self.assertEqual(len(blueprint.table.added_columns), 13)
print(blueprint.to_sql())
self.assertEqual(
blueprint.to_sql(),
(
"CREATE TABLE users (id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY 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 DEFAULT CURRENT_TIMESTAMP, "
"created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT users_email_unique UNIQUE (email))"
"CREATE TABLE `users` (`id` INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, "
"`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 DEFAULT CURRENT_TIMESTAMP, "
"`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT users_email_unique UNIQUE (email))"
),
)

Expand All @@ -92,38 +93,39 @@ def test_can_add_primary_constraint_without_column_name(self):
blueprint.string("name")
blueprint.string("email")
self.assertEqual(len(blueprint.table.added_columns), 3)
print(blueprint.to_sql())
self.assertTrue(
blueprint.to_sql().startswith(
"CREATE TABLE users (user_id INT(11) NOT NULL PRIMARY KEY"
"CREATE TABLE `users` (`user_id` INT(11) NOT NULL PRIMARY KEY"
)
)

# def test_can_advanced_table_creation2(self):
# with self.schema.create("users") as blueprint:
# blueprint.increments("id")
# blueprint.string("name")
# blueprint.string("duration")
# blueprint.string("url")
# blueprint.datetime("published_at")
# blueprint.string("thumbnail").nullable()
# blueprint.integer("premium")
# blueprint.integer("author_id").unsigned().nullable()
# blueprint.foreign("author_id").references("id").on("users").on_delete(
# "CASCADE"
# )
# blueprint.text("description")
# blueprint.timestamps()

# self.assertEqual(len(blueprint.table.added_columns), 11)
# self.assertEqual(
# blueprint.to_sql(),
# (
# "CREATE TABLE users (id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), "
# "duration VARCHAR(255), url VARCHAR(255), published_at DATETIME, thumbnail VARCHAR(255), "
# "premium INT(11), author_id INT UNSIGNED, description TEXT, created_at TIMESTAMP, "
# "updated_at TIMESTAMP, CONSTRAINT users_author_id_foreign FOREIGN KEY (author_id) REFERENCES users(id))"
# ),
# )
def test_can_advanced_table_creation2(self):
with self.schema.create("users") as blueprint:
blueprint.big_increments("id")
blueprint.string("name")
blueprint.string("duration")
blueprint.string("url")
blueprint.datetime("published_at")
blueprint.string("thumbnail").nullable()
blueprint.integer("premium")
blueprint.integer("author_id").unsigned().nullable()
blueprint.foreign("author_id").references("id").on("users").on_delete(
"CASCADE"
)
blueprint.text("description")
blueprint.timestamps()

self.assertEqual(len(blueprint.table.added_columns), 11)
self.assertEqual(
blueprint.to_sql(),
(
"CREATE TABLE `users` (`id` BIGINT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` VARCHAR(255) NOT NULL, "
"`duration` VARCHAR(255) NOT NULL, `url` VARCHAR(255) NOT NULL, `published_at` DATETIME NOT NULL, `thumbnail` VARCHAR(255) NULL, "
"`premium` INT(11) NOT NULL, `author_id` INT UNSIGNED NULL, `description` TEXT NOT NULL, `created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP, "
"`updated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT users_author_id_foreign FOREIGN KEY (author_id) REFERENCES users(id))"
),
)

def test_has_table(self):
schema_sql = self.schema.has_table("users")
Expand Down
Loading

0 comments on commit c70129f

Please sign in to comment.