Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brianna/step-one #15

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions schema/deploy/insert_todo.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Deploy todo_app:insert_todo to pg
-- requires: todos
-- requires: todo_appschema

BEGIN;

create or replace function todo_app.insert_todo(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In most cases you won't need to create functions to insert your data. postgraphile will automatically generate graphql mutations to insert, update and delete data.

task text,
completed boolean
) returns void language sql security definer as $$

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defining a function with security definer is to be avoided if possible. A security definer function will be executed with the privileges of the user that creates it.

insert into todo_app.todos(task, completed) values($1, $2);
$$;

COMMIT;
11 changes: 11 additions & 0 deletions schema/deploy/seed_todo.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Deploy todo_app:seed_todo to pg
-- requires: insert_todo
-- requires: todos
-- requires: todo_appschema

BEGIN;

select todo_app.insert_todo('task not done', false);
select todo_app.insert_todo('task done', true);

COMMIT;
Comment on lines +6 to +11

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for seed data, we've been following the following pattern in our project:

  • if the data is actually "seed" data, i.e. the initial data which can later be modified by our users, then a sqitch migration, like this is good
  • if the data is hardcoded data, which users are not expected to change, but developers may want to change between releases, we use a different set of sql scripts containing idempotent changes (example from the ciip repo).

7 changes: 7 additions & 0 deletions schema/deploy/todo_appschema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Deploy todo_app:todo_appschema to pg

BEGIN;

create schema todo_app;

COMMIT;
16 changes: 16 additions & 0 deletions schema/deploy/todos.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Deploy todo_app:todos to pg
-- requires: todo_appschema

BEGIN;

set client_min_messages = 'warning';

create table todo_app.todos (
id integer primary key generated always as identity,
task text not null,
completed boolean not null default false,
date_created timestamptz not null default now(),
date_updated timestamptz not null default now()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally forgot to include default now() and to get it in, I had to revert and then redeploy. Is that the right way to do it?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In your development environment, yes. For some changes the way might even be to drop and recreate your db.

If this happens in a prod environment, you will have to create a new change which alters the column. The changes that are already deployed should be immutable. You can't revert and redeploy there otherwise you're dropping the tables and loosing your data

);

COMMIT;
7 changes: 7 additions & 0 deletions schema/revert/insert_todo.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert todo_app:insert_todo from pg

BEGIN;

drop function todo_app.insert_todo(text, boolean);

COMMIT;
7 changes: 7 additions & 0 deletions schema/revert/seed_todo.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert todo_app:seed_todo from pg

BEGIN;

delete from todo_app.todos;

COMMIT;
7 changes: 7 additions & 0 deletions schema/revert/todo_appschema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert todo_app:todo_appschema from pg

BEGIN;

drop schema todo_app;

COMMIT;
7 changes: 7 additions & 0 deletions schema/revert/todos.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert todo_app:todos from pg

BEGIN;

drop table todo_app.todos;

COMMIT;
11 changes: 11 additions & 0 deletions schema/sqitch.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[core]
engine = pg
# plan_file = sqitch.plan
# top_dir = .
[engine "pg"]
target = todo_app
[target "todo_app"]
uri = db:pg://localhost/todo_app
[user]
name = Brianna Cerkiewicz
email = [email protected]
7 changes: 7 additions & 0 deletions schema/sqitch.plan
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%syntax-version=1.0.0
%project=todo_app

todo_appschema 2022-02-23T22:43:21Z Brianna Cerkiewicz <briannacerkiewicz@pop-os> # Add schema for all todo objects.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remember to configure sqitch to have your work email in your profile

todos [todo_appschema] 2022-02-23T22:48:12Z Brianna Cerkiewicz <briannacerkiewicz@pop-os> # Create table to hold todos.
insert_todo [todos todo_appschema] 2022-02-23T23:18:41Z Brianna Cerkiewicz <briannacerkiewicz@pop-os> # Creates a function to insert a todo.
seed_todo [insert_todo todos todo_appschema] 2022-02-24T00:23:11Z Brianna Cerkiewicz <briannacerkiewicz@pop-os> # Creates seed todos.
7 changes: 7 additions & 0 deletions schema/verify/insert_todo.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify todo_app:insert_todo on pg

BEGIN;

select has_function_privilege('todo_app.insert_todo(text, boolean)', 'execute');

ROLLBACK;
14 changes: 14 additions & 0 deletions schema/verify/seed_todo.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Verify todo_app:seed_todo on pg


BEGIN;
do $$
declare
count integer;

BEGIN
count := (select count(id) from todo_app.todos);
assert count = 2;
end $$

ROLLBACK;
7 changes: 7 additions & 0 deletions schema/verify/todo_appschema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify todo_app:todo_appschema on pg

BEGIN;

select pg_catalog.has_schema_privilege('todo_app', 'usage');

ROLLBACK;
9 changes: 9 additions & 0 deletions schema/verify/todos.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Verify todo_app:todos on pg

BEGIN;

select id, task, completed, date_created, date_updated
FROM todo_app.todos
WHERE FALSE;

ROLLBACK;