Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ozym committed Nov 23, 2024
1 parent 858bd2c commit c9c448f
Show file tree
Hide file tree
Showing 7 changed files with 619 additions and 118 deletions.
166 changes: 127 additions & 39 deletions cmd/deltadb/delta.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
195 changes: 195 additions & 0 deletions meta/sqlite/asset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
package sqlite

Check failure on line 1 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / golangci-lint / lint

: # github.com/GeoNet/delta/meta/sqlite

import (
"fmt"
)

const makeCreate = `
DROP TABLE IF EXISTS make;
CREATE TABLE IF NOT EXISTS make (
make_id INTEGER PRIMARY KEY NOT NULL,
make TEXT NOT NULL,
UNIQUE (make)
);`

var mmake = Table{

Check failure on line 15 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / golangci-lint / lint

undefined: Table

Check failure on line 15 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / govulncheck / govulncheck

undefined: Table

Check failure on line 15 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-vet / go-vet

undefined: Table

Check failure on line 15 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-test / go-test

undefined: Table

Check failure on line 15 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-build / go-build-smoke-test

undefined: Table
Create: makeCreate,
Select: func() string {
return "SELECT make_id FROM make WHERE make = ?"
},
Insert: func() string {
return "INSERT OR IGNORE INTO make (make) VALUES (?);"
},

Fields: []string{"Make"},
}

const modelCreate = `
DROP TABLE IF EXISTS model;
CREATE TABLE IF NOT EXISTS model (
model_id INTEGER PRIMARY KEY NOT NULL,
make_id INTEGER NOT NULL,
model TEXT NOT NULL,
FOREIGN KEY (make_id) REFERENCES make (make_id),
UNIQUE (make_id, model)
);`

var model = Table{

Check failure on line 37 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / golangci-lint / lint

undefined: Table

Check failure on line 37 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / govulncheck / govulncheck

undefined: Table

Check failure on line 37 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-vet / go-vet

undefined: Table

Check failure on line 37 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-test / go-test

undefined: Table

Check failure on line 37 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-build / go-build-smoke-test

undefined: Table
Create: modelCreate,
Select: func() string {
return fmt.Sprintf("SELECT model_id FROM model WHERE make_id = (%s) AND model = ?", mmake.Select())
},
Insert: func() string {
return fmt.Sprintf("INSERT OR IGNORE INTO model (make_id, model) VALUES ((%s), ?);", mmake.Select())
},
Fields: []string{"Make", "Model"},
}

const assetCreate = `
DROP TABLE IF EXISTS asset;
CREATE TABLE IF NOT EXISTS asset (
asset_id INTEGER PRIMARY KEY NOT NULL,
model_id INTEGER NOT NULL,
serial TEXT NOT NULL,
number TEXT DEFAULT "" NOT NULL,
notes TEXT DEFAULT "" NOT NULL,
FOREIGN KEY (model_id) REFERENCES model (model_id),
UNIQUE (model_id,serial)
);`

var asset = Table{

Check failure on line 60 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / golangci-lint / lint

undefined: Table

Check failure on line 60 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / govulncheck / govulncheck

undefined: Table

Check failure on line 60 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-vet / go-vet

undefined: Table

Check failure on line 60 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-test / go-test

undefined: Table

Check failure on line 60 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-build / go-build-smoke-test

undefined: Table
Create: assetCreate,
Select: func() string {
return fmt.Sprintf("SELECT asset_id FROM asset WHERE model_id = (%s) AND serial = ?", model.Select())
},
Insert: func() string {
return fmt.Sprintf("INSERT OR IGNORE INTO asset (model_id, serial, number, notes) VALUES ((%s), ?, ?, ?);", model.Select())
},
Fields: []string{"Make", "Model", "Serial", "Number", "Notes"},
}

