Overview
Comment:Store version in user_version pragma now
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | dev
Files: files | file ages | folders
SHA3-256: ddc306861e933a9ddd6a5cc7a2af44d43f25abc620b8641f2f9dff86a70ead38
User & Date: bohwaz on 2021-01-28 16:08:29
Other Links: branch diff | manifest | tags
Context
2021-01-28
16:12
Remove version from config table check-in: 3a1523dad5 user: bohwaz tags: dev
16:08
Store version in user_version pragma now check-in: ddc306861e user: bohwaz tags: dev
2021-01-27
19:05
Fix page creation check-in: 79a5fe72d5 user: bohwaz tags: dev
Changes

Modified src/include/lib/Garradin/DB.php from [98b21aac12] to [8fdc67bb4a].

9
10
11
12
13
14
15


16
17
18
19
20
21
22
    /**
     * Application ID pour SQLite
     * @link https://www.sqlite.org/pragma.html#pragma_application_id
     */
    const APPID = 0x5da2d811;

    static protected $_instance = null;



    static public function getInstance($create = false, $readonly = false)
    {
        if (null === self::$_instance) {
            self::$_instance = new DB('sqlite', ['file' => DB_FILE]);
        }








>
>







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    /**
     * Application ID pour SQLite
     * @link https://www.sqlite.org/pragma.html#pragma_application_id
     */
    const APPID = 0x5da2d811;

    static protected $_instance = null;

    protected $_version = -1;

    static public function getInstance($create = false, $readonly = false)
    {
        if (null === self::$_instance) {
            self::$_instance = new DB('sqlite', ['file' => DB_FILE]);
        }

45
46
47
48
49
50
51




























































52
53
54
55
56
57
58
        // Performance enhancement
        // see https://www.cs.utexas.edu/~jaya/slides/apsys17-sqlite-slides.pdf
        // https://ericdraken.com/sqlite-performance-testing/
        $this->exec(sprintf('PRAGMA journal_mode = WAL; PRAGMA synchronous = NORMAL; PRAGMA journal_size_limit = %d;', 32 * 1024 * 1024));

        $this->db->createFunction('transliterate_to_ascii', ['Garradin\Utils', 'transliterateToAscii']);
    }





























































    public function close(): void
    {
        parent::close();
        self::$_instance = null;
    }








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
        // Performance enhancement
        // see https://www.cs.utexas.edu/~jaya/slides/apsys17-sqlite-slides.pdf
        // https://ericdraken.com/sqlite-performance-testing/
        $this->exec(sprintf('PRAGMA journal_mode = WAL; PRAGMA synchronous = NORMAL; PRAGMA journal_size_limit = %d;', 32 * 1024 * 1024));

        $this->db->createFunction('transliterate_to_ascii', ['Garradin\Utils', 'transliterateToAscii']);
    }

    public function version(): ?string
    {
        if (-1 === $this->_version) {
            $this->connect();
            $v = (int) $this->db->querySingle('PRAGMA user_version;');
            $v = self::parseVersion($v);

            if (null === $v) {
                // For legacy version before 1.1.0
                $v = $this->db->querySingle('SELECT valeur FROM config WHERE cle = \'version\';');
            }

            $this->_version = $v ?: null;
        }

        return $this->_version;
    }

    static public function parseVersion(int $v): ?string
    {
        if ($v > 0) {
            $major = intval($v / 1000000);
            $v -= $major * 1000000;
            $minor = intval($v / 10000);
            $v -= $minor * 10000;
            $release = intval($v / 100);
            $v -= $release * 100;
            $type = $v;

            if ($type == 0) {
                $type = '';
            }
            elseif ($type > 50) {
                $type = '-rc' . ($type - 50);
            }
            else {
                $type = '-beta' . $type;
            }

            $v = sprintf('%d.%d.%d%s', $major, $minor, $release, $type);
        }

        return $v ?: null;
    }

    /**
     * Save version to database
     * Only rc and beta strings are allowed, others will throw an error
     * @param string $version Version string, eg. 1.2.3-rc2
     */
    public function setVersion(string $version): void
    {
        if (!preg_match('/^\d+\.\d+\.\d+(?:-(?:beta|rc)(\d+))?$/', $version, $match)) {
            throw new \InvalidArgumentException('Invalid version number: ' . $version);
        }

        $version = ($match[1] * 100 * 100 * 100) + ($match[2] * 100 * 100) + ($match[3] * 100) + ($match[4]);
        $this->db->exec(sprintf('PRAGMA user_version = %d;', $version));
    }

    public function close(): void
    {
        parent::close();
        self::$_instance = null;
    }