Skip to content

Commit 2084066

Browse files
authored
Use database triggers to update 'modified_at' fields (#257)
* Use database triggers to update 'modified_at' fields * Fixed function syntax * Update recipe modified when changing tags * Removed accidental change from previous PR * Added first github actions workflow * Add full build * Fix OoO * Oops * Include CC tools * WIP: More build workflow * Push docker images * Enable experimental CLI * Install go deps in install target * Let's do it the old way * Set names * Removed extraneous var * Add go linting * Publish release artifacts * Bring in Sonar Scan * Remove bangs * Must be master apparently * Fix up sonar props
1 parent b54ac18 commit 2084066

File tree

19 files changed

+221
-98
lines changed

19 files changed

+221
-98
lines changed

.github/workflows/ci.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: Continuous Integration
2+
on:
3+
push:
4+
branches: [master]
5+
pull_request:
6+
branches: [master]
7+
release:
8+
types: [published]
9+
jobs:
10+
build:
11+
name: Build
12+
runs-on: ubuntu-20.04
13+
steps:
14+
- uses: actions/checkout@v2
15+
- uses: actions/setup-node@v1
16+
with:
17+
node-version: '12'
18+
- uses: actions/setup-go@v2
19+
with:
20+
go-version: '^1.15.0'
21+
- uses: docker/setup-buildx-action@v1
22+
with:
23+
install: true
24+
driver: docker
25+
- uses: docker/login-action@v1
26+
with:
27+
username: ${{ secrets.DOCKERHUB_USERNAME }}
28+
password: ${{ secrets.DOCKERHUB_TOKEN }}
29+
- run: sudo apt install zip gcc-arm-linux-gnueabihf libc6-dev-armhf-cross gcc-mingw-w64-x86-64
30+
- run: make install
31+
- uses: sonarsource/sonarcloud-github-action@master
32+
env:
33+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
35+
- run: make lint
36+
- run: make build docker
37+
- run: make docker-publish
38+
if: startswith(github.ref, 'refs/heads/master')
39+
env:
40+
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
41+
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
42+
- run: make docker-publish DOCKER_TAG=dev
43+
if: startswith(github.ref, 'refs/pull/')
44+
env:
45+
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
46+
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
47+
- run: make docker-publish DOCKER_TAG=$(echo $GITHUB_REF | sed -e "/^refs\/tags\///")
48+
if: startswith(github.ref, 'refs/tags/')
49+
env:
50+
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
51+
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
52+
- run: make archive
53+
if: startsWith(github.ref, 'refs/tags/')
54+
- uses: softprops/action-gh-release@v1
55+
if: startsWith(github.ref, 'refs/tags/')
56+
with:
57+
files: |
58+
build/gomp-linux-amd64.tar.gz
59+
build/gomp-linux-armhf.tar.gz
60+
build/gomp-windows-amd64.zip
61+
env:
62+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.nvmrc

Lines changed: 0 additions & 1 deletion
This file was deleted.

.travis.yml

Lines changed: 0 additions & 80 deletions
This file was deleted.

.vscode/launch.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"mode": "debug",
1212
"program": "${workspaceFolder}",
1313
"env": {
14-
"GOMP_PORT": 12345,
1514
"GOMP_IS_DEVELOPMENT": 1,
1615
"GOMP_BASE_ASSETS_PATH": "static/build/default"
1716
}

Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ reinstall: uninstall install
1919

2020
.PHONY: install
2121
install:
22+
go install
2223
cd static && npm install --silent
2324

2425
.PHONY: uninstall
@@ -27,6 +28,7 @@ uninstall:
2728

2829
.PHONY: lint
2930
lint:
31+
go vet ./...
3032
cd static && npm run lint
3133

3234
.PHONY: build
@@ -93,6 +95,21 @@ docker-linux-armhf: build-linux-armhf
9395
.PHONY: docker
9496
docker: docker-linux-amd64 docker-linux-armhf
9597

98+
.PHONY: docker-publish
99+
ifndef DOCKER_TAG
100+
docker-publish:
101+
docker push cwmr/gomp:amd64
102+
docker push cwmr/gomp:arm
103+
docker run --rm mplatform/manifest-tool --username ${DOCKERHUB_USERNAME} --password ${DOCKERHUB_TOKEN} push from-args --platforms linux/amd64,linux/arm --template cwmr/gomp:ARCH --target cwmr/gomp:latest
104+
else
105+
docker-publish:
106+
docker tag cwmr/gomp:amd64 cwmr/gomp:$(DOCKER_TAG)-amd64
107+
docker tag cwmr/gomp:arm cwmr/gomp:$(DOCKER_TAG)-arm
108+
docker push cwmr/gomp:$(DOCKER_TAG)-amd64
109+
docker push cwmr/gomp:$(DOCKER_TAG)-arm
110+
docker run --rm mplatform/manifest-tool --username ${DOCKERHUB_USERNAME} --password ${DOCKERHUB_TOKEN} push from-args --platforms linux/amd64,linux/arm --template cwmr/gomp:$(DOCKER_TAG)-ARCH --target cwmr/gomp:$(DOCKER_TAG)
111+
endif
112+
96113
.PHONY: archive
97114
archive:
98115
rm -f $(BUILD_DIR)/gomp-linux-amd64.tar.gz
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
BEGIN;
2+
3+
DROP TRIGGER on_recipe_update ON recipe;
4+
DROP FUNCTION on_recipe_update();
5+
6+
DROP TRIGGER on_recipe_tag_insert ON recipe_tag;
7+
DROP FUNCTION on_recipe_tag_insert();
8+
9+
DROP TRIGGER on_recipe_tag_delete ON recipe_tag;
10+
DROP FUNCTION on_recipe_tag_delete();
11+
12+
DROP TRIGGER on_recipe_note_update ON recipe_note;
13+
DROP FUNCTION on_recipe_note_update();
14+
15+
DROP TRIGGER on_recipe_image_update ON recipe_image;
16+
DROP FUNCTION on_recipe_image_update();
17+
18+
COMMIT;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
BEGIN;
2+
3+
-- recipe
4+
CREATE FUNCTION on_recipe_update() RETURNS TRIGGER AS $$
5+
BEGIN
6+
UPDATE recipe SET modified_at = CURRENT_TIMESTAMP WHERE id = NEW.id;
7+
8+
RETURN NEW;
9+
END;
10+
$$ LANGUAGE plpgsql;
11+
12+
CREATE TRIGGER on_recipe_update
13+
AFTER UPDATE ON recipe
14+
FOR EACH ROW
15+
WHEN (OLD.* IS DISTINCT FROM NEW.*)
16+
EXECUTE FUNCTION on_recipe_update();
17+
18+
CREATE FUNCTION on_recipe_tag_insert() RETURNS TRIGGER AS $$
19+
BEGIN
20+
UPDATE recipe SET modified_at = CURRENT_TIMESTAMP WHERE id = NEW.recipe_id;
21+
22+
RETURN NEW;
23+
END;
24+
$$ LANGUAGE plpgsql;
25+
26+
CREATE TRIGGER on_recipe_tag_insert
27+
AFTER INSERT ON recipe_tag
28+
FOR EACH ROW
29+
EXECUTE FUNCTION on_recipe_tag_insert();
30+
31+
CREATE FUNCTION on_recipe_tag_delete() RETURNS TRIGGER AS $$
32+
BEGIN
33+
UPDATE recipe SET modified_at = CURRENT_TIMESTAMP WHERE id = OLD.recipe_id;
34+
35+
RETURN OLD;
36+
END;
37+
$$ LANGUAGE plpgsql;
38+
39+
CREATE TRIGGER on_recipe_tag_delete
40+
AFTER DELETE ON recipe_tag
41+
FOR EACH ROW
42+
EXECUTE FUNCTION on_recipe_tag_delete();
43+
44+
-- recipe_note
45+
CREATE FUNCTION on_recipe_note_update() RETURNS TRIGGER AS $$
46+
BEGIN
47+
UPDATE recipe_note SET modified_at = CURRENT_TIMESTAMP WHERE id = NEW.id;
48+
49+
RETURN NEW;
50+
END;
51+
$$ LANGUAGE plpgsql;
52+
53+
CREATE TRIGGER on_recipe_note_update
54+
AFTER UPDATE ON recipe_note
55+
FOR EACH ROW
56+
WHEN (OLD.* IS DISTINCT FROM NEW.*)
57+
EXECUTE FUNCTION on_recipe_note_update();
58+
59+
-- recipe_image
60+
CREATE FUNCTION on_recipe_image_update() RETURNS TRIGGER AS $$
61+
BEGIN
62+
UPDATE recipe_image SET modified_at = CURRENT_TIMESTAMP WHERE id = NEW.id;
63+
64+
RETURN NEW;
65+
END;
66+
$$ LANGUAGE plpgsql;
67+
68+
CREATE TRIGGER on_recipe_image_update
69+
AFTER UPDATE ON recipe_image
70+
FOR EACH ROW
71+
WHEN (OLD.* IS DISTINCT FROM NEW.*)
72+
EXECUTE FUNCTION on_recipe_image_update();
73+
74+
COMMIT;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DROP TRIGGER on_recipe_update;
2+
DROP TRIGGER on_recipe_tag_insert;
3+
DROP TRIGGER on_recipe_tag_delete;
4+
DROP TRIGGER on_recipe_note_update;
5+
DROP TRIGGER on_recipe_image_update;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
-- recipe
2+
CREATE TRIGGER on_recipe_update
3+
AFTER UPDATE ON recipe
4+
BEGIN
5+
UPDATE recipe SET modified_at = CURRENT_TIMESTAMP WHERE id = NEW.id;
6+
END;
7+
8+
CREATE TRIGGER on_recipe_tag_insert
9+
AFTER INSERT ON recipe_tag
10+
BEGIN
11+
UPDATE recipe SET modified_at = CURRENT_TIMESTAMP WHERE id = NEW.recipe_id;
12+
END;
13+
14+
CREATE TRIGGER on_recipe_tag_delete
15+
AFTER DELETE ON recipe_tag
16+
BEGIN
17+
UPDATE recipe SET modified_at = CURRENT_TIMESTAMP WHERE id = OLD.recipe_id;
18+
END;
19+
20+
-- recipe_note
21+
CREATE TRIGGER on_recipe_note_update
22+
AFTER UPDATE ON recipe_note
23+
BEGIN
24+
UPDATE recipe_note SET modified_at = CURRENT_TIMESTAMP WHERE id = NEW.id;
25+
END;
26+
27+
-- recipe_image
28+
CREATE TRIGGER on_recipe_image_update
29+
AFTER UPDATE ON recipe_image
30+
BEGIN
31+
UPDATE recipe_image SET modified_at = CURRENT_TIMESTAMP WHERE id = NEW.id;
32+
END;

db/postgres/image.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type recipeImageDriver struct {
1414

1515
func newRecipeImageDriver(driver *driver) *recipeImageDriver {
1616
return &recipeImageDriver{
17-
RecipeImageDriver: &sqlcommon.RecipeImageDriver{driver.Driver},
17+
RecipeImageDriver: &sqlcommon.RecipeImageDriver{Driver: driver.Driver},
1818
}
1919
}
2020

db/postgres/note.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type noteDriver struct {
1212

1313
func newNoteDriver(driver *driver) *noteDriver {
1414
return &noteDriver{
15-
NoteDriver: &sqlcommon.NoteDriver{driver.Driver},
15+
NoteDriver: &sqlcommon.NoteDriver{Driver: driver.Driver},
1616
}
1717
}
1818

db/postgres/recipe.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type recipeDriver struct {
1919
func newRecipeDriver(driver *driver) *recipeDriver {
2020
return &recipeDriver{
2121
driver: driver,
22-
RecipeDriver: &sqlcommon.RecipeDriver{driver.Driver},
22+
RecipeDriver: &sqlcommon.RecipeDriver{Driver: driver.Driver},
2323
}
2424
}
2525

@@ -92,7 +92,7 @@ func (d recipeDriver) Update(recipe *models.Recipe) error {
9292
func (d recipeDriver) UpdateTx(recipe *models.Recipe, tx *sqlx.Tx) error {
9393
_, err := tx.Exec(
9494
"UPDATE recipe "+
95-
"SET name = $1, serving_size = $2, nutrition_info = $3, ingredients = $4, directions = $5, source_url = $6, modified_at = CURRENT_TIMESTAMP "+
95+
"SET name = $1, serving_size = $2, nutrition_info = $3, ingredients = $4, directions = $5, source_url = $6 "+
9696
"WHERE id = $7",
9797
recipe.Name, recipe.ServingSize, recipe.NutritionInfo, recipe.Ingredients, recipe.Directions, recipe.SourceURL, recipe.ID)
9898
if err != nil {

db/postgres/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type userDriver struct {
1212

1313
func newUserDriver(driver *driver) *userDriver {
1414
return &userDriver{
15-
UserDriver: &sqlcommon.UserDriver{driver.Driver},
15+
UserDriver: &sqlcommon.UserDriver{Driver: driver.Driver},
1616
}
1717
}
1818

db/sqlcommon/note.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ func (d NoteDriver) Update(note *models.Note) error {
1616
}
1717

1818
func (d NoteDriver) UpdateTx(note *models.Note, tx *sqlx.Tx) error {
19-
_, err := tx.Exec("UPDATE recipe_note SET note = $1, modified_at = CURRENT_TIMESTAMP "+
20-
"WHERE ID = $2 AND recipe_id = $3",
19+
_, err := tx.Exec("UPDATE recipe_note SET note = $1 WHERE ID = $2 AND recipe_id = $3",
2120
note.Note, note.ID, note.RecipeID)
2221
return err
2322
}

db/sqlite3/image.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type recipeImageDriver struct {
1414

1515
func newRecipeImageDriver(driver *driver) *recipeImageDriver {
1616
return &recipeImageDriver{
17-
RecipeImageDriver: &sqlcommon.RecipeImageDriver{driver.Driver},
17+
RecipeImageDriver: &sqlcommon.RecipeImageDriver{Driver: driver.Driver},
1818
}
1919
}
2020

db/sqlite3/note.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type noteDriver struct {
1212

1313
func newNoteDriver(driver *driver) *noteDriver {
1414
return &noteDriver{
15-
NoteDriver: &sqlcommon.NoteDriver{driver.Driver},
15+
NoteDriver: &sqlcommon.NoteDriver{Driver: driver.Driver},
1616
}
1717
}
1818

0 commit comments

Comments
 (0)