const firmwareCreate = `
DROP TABLE IF EXISTS firmware;
CREATE TABLE IF NOT EXISTS firmware (
firmware_id INTEGER PRIMARY KEY NOT NULL,
asset_id INTEGER NOT NULL,
version TEXT NOT NULL,
notes TEXT NOT NULL,
start_date DATETIME NOT NULL CHECK (start_date IS strftime('%Y-%m-%dT%H:%M:%SZ', start_date)),
end_date DATETIME NOT NULL CHECK (end_date IS strftime('%Y-%m-%dT%H:%M:%SZ', end_date)),
FOREIGN KEY (asset_id) REFERENCES asset (asset_id),
UNIQUE (asset_id, start_date, end_date)
);
CREATE TRIGGER IF NOT EXISTS no_overlap_on_firmware BEFORE INSERT ON firmware
WHEN EXISTS (
SELECT * FROM firmware
WHERE datetime(start_date) <= datetime(NEW.end_date)
AND datetime(end_date) > datetime(NEW.start_date)
AND asset_id = NEW.asset_id
)
BEGIN
SELECT RAISE(FAIL, "overlapping intervals on firmware");
END;
`

var firmware = Table{

Check failure on line 95 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / golangci-lint / lint

undefined: Table

Check failure on line 95 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / govulncheck / govulncheck

undefined: Table

Check failure on line 95 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-vet / go-vet

undefined: Table

Check failure on line 95 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-test / go-test

undefined: Table

Check failure on line 95 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-build / go-build-smoke-test

undefined: Table
Create: firmwareCreate,
Select: func() string {
return fmt.Sprintf("SELECT firmware_id FROM firmware WHERE asset_id = (%s) AND start_date = ? AND end_date = ?",
asset.Select())
},
Insert: func() string {
return fmt.Sprintf("INSERT OR IGNORE INTO firmware (asset_id, version, notes, start_date, end_date) VALUES ((%s), ?, ?, ?, ?);",
asset.Select())
},
Fields: []string{"Make", "Model", "Serial", "Version", "Notes", "Start Date", "End Date"},
}

const channelCreate = `
DROP TABLE IF EXISTS channel;
CREATE TABLE IF NOT EXISTS channel (
channel_id INTEGER PRIMARY KEY NOT NULL,
model_id INTEGER NOT NULL,
channel_type TEXT NOT NULL,
number REAL DEFAULT 0 NOT NULL,
sampling_rate REAL NOT NULL,
response TEXT DEFAULT "" NOT NULL,
FOREIGN KEY (model_id) REFERENCES model (model_id),
UNIQUE(model_id, channel_type, number, sampling_rate)
);
`

var channel = Table{

Check failure on line 122 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / golangci-lint / lint

undefined: Table

Check failure on line 122 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / govulncheck / govulncheck

undefined: Table

Check failure on line 122 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-vet / go-vet

undefined: Table

Check failure on line 122 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-test / go-test

undefined: Table

Check failure on line 122 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-build / go-build-smoke-test

undefined: Table
Create: channelCreate,
Insert: func() string {
return fmt.Sprintf("INSERT OR IGNORE INTO channel (model_id, channel_type, number, sampling_rate, response) VALUES ((%s), ?, ?, ?, ?);",
model.Select())
},
Fields: []string{"Make", "Model", "Type", "Number", "SamplingRate", "Response"},
}

const componentCreate = `
DROP TABLE IF EXISTS component;
CREATE TABLE IF NOT EXISTS component (
component_id INTEGER PRIMARY KEY NOT NULL,
model_id INTEGER NOT NULL,
component_type TEXT NULL,
number REAL NOT NULL,
source TEXT NULL,
subsource TEXT NOT NULL,
dip REAL NOT NULL,
azimuth REAL NOT NULL,
types TEXT NOT NULL,
sampling_rate REAL NULL,
response TEXT NOT NULL,
FOREIGN KEY (model_id) REFERENCES model (model_id),
UNIQUE(model_id, number, source, subsource, sampling_rate)
);
`

var component = Table{

Check failure on line 150 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / golangci-lint / lint

undefined: Table

Check failure on line 150 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / govulncheck / govulncheck

undefined: Table

Check failure on line 150 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-vet / go-vet

undefined: Table

Check failure on line 150 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-test / go-test

undefined: Table

Check failure on line 150 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-build / go-build-smoke-test

undefined: Table
Create: componentCreate,
Insert: func() string {
return fmt.Sprintf("INSERT OR IGNORE INTO component (model_id, component_type, number, source, subsource, dip, azimuth, types, sampling_rate, response) VALUES ((%s), ?, ?, ?, ?, ?, ?, ?, ?, ?);",
model.Select())
},
Fields: []string{"Make", "Model", "Type", "Number", "Source", "Subsource", "Dip", "Azimuth", "Types", "Sampling Rate", "Response"},
}

