Implement proper DB migrations using Goose. Upgrade go packages.
This commit is contained in:
		
							parent
							
								
									6902facab6
								
							
						
					
					
						commit
						444e5065a4
					
				
					 12 changed files with 255 additions and 117 deletions
				
			
		
							
								
								
									
										14
									
								
								migrations/00001_log.sql
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								migrations/00001_log.sql
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,14 @@
 | 
			
		|||
-- +goose Up
 | 
			
		||||
-- +goose StatementBegin
 | 
			
		||||
CREATE TABLE IF NOT EXISTS opendtu_log (
 | 
			
		||||
    timestamp TIMESTAMPTZ UNIQUE DEFAULT CURRENT_TIMESTAMP,
 | 
			
		||||
    power NUMERIC,
 | 
			
		||||
    yieldday NUMERIC,
 | 
			
		||||
    yieldtotal NUMERIC
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE INDEX IF NOT EXISTS opendtu_log_timestamp_idx ON opendtu_log (timestamp);
 | 
			
		||||
-- +goose StatementEnd
 | 
			
		||||
-- +goose Down
 | 
			
		||||
-- +goose StatementBegin
 | 
			
		||||
-- +goose StatementEnd
 | 
			
		||||
							
								
								
									
										19
									
								
								migrations/00002_inverters.sql
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								migrations/00002_inverters.sql
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,19 @@
 | 
			
		|||
-- +goose Up
 | 
			
		||||
-- +goose StatementBegin
 | 
			
		||||
CREATE TABLE IF NOT EXISTS opendtu_inverters (
 | 
			
		||||
    timestamp TIMESTAMPTZ,
 | 
			
		||||
    -- FOREIGN KEY (timestamp) REFERENCES opendtu_log(timestamp),
 | 
			
		||||
    -- 	inverter_serial is TEXT as some non-Hoymiles inverters use non-numeric serial numbers.
 | 
			
		||||
    -- 	An additional advantage is that it makes plotting in Grafana easier.
 | 
			
		||||
    inverter_serial TEXT,
 | 
			
		||||
    name TEXT,
 | 
			
		||||
    producing BOOL,
 | 
			
		||||
    limit_relative NUMERIC,
 | 
			
		||||
    limit_absolute NUMERIC
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE INDEX IF NOT EXISTS opendtu_inverters_timestamp_idx ON opendtu_inverters (timestamp);
 | 
			
		||||
-- +goose StatementEnd
 | 
			
		||||
-- +goose Down
 | 
			
		||||
-- +goose StatementBegin
 | 
			
		||||
-- +goose StatementEnd
 | 
			
		||||
							
								
								
									
										20
									
								
								migrations/00003_inverters_ac.sql
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								migrations/00003_inverters_ac.sql
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,20 @@
 | 
			
		|||
-- +goose Up
 | 
			
		||||
-- +goose StatementBegin
 | 
			
		||||
CREATE TABLE IF NOT EXISTS opendtu_inverters_ac (
 | 
			
		||||
    timestamp TIMESTAMPTZ,
 | 
			
		||||
    -- FOREIGN KEY (timestamp) REFERENCES opendtu_log(timestamp),
 | 
			
		||||
    inverter_serial TEXT,
 | 
			
		||||
    ac_number INT,
 | 
			
		||||
    power NUMERIC,
 | 
			
		||||
    voltage NUMERIC,
 | 
			
		||||
    current NUMERIC,
 | 
			
		||||
    frequency NUMERIC,
 | 
			
		||||
    powerfactor NUMERIC,
 | 
			
		||||
    reactivepower NUMERIC
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE INDEX IF NOT EXISTS opendtu_inverters_ac_timestamp_idx ON opendtu_inverters_ac (timestamp);
 | 
			
		||||
-- +goose StatementEnd
 | 
			
		||||
-- +goose Down
 | 
			
		||||
-- +goose StatementBegin
 | 
			
		||||
-- +goose StatementEnd
 | 
			
		||||
							
								
								
									
										22
									
								
								migrations/00004_inverters_dc.sql
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								migrations/00004_inverters_dc.sql
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,22 @@
 | 
			
		|||
-- +goose Up
 | 
			
		||||
-- +goose StatementBegin
 | 
			
		||||
CREATE TABLE IF NOT EXISTS opendtu_inverters_dc (
 | 
			
		||||
    -- id SERIAL PRIMARY KEY,
 | 
			
		||||
    timestamp TIMESTAMPTZ,
 | 
			
		||||
    -- FOREIGN KEY (timestamp) REFERENCES opendtu_log(timestamp),
 | 
			
		||||
    inverter_serial TEXT,
 | 
			
		||||
    dc_number INT,
 | 
			
		||||
    name TEXT,
 | 
			
		||||
    power NUMERIC,
 | 
			
		||||
    voltage NUMERIC,
 | 
			
		||||
    current NUMERIC,
 | 
			
		||||
    yieldday NUMERIC,
 | 
			
		||||
    yieldtotal NUMERIC,
 | 
			
		||||
    irradiation NUMERIC
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE INDEX IF NOT EXISTS opendtu_inverters_dc_timestamp_idx ON opendtu_inverters_dc (timestamp);
 | 
			
		||||
-- +goose StatementEnd
 | 
			
		||||
-- +goose Down
 | 
			
		||||
-- +goose StatementBegin
 | 
			
		||||
-- +goose StatementEnd
 | 
			
		||||
							
								
								
									
										19
									
								
								migrations/00005_inverters_inv.sql
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								migrations/00005_inverters_inv.sql
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,19 @@
 | 
			
		|||
-- +goose Up
 | 
			
		||||
-- +goose StatementBegin
 | 
			
		||||
CREATE TABLE IF NOT EXISTS opendtu_inverters_inv (
 | 
			
		||||
    -- id SERIAL PRIMARY KEY,
 | 
			
		||||
    timestamp TIMESTAMPTZ,
 | 
			
		||||
    -- FOREIGN KEY (timestamp) REFERENCES opendtu_log(timestamp),
 | 
			
		||||
    inverter_serial TEXT,
 | 
			
		||||
    temperature NUMERIC,
 | 
			
		||||
    power_dc NUMERIC,
 | 
			
		||||
    yieldday NUMERIC,
 | 
			
		||||
    yieldtotal NUMERIC,
 | 
			
		||||
    efficiency NUMERIC
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE INDEX IF NOT EXISTS opendtu_inverters_inv_timestamp_idx ON opendtu_inverters_inv (timestamp);
 | 
			
		||||
-- +goose StatementEnd
 | 
			
		||||
-- +goose Down
 | 
			
		||||
-- +goose StatementBegin
 | 
			
		||||
-- +goose StatementEnd
 | 
			
		||||
							
								
								
									
										17
									
								
								migrations/00006_events.sql
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								migrations/00006_events.sql
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,17 @@
 | 
			
		|||
-- +goose Up
 | 
			
		||||
-- +goose StatementBegin
 | 
			
		||||
CREATE TABLE IF NOT EXISTS opendtu_events (
 | 
			
		||||
	-- id SERIAL PRIMARY KEY,
 | 
			
		||||
	timestamp TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
 | 
			
		||||
	inverter_serial TEXT,
 | 
			
		||||
	message_id INT,
 | 
			
		||||
	message TEXT,
 | 
			
		||||
	start_time INT,
 | 
			
		||||
	end_time INT
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE INDEX IF NOT EXISTS opendtu_events_timestamp_idx ON opendtu_events (timestamp);
 | 
			
		||||
-- +goose StatementEnd
 | 
			
		||||
-- +goose Down
 | 
			
		||||
-- +goose StatementBegin
 | 
			
		||||
-- +goose StatementEnd
 | 
			
		||||
							
								
								
									
										26
									
								
								migrations/00007_events.sql
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								migrations/00007_events.sql
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,26 @@
 | 
			
		|||
-- +goose Up
 | 
			
		||||
-- +goose StatementBegin
 | 
			
		||||
DO $$
 | 
			
		||||
BEGIN
 | 
			
		||||
    -- Check if start_timestamp column exists
 | 
			
		||||
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns 
 | 
			
		||||
                WHERE table_name='opendtu_events' 
 | 
			
		||||
                AND column_name='start_timestamp') THEN
 | 
			
		||||
        -- Add start_timestamp column
 | 
			
		||||
        ALTER TABLE opendtu_events 
 | 
			
		||||
        ADD COLUMN start_timestamp TIMESTAMPTZ;
 | 
			
		||||
    END IF;
 | 
			
		||||
 | 
			
		||||
    -- Check if end_timestamp column exists
 | 
			
		||||
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns 
 | 
			
		||||
                WHERE table_name='opendtu_events' 
 | 
			
		||||
                AND column_name='end_timestamp') THEN
 | 
			
		||||
        -- Add end_timestamp column
 | 
			
		||||
        ALTER TABLE opendtu_events 
 | 
			
		||||
        ADD COLUMN end_timestamp TIMESTAMPTZ;
 | 
			
		||||
    END IF;
 | 
			
		||||
END $$;
 | 
			
		||||
-- +goose StatementEnd
 | 
			
		||||
-- +goose Down
 | 
			
		||||
-- +goose StatementBegin
 | 
			
		||||
-- +goose StatementEnd
 | 
			
		||||
							
								
								
									
										16
									
								
								migrations/00008_hints.sql
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								migrations/00008_hints.sql
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,16 @@
 | 
			
		|||
-- +goose Up
 | 
			
		||||
-- +goose StatementBegin
 | 
			
		||||
CREATE TABLE IF NOT EXISTS opendtu_hints (
 | 
			
		||||
    -- id SERIAL PRIMARY KEY,
 | 
			
		||||
    timestamp TIMESTAMPTZ,
 | 
			
		||||
    -- FOREIGN KEY (timestamp) REFERENCES opendtu_log(timestamp),
 | 
			
		||||
    time_sync BOOL,
 | 
			
		||||
    radio_problem BOOL,
 | 
			
		||||
    default_password BOOL
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE INDEX IF NOT EXISTS opendtu_hints_timestamp_idx ON opendtu_hints (timestamp);
 | 
			
		||||
-- +goose StatementEnd
 | 
			
		||||
-- +goose Down
 | 
			
		||||
-- +goose StatementBegin
 | 
			
		||||
-- +goose StatementEnd
 | 
			
		||||
							
								
								
									
										6
									
								
								migrations/fs.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								migrations/fs.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,6 @@
 | 
			
		|||
package migrations
 | 
			
		||||
 | 
			
		||||
import "embed"
 | 
			
		||||
 | 
			
		||||
//go:embed *.sql
 | 
			
		||||
var FS embed.FS
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue