-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
update.go
1077 lines (945 loc) · 31.9 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package node
import (
"context"
"database/sql"
"encoding/hex"
"fmt"
"os"
"strconv"
"strings"
"github.com/lxc/incus/v6/internal/server/db/query"
"github.com/lxc/incus/v6/internal/server/db/schema"
internalUtil "github.com/lxc/incus/v6/internal/util"
"github.com/lxc/incus/v6/shared/logger"
"github.com/lxc/incus/v6/shared/units"
"github.com/lxc/incus/v6/shared/util"
)
// Schema for the local database.
func Schema() *schema.Schema {
schema := schema.NewFromMap(updates)
schema.Fresh(freshSchema)
return schema
}
// FreshSchema returns the fresh schema definition of the local database.
func FreshSchema() string {
return freshSchema
}
// SchemaDotGo refreshes the schema.go file in this package, using the updates
// defined here.
func SchemaDotGo() error {
return schema.DotGo(updates, "schema")
}
/* Database updates are one-time actions that are needed to move an
existing database from one version of the schema to the next.
Those updates are applied at startup time before anything else
is initialized. This means that they should be entirely
self-contained and not touch anything but the database.
Calling API functions isn't allowed as such functions may themselves
depend on a newer DB schema and so would fail when upgrading a very old
version.
DO NOT USE this mechanism for one-time actions which do not involve
changes to the database schema. Use patches instead (see patches.go).
REMEMBER to run "make update-schema" after you add a new update function to
this slice. That will refresh the schema declaration in db/schema.go and
include the effect of applying your patch as well.
Only append to the updates list, never remove entries and never re-order them.
*/
var updates = map[int]schema.Update{
1: updateFromV0,
2: updateFromV1,
3: updateFromV2,
4: updateFromV3,
5: updateFromV4,
6: updateFromV5,
7: updateFromV6,
8: updateFromV7,
9: updateFromV8,
10: updateFromV9,
11: updateFromV10,
12: updateFromV11,
13: updateFromV12,
14: updateFromV13,
15: updateFromV14,
16: updateFromV15,
17: updateFromV16,
18: updateFromV17,
19: updateFromV18,
20: updateFromV19,
21: updateFromV20,
22: updateFromV21,
23: updateFromV22,
24: updateFromV23,
25: updateFromV24,
26: updateFromV25,
27: updateFromV26,
28: updateFromV27,
29: updateFromV28,
30: updateFromV29,
31: updateFromV30,
32: updateFromV31,
33: updateFromV32,
34: updateFromV33,
35: updateFromV34,
36: updateFromV35,
37: updateFromV36,
38: updateFromV37,
39: updateFromV38,
40: updateFromV39,
41: updateFromV40,
42: updateFromV41,
43: updateFromV42,
}
// UpdateFromPreClustering is the last schema version where clustering support
// was not available, and hence no cluster dqlite database is used.
const UpdateFromPreClustering = 36
// Schema updates begin here
// updateFromV42 ensures key and value fields in config table are TEXT NOT NULL.
func updateFromV42(ctx context.Context, tx *sql.Tx) error {
stmt := `
CREATE TABLE "config_new" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
UNIQUE (key)
);
INSERT INTO "config_new" SELECT * FROM "config";
DROP TABLE "config";
ALTER TABLE "config_new" RENAME TO "config";
`
_, err := tx.Exec(stmt)
return err
}
func updateFromV41(ctx context.Context, tx *sql.Tx) error {
stmt := `
ALTER TABLE raft_nodes ADD COLUMN name TEXT NOT NULL default "";
`
_, err := tx.Exec(stmt)
return err
}
func updateFromV40(ctx context.Context, tx *sql.Tx) error {
stmt := `
CREATE TABLE certificates (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
fingerprint TEXT NOT NULL,
type INTEGER NOT NULL,
name TEXT NOT NULL,
certificate TEXT NOT NULL,
UNIQUE (fingerprint)
);
`
_, err := tx.Exec(stmt)
return err
}
// Fix the address of the bootstrap node being set to "0" in the raft_nodes
// table.
func updateFromV39(ctx context.Context, tx *sql.Tx) error {
type node struct {
ID uint64
Address string
}
sql := "SELECT id, address FROM raft_nodes"
nodes := []node{}
err := query.Scan(ctx, tx, sql, func(scan func(dest ...any) error) error {
n := node{}
err := scan(&n.ID, &n.Address)
if err != nil {
return err
}
nodes = append(nodes, n)
return nil
})
if err != nil {
return fmt.Errorf("Failed to fetch raft nodes: %w", err)
}
if len(nodes) != 1 {
return nil
}
info := nodes[0]
if info.ID != 1 || info.Address != "0" {
return nil
}
config, err := query.SelectConfig(ctx, tx, "config", "")
if err != nil {
return err
}
address := config["cluster.https_address"]
if address != "" {
_, err := tx.Exec("UPDATE raft_nodes SET address=? WHERE id=1", address)
if err != nil {
return err
}
}
return nil
}
// Add role column to raft_nodes table. All existing entries will have role "0"
// which means voter.
func updateFromV38(ctx context.Context, tx *sql.Tx) error {
stmt := `
ALTER TABLE raft_nodes ADD COLUMN role INTEGER NOT NULL DEFAULT 0;
`
_, err := tx.Exec(stmt)
return err
}
// Copy core.https_address to cluster.https_address in case this node is
// clustered.
func updateFromV37(ctx context.Context, tx *sql.Tx) error {
count, err := query.Count(ctx, tx, "raft_nodes", "")
if err != nil {
return fmt.Errorf("Fetch count of Raft nodes: %w", err)
}
if count == 0 {
// This node is not clustered, nothing to do.
return nil
}
// Copy the core.https_address config.
_, err = tx.Exec(`
INSERT INTO config (key, value)
SELECT 'cluster.https_address', value FROM config WHERE key = 'core.https_address'
`)
if err != nil {
return fmt.Errorf("Insert cluster.https_address config: %w", err)
}
return nil
}
// Add a raft_nodes table to be used when running in clustered mode. It lists
// the current nodes in the cluster that are participating in the dqlite
// database Raft cluster.
//
// The 'id' column contains the raft server ID of the database node, and the
// 'address' column its network address. Both are used internally by the raft
// Go package to manage the cluster.
//
// Typical setups will have 3 cluster members that participate to the dqlite
// database Raft cluster, and an arbitrary number of additional cluster
// members that don't. Non-database nodes are not tracked in this table, but rather
// in the nodes table of the cluster database itself.
//
// The data in this table must be replicated on all nodes of the
// cluster, regardless of whether they are part of the raft cluster or not, and
// all nodes will consult this table when they need to find out a leader to
// send SQL queries to.
func updateFromV36(ctx context.Context, tx *sql.Tx) error {
stmts := `
CREATE TABLE raft_nodes (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
address TEXT NOT NULL,
UNIQUE (address)
);
DELETE FROM config WHERE NOT key='core.https_address';
DROP TABLE certificates;
DROP TABLE containers_devices_config;
DROP TABLE containers_devices;
DROP TABLE containers_config;
DROP TABLE containers_profiles;
DROP TABLE containers;
DROP TABLE images_aliases;
DROP TABLE images_properties;
DROP TABLE images_source;
DROP TABLE images;
DROP TABLE networks_config;
DROP TABLE networks;
DROP TABLE profiles_devices_config;
DROP TABLE profiles_devices;
DROP TABLE profiles_config;
DROP TABLE profiles;
DROP TABLE storage_volumes_config;
DROP TABLE storage_volumes;
DROP TABLE storage_pools_config;
DROP TABLE storage_pools;
`
_, err := tx.Exec(stmts)
return err
}
func updateFromV35(ctx context.Context, tx *sql.Tx) error {
stmts := `
CREATE TABLE tmp (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name VARCHAR(255) NOT NULL,
image_id INTEGER NOT NULL,
description TEXT,
FOREIGN KEY (image_id) REFERENCES images (id) ON DELETE CASCADE,
UNIQUE (name)
);
INSERT INTO tmp (id, name, image_id, description)
SELECT id, name, image_id, description
FROM images_aliases;
DROP TABLE images_aliases;
ALTER TABLE tmp RENAME TO images_aliases;
ALTER TABLE networks ADD COLUMN description TEXT;
ALTER TABLE storage_pools ADD COLUMN description TEXT;
ALTER TABLE storage_volumes ADD COLUMN description TEXT;
ALTER TABLE containers ADD COLUMN description TEXT;
`
_, err := tx.Exec(stmts)
return err
}
func updateFromV34(ctx context.Context, tx *sql.Tx) error {
stmt := `
CREATE TABLE IF NOT EXISTS storage_pools (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name VARCHAR(255) NOT NULL,
driver VARCHAR(255) NOT NULL,
UNIQUE (name)
);
CREATE TABLE IF NOT EXISTS storage_pools_config (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
storage_pool_id INTEGER NOT NULL,
key VARCHAR(255) NOT NULL,
value TEXT,
UNIQUE (storage_pool_id, key),
FOREIGN KEY (storage_pool_id) REFERENCES storage_pools (id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS storage_volumes (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name VARCHAR(255) NOT NULL,
storage_pool_id INTEGER NOT NULL,
type INTEGER NOT NULL,
UNIQUE (storage_pool_id, name, type),
FOREIGN KEY (storage_pool_id) REFERENCES storage_pools (id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS storage_volumes_config (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
storage_volume_id INTEGER NOT NULL,
key VARCHAR(255) NOT NULL,
value TEXT,
UNIQUE (storage_volume_id, key),
FOREIGN KEY (storage_volume_id) REFERENCES storage_volumes (id) ON DELETE CASCADE
);`
_, err := tx.Exec(stmt)
return err
}
func updateFromV33(ctx context.Context, tx *sql.Tx) error {
stmt := `
CREATE TABLE IF NOT EXISTS networks (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name VARCHAR(255) NOT NULL,
UNIQUE (name)
);
CREATE TABLE IF NOT EXISTS networks_config (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
network_id INTEGER NOT NULL,
key VARCHAR(255) NOT NULL,
value TEXT,
UNIQUE (network_id, key),
FOREIGN KEY (network_id) REFERENCES networks (id) ON DELETE CASCADE
);`
_, err := tx.Exec(stmt)
return err
}
func updateFromV32(ctx context.Context, tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE containers ADD COLUMN last_use_date DATETIME;")
return err
}
func updateFromV31(ctx context.Context, tx *sql.Tx) error {
stmt := `
CREATE TABLE IF NOT EXISTS patches (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name VARCHAR(255) NOT NULL,
applied_at DATETIME NOT NULL,
UNIQUE (name)
);`
_, err := tx.Exec(stmt)
return err
}
func updateFromV30(ctx context.Context, tx *sql.Tx) error {
// NOTE: this database update contained daemon-level logic which
// was been moved to patchUpdateFromV15 in patches.go.
return nil
}
func updateFromV29(ctx context.Context, tx *sql.Tx) error {
if util.PathExists(internalUtil.VarPath("zfs.img")) {
err := os.Chmod(internalUtil.VarPath("zfs.img"), 0600)
if err != nil {
return err
}
}
return nil
}
func updateFromV28(ctx context.Context, tx *sql.Tx) error {
stmt := `
INSERT INTO profiles_devices (profile_id, name, type) SELECT id, "aadisable", 2 FROM profiles WHERE name="docker";
INSERT INTO profiles_devices_config (profile_device_id, key, value) SELECT profiles_devices.id, "source", "/dev/null" FROM profiles_devices LEFT JOIN profiles WHERE profiles_devices.profile_id = profiles.id AND profiles.name = "docker" AND profiles_devices.name = "aadisable";
INSERT INTO profiles_devices_config (profile_device_id, key, value) SELECT profiles_devices.id, "path", "/sys/module/apparmor/parameters/enabled" FROM profiles_devices LEFT JOIN profiles WHERE profiles_devices.profile_id = profiles.id AND profiles.name = "docker" AND profiles_devices.name = "aadisable";`
_, _ = tx.Exec(stmt)
return nil
}
func updateFromV27(ctx context.Context, tx *sql.Tx) error {
_, err := tx.Exec("UPDATE profiles_devices SET type=3 WHERE type='unix-char';")
return err
}
func updateFromV26(ctx context.Context, tx *sql.Tx) error {
stmt := `
ALTER TABLE images ADD COLUMN auto_update INTEGER NOT NULL DEFAULT 0;
CREATE TABLE IF NOT EXISTS images_source (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
image_id INTEGER NOT NULL,
server TEXT NOT NULL,
protocol INTEGER NOT NULL,
certificate TEXT NOT NULL,
alias VARCHAR(255) NOT NULL,
FOREIGN KEY (image_id) REFERENCES images (id) ON DELETE CASCADE
);`
_, err := tx.Exec(stmt)
return err
}
func updateFromV25(ctx context.Context, tx *sql.Tx) error {
stmt := `
INSERT INTO profiles (name, description) VALUES ("docker", "Profile supporting docker in containers");
INSERT INTO profiles_config (profile_id, key, value) SELECT id, "security.nesting", "true" FROM profiles WHERE name="docker";
INSERT INTO profiles_config (profile_id, key, value) SELECT id, "linux.kernel_modules", "overlay, nf_nat" FROM profiles WHERE name="docker";
INSERT INTO profiles_devices (profile_id, name, type) SELECT id, "fuse", "unix-char" FROM profiles WHERE name="docker";
INSERT INTO profiles_devices_config (profile_device_id, key, value) SELECT profiles_devices.id, "path", "/dev/fuse" FROM profiles_devices LEFT JOIN profiles WHERE profiles_devices.profile_id = profiles.id AND profiles.name = "docker";`
_, _ = tx.Exec(stmt)
return nil
}
func updateFromV24(ctx context.Context, tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE containers ADD COLUMN stateful INTEGER NOT NULL DEFAULT 0;")
return err
}
func updateFromV23(ctx context.Context, tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE profiles ADD COLUMN description TEXT;")
return err
}
func updateFromV22(ctx context.Context, tx *sql.Tx) error {
stmt := `
DELETE FROM containers_devices_config WHERE key='type';
DELETE FROM profiles_devices_config WHERE key='type';`
_, err := tx.Exec(stmt)
return err
}
func updateFromV21(ctx context.Context, tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE containers ADD COLUMN creation_date DATETIME NOT NULL DEFAULT 0;")
return err
}
func updateFromV20(ctx context.Context, tx *sql.Tx) error {
stmt := `
UPDATE containers_devices SET name='__upgrade_root' WHERE name='root';
UPDATE profiles_devices SET name='__upgrade_root' WHERE name='root';
INSERT INTO containers_devices (container_id, name, type) SELECT id, "root", 2 FROM containers;
INSERT INTO containers_devices_config (container_device_id, key, value) SELECT id, "path", "/" FROM containers_devices WHERE name='root';`
_, err := tx.Exec(stmt)
return err
}
func updateFromV19(ctx context.Context, tx *sql.Tx) error {
stmt := `
DELETE FROM containers_config WHERE container_id NOT IN (SELECT id FROM containers);
DELETE FROM containers_devices_config WHERE container_device_id NOT IN (SELECT id FROM containers_devices WHERE container_id IN (SELECT id FROM containers));
DELETE FROM containers_devices WHERE container_id NOT IN (SELECT id FROM containers);
DELETE FROM containers_profiles WHERE container_id NOT IN (SELECT id FROM containers);
DELETE FROM images_aliases WHERE image_id NOT IN (SELECT id FROM images);
DELETE FROM images_properties WHERE image_id NOT IN (SELECT id FROM images);`
_, err := tx.Exec(stmt)
return err
}
func updateFromV18(ctx context.Context, tx *sql.Tx) error {
var id int
var value string
// Update container config
rows, err := tx.QueryContext(ctx, "SELECT id, value FROM containers_config WHERE key='limits.memory'")
if err != nil {
return err
}
defer func() { _ = rows.Close() }()
for rows.Next() {
err := rows.Scan(&id, &value)
if err != nil {
return err
}
// If already an integer, don't touch
_, err = strconv.Atoi(value)
if err == nil {
continue
}
// Generate the new value
value = strings.ToUpper(value)
value += "B"
// Deal with completely broken values
_, err = units.ParseByteSizeString(value)
if err != nil {
logger.Debugf("Invalid container memory limit, id=%d value=%s, removing", id, value)
_, err = tx.Exec("DELETE FROM containers_config WHERE id=?;", id)
if err != nil {
return err
}
}
// Set the new value
_, err = tx.Exec("UPDATE containers_config SET value=? WHERE id=?", value, id)
if err != nil {
return err
}
}
err = rows.Err()
if err != nil {
return err
}
// Update profiles config
rows, err = tx.QueryContext(ctx, "SELECT id, value FROM profiles_config WHERE key='limits.memory'")
if err != nil {
return err
}
defer func() { _ = rows.Close() }()
for rows.Next() {
err := rows.Scan(&id, &value)
if err != nil {
return err
}
// If already an integer, don't touch
_, err = strconv.Atoi(value)
if err == nil {
continue
}
// Generate the new value
value = strings.ToUpper(value)
value += "B"
// Deal with completely broken values
_, err = units.ParseByteSizeString(value)
if err != nil {
logger.Debugf("Invalid profile memory limit, id=%d value=%s, removing", id, value)
_, err = tx.Exec("DELETE FROM profiles_config WHERE id=?;", id)
if err != nil {
return err
}
}
// Set the new value
_, err = tx.Exec("UPDATE profiles_config SET value=? WHERE id=?", value, id)
if err != nil {
return err
}
}
err = rows.Err()
if err != nil {
return err
}
return nil
}
func updateFromV17(ctx context.Context, tx *sql.Tx) error {
stmt := `
DELETE FROM profiles_config WHERE key LIKE 'volatile.%';
UPDATE containers_config SET key='limits.cpu' WHERE key='limits.cpus';
UPDATE profiles_config SET key='limits.cpu' WHERE key='limits.cpus';`
_, err := tx.Exec(stmt)
return err
}
func updateFromV16(ctx context.Context, tx *sql.Tx) error {
stmt := `
UPDATE config SET key='storage.lvm_vg_name' WHERE key = 'core.lvm_vg_name';
UPDATE config SET key='storage.lvm_thinpool_name' WHERE key = 'core.lvm_thinpool_name';`
_, err := tx.Exec(stmt)
return err
}
func updateFromV15(ctx context.Context, tx *sql.Tx) error {
// NOTE: this database update contained daemon-level logic which
// was been moved to patchUpdateFromV15 in patches.go.
return nil
}
func updateFromV14(ctx context.Context, tx *sql.Tx) error {
stmt := `
PRAGMA foreign_keys=OFF; -- So that integrity doesn't get in the way for now
DELETE FROM containers_config WHERE key="volatile.last_state.power";
INSERT INTO containers_config (container_id, key, value)
SELECT id, "volatile.last_state.power", "RUNNING"
FROM containers WHERE power_state=1;
INSERT INTO containers_config (container_id, key, value)
SELECT id, "volatile.last_state.power", "STOPPED"
FROM containers WHERE power_state != 1;
CREATE TABLE tmp (
id INTEGER primary key AUTOINCREMENT NOT NULL,
name VARCHAR(255) NOT NULL,
architecture INTEGER NOT NULL,
type INTEGER NOT NULL,
ephemeral INTEGER NOT NULL DEFAULT 0,
UNIQUE (name)
);
INSERT INTO tmp SELECT id, name, architecture, type, ephemeral FROM containers;
DROP TABLE containers;
ALTER TABLE tmp RENAME TO containers;
PRAGMA foreign_keys=ON; -- Make sure we turn integrity checks back on.`
_, err := tx.Exec(stmt)
return err
}
func updateFromV13(ctx context.Context, tx *sql.Tx) error {
stmt := `
UPDATE containers_config SET key='volatile.base_image' WHERE key = 'volatile.baseImage';`
_, err := tx.Exec(stmt)
return err
}
func updateFromV12(ctx context.Context, tx *sql.Tx) error {
stmt := `
ALTER TABLE images ADD COLUMN cached INTEGER NOT NULL DEFAULT 0;
ALTER TABLE images ADD COLUMN last_use_date DATETIME;`
_, err := tx.Exec(stmt)
return err
}
func updateFromV11(ctx context.Context, tx *sql.Tx) error {
// NOTE: this database update contained daemon-level logic which
// was been moved to patchUpdateFromV15 in patches.go.
return nil
}
func updateFromV10(ctx context.Context, tx *sql.Tx) error {
// NOTE: this database update contained daemon-level logic which
// was been moved to patchUpdateFromV10 in patches.go.
return nil
}
func updateFromV9(ctx context.Context, tx *sql.Tx) error {
stmt := `
CREATE TABLE tmp (
id INTEGER primary key AUTOINCREMENT NOT NULL,
container_id INTEGER NOT NULL,
name VARCHAR(255) NOT NULL,
type VARCHAR(255) NOT NULL default "none",
FOREIGN KEY (container_id) REFERENCES containers (id) ON DELETE CASCADE,
UNIQUE (container_id, name)
);
INSERT INTO tmp SELECT * FROM containers_devices;
UPDATE containers_devices SET type=0 WHERE id IN (SELECT id FROM tmp WHERE type="none");
UPDATE containers_devices SET type=1 WHERE id IN (SELECT id FROM tmp WHERE type="nic");
UPDATE containers_devices SET type=2 WHERE id IN (SELECT id FROM tmp WHERE type="disk");
UPDATE containers_devices SET type=3 WHERE id IN (SELECT id FROM tmp WHERE type="unix-char");
UPDATE containers_devices SET type=4 WHERE id IN (SELECT id FROM tmp WHERE type="unix-block");
DROP TABLE tmp;
CREATE TABLE tmp (
id INTEGER primary key AUTOINCREMENT NOT NULL,
profile_id INTEGER NOT NULL,
name VARCHAR(255) NOT NULL,
type VARCHAR(255) NOT NULL default "none",
FOREIGN KEY (profile_id) REFERENCES profiles (id) ON DELETE CASCADE,
UNIQUE (profile_id, name)
);
INSERT INTO tmp SELECT * FROM profiles_devices;
UPDATE profiles_devices SET type=0 WHERE id IN (SELECT id FROM tmp WHERE type="none");
UPDATE profiles_devices SET type=1 WHERE id IN (SELECT id FROM tmp WHERE type="nic");
UPDATE profiles_devices SET type=2 WHERE id IN (SELECT id FROM tmp WHERE type="disk");
UPDATE profiles_devices SET type=3 WHERE id IN (SELECT id FROM tmp WHERE type="unix-char");
UPDATE profiles_devices SET type=4 WHERE id IN (SELECT id FROM tmp WHERE type="unix-block");
DROP TABLE tmp;`
_, err := tx.Exec(stmt)
return err
}
func updateFromV8(ctx context.Context, tx *sql.Tx) error {
stmt := `
UPDATE certificates SET fingerprint = replace(fingerprint, " ", "");`
_, err := tx.Exec(stmt)
return err
}
func updateFromV7(ctx context.Context, tx *sql.Tx) error {
stmt := `
UPDATE config SET key='core.trust_password' WHERE key IN ('password', 'trust_password', 'trust-password', 'core.trust-password');
DELETE FROM config WHERE key != 'core.trust_password';`
_, err := tx.Exec(stmt)
return err
}
func updateFromV6(ctx context.Context, tx *sql.Tx) error {
// This update recreates the schemas that need an ON DELETE CASCADE foreign
// key.
stmt := `
PRAGMA foreign_keys=OFF; -- So that integrity doesn't get in the way for now
CREATE TEMP TABLE tmp AS SELECT * FROM containers_config;
DROP TABLE containers_config;
CREATE TABLE IF NOT EXISTS containers_config (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
container_id INTEGER NOT NULL,
key VARCHAR(255) NOT NULL,
value TEXT,
FOREIGN KEY (container_id) REFERENCES containers (id) ON DELETE CASCADE,
UNIQUE (container_id, key)
);
INSERT INTO containers_config SELECT * FROM tmp;
DROP TABLE tmp;
CREATE TEMP TABLE tmp AS SELECT * FROM containers_devices;
DROP TABLE containers_devices;
CREATE TABLE IF NOT EXISTS containers_devices (
id INTEGER primary key AUTOINCREMENT NOT NULL,
container_id INTEGER NOT NULL,
name VARCHAR(255) NOT NULL,
type INTEGER NOT NULL default 0,
FOREIGN KEY (container_id) REFERENCES containers (id) ON DELETE CASCADE,
UNIQUE (container_id, name)
);
INSERT INTO containers_devices SELECT * FROM tmp;
DROP TABLE tmp;
CREATE TEMP TABLE tmp AS SELECT * FROM containers_devices_config;
DROP TABLE containers_devices_config;
CREATE TABLE IF NOT EXISTS containers_devices_config (
id INTEGER primary key AUTOINCREMENT NOT NULL,
container_device_id INTEGER NOT NULL,
key VARCHAR(255) NOT NULL,
value TEXT,
FOREIGN KEY (container_device_id) REFERENCES containers_devices (id) ON DELETE CASCADE,
UNIQUE (container_device_id, key)
);
INSERT INTO containers_devices_config SELECT * FROM tmp;
DROP TABLE tmp;
CREATE TEMP TABLE tmp AS SELECT * FROM containers_profiles;
DROP TABLE containers_profiles;
CREATE TABLE IF NOT EXISTS containers_profiles (
id INTEGER primary key AUTOINCREMENT NOT NULL,
container_id INTEGER NOT NULL,
profile_id INTEGER NOT NULL,
apply_order INTEGER NOT NULL default 0,
UNIQUE (container_id, profile_id),
FOREIGN KEY (container_id) REFERENCES containers(id) ON DELETE CASCADE,
FOREIGN KEY (profile_id) REFERENCES profiles(id) ON DELETE CASCADE
);
INSERT INTO containers_profiles SELECT * FROM tmp;
DROP TABLE tmp;
CREATE TEMP TABLE tmp AS SELECT * FROM images_aliases;
DROP TABLE images_aliases;
CREATE TABLE IF NOT EXISTS images_aliases (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name VARCHAR(255) NOT NULL,
image_id INTEGER NOT NULL,
description VARCHAR(255),
FOREIGN KEY (image_id) REFERENCES images (id) ON DELETE CASCADE,
UNIQUE (name)
);
INSERT INTO images_aliases SELECT * FROM tmp;
DROP TABLE tmp;
CREATE TEMP TABLE tmp AS SELECT * FROM images_properties;
DROP TABLE images_properties;
CREATE TABLE IF NOT EXISTS images_properties (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
image_id INTEGER NOT NULL,
type INTEGER NOT NULL,
key VARCHAR(255) NOT NULL,
value TEXT,
FOREIGN KEY (image_id) REFERENCES images (id) ON DELETE CASCADE
);
INSERT INTO images_properties SELECT * FROM tmp;
DROP TABLE tmp;
CREATE TEMP TABLE tmp AS SELECT * FROM profiles_config;
DROP TABLE profiles_config;
CREATE TABLE IF NOT EXISTS profiles_config (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
profile_id INTEGER NOT NULL,
key VARCHAR(255) NOT NULL,
value VARCHAR(255),
UNIQUE (profile_id, key),
FOREIGN KEY (profile_id) REFERENCES profiles(id) ON DELETE CASCADE
);
INSERT INTO profiles_config SELECT * FROM tmp;
DROP TABLE tmp;
CREATE TEMP TABLE tmp AS SELECT * FROM profiles_devices;
DROP TABLE profiles_devices;
CREATE TABLE IF NOT EXISTS profiles_devices (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
profile_id INTEGER NOT NULL,
name VARCHAR(255) NOT NULL,
type INTEGER NOT NULL default 0,
UNIQUE (profile_id, name),
FOREIGN KEY (profile_id) REFERENCES profiles (id) ON DELETE CASCADE
);
INSERT INTO profiles_devices SELECT * FROM tmp;
DROP TABLE tmp;
CREATE TEMP TABLE tmp AS SELECT * FROM profiles_devices_config;
DROP TABLE profiles_devices_config;
CREATE TABLE IF NOT EXISTS profiles_devices_config (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
profile_device_id INTEGER NOT NULL,
key VARCHAR(255) NOT NULL,
value TEXT,
UNIQUE (profile_device_id, key),
FOREIGN KEY (profile_device_id) REFERENCES profiles_devices (id) ON DELETE CASCADE
);
INSERT INTO profiles_devices_config SELECT * FROM tmp;
DROP TABLE tmp;
PRAGMA foreign_keys=ON; -- Make sure we turn integrity checks back on.`
_, err := tx.Exec(stmt)
if err != nil {
return err
}
// Get the rows with broken foreign keys an nuke them
rows, err := tx.QueryContext(ctx, "PRAGMA foreign_key_check;")
if err != nil {
return err
}
defer func() { _ = rows.Close() }()
var tablestodelete []string
var rowidtodelete []int
for rows.Next() {
var tablename string
var rowid int
var targetname string
var keynumber int
err := rows.Scan(&tablename, &rowid, &targetname, &keynumber)
if err != nil {
return err
}
tablestodelete = append(tablestodelete, tablename)
rowidtodelete = append(rowidtodelete, rowid)
}
for i := range tablestodelete {
_, err = tx.Exec(fmt.Sprintf("DELETE FROM %s WHERE rowid = %d;", tablestodelete[i], rowidtodelete[i]))
if err != nil {
return err
}
}
return nil
}
func updateFromV5(ctx context.Context, tx *sql.Tx) error {
stmt := `
ALTER TABLE containers ADD COLUMN power_state INTEGER NOT NULL DEFAULT 0;
ALTER TABLE containers ADD COLUMN ephemeral INTEGER NOT NULL DEFAULT 0;`
_, err := tx.Exec(stmt)
return err
}
func updateFromV4(ctx context.Context, tx *sql.Tx) error {
stmt := `
CREATE TABLE IF NOT EXISTS config (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
key VARCHAR(255) NOT NULL,
value TEXT,
UNIQUE (key)
);`
_, err := tx.Exec(stmt)
if err != nil {
return err
}
passfname := internalUtil.VarPath("adminpwd")
passOut, err := os.Open(passfname)
oldPassword := ""
if err == nil {
defer func() { _ = passOut.Close() }()
buff := make([]byte, 96)
_, err = passOut.Read(buff)
if err != nil {
return err
}
oldPassword = hex.EncodeToString(buff)
stmt := `INSERT INTO config (key, value) VALUES ("core.trust_password", ?);`
_, err := tx.Exec(stmt, oldPassword)
if err != nil {
return err
}
return os.Remove(passfname)
}
return nil
}
func updateFromV3(ctx context.Context, tx *sql.Tx) error {
// Attempt to create a default profile (but don't fail if already there)
_, _ = tx.Exec("INSERT INTO profiles (name) VALUES (\"default\");")
return nil
}
func updateFromV2(ctx context.Context, tx *sql.Tx) error {
stmt := `
CREATE TABLE IF NOT EXISTS containers_devices (
id INTEGER primary key AUTOINCREMENT NOT NULL,
container_id INTEGER NOT NULL,
name VARCHAR(255) NOT NULL,
type INTEGER NOT NULL default 0,
FOREIGN KEY (container_id) REFERENCES containers (id) ON DELETE CASCADE,
UNIQUE (container_id, name)
);
CREATE TABLE IF NOT EXISTS containers_devices_config (
id INTEGER primary key AUTOINCREMENT NOT NULL,
container_device_id INTEGER NOT NULL,
key VARCHAR(255) NOT NULL,
value TEXT,
FOREIGN KEY (container_device_id) REFERENCES containers_devices (id),
UNIQUE (container_device_id, key)
);
CREATE TABLE IF NOT EXISTS containers_profiles (
id INTEGER primary key AUTOINCREMENT NOT NULL,
container_id INTEGER NOT NULL,
profile_id INTEGER NOT NULL,
apply_order INTEGER NOT NULL default 0,
UNIQUE (container_id, profile_id),
FOREIGN KEY (container_id) REFERENCES containers(id) ON DELETE CASCADE,
FOREIGN KEY (profile_id) REFERENCES profiles(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS profiles (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name VARCHAR(255) NOT NULL,
UNIQUE (name)
);
CREATE TABLE IF NOT EXISTS profiles_config (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
profile_id INTEGER NOT NULL,
key VARCHAR(255) NOT NULL,
value VARCHAR(255),
UNIQUE (profile_id, key),
FOREIGN KEY (profile_id) REFERENCES profiles(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS profiles_devices (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
profile_id INTEGER NOT NULL,
name VARCHAR(255) NOT NULL,
type INTEGER NOT NULL default 0,
UNIQUE (profile_id, name),
FOREIGN KEY (profile_id) REFERENCES profiles (id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS profiles_devices_config (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,