@zukhruf/sqlite-drift 0.0.0-stage → 0.3.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +175 -2
- package/dist/declaration.d.ts +12 -0
- package/dist/declaration.d.ts.map +1 -0
- package/dist/declaration.js +169 -0
- package/dist/declaration.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +45 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ezzabuzaid
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,176 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @zukhruf/sqlite-drift
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A package keeps tables, indexes, views and triggers in a SQLite database. A later version of the package can declare them otherwise, for example with one more column. The database still stores the objects as the earlier version made them, and the new code fails on them, for example with `no such column`. This package compares the stored objects with their declaration, and makes the database store them as declared. No person keeps a version number. This is also known as declarative schema management, and the difference that it finds is known as schema drift.
|
|
4
|
+
|
|
5
|
+
The words in these documents have one meaning each. See the glossary in [CONTEXT.md](./CONTEXT.md).
|
|
6
|
+
|
|
7
|
+
## Use it
|
|
8
|
+
|
|
9
|
+
This project is an experiment. It uses only `node:sqlite`.
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install @zukhruf/sqlite-drift
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { mkdtempDisposable } from 'node:fs/promises';
|
|
17
|
+
import { tmpdir } from 'node:os';
|
|
18
|
+
import { join } from 'node:path';
|
|
19
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
20
|
+
|
|
21
|
+
import { Declaration } from '@zukhruf/sqlite-drift';
|
|
22
|
+
|
|
23
|
+
await using scratch = await mkdtempDisposable(join(tmpdir(), 'drift-'));
|
|
24
|
+
using database = new DatabaseSync(join(scratch.path, 'app.sqlite'));
|
|
25
|
+
// An earlier version made the table without failed_at.
|
|
26
|
+
database.exec(`CREATE TABLE failures (directory TEXT PRIMARY KEY, error TEXT NOT NULL);
|
|
27
|
+
INSERT INTO failures VALUES ('/data/notes', 'denied')`);
|
|
28
|
+
|
|
29
|
+
const failures = new Declaration(
|
|
30
|
+
[
|
|
31
|
+
'CREATE TABLE failures (directory TEXT PRIMARY KEY, error TEXT NOT NULL, failed_at TEXT NOT NULL)',
|
|
32
|
+
],
|
|
33
|
+
{ rows: 'replace' },
|
|
34
|
+
);
|
|
35
|
+
console.log(failures.drift(database));
|
|
36
|
+
failures.apply(database);
|
|
37
|
+
console.log(failures.drift(database));
|
|
38
|
+
console.log(database.prepare('SELECT count(*) AS rows FROM failures').get());
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Output:
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
stale
|
|
45
|
+
fits
|
|
46
|
+
[Object: null prototype] { rows: 0 }
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## A declaration compares the text that SQLite stores
|
|
50
|
+
|
|
51
|
+
- `new Declaration(statements, { rows })` makes the objects once in an empty database in memory, and keeps the text that SQLite stores for each one. [ADR 0001](./docs/adr/0001-compare-the-text-that-sqlite-stores.md) tells why.
|
|
52
|
+
- `rows` is `'replace'` or `'keep'`. It tells `apply` what to do with the rows of a stale table. It has no default, so no caller loses rows because it forgot an option.
|
|
53
|
+
- `drift(database)` gives `missing` when the database stores none of the objects, `fits` when it stores each one as declared, and `stale` for all other cases. It only reads, so it also works on a database opened read-only.
|
|
54
|
+
- Each change to a CREATE statement is drift: a column, a type, a CHECK, STRICT, the query of a view.
|
|
55
|
+
- A change of meaning with no change of text is not drift. When the meaning of a column changes, change its name too.
|
|
56
|
+
- SQLite takes names that are different only in ASCII case as one name.
|
|
57
|
+
|
|
58
|
+
## Apply makes the database store the declaration
|
|
59
|
+
|
|
60
|
+
- `apply(database)` does nothing when the drift is `fits`.
|
|
61
|
+
- When no stored object is stale, `apply` makes each missing object, and the other objects keep their rows.
|
|
62
|
+
- With `rows: 'replace'`, when one stored object is stale, `apply` replaces every object of the declaration, and their rows are gone. [ADR 0002](./docs/adr/0002-a-stale-object-replaces-the-whole-declaration.md) tells why.
|
|
63
|
+
- With `rows: 'keep'`, `apply` rebuilds each stale table and keeps its rows. See [Keep the rows](#keep-the-rows).
|
|
64
|
+
- `apply` does not drop an object that the declaration does not name.
|
|
65
|
+
- In a transaction of the caller, `apply` works in a savepoint, and a rollback of the caller removes its changes. With no transaction, `apply` writes in its own `BEGIN IMMEDIATE` transaction. [`@zukhruf/sqlite-transaction`](../transaction) does this work, so when SQLite ends the transaction itself, `apply` throws the original error.
|
|
66
|
+
|
|
67
|
+
## Keep the rows
|
|
68
|
+
|
|
69
|
+
With `rows: 'keep'`, `apply` keeps the rows of each stale table. It replaces a stale index, trigger or view, because these have no rows. It rebuilds each stale table, in the order of the declaration:
|
|
70
|
+
|
|
71
|
+
1. It renames the stored table to a temporary name, with `PRAGMA legacy_alter_table` on. Thus the references of other tables, views and triggers stay on the name of the table.
|
|
72
|
+
2. It makes the table from its declared text, so SQLite stores the declared text.
|
|
73
|
+
3. It copies the columns that both tables have, by name. SQLite takes names that are different only in ASCII case as one name.
|
|
74
|
+
4. It drops the old table, and makes again the indexes and triggers on the table that the declaration does not name.
|
|
75
|
+
5. It runs `PRAGMA foreign_key_check`. When a kept row breaks a foreign key, `apply` fails.
|
|
76
|
+
|
|
77
|
+
[ADR 0003](./docs/adr/0003-keep-rows-rebuilds-a-table-and-keeps-its-references.md) tells why.
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
import { mkdtempDisposable } from 'node:fs/promises';
|
|
81
|
+
import { tmpdir } from 'node:os';
|
|
82
|
+
import { join } from 'node:path';
|
|
83
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
84
|
+
|
|
85
|
+
import { Declaration } from '@zukhruf/sqlite-drift';
|
|
86
|
+
|
|
87
|
+
await using scratch = await mkdtempDisposable(join(tmpdir(), 'drift-'));
|
|
88
|
+
using database = new DatabaseSync(join(scratch.path, 'app.sqlite'));
|
|
89
|
+
// An earlier version made runs without status. Cases refer to runs.
|
|
90
|
+
database.exec(`CREATE TABLE runs (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
|
|
91
|
+
CREATE TABLE cases (run INTEGER NOT NULL REFERENCES runs (id) ON DELETE CASCADE);
|
|
92
|
+
INSERT INTO runs VALUES (1, 'nightly');
|
|
93
|
+
INSERT INTO cases VALUES (1)`);
|
|
94
|
+
|
|
95
|
+
const runs = new Declaration(
|
|
96
|
+
[
|
|
97
|
+
"CREATE TABLE runs (id INTEGER PRIMARY KEY, name TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'done')",
|
|
98
|
+
],
|
|
99
|
+
{ rows: 'keep' },
|
|
100
|
+
);
|
|
101
|
+
console.log(runs.drift(database));
|
|
102
|
+
runs.apply(database);
|
|
103
|
+
console.log(runs.drift(database));
|
|
104
|
+
console.log(database.prepare('SELECT * FROM runs').all());
|
|
105
|
+
console.log(database.prepare('SELECT count(*) AS cases FROM cases').get());
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Output:
|
|
109
|
+
|
|
110
|
+
```
|
|
111
|
+
stale
|
|
112
|
+
fits
|
|
113
|
+
[ [Object: null prototype] { id: 1, name: 'nightly', status: 'done' } ]
|
|
114
|
+
[Object: null prototype] { cases: 1 }
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
- A column that only the declaration has gets its default.
|
|
118
|
+
- A column that only the stored table has is discarded, with its values.
|
|
119
|
+
- `apply` does not find a renamed column. A new name reads as one column dropped and one column added, so the values of the old column are discarded. To keep them, copy them yourself before `apply`.
|
|
120
|
+
- `apply` does not fill values and does not remove rows. When a stored row does not fit the declaration (NOT NULL, CHECK, a foreign key, STRICT), `apply` fails with the error of SQLite, and the table and its rows stay as they were. To keep such rows, change them with your own SQL before `apply`.
|
|
121
|
+
- When foreign keys are on and a table refers to a stale table (also the stale table to itself), `apply` must turn foreign keys off. SQLite ignores `PRAGMA foreign_keys` in a transaction. Thus `apply` turns foreign keys off before its own transaction, and on again after it. In a transaction of the caller, `apply` fails before it changes the database. Call `apply` outside a transaction, or turn foreign keys off before it.
|
|
122
|
+
- When no table refers to the stale table, `apply` also works in a transaction of the caller, with foreign keys on.
|
|
123
|
+
- After a rebuild, `PRAGMA legacy_alter_table` has its old value again, also when `apply` fails.
|
|
124
|
+
|
|
125
|
+
## Write a declaration
|
|
126
|
+
|
|
127
|
+
- List a table before the tables that refer to it.
|
|
128
|
+
- A declaration must stand alone in an empty database. Do not declare a trigger or an index on a table that the declaration does not name, and do not use a function that only your connection registers.
|
|
129
|
+
- With `rows: 'replace'`, give each table its own declaration when it must keep its rows while another table changes.
|
|
130
|
+
|
|
131
|
+
## Errors
|
|
132
|
+
|
|
133
|
+
| Error | When |
|
|
134
|
+
| ----------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
|
|
135
|
+
| The error of SQLite | `new Declaration` fails when a statement does not run in an empty database. |
|
|
136
|
+
| `database is locked` | `apply` with no transaction waits for the busy timeout of the connection, then fails. |
|
|
137
|
+
| The error of a statement | `apply` fails, and the database stays as it was. |
|
|
138
|
+
| `Keeping the rows of a table that other tables reference needs foreign keys off, …` | `rows: 'keep'`, in a transaction of the caller, with foreign keys on, and another table refers to a stale table. Nothing changes. |
|
|
139
|
+
| The error of SQLite, for example `NOT NULL constraint failed: runs.name` | `rows: 'keep'`, and a stored row does not fit the declared table. The table and its rows stay as they were. |
|
|
140
|
+
| `The kept rows break a foreign key: …` | `rows: 'keep'`, and `PRAGMA foreign_key_check` finds a kept row that refers to no row. The database stays as it was. |
|
|
141
|
+
|
|
142
|
+
## The same concepts in other systems
|
|
143
|
+
|
|
144
|
+
| System | What it does |
|
|
145
|
+
| ------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
146
|
+
| Röthlisberger's SQLite migrator | It makes the declaration in memory and compares `sqlite_schema`. It changes a stale table with the 12 steps of SQLite and keeps the rows. |
|
|
147
|
+
| SQLite's 12 steps | "Making Other Kinds Of Table Schema Changes" in [the ALTER TABLE page](https://www.sqlite.org/lang_altertable.html): make a new table, copy the rows, drop the old table, rename the new one. `rows: 'keep'` renames the old table first instead. ADR 0003 tells why. |
|
|
148
|
+
| sqlite-utils `table.transform()` | It rebuilds a table with the 12 steps and keeps the rows. A caller gives a map of renamed columns. `rows: 'keep'` has no such map. |
|
|
149
|
+
| Airbyte destinations | Each sync compares the final table with the stream (`existingSchemaMatchesStreamConfig`), and a soft reset makes a stale table again. |
|
|
150
|
+
| Drizzle Kit `push`, Prisma `db push` | They compare a declaration in TypeScript or in the Prisma language with the database, and change the database. |
|
|
151
|
+
| Android Room | It keeps a version number for migrations, and a hash of the declaration that finds a number that a person forgot to raise. |
|
|
152
|
+
| Kubernetes controllers | Each loop compares the desired state with the state that exists, and changes what is different. |
|
|
153
|
+
|
|
154
|
+
## Documentation
|
|
155
|
+
|
|
156
|
+
- [0001 Compare the text that SQLite stores](./docs/adr/0001-compare-the-text-that-sqlite-stores.md)
|
|
157
|
+
- [0002 A stale object replaces the whole declaration](./docs/adr/0002-a-stale-object-replaces-the-whole-declaration.md)
|
|
158
|
+
- [0003 Keep rows rebuilds a table and keeps its references](./docs/adr/0003-keep-rows-rebuilds-a-table-and-keeps-its-references.md)
|
|
159
|
+
|
|
160
|
+
## Development
|
|
161
|
+
|
|
162
|
+
This package is part of the [zukhruf](../../../README.md) workspace. Run the commands from the workspace root.
|
|
163
|
+
|
|
164
|
+
```sh
|
|
165
|
+
npm install
|
|
166
|
+
npx nx run sqlite-drift:test # builds, then runs the tests in src/
|
|
167
|
+
npx nx run sqlite-drift:typecheck # formats, lints, then type checks
|
|
168
|
+
npx nx run sqlite-drift:build # compiles src/ to dist/
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
```
|
|
172
|
+
src/
|
|
173
|
+
index.ts the exports
|
|
174
|
+
declaration.ts Declaration and Drift
|
|
175
|
+
declaration.test.ts the tests, on real SQLite files
|
|
176
|
+
```
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
2
|
+
export type Drift = 'missing' | 'stale' | 'fits';
|
|
3
|
+
export type Rows = 'replace' | 'keep';
|
|
4
|
+
export declare class Declaration {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(statements: readonly string[], options: {
|
|
7
|
+
readonly rows: Rows;
|
|
8
|
+
});
|
|
9
|
+
drift(database: DatabaseSync): Drift;
|
|
10
|
+
apply(database: DatabaseSync): void;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=declaration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"declaration.d.ts","sourceRoot":"","sources":["../src/declaration.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAM3C,MAAM,MAAM,KAAK,GAAG,SAAS,GAAG,OAAO,GAAG,MAAM,CAAC;AAIjD,MAAM,MAAM,IAAI,GAAG,SAAS,GAAG,MAAM,CAAC;AAgBtC,qBAAa,WAAW;;gBAIV,UAAU,EAAE,SAAS,MAAM,EAAE,EAAE,OAAO,EAAE;QAAE,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAA;KAAE;IAgB3E,KAAK,CAAC,QAAQ,EAAE,YAAY,GAAG,KAAK;IAkBpC,KAAK,CAAC,QAAQ,EAAE,YAAY,GAAG,IAAI;CAuHpC"}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
3
|
+
import { transaction } from '@zukhruf/sqlite-transaction';
|
|
4
|
+
// The tables, indexes, views and triggers a caller owns in a SQLite database,
|
|
5
|
+
// by the CREATE statements that make them. SQLite keeps the text of each
|
|
6
|
+
// CREATE in sqlite_schema. A declaration makes its objects once in an empty
|
|
7
|
+
// database in memory, so the declared text and the stored text both come from
|
|
8
|
+
// SQLite, and they compare as they are.
|
|
9
|
+
export class Declaration {
|
|
10
|
+
#objects;
|
|
11
|
+
#rows;
|
|
12
|
+
constructor(statements, options) {
|
|
13
|
+
using blank = new DatabaseSync(':memory:');
|
|
14
|
+
for (const statement of statements)
|
|
15
|
+
blank.exec(statement);
|
|
16
|
+
this.#objects = blank
|
|
17
|
+
.prepare('SELECT "type", "name", "sql" FROM sqlite_schema WHERE "sql" IS NOT NULL ORDER BY rowid')
|
|
18
|
+
.all()
|
|
19
|
+
.map(({ type, name, sql }) => ({
|
|
20
|
+
type: String(type),
|
|
21
|
+
name: String(name),
|
|
22
|
+
sql: String(sql),
|
|
23
|
+
}));
|
|
24
|
+
this.#rows = options.rows;
|
|
25
|
+
}
|
|
26
|
+
drift(database) {
|
|
27
|
+
const pairs = this.#objects.map((object) => ({
|
|
28
|
+
object,
|
|
29
|
+
found: stored(database, object.name),
|
|
30
|
+
}));
|
|
31
|
+
if (pairs.every(({ found }) => found === undefined))
|
|
32
|
+
return 'missing';
|
|
33
|
+
return pairs.every(({ object, found }) => found?.sql === object.sql)
|
|
34
|
+
? 'fits'
|
|
35
|
+
: 'stale';
|
|
36
|
+
}
|
|
37
|
+
// Creates the objects the database does not store. When any stored object
|
|
38
|
+
// differs from its declaration, rows 'replace' replaces every object of the
|
|
39
|
+
// declaration, with its rows: a table can refer to another, and SQLite drops
|
|
40
|
+
// a table only after nothing refers to its rows. Rows 'keep' makes each stale
|
|
41
|
+
// object again, and moves the rows of a stale table into the new one (ADR
|
|
42
|
+
// 0003). Works inside the caller's transaction, or in a write of its own.
|
|
43
|
+
// Objects the declaration does not name stay.
|
|
44
|
+
apply(database) {
|
|
45
|
+
if (this.drift(database) === 'fits')
|
|
46
|
+
return;
|
|
47
|
+
// SQLite ignores PRAGMA foreign_keys in a transaction. With foreign keys
|
|
48
|
+
// on, the rename of a referenced table changes the references to the old
|
|
49
|
+
// table, and its drop then deletes the rows that refer to it.
|
|
50
|
+
const unchecked = this.#rows === 'keep' &&
|
|
51
|
+
Number(database.prepare('PRAGMA foreign_keys').get()?.foreign_keys) ===
|
|
52
|
+
1 &&
|
|
53
|
+
this.#stale(database).some(({ object, found }) => object.type === 'table' &&
|
|
54
|
+
found.type === 'table' &&
|
|
55
|
+
referenced(database, found.name));
|
|
56
|
+
if (unchecked) {
|
|
57
|
+
if (database.isTransaction)
|
|
58
|
+
throw new Error('Keeping the rows of a table that other tables reference needs foreign keys off, which SQLite cannot change inside a transaction. Call apply outside a transaction, or turn foreign keys off before it.');
|
|
59
|
+
database.exec('PRAGMA foreign_keys = OFF');
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
transaction(database, () => {
|
|
63
|
+
const stale = this.#stale(database);
|
|
64
|
+
if (this.#rows === 'keep')
|
|
65
|
+
this.#keep(database, stale);
|
|
66
|
+
else if (stale.length > 0)
|
|
67
|
+
for (const object of this.#objects.toReversed()) {
|
|
68
|
+
const found = stored(database, object.name);
|
|
69
|
+
if (found !== undefined)
|
|
70
|
+
drop(database, found);
|
|
71
|
+
}
|
|
72
|
+
for (const object of this.#objects)
|
|
73
|
+
if (stored(database, object.name) === undefined)
|
|
74
|
+
database.exec(object.sql);
|
|
75
|
+
}, { mode: 'immediate' });
|
|
76
|
+
}
|
|
77
|
+
finally {
|
|
78
|
+
if (unchecked)
|
|
79
|
+
database.exec('PRAGMA foreign_keys = ON');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
#stale(database) {
|
|
83
|
+
return this.#objects.flatMap((object) => {
|
|
84
|
+
const found = stored(database, object.name);
|
|
85
|
+
return found !== undefined && found.sql !== object.sql
|
|
86
|
+
? [{ object, found }]
|
|
87
|
+
: [];
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
// Drops each stale index, trigger and view, and rebuilds each stale table.
|
|
91
|
+
// SQLite keeps PRAGMA legacy_alter_table after a rollback, so the old value
|
|
92
|
+
// comes back in any case.
|
|
93
|
+
#keep(database, stale) {
|
|
94
|
+
const tables = stale.filter(({ object, found }) => object.type === 'table' && found.type === 'table');
|
|
95
|
+
for (const entry of stale)
|
|
96
|
+
if (!tables.includes(entry))
|
|
97
|
+
drop(database, entry.found);
|
|
98
|
+
if (tables.length === 0)
|
|
99
|
+
return;
|
|
100
|
+
const legacy = Number(database.prepare('PRAGMA legacy_alter_table').get()?.legacy_alter_table);
|
|
101
|
+
database.exec('PRAGMA legacy_alter_table = ON');
|
|
102
|
+
try {
|
|
103
|
+
for (const { object, found } of tables)
|
|
104
|
+
this.#rebuild(database, object, found);
|
|
105
|
+
}
|
|
106
|
+
finally {
|
|
107
|
+
database.exec(`PRAGMA legacy_alter_table = ${legacy}`);
|
|
108
|
+
}
|
|
109
|
+
const broken = database.prepare('PRAGMA foreign_key_check').get();
|
|
110
|
+
if (broken !== undefined)
|
|
111
|
+
throw new Error(`The kept rows break a foreign key: row ${String(broken.rowid)} of ${String(broken.table)} refers to a row of ${String(broken.parent)} that does not exist.`);
|
|
112
|
+
}
|
|
113
|
+
// Renames the stored table away, makes it from its declared text, and copies
|
|
114
|
+
// the columns that both have, by name. With legacy_alter_table on, the
|
|
115
|
+
// rename leaves the references of other tables, views and triggers to the
|
|
116
|
+
// name. A column only the declaration has gets its default, and a column
|
|
117
|
+
// only the stored table has is discarded. The indexes and triggers on the
|
|
118
|
+
// table go with the old table, so those the declaration does not name are
|
|
119
|
+
// made again here, and the others when apply makes the missing objects.
|
|
120
|
+
#rebuild(database, object, found) {
|
|
121
|
+
const declared = new Set(this.#objects.map(({ name }) => name.toLowerCase()));
|
|
122
|
+
const undeclared = database
|
|
123
|
+
.prepare(`SELECT "name", "sql" FROM sqlite_schema WHERE lower("tbl_name") = lower(?) AND "type" IN ('index', 'trigger') AND "sql" IS NOT NULL`)
|
|
124
|
+
.all(found.name)
|
|
125
|
+
.filter(({ name }) => !declared.has(String(name).toLowerCase()))
|
|
126
|
+
.map(({ sql }) => String(sql));
|
|
127
|
+
const old = `${found.name}_${randomUUID().replaceAll('-', '')}`;
|
|
128
|
+
database.exec(`ALTER TABLE ${quote(found.name)} RENAME TO ${quote(old)}`);
|
|
129
|
+
database.exec(object.sql);
|
|
130
|
+
const columns = new Set(names(database, object.name).map((name) => name.toLowerCase()));
|
|
131
|
+
const shared = names(database, old)
|
|
132
|
+
.filter((name) => columns.has(name.toLowerCase()))
|
|
133
|
+
.map(quote)
|
|
134
|
+
.join(', ');
|
|
135
|
+
if (shared !== '')
|
|
136
|
+
database.exec(`INSERT INTO ${quote(object.name)} (${shared}) SELECT ${shared} FROM ${quote(old)}`);
|
|
137
|
+
database.exec(`DROP TABLE ${quote(old)}`);
|
|
138
|
+
for (const sql of undeclared)
|
|
139
|
+
database.exec(sql);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
// SQLite takes names that differ only in ASCII case for one object.
|
|
143
|
+
function stored(database, name) {
|
|
144
|
+
const row = database
|
|
145
|
+
.prepare('SELECT "type", "name", "sql" FROM sqlite_schema WHERE lower("name") = lower(?)')
|
|
146
|
+
.get(name);
|
|
147
|
+
return row === undefined
|
|
148
|
+
? undefined
|
|
149
|
+
: { type: String(row.type), name: String(row.name), sql: String(row.sql) };
|
|
150
|
+
}
|
|
151
|
+
function quote(name) {
|
|
152
|
+
return `"${name.replaceAll('"', '""')}"`;
|
|
153
|
+
}
|
|
154
|
+
function drop(database, object) {
|
|
155
|
+
database.exec(`DROP ${object.type.toUpperCase()} ${quote(object.name)}`);
|
|
156
|
+
}
|
|
157
|
+
function names(database, table) {
|
|
158
|
+
return database
|
|
159
|
+
.prepare('SELECT "name" FROM pragma_table_info(?)')
|
|
160
|
+
.all(table)
|
|
161
|
+
.map(({ name }) => String(name));
|
|
162
|
+
}
|
|
163
|
+
// Whether a table, the same one too, has a foreign key to the table.
|
|
164
|
+
function referenced(database, table) {
|
|
165
|
+
return (database
|
|
166
|
+
.prepare(`SELECT 1 FROM sqlite_schema AS "s", pragma_foreign_key_list("s"."name") AS "f" WHERE "s"."type" = 'table' AND lower("f"."table") = lower(?)`)
|
|
167
|
+
.get(table) !== undefined);
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=declaration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"declaration.js","sourceRoot":"","sources":["../src/declaration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,OAAO,EAAE,WAAW,EAAE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SACL,OAAO,CACN,6IAA6I,CAC9I;SACA,GAAG,CAAC,KAAK,CAAC,KAAK,SAAS,CAC5B,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,KAAK,KAAK,EAAE,KAAK,IAAI,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAyB,MAAM,kBAAkB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zukhruf/sqlite-drift",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.31",
|
|
4
|
+
"description": "Declarative schema management for SQLite. A declaration lists the CREATE statements of the tables, indexes, views and triggers that one caller owns. The package compares them with the text that SQLite stores, gives the drift (missing, fits or stale), and makes the database store each object as declared. No person keeps a version number.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/JanuaryLabs/zukhruf",
|
|
9
|
+
"directory": "packages/sqlite/drift"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/JanuaryLabs/zukhruf/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/JanuaryLabs/zukhruf/tree/main/packages/sqlite/drift#readme",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"sqlite",
|
|
17
|
+
"node-sqlite",
|
|
18
|
+
"schema",
|
|
19
|
+
"schema-drift",
|
|
20
|
+
"declarative-schema",
|
|
21
|
+
"migration",
|
|
22
|
+
"sqlite-schema",
|
|
23
|
+
"ddl"
|
|
24
|
+
],
|
|
25
|
+
"type": "module",
|
|
26
|
+
"exports": {
|
|
27
|
+
"./package.json": "./package.json",
|
|
28
|
+
".": {
|
|
29
|
+
"@zukhruf/source": "./src/index.ts",
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"import": "./dist/index.js",
|
|
32
|
+
"default": "./dist/index.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"!dist/spec/**",
|
|
38
|
+
"!**/*.tsbuildinfo",
|
|
39
|
+
"!**/*.test.*"
|
|
40
|
+
],
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@zukhruf/sqlite-transaction": "^0.3.31"
|
|
46
|
+
}
|
|
47
|
+
}
|