Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] SQLite: Add support for strict tables #51413

Closed

Conversation

ryangjchandler
Copy link
Contributor

@ryangjchandler ryangjchandler commented May 14, 2024

This pull request adds support for strict tables in SQLite.

Schema::create('users', function (Blueprint $table) {
	$table->id();
	$table->strict(); // This makes the table "STRICT".
});

I think this is a worthy addition to the framework, but I'm not entirely sure I've gone the right way about doing it.

a) It feels weird having a method specific to this one SQLite feature on the Blueprint class.
b) Should this be something that you call inside of the Schema::create() callback?

I considered adding a specific createStrict() method, but that doesn't feel like a "Laravel-esque" thing to do?
I also thought about making Schema::create(...)->strict(), but that would be a bit of a refactor since the Schema::create() method returns void atm.

I'm not sure. Some opinions and thoughts would be much appreciated.

Copy link
Contributor

@hafezdivandari hafezdivandari left a comment

Choose a reason for hiding this comment

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

You may add public $strict = false; property to the Blueprint class instead.

But What's the point of implementing strict mode when most of the current column types are not compatible and won't work with strict mode enabled? According to docs:

The column datatype must be one of following:
* INT
* INTEGER
* REAL
* TEXT
* BLOB
* ANY
No other datatype names are allowed, though new types might be added in future releases of SQLite.

$blueprint->temporary ? 'create temporary' : 'create',
$this->wrapTable($blueprint),
implode(', ', $this->getColumns($blueprint)),
$this->addForeignKeys($this->getCommandsByName($blueprint, 'foreign')),
$this->addPrimaryKeys($this->getCommandByName($blueprint, 'primary'))
$this->addPrimaryKeys($this->getCommandByName($blueprint, 'primary')),
$this->getCommandByName($blueprint, 'strict') ? ' strict' : '',
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
$this->getCommandByName($blueprint, 'strict') ? ' strict' : '',
$blueprint->strict ? ' strict' : '',

*/
public function strict()
{
return $this->addCommand('strict');
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return $this->addCommand('strict');
$this->strict = true;

@ryangjchandler
Copy link
Contributor Author

ryangjchandler commented May 15, 2024

@hafezdivandari r.e. the column datatypes – creating a STRICT table in SQLite actually prevents you from using invalid column types.

So if you try to use DATETIME, it will throw an error because that's not a real SQLite column type. Regular SQLite would try to convert DATETIME into a valid "affinity" (as they call it) and allow you to store basically anything inside of it.

@hafezdivandari
Copy link
Contributor

hafezdivandari commented May 15, 2024

@ryangjchandler I mean most of the current types are considered invalid, even the query on your added example test will throw an exception! If we want to implement strict mode, we should use strict types only, please check:

/**
* Create the column definition for a char type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeChar(Fluent $column)
{
return 'varchar';
}
/**
* Create the column definition for a string type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeString(Fluent $column)
{
return 'varchar';
}
/**
* Create the column definition for a tiny text type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeTinyText(Fluent $column)
{
return 'text';
}
/**
* Create the column definition for a text type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeText(Fluent $column)
{
return 'text';
}
/**
* Create the column definition for a medium text type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeMediumText(Fluent $column)
{
return 'text';
}
/**
* Create the column definition for a long text type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeLongText(Fluent $column)
{
return 'text';
}
/**
* Create the column definition for an integer type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeInteger(Fluent $column)
{
return 'integer';
}
/**
* Create the column definition for a big integer type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeBigInteger(Fluent $column)
{
return 'integer';
}
/**
* Create the column definition for a medium integer type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeMediumInteger(Fluent $column)
{
return 'integer';
}
/**
* Create the column definition for a tiny integer type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeTinyInteger(Fluent $column)
{
return 'integer';
}
/**
* Create the column definition for a small integer type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeSmallInteger(Fluent $column)
{
return 'integer';
}
/**
* Create the column definition for a float type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeFloat(Fluent $column)
{
return 'float';
}
/**
* Create the column definition for a double type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeDouble(Fluent $column)
{
return 'double';
}
/**
* Create the column definition for a decimal type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeDecimal(Fluent $column)
{
return 'numeric';
}
/**
* Create the column definition for a boolean type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeBoolean(Fluent $column)
{
return 'tinyint(1)';
}
/**
* Create the column definition for an enumeration type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeEnum(Fluent $column)
{
return sprintf(
'varchar check ("%s" in (%s))',
$column->name,
$this->quoteString($column->allowed)
);
}
/**
* Create the column definition for a json type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeJson(Fluent $column)
{
return 'text';
}
/**
* Create the column definition for a jsonb type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeJsonb(Fluent $column)
{
return 'text';
}
/**
* Create the column definition for a date type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeDate(Fluent $column)
{
return 'date';
}
/**
* Create the column definition for a date-time type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeDateTime(Fluent $column)
{
return $this->typeTimestamp($column);
}
/**
* Create the column definition for a date-time (with time zone) type.
*
* Note: "SQLite does not have a storage class set aside for storing dates and/or times."
*
* @link https://www.sqlite.org/datatype3.html
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeDateTimeTz(Fluent $column)
{
return $this->typeDateTime($column);
}
/**
* Create the column definition for a time type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeTime(Fluent $column)
{
return 'time';
}
/**
* Create the column definition for a time (with time zone) type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeTimeTz(Fluent $column)
{
return $this->typeTime($column);
}
/**
* Create the column definition for a timestamp type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeTimestamp(Fluent $column)
{
if ($column->useCurrent) {
$column->default(new Expression('CURRENT_TIMESTAMP'));
}
return 'datetime';
}
/**
* Create the column definition for a timestamp (with time zone) type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeTimestampTz(Fluent $column)
{
return $this->typeTimestamp($column);
}
/**
* Create the column definition for a year type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeYear(Fluent $column)
{
return $this->typeInteger($column);
}
/**
* Create the column definition for a binary type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeBinary(Fluent $column)
{
return 'blob';
}
/**
* Create the column definition for a uuid type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeUuid(Fluent $column)
{
return 'varchar';
}
/**
* Create the column definition for an IP address type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeIpAddress(Fluent $column)
{
return 'varchar';
}
/**
* Create the column definition for a MAC address type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeMacAddress(Fluent $column)
{
return 'varchar';
}
/**
* Create the column definition for a spatial Geometry type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeGeometry(Fluent $column)
{
return 'geometry';
}
/**
* Create the column definition for a spatial Geography type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeGeography(Fluent $column)
{
return $this->typeGeometry($column);
}

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants