refactored SchemaBuilder

- rename class to ColumnSchemaBuilder as this is more appropriate
- changed internal organisation to match how the rest of schema related classes work
  - the ColumnSchemaBuilder is now created the same way as QueryBuilder is
- removed static call magic and method annotations, now real methods are called as they are
- the whole code works on objects in a db context now instead of setting database connection in global state
- trait is now used by Migration by default but can be used in other contexts as well

Migration usage is now as follows:

```php
$this->createTable('example_table', [
  'id' => $this->primaryKey(),
  'name' => $this->string(64)->notNull(),
  'type' => $this->integer()->notNull()->defaultValue(10),
  'description' => $this->text(),
  'rule_name' => $this->string(64),
  'data' => $this->text(),
  'created_at' => $this->datetime()->notNull(),
  'updated_at' => $this->datetime(),
]);
```
This commit is contained in:
Carsten Brandt
2015-08-03 23:02:56 +02:00
parent 1948754d00
commit 681db52ba6
21 changed files with 789 additions and 886 deletions

View File

@@ -16,62 +16,63 @@ class PostgreSQLQueryBuilderTest extends QueryBuilderTest
public function columnTypes()
{
return [
[Schema::TYPE_PK, Schema::primaryKey(), 'serial NOT NULL PRIMARY KEY'],
[Schema::TYPE_PK . '(8)', Schema::primaryKey(8), 'serial NOT NULL PRIMARY KEY'],
[Schema::TYPE_PK . ' CHECK (value > 5)', Schema::primaryKey()->check('value > 5'), 'serial NOT NULL PRIMARY KEY CHECK (value > 5)'],
[Schema::TYPE_PK . '(8) CHECK (value > 5)', Schema::primaryKey(8)->check('value > 5'), 'serial NOT NULL PRIMARY KEY CHECK (value > 5)'],
[Schema::TYPE_STRING, Schema::string(), 'varchar(255)'],
[Schema::TYPE_STRING . '(32)', Schema::string(32), 'varchar(32)'],
[Schema::TYPE_STRING . ' CHECK (value LIKE \'test%\')', Schema::string()->check('value LIKE \'test%\''), 'varchar(255) CHECK (value LIKE \'test%\')'],
[Schema::TYPE_STRING . '(32) CHECK (value LIKE \'test%\')', Schema::string(32)->check('value LIKE \'test%\''), 'varchar(32) CHECK (value LIKE \'test%\')'],
[Schema::TYPE_STRING . ' NOT NULL', Schema::string()->notNull(), 'varchar(255) NOT NULL'],
[Schema::TYPE_TEXT, Schema::text(), 'text'],
[Schema::TYPE_TEXT . '(255)', Schema::text(255), 'text'],
[Schema::TYPE_TEXT . ' CHECK (value LIKE \'test%\')', Schema::text()->check('value LIKE \'test%\''), 'text CHECK (value LIKE \'test%\')'],
[Schema::TYPE_TEXT . '(255) CHECK (value LIKE \'test%\')', Schema::text(255)->check('value LIKE \'test%\''), 'text CHECK (value LIKE \'test%\')'],
[Schema::TYPE_TEXT . ' NOT NULL', Schema::text()->notNull(), 'text NOT NULL'],
[Schema::TYPE_TEXT . '(255) NOT NULL', Schema::text(255)->notNull(), 'text NOT NULL'],
[Schema::TYPE_SMALLINT, Schema::smallInteger(), 'smallint'],
[Schema::TYPE_SMALLINT . '(8)', Schema::smallInteger(8), 'smallint'],
[Schema::TYPE_INTEGER, Schema::integer(), 'integer'],
[Schema::TYPE_INTEGER . '(8)', Schema::integer(8), 'integer'],
[Schema::TYPE_INTEGER . ' CHECK (value > 5)', Schema::integer()->check('value > 5'), 'integer CHECK (value > 5)'],
[Schema::TYPE_INTEGER . '(8) CHECK (value > 5)', Schema::integer(8)->check('value > 5'), 'integer CHECK (value > 5)'],
[Schema::TYPE_INTEGER . ' NOT NULL', Schema::integer()->notNull(), 'integer NOT NULL'],
[Schema::TYPE_BIGINT, Schema::bigInteger(), 'bigint'],
[Schema::TYPE_BIGINT . '(8)', Schema::bigInteger(8), 'bigint'],
[Schema::TYPE_BIGINT . ' CHECK (value > 5)', Schema::bigInteger()->check('value > 5'), 'bigint CHECK (value > 5)'],
[Schema::TYPE_BIGINT . '(8) CHECK (value > 5)', Schema::bigInteger(8)->check('value > 5'), 'bigint CHECK (value > 5)'],
[Schema::TYPE_BIGINT . ' NOT NULL', Schema::bigInteger()->notNull(), 'bigint NOT NULL'],
[Schema::TYPE_FLOAT, Schema::float(), 'double precision'],
[Schema::TYPE_FLOAT . ' CHECK (value > 5.6)', Schema::float()->check('value > 5.6'), 'double precision CHECK (value > 5.6)'],
[Schema::TYPE_FLOAT . '(16,5) CHECK (value > 5.6)', Schema::float(16, 5)->check('value > 5.6'), 'double precision CHECK (value > 5.6)'],
[Schema::TYPE_FLOAT . ' NOT NULL', Schema::float()->notNull(), 'double precision NOT NULL'],
[Schema::TYPE_DECIMAL, Schema::decimal(), 'numeric(10,0)'],
[Schema::TYPE_DECIMAL . '(12,4)', Schema::decimal(12, 4), 'numeric(12,4)'],
[Schema::TYPE_DECIMAL . ' CHECK (value > 5.6)', Schema::decimal()->check('value > 5.6'), 'numeric(10,0) CHECK (value > 5.6)'],
[Schema::TYPE_DECIMAL . '(12,4) CHECK (value > 5.6)', Schema::decimal(12, 4)->check('value > 5.6'), 'numeric(12,4) CHECK (value > 5.6)'],
[Schema::TYPE_DECIMAL . ' NOT NULL', Schema::decimal()->notNull(), 'numeric(10,0) NOT NULL'], [Schema::TYPE_DATETIME, Schema::dateTime(), 'timestamp(0)'],
[Schema::TYPE_DATETIME . " CHECK (value BETWEEN '2011-01-01' AND '2013-01-01')", Schema::dateTime()->check("value BETWEEN '2011-01-01' AND '2013-01-01'"), "timestamp(0) CHECK (value BETWEEN '2011-01-01' AND '2013-01-01')"],
[Schema::TYPE_DATETIME . ' NOT NULL', Schema::dateTime()->notNull(), 'timestamp(0) NOT NULL'],
[Schema::TYPE_TIMESTAMP, Schema::timestamp(), 'timestamp(0)'],
[Schema::TYPE_TIMESTAMP . " CHECK (value BETWEEN '2011-01-01' AND '2013-01-01')", Schema::timestamp()->check("value BETWEEN '2011-01-01' AND '2013-01-01'"), "timestamp(0) CHECK (value BETWEEN '2011-01-01' AND '2013-01-01')"],
[Schema::TYPE_TIMESTAMP . ' NOT NULL', Schema::timestamp()->notNull(), 'timestamp(0) NOT NULL'],
[Schema::TYPE_TIMESTAMP . '(4)', Schema::timestamp(4), 'timestamp(4)'],
[Schema::TYPE_TIME, Schema::time(), 'time(0)'],
[Schema::TYPE_TIME . " CHECK (value BETWEEN '12:00:00' AND '13:01:01')", Schema::time()->check("value BETWEEN '12:00:00' AND '13:01:01'"), "time(0) CHECK (value BETWEEN '12:00:00' AND '13:01:01')"],
[Schema::TYPE_TIME . ' NOT NULL', Schema::time()->notNull(), 'time(0) NOT NULL'],
[Schema::TYPE_DATE, Schema::date(), 'date'],
[Schema::TYPE_DATE . " CHECK (value BETWEEN '2011-01-01' AND '2013-01-01')", Schema::date()->check("value BETWEEN '2011-01-01' AND '2013-01-01'"), "date CHECK (value BETWEEN '2011-01-01' AND '2013-01-01')"],
[Schema::TYPE_DATE . ' NOT NULL', Schema::date()->notNull(), 'date NOT NULL'],
[Schema::TYPE_BINARY, Schema::binary(), 'bytea'],
[Schema::TYPE_BOOLEAN, Schema::boolean(), 'boolean'],
[Schema::TYPE_BOOLEAN . ' NOT NULL DEFAULT TRUE', Schema::boolean()->notNull()->defaultValue(true), 'boolean NOT NULL DEFAULT TRUE'],
[Schema::TYPE_MONEY, Schema::money(), 'numeric(19,4)'],
[Schema::TYPE_MONEY . '(16,2)', Schema::money(16, 2), 'numeric(16,2)'],
[Schema::TYPE_MONEY . ' CHECK (value > 0.0)', Schema::money()->check('value > 0.0'), 'numeric(19,4) CHECK (value > 0.0)'],
[Schema::TYPE_MONEY . '(16,2) CHECK (value > 0.0)', Schema::money(16, 2)->check('value > 0.0'), 'numeric(16,2) CHECK (value > 0.0)'],
[Schema::TYPE_MONEY . ' NOT NULL', Schema::money()->notNull(), 'numeric(19,4) NOT NULL'],
[Schema::TYPE_PK, $this->primaryKey(), 'serial NOT NULL PRIMARY KEY'],
[Schema::TYPE_PK . '(8)', $this->primaryKey(8), 'serial NOT NULL PRIMARY KEY'],
[Schema::TYPE_PK . ' CHECK (value > 5)', $this->primaryKey()->check('value > 5'), 'serial NOT NULL PRIMARY KEY CHECK (value > 5)'],
[Schema::TYPE_PK . '(8) CHECK (value > 5)', $this->primaryKey(8)->check('value > 5'), 'serial NOT NULL PRIMARY KEY CHECK (value > 5)'],
[Schema::TYPE_STRING, $this->string(), 'varchar(255)'],
[Schema::TYPE_STRING . '(32)', $this->string(32), 'varchar(32)'],
[Schema::TYPE_STRING . ' CHECK (value LIKE \'test%\')', $this->string()->check('value LIKE \'test%\''), 'varchar(255) CHECK (value LIKE \'test%\')'],
[Schema::TYPE_STRING . '(32) CHECK (value LIKE \'test%\')', $this->string(32)->check('value LIKE \'test%\''), 'varchar(32) CHECK (value LIKE \'test%\')'],
[Schema::TYPE_STRING . ' NOT NULL', $this->string()->notNull(), 'varchar(255) NOT NULL'],
[Schema::TYPE_TEXT, $this->text(), 'text'],
[Schema::TYPE_TEXT . '(255)', $this->text(255), 'text'],
[Schema::TYPE_TEXT . ' CHECK (value LIKE \'test%\')', $this->text()->check('value LIKE \'test%\''), 'text CHECK (value LIKE \'test%\')'],
[Schema::TYPE_TEXT . '(255) CHECK (value LIKE \'test%\')', $this->text(255)->check('value LIKE \'test%\''), 'text CHECK (value LIKE \'test%\')'],
[Schema::TYPE_TEXT . ' NOT NULL', $this->text()->notNull(), 'text NOT NULL'],
[Schema::TYPE_TEXT . '(255) NOT NULL', $this->text(255)->notNull(), 'text NOT NULL'],
[Schema::TYPE_SMALLINT, $this->smallInteger(), 'smallint'],
[Schema::TYPE_SMALLINT . '(8)', $this->smallInteger(8), 'smallint'],
[Schema::TYPE_INTEGER, $this->integer(), 'integer'],
[Schema::TYPE_INTEGER . '(8)', $this->integer(8), 'integer'],
[Schema::TYPE_INTEGER . ' CHECK (value > 5)', $this->integer()->check('value > 5'), 'integer CHECK (value > 5)'],
[Schema::TYPE_INTEGER . '(8) CHECK (value > 5)', $this->integer(8)->check('value > 5'), 'integer CHECK (value > 5)'],
[Schema::TYPE_INTEGER . ' NOT NULL', $this->integer()->notNull(), 'integer NOT NULL'],
[Schema::TYPE_BIGINT, $this->bigInteger(), 'bigint'],
[Schema::TYPE_BIGINT . '(8)', $this->bigInteger(8), 'bigint'],
[Schema::TYPE_BIGINT . ' CHECK (value > 5)', $this->bigInteger()->check('value > 5'), 'bigint CHECK (value > 5)'],
[Schema::TYPE_BIGINT . '(8) CHECK (value > 5)', $this->bigInteger(8)->check('value > 5'), 'bigint CHECK (value > 5)'],
[Schema::TYPE_BIGINT . ' NOT NULL', $this->bigInteger()->notNull(), 'bigint NOT NULL'],
[Schema::TYPE_FLOAT, $this->float(), 'double precision'],
[Schema::TYPE_FLOAT . ' CHECK (value > 5.6)', $this->float()->check('value > 5.6'), 'double precision CHECK (value > 5.6)'],
[Schema::TYPE_FLOAT . '(16,5) CHECK (value > 5.6)', $this->float(16, 5)->check('value > 5.6'), 'double precision CHECK (value > 5.6)'],
[Schema::TYPE_FLOAT . ' NOT NULL', $this->float()->notNull(), 'double precision NOT NULL'],
[Schema::TYPE_DECIMAL, $this->decimal(), 'numeric(10,0)'],
[Schema::TYPE_DECIMAL . '(12,4)', $this->decimal(12, 4), 'numeric(12,4)'],
[Schema::TYPE_DECIMAL . ' CHECK (value > 5.6)', $this->decimal()->check('value > 5.6'), 'numeric(10,0) CHECK (value > 5.6)'],
[Schema::TYPE_DECIMAL . '(12,4) CHECK (value > 5.6)', $this->decimal(12, 4)->check('value > 5.6'), 'numeric(12,4) CHECK (value > 5.6)'],
[Schema::TYPE_DECIMAL . ' NOT NULL', $this->decimal()->notNull(), 'numeric(10,0) NOT NULL'],
[Schema::TYPE_DATETIME, $this->dateTime(), 'timestamp(0)'],
[Schema::TYPE_DATETIME . " CHECK (value BETWEEN '2011-01-01' AND '2013-01-01')", $this->dateTime()->check("value BETWEEN '2011-01-01' AND '2013-01-01'"), "timestamp(0) CHECK (value BETWEEN '2011-01-01' AND '2013-01-01')"],
[Schema::TYPE_DATETIME . ' NOT NULL', $this->dateTime()->notNull(), 'timestamp(0) NOT NULL'],
[Schema::TYPE_TIMESTAMP, $this->timestamp(), 'timestamp(0)'],
[Schema::TYPE_TIMESTAMP . " CHECK (value BETWEEN '2011-01-01' AND '2013-01-01')", $this->timestamp()->check("value BETWEEN '2011-01-01' AND '2013-01-01'"), "timestamp(0) CHECK (value BETWEEN '2011-01-01' AND '2013-01-01')"],
[Schema::TYPE_TIMESTAMP . ' NOT NULL', $this->timestamp()->notNull(), 'timestamp(0) NOT NULL'],
[Schema::TYPE_TIMESTAMP . '(4)', $this->timestamp(4), 'timestamp(4)'],
[Schema::TYPE_TIME, $this->time(), 'time(0)'],
[Schema::TYPE_TIME . " CHECK (value BETWEEN '12:00:00' AND '13:01:01')", $this->time()->check("value BETWEEN '12:00:00' AND '13:01:01'"), "time(0) CHECK (value BETWEEN '12:00:00' AND '13:01:01')"],
[Schema::TYPE_TIME . ' NOT NULL', $this->time()->notNull(), 'time(0) NOT NULL'],
[Schema::TYPE_DATE, $this->date(), 'date'],
[Schema::TYPE_DATE . " CHECK (value BETWEEN '2011-01-01' AND '2013-01-01')", $this->date()->check("value BETWEEN '2011-01-01' AND '2013-01-01'"), "date CHECK (value BETWEEN '2011-01-01' AND '2013-01-01')"],
[Schema::TYPE_DATE . ' NOT NULL', $this->date()->notNull(), 'date NOT NULL'],
[Schema::TYPE_BINARY, $this->binary(), 'bytea'],
[Schema::TYPE_BOOLEAN, $this->boolean(), 'boolean'],
[Schema::TYPE_BOOLEAN . ' NOT NULL DEFAULT TRUE', $this->boolean()->notNull()->defaultValue(true), 'boolean NOT NULL DEFAULT TRUE'],
[Schema::TYPE_MONEY, $this->money(), 'numeric(19,4)'],
[Schema::TYPE_MONEY . '(16,2)', $this->money(16, 2), 'numeric(16,2)'],
[Schema::TYPE_MONEY . ' CHECK (value > 0.0)', $this->money()->check('value > 0.0'), 'numeric(19,4) CHECK (value > 0.0)'],
[Schema::TYPE_MONEY . '(16,2) CHECK (value > 0.0)', $this->money(16, 2)->check('value > 0.0'), 'numeric(16,2) CHECK (value > 0.0)'],
[Schema::TYPE_MONEY . ' NOT NULL', $this->money()->notNull(), 'numeric(19,4) NOT NULL'],
];
}