const calibrationCreate = `
DROP TABLE IF EXISTS calibration;
CREATE TABLE IF NOT EXISTS calibration (
calibration_id INTEGER PRIMARY KEY NOT NULL,
asset_id INTEGER NOT NULL,
number TEXT NOT NULL,
scale_factor REAL DEFAULT 1.0 NOT NULL,
scale_bias REAL DEFAULT 0.0 NOT NULL,
scale_absolute REAL DEFAULT 0.0 NOT NULL,
frequency REAL NULL,
start_date DATETIME NOT NULL CHECK (start_date IS strftime('%Y-%m-%dT%H:%M:%SZ', start_date)),
end_date DATETIME NOT NULL CHECK (end_date IS strftime('%Y-%m-%dT%H:%M:%SZ', end_date)),
FOREIGN KEY (asset_id) REFERENCES asset (asset_id),
UNIQUE(asset_id, number, start_date, end_date)
);
CREATE TRIGGER IF NOT EXISTS no_overlap_on_calibration BEFORE INSERT ON calibration
WHEN EXISTS (
SELECT * FROM calibration
WHERE datetime(start_date) <= datetime(NEW.end_date)
AND datetime(end_date) > datetime(NEW.start_date)
AND asset_id = NEW.asset_id
AND number = NEW.number
)
BEGIN
SELECT RAISE(FAIL, "overlapping intervals on calibration");
END;
`

var calibration = Table{

Check failure on line 187 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / golangci-lint / lint

undefined: Table

Check failure on line 187 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / govulncheck / govulncheck

undefined: Table

Check failure on line 187 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-vet / go-vet

undefined: Table

Check failure on line 187 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-test / go-test

undefined: Table

Check failure on line 187 in meta/sqlite/asset.go

View workflow job for this annotation

GitHub Actions / delta-apps / go-build / go-build-smoke-test

undefined: Table
Create: calibrationCreate,
Insert: func() string {
return fmt.Sprintf("INSERT OR IGNORE INTO calibration (asset_id, number, scale_factor, scale_bias, scale_absolute, frequency, start_date, end_date) VALUES ((%s), ?, ?, ?, ?, ?, ?, ?);",
asset.Select(),
)
},
Fields: []string{"Make", "Model", "Serial", "Number", "Scale Factor", "Scale Bias", "Scale Absolute", "Frequency", "Start Date", "End Date"},
}
30 changes: 14 additions & 16 deletions meta/sqlite/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,20 @@ func (d DB) Init(ctx context.Context, list []meta.TableList) error {
for _, l := range list {

switch l.Table.Name() {
/*
case "Placename":
if err := d.exec(ctx, tx, placenameCreate); err != nil {
return err
}
if err := d.prepare(ctx, tx, placenameInsert(), columns(l, placenameFields(), nil, "")...); err != nil {
return err
}
*/
case "Placename":
if err := d.exec(ctx, tx, placename.Create); err != nil {
return err
}
if err := d.prepare(ctx, tx, placename.Insert(), placename.Columns(l)...); err != nil {
return err
}
case "Citation":
if err := d.exec(ctx, tx, citation.Create); err != nil {
return err
}
if err := d.prepare(ctx, tx, citation.Insert(), citation.Columns(l)...); err != nil {
return err
}
case "Asset":
if err := d.exec(ctx, tx, makeCreate); err != nil {
return err
Expand Down Expand Up @@ -411,13 +416,6 @@ func (d DB) Init(ctx context.Context, list []meta.TableList) error {
if err := d.prepare(ctx, tx, connectionInsert(), columns(l, connectionFields(), nil, "")...); err != nil {
return err
}
case "Citation":
if err := d.exec(ctx, tx, citationCreate); err != nil {
return err
}
if err := d.prepare(ctx, tx, citationInsert(), columns(l, citationFields(), nil, "")...); err != nil {
return err
}
case "Gauge":
if err := d.exec(ctx, tx, gaugeCreate); err != nil {
return err
Expand Down
Loading

0 comments on commit c9c448f

Please sign in to comment.