From fb21d434251de017080791a1587284842c9d3acc Mon Sep 17 00:00:00 2001 From: d1slike Date: Fri, 24 May 2024 16:26:00 +0300 Subject: [PATCH] Use idempotent module id --- cmd/migrate/entities.go | 43 +++++++++ cmd/migrate/go.mod | 33 ++++++- cmd/migrate/go.sum | 149 ++++++++++++++++++++++++++++++++ cmd/migrate/main.go | 136 ++++++++++++++++++++++++++++- cmd/migrate/migration/runner.go | 86 ++++++++++++++++++ migrations/embed.go | 10 +++ repository/module.go | 12 ++- service/module/service.go | 16 ++-- 8 files changed, 467 insertions(+), 18 deletions(-) create mode 100644 cmd/migrate/entities.go create mode 100644 cmd/migrate/go.sum create mode 100644 cmd/migrate/migration/runner.go create mode 100644 migrations/embed.go diff --git a/cmd/migrate/entities.go b/cmd/migrate/entities.go new file mode 100644 index 0000000..519f41a --- /dev/null +++ b/cmd/migrate/entities.go @@ -0,0 +1,43 @@ +package main + +import ( + "time" + + "github.com/txix-open/isp-kit/json" +) + +type Module struct { + Id string + Name string + LastConnectedAt *time.Time + LastDisconnectedAt *time.Time + CreatedAt time.Time +} + +type Config struct { + Id string + ModuleId string + Name string + Version int + Active bool + Data json.RawMessage + CreatedAt time.Time + UpdatedAt time.Time +} + +type ConfigHistory struct { + Id string + ConfigId string + ConfigVersion int + Data json.RawMessage + CreatedAt time.Time +} + +type ConfigSchema struct { + Id string + ModuleId string + Version string + Schema json.RawMessage + CreatedAt time.Time + UpdatedAt time.Time +} diff --git a/cmd/migrate/go.mod b/cmd/migrate/go.mod index 240ddda..7d214ce 100644 --- a/cmd/migrate/go.mod +++ b/cmd/migrate/go.mod @@ -2,17 +2,44 @@ module isp-config-service/migrate go 1.22 -require github.com/txix-open/isp-kit v1.31.2 +replace isp-config-service => ../.. require ( + github.com/jmoiron/sqlx v1.4.0 + github.com/mattn/go-sqlite3 v1.14.22 + github.com/pkg/errors v0.9.1 + github.com/pressly/goose/v3 v3.20.0 + github.com/txix-open/isp-kit v1.31.2 + isp-config-service v0.0.0-00010101000000-000000000000 +) + +require ( + github.com/Masterminds/squirrel v1.5.4 // indirect github.com/iancoleman/strcase v0.3.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect github.com/jackc/pgx/v5 v5.5.5 // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect - github.com/jmoiron/sqlx v1.4.0 // indirect - github.com/pkg/errors v0.9.1 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect + github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect + github.com/mfridman/interpolate v0.0.2 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/rogpeppe/go-internal v1.12.0 // indirect + github.com/sethvargo/go-retry v0.2.4 // indirect + github.com/tidwall/gjson v1.17.1 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.1 // indirect + github.com/txix-open/bellows v1.2.0 // indirect + github.com/txix-open/etp/v3 v3.1.0 // indirect + go.uber.org/multierr v1.11.0 // indirect + go.uber.org/zap v1.27.0 // indirect golang.org/x/crypto v0.22.0 // indirect golang.org/x/sync v0.7.0 // indirect golang.org/x/text v0.14.0 // indirect + gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect + nhooyr.io/websocket v1.8.11 // indirect ) diff --git a/cmd/migrate/go.sum b/cmd/migrate/go.sum new file mode 100644 index 0000000..f8d1a45 --- /dev/null +++ b/cmd/migrate/go.sum @@ -0,0 +1,149 @@ +filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= +filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= +github.com/Masterminds/squirrel v1.5.4 h1:uUcX/aBc8O7Fg9kaISIUsHXdKuqehiXAMQTYX8afzqM= +github.com/Masterminds/squirrel v1.5.4/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10= +github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= +github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= +github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-resty/resty/v2 v2.12.0 h1:rsVL8P90LFvkUYq/V5BTVe203WfRIU4gvcf+yfzJzGA= +github.com/go-resty/resty/v2 v2.12.0/go.mod h1:o0yGPrkS3lOe1+eFajk6kBW8ScXzwU3hD69/gt2yB/0= +github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= +github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hashicorp/golang-lru v1.0.2 h1:dV3g9Z/unq5DpblPpw+Oqcv4dU/1omnb4Ok8iPY6p1c= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= +github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI= +github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA= +github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw= +github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A= +github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= +github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o= +github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw= +github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o= +github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk= +github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6FmdpVm2joNMFikkuWg0EoCKLGUMNw= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY= +github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4= +github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pressly/goose/v3 v3.20.0 h1:uPJdOxF/Ipj7ABVNOAMJXSxwFXZGwMGHNqjC8e61VA0= +github.com/pressly/goose/v3 v3.20.0/go.mod h1:BRfF2GcG4FTG12QfdBVy3q1yveaf4ckL9vWwEcIO3lA= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= +github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= +github.com/sethvargo/go-retry v0.2.4 h1:T+jHEQy/zKJf5s95UkguisicE0zuF9y7+/vgz08Ocec= +github.com/sethvargo/go-retry v0.2.4/go.mod h1:1afjQuvh7s4gflMObvjLPaWgluLLyhA1wmVZ6KLpICw= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U= +github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= +github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/txix-open/bellows v1.2.0 h1:CXv8nQaZtB/micraeRilYyj/gtfv+bqBgP5aPYQgjeY= +github.com/txix-open/bellows v1.2.0/go.mod h1:qbKCy+RTgD30Qpw1fyb3y3jp5Y9mGhLLxgae1l0W92o= +github.com/txix-open/etp/v3 v3.1.0 h1:EQdjm0x2K6Dr6W8oRvs/dSU4rnw+g9o1THahIxLBC3E= +github.com/txix-open/etp/v3 v3.1.0/go.mod h1:aXqeKBdu55VOtmzx+PMlDAcz6eTNBMt3vP3NkiE1qj0= +github.com/txix-open/isp-kit v1.31.2 h1:qt6Om1wF+vpbIz01jQix9L20h9ftwn3/SasgO1RT9T0= +github.com/txix-open/isp-kit v1.31.2/go.mod h1:t6/NHdK5JvpIiQU4+NUjky4jLbo7Bz/4hzpoBxZt64U= +github.com/txix-open/validator/v10 v10.0.0-20240401163703-e83541b40f99 h1:Z0Sqf+U+Sc6hfIXtRCIZlbJprkCjwMFbS4ZbEPQ44To= +github.com/txix-open/validator/v10 v10.0.0-20240401163703-e83541b40f99/go.mod h1:pA0NYmhL0uEkkxj55S+S7MZU4e2QIt46x9XSrzrWc3c= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= +go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= +golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= +golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= +golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= +golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= +golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= +gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 h1:5D53IMaUuA5InSeMu9eJtlQXS2NxAhyWQvkKEgXZhHI= +modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6/go.mod h1:Qz0X07sNOR1jWYCrJMEnbW/X55x206Q7Vt4mz6/wHp4= +modernc.org/libc v1.41.0 h1:g9YAc6BkKlgORsUWj+JwqoB1wU3o4DE3bM3yvA3k+Gk= +modernc.org/libc v1.41.0/go.mod h1:w0eszPsiXoOnoMJgrXjglgLuDy/bt5RR4y3QzUUeodY= +modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4= +modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo= +modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E= +modernc.org/memory v1.7.2/go.mod h1:NO4NVCQy0N7ln+T9ngWqOQfi7ley4vpwvARR+Hjw95E= +modernc.org/sqlite v1.29.6 h1:0lOXGrycJPptfHDuohfYgNqoe4hu+gYuN/pKgY5XjS4= +modernc.org/sqlite v1.29.6/go.mod h1:S02dvcmm7TnTRvGhv8IGYyLnIt7AS2KPaB1F/71p75U= +modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA= +modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0= +modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y= +modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= +nhooyr.io/websocket v1.8.11 h1:f/qXNc2/3DpoSZkHt1DQu6rj4zGC8JmkkLkWss0MgN0= +nhooyr.io/websocket v1.8.11/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c= diff --git a/cmd/migrate/main.go b/cmd/migrate/main.go index a0bd1f8..f012e70 100644 --- a/cmd/migrate/main.go +++ b/cmd/migrate/main.go @@ -1,11 +1,143 @@ package main import ( - "context" + "fmt" + "os" + "time" + "github.com/jmoiron/sqlx" + _ "github.com/mattn/go-sqlite3" + "github.com/pkg/errors" + "github.com/txix-open/isp-kit/app" "github.com/txix-open/isp-kit/db" + "isp-config-service/entity" + "isp-config-service/entity/xtypes" + "isp-config-service/migrate/migration" + "isp-config-service/migrations" + "isp-config-service/repository" ) func main() { - db.Open(context.Background(), "") + if len(os.Args) == 1 { + fmt.Println("usage: migrate postgres://username:password@localhost:5432/database_name?search_path=config_service") + os.Exit(1) + } + app, err := app.New() + if err != nil { + panic(err) + } + logger := app.Logger() + ctx := app.Context() + + pgDb, err := db.Open(ctx, os.Args[1]) + if err != nil { + panic(err) + } + defer pgDb.Close() + + fileName := fmt.Sprintf("db_%d.sqlite", time.Now().Unix()) + dbClient := &db.Client{} + dbClient.DB, err = sqlx.Open("sqlite3", fileName) + dbClient.DB.MapperFunc(db.ToSnakeCase) + if err != nil { + panic(errors.WithMessage(err, "err opening db")) + } + defer dbClient.Close() + + mr := migration.NewRunner(migration.DialectSqlite3, migrations.Migrations, logger) + err = mr.Run(ctx, dbClient.DB.DB) + if err != nil { + panic(errors.WithMessage(err, "err connecting to db")) + } + + moduleRepo := repository.NewModule(dbClient) + configRepo := repository.NewConfig(dbClient) + configSchemaRepo := repository.NewConfigSchema(dbClient) + configHistoryRepo := repository.NewConfigHistory(dbClient) + + modules := make([]Module, 0) + err = pgDb.Select(ctx, &modules, "select * from modules") + if err != nil { + panic(errors.WithMessage(err, "err selecting modules")) + } + for _, module := range modules { + result := entity.Module{ + Id: module.Id, + Name: module.Name, + CreatedAt: xtypes.Time(module.CreatedAt), + } + err := moduleRepo.Upsert(ctx, result) + if err != nil { + panic(errors.WithMessagef(err, "err upserting module: %s", module.Name)) + } + } + logger.Info(ctx, "modules migrated") + + configs := make([]Config, 0) + err = pgDb.Select(ctx, &configs, "select * from configs") + if err != nil { + panic(errors.WithMessage(err, "err selecting configs")) + } + for _, config := range configs { + result := entity.Config{ + Id: config.Id, + Name: config.Name, + ModuleId: config.ModuleId, + Data: config.Data, + Version: config.Version, + Active: xtypes.Bool(config.Active), + AdminId: 0, + CreatedAt: xtypes.Time(config.CreatedAt), + UpdatedAt: xtypes.Time(config.UpdatedAt), + } + err := configRepo.Insert(ctx, result) + if err != nil { + panic(errors.WithMessagef(err, "err inserting config: %s", config.Id)) + } + } + logger.Info(ctx, "configs migrated") + + schemas := make([]ConfigSchema, 0) + err = pgDb.Select(ctx, &schemas, "select * from config_schemas") + if err != nil { + panic(errors.WithMessage(err, "err selecting schemas")) + } + for _, schema := range schemas { + result := entity.ConfigSchema{ + Id: schema.Id, + ModuleId: schema.ModuleId, + Data: schema.Schema, + ModuleVersion: schema.Version, + CreatedAt: xtypes.Time(schema.CreatedAt), + UpdatedAt: xtypes.Time(schema.UpdatedAt), + } + err := configSchemaRepo.Upsert(ctx, result) + if err != nil { + panic(errors.WithMessagef(err, "err upserting config_schema: %s", schema.Id)) + } + } + logger.Info(ctx, "config schemas migrated") + + configHistories := make([]ConfigHistory, 0) + err = pgDb.Select(ctx, &configHistories, "select * from version_config") + if err != nil { + panic(errors.WithMessage(err, "err selecting configs")) + } + for _, history := range configHistories { + result := entity.ConfigHistory{ + Id: history.Id, + ConfigId: history.ConfigId, + Data: history.Data, + Version: history.ConfigVersion, + AdminId: 0, + CreatedAt: xtypes.Time(history.CreatedAt), + } + err := configHistoryRepo.Insert(ctx, result) + if err != nil { + panic(errors.WithMessagef(err, "err inserting config_history: %s", history.Id)) + } + } + logger.Info(ctx, "config history migrated") + + logger.Info(ctx, "done!") } diff --git a/cmd/migrate/migration/runner.go b/cmd/migrate/migration/runner.go new file mode 100644 index 0000000..82bb783 --- /dev/null +++ b/cmd/migrate/migration/runner.go @@ -0,0 +1,86 @@ +package migration + +import ( + "context" + "database/sql" + "fmt" + "io/fs" + "path/filepath" + "strings" + "time" + + "github.com/pkg/errors" + "github.com/pressly/goose/v3" + "github.com/txix-open/isp-kit/log" +) + +const ( + DialectPostgreSQL = goose.DialectPostgres + DialectSqlite3 = goose.DialectSQLite3 + DialectClickhouse = goose.DialectClickHouse +) + +type Runner struct { + dialect goose.Dialect + fs fs.FS + logger log.Logger +} + +func NewRunner( + dialect goose.Dialect, + fs fs.FS, + logger log.Logger, +) Runner { + return Runner{ + dialect: dialect, + fs: fs, + logger: logger, + } +} + +func (r Runner) Run(ctx context.Context, db *sql.DB, gooseOpts ...goose.ProviderOption) error { + ctx = log.ToContext(ctx, log.String("worker", "goose_db_migration")) + + provider, err := goose.NewProvider(r.dialect, db, r.fs, gooseOpts...) + if err != nil { + return errors.WithMessage(err, "get goose provider") + } + + dbVersion, err := provider.GetDBVersion(ctx) + if err != nil { + return errors.WithMessage(err, "get db version") + } + r.logger.Info(ctx, fmt.Sprintf("current db version: %d", dbVersion)) + + migrations, err := provider.Status(ctx) + if err != nil { + return errors.WithMessage(err, "get status migrations") + } + r.logger.Info(ctx, "print migration list") + if len(migrations) == 0 { + r.logger.Info(ctx, "no migrations") + } + for _, migration := range migrations { + appliedAt := "Pending" + if !migration.AppliedAt.IsZero() { + appliedAt = migration.AppliedAt.Format(time.RFC3339) + } + msg := fmt.Sprintf( + "migration: %s %s %s", + filepath.Base(migration.Source.Path), + strings.ToUpper(string(migration.State)), + appliedAt, + ) + r.logger.Info(ctx, msg) + } + + result, err := provider.Up(ctx) + if err != nil { + return errors.WithMessage(err, "apply pending migrations") + } + for _, migrationResult := range result { + r.logger.Info(ctx, fmt.Sprintf("applied migration: %s", migrationResult.String())) + } + + return nil +} diff --git a/migrations/embed.go b/migrations/embed.go new file mode 100644 index 0000000..753aaf1 --- /dev/null +++ b/migrations/embed.go @@ -0,0 +1,10 @@ +package migrations + +import ( + "embed" +) + +var ( + //go:embed * + Migrations embed.FS +) diff --git a/repository/module.go b/repository/module.go index 2fb6323..0bc84f3 100644 --- a/repository/module.go +++ b/repository/module.go @@ -21,25 +21,23 @@ func NewModule(db db.DB) Module { } } -func (r Module) Upsert(ctx context.Context, module entity.Module) (string, error) { +func (r Module) Upsert(ctx context.Context, module entity.Module) error { ctx = sql_metrics.OperationLabelToContext(ctx, "Module.Upsert") query, args, err := squirrel.Insert(Table("module")). Columns("id", "name", "last_connected_at"). Values(module.Id, module.Name, squirrel.Expr("unixepoch()")). Suffix(`on conflict (name) do update set last_connected_at = unixepoch()`). - Suffix("returning id"). ToSql() if err != nil { - return "", errors.WithMessage(err, "build query") + return errors.WithMessage(err, "build query") } - result := make(map[string]string) - err = r.db.SelectRow(ctx, &result, query, args...) + _, err = r.db.Exec(ctx, query, args...) if err != nil { - return "", errors.WithMessagef(err, "select: %s", query) + return errors.WithMessagef(err, "select: %s", query) } - return result["id"], nil + return nil } func (r Module) SetDisconnectedAtNow( diff --git a/service/module/service.go b/service/module/service.go index 722ea61..76527dc 100644 --- a/service/module/service.go +++ b/service/module/service.go @@ -23,7 +23,7 @@ const ( ) type Repo interface { - Upsert(ctx context.Context, module entity.Module) (string, error) + Upsert(ctx context.Context, module entity.Module) error SetDisconnectedAtNow( ctx context.Context, moduleId string, @@ -90,11 +90,12 @@ func NewService( func (s Service) OnConnect(ctx context.Context, conn *etp.Conn, moduleName string) error { s.logger.Info(ctx, "module connected", helpers.LogFields(conn)...) + moduleId := idByKey(moduleName) module := entity.Module{ - Id: uuid.NewString(), + Id: moduleId, Name: moduleName, } - moduleId, err := s.moduleRepo.Upsert(ctx, module) + err := s.moduleRepo.Upsert(ctx, module) if err != nil { return errors.WithMessage(err, "upsert module in store") } @@ -238,10 +239,8 @@ func (s Service) OnModuleConfigSchema( return errors.WithMessage(err, "get active config") } if config == nil { - hash := sha256.Sum256([]byte(moduleId)) - initialConfigId := hex.EncodeToString(hash[:]) initialConfig := entity.Config{ - Id: initialConfigId, + Id: idByKey(moduleId), Name: helpers.ModuleName(conn), ModuleId: moduleId, Data: data.Config, @@ -272,3 +271,8 @@ func (s Service) OnModuleConfigSchema( return nil } + +func idByKey(key string) string { + hash := sha256.Sum256([]byte(key)) + return hex.EncodeToString(hash[:]) +}