• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ICanBoogie / ActiveRecord / 11644481598

02 Nov 2024 05:17PM UTC coverage: 86.318% (-0.3%) from 86.616%
11644481598

push

github

olvlvl
Test add_binary, add_blob

1369 of 1586 relevant lines covered (86.32%)

24.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.7
/lib/ActiveRecord/Driver/TableRendererForPostgreSQL.php
1
<?php
2

3
namespace ICanBoogie\ActiveRecord\Driver;
4

5
use ICanBoogie\ActiveRecord\Schema;
6
use ICanBoogie\ActiveRecord\Schema\BelongsTo;
7
use ICanBoogie\ActiveRecord\Schema\Binary;
8
use ICanBoogie\ActiveRecord\Schema\Blob;
9
use ICanBoogie\ActiveRecord\Schema\Boolean;
10
use ICanBoogie\ActiveRecord\Schema\Column;
11
use ICanBoogie\ActiveRecord\Schema\Date;
12
use ICanBoogie\ActiveRecord\Schema\DateTime;
13
use ICanBoogie\ActiveRecord\Schema\Integer;
14
use ICanBoogie\ActiveRecord\Schema\Serial;
15
use ICanBoogie\ActiveRecord\Schema\Text;
16
use ICanBoogie\ActiveRecord\Schema\Time;
17
use InvalidArgumentException;
18

19
use function in_array;
20

21

22
/**
23
 * @see https://www.sqlite.org/lang_createtable.html
24
 */
25
final class TableRendererForPostgreSQL extends TableRenderer
26
{
27
    use RenderCreateIndexForMySQL;
28
    use RenderTableConstraintsForMySQL;
29

30
    protected function render_column_defs(Schema $schema): array
31
    {
32
        $render = [];
5✔
33

34
        foreach ($schema->columns as $name => $column) {
5✔
35
            $type = $this->render_type_name($column);
5✔
36
            $constraint = $this->render_column_constraint($column);
5✔
37

38
            $render[] = "$name $type $constraint";
5✔
39
        }
40

41
        return $render;
5✔
42
    }
43

44
    protected function render_type_name(Column $column): string
45
    {
46
        return match ($column::class) {
5✔
47
            Serial::class => match ($column->size) {
2✔
48
                Integer::SIZE_SMALL => "SMALLSERIAL",
×
49
                Integer::SIZE_REGULAR => "SERIAL",
2✔
50
                Integer::SIZE_BIG => "BIGSERIAL",
×
51
                default => throw new InvalidArgumentException(
2✔
52
                    "Serial size {$column->size} is not supported by PostgreSQL"
2✔
53
                )
2✔
54
            },
2✔
55
            BelongsTo::class, Integer::class => match ($column->size) {
2✔
56
                Integer::SIZE_SMALL => "SMALLINT",
×
57
                Integer::SIZE_REGULAR => "INTEGER",
2✔
58
                Integer::SIZE_BIG => "BIGINT",
1✔
59
                default => throw new InvalidArgumentException(
2✔
60
                    "Integer size {$column->size} is not supported by PostgreSQL"
2✔
61
                )
2✔
62
            },
2✔
63

64
            // https://www.postgresql.org/docs/current/datatype-bit.html
65
            Binary::class => $column->fixed
1✔
66
                ? "BIT($column->size)"
1✔
67
                : "BIT VARYING($column->size)",
1✔
68

69
            // https://www.postgresql.org/docs/current/datatype-character.html
70
            Text::class => "TEXT",
1✔
71
            // https://www.postgresql.org/docs/current/datatype-binary.html
72
            Blob::class => "BYTEA",
1✔
73

74
            // https://www.postgresql.org/docs/current/datatype-datetime.html
75
            DateTime::class => "TIMESTAMP",
1✔
76

77
            default => parent::render_type_name($column)
5✔
78
        };
5✔
79
    }
80

81
    private function render_column_constraint(Column $column): string
82
    {
83
        $constraint = '';
5✔
84

85
        if ($column instanceof Integer && !$column instanceof Boolean && !$column instanceof Serial) {
5✔
86
            $constraint .= $column->unsigned ? " UNSIGNED" : '';
2✔
87
        }
88

89
        $constraint .= $column->null ? " NULL" : " NOT NULL";
5✔
90
        $constraint .= $column->default !== null ? " DEFAULT " . $this->format_default($column->default) : '';
5✔
91
        $constraint .= $column->unique ? " UNIQUE" : '';
5✔
92
        $constraint .= $column->collate ? " COLLATE $column->collate" : '';
5✔
93

94
        // foreign-key-clause goes here
95

96
        return ltrim($constraint);
5✔
97
    }
98

99
    private function format_default(string $default): string
100
    {
101
        if (in_array($default, [ DateTime::CURRENT_TIMESTAMP, Date::CURRENT_DATE, Time::CURRENT_TIME ])) {
1✔
102
            return "($default)";
1✔
103
        }
104

105
        return $default;
×
106
    }
107

108
    protected function render_table_options(): array
109
    {
110
        return [];
5✔
111
    }
112
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc