11-- https://dba.stackexchange.com/questions/68266/what-is-the-best-way-to-store-an-email-address-in-postgresql/165923#165923
22
3- CREATE DOMAIN email
4- CHECK ( value ~ ' ^[\w .!#$%&' ' *+/=?^`{|}~-]+@[:alnum:](?:[[:alnum:]-]{0,61}[:alnum:])?(?:\. [:alnum:](?:[[:alnum:]-]{0,61}[:alnum:])?)*$' );
3+ CREATE DOMAIN email TEXT CHECK ( value ~ ' ^[\w .!#$%&' ' *+/=?^`{|}~-]+@[:alnum:](?:[[:alnum:]-]{0,61}[:alnum:])?(?:\. [:alnum:](?:[[:alnum:]-]{0,61}[:alnum:])?)*$' );
54 -- CHECK ( value ~ '^[a-zA-Z0-9.!#$%&''*+/=?^_`{|}~-]+@[[a-zA-Z0-9]](?:[a-zA-Z0-9-]{0,61}[[a-zA-Z0-9]])?(?:\.[[a-zA-Z0-9]](?:[a-zA-Z0-9-]{0,61}[[a-zA-Z0-9]])?)*$' );
65
6+ CREATE DOMAIN phone_number TEXT CHECK ( value ~ ' ^[0-9\(\) \- ]+$' )
77
88-- https://dba.stackexchange.com/questions/164796/how-do-i-store-phone-numbers-in-postgresql
99
10- CREATE EXTENSION pg_libphonenumber;
10+ -- CREATE EXTENSION pg_libphonenumber;
11+
12+ CREATE TABLE IF NOT EXISTS drivers(
13+ id SERIAL PRIMARY KEY ,
14+ first_name TEXT NOT NULL ,
15+ last_name TEXT NOT NULL ,
16+ phone phone_number NOT NULL ,
17+ email email NOT NULL
18+ );
19+
20+ CREATE TABLE IF NOT EXISTS routes(
21+ id SERIAL PRIMARY KEY ,
22+ created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP ,
23+
24+ );
25+
26+ CREATE TABLE IF NOT EXISTS payrolls(
27+ id SERIAL PRIMARY KEY ,
28+ created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP ,
29+
30+ );
31+
32+ CREATE TYPE statement_status AS ENUM (' mailed' , ' received' , ' payed' );
1133
1234CREATE TYPE statement_status AS ENUM (' immediate' , ' daily' , ' weekly' , ' biweekly' , ' bimonthly' , ' monthly' );
1335
1436CREATE TYPE account_status AS ENUM (' active' , ' suspended' );
1537
38+ CREATE TABLE IF NOT EXISTS locations (
39+ id SERIAL PRIMARY KEY ,
40+ name TEXT ,
41+ address TEXT NOT NULL ,
42+ verified boolean NOT NULL DEFAULT false,
43+ latitude float NOT NULL ,
44+ longitude float NOT NULL
45+ );
46+
1647CREATE TABLE IF NOT EXISTS accounts (
1748 id SERIAL PRIMARY KEY ,
1849 name TEXT NOT NULL ,
@@ -22,17 +53,14 @@ CREATE TABLE IF NOT EXISTS accounts (
2253 discount SMALLINT NOT NULL DEFAULT 0 CHECK (discount <= 100 ),
2354 created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP ,
2455 update_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP ,
25- balance ' money ' -- update by trigger on statements
56+ balance NUMERIC ( 5 , 2 ) NOT NULL DEFAULT 0 , -- update by trigger on statements WIP money
2657
2758 status account_status NOT NULL ,
28- frequency INTEGER NOT NULL DEFAULT 0
59+ frequency INTEGER NOT NULL DEFAULT 0
2960
30- -- t.integer "users_count", default: 0
31- -- t.string "ref_pattern"
61+ );
3262
33- )
34-
35- CREATE TABLE users (
63+ CREATE IF NOT EXISTS TABLE users(
3664 id SERIAL PRIMARY KEY ,
3765 first_name TEXT NOT NULL ,
3866 last_name TEXT NOT NULL ,
@@ -63,67 +91,50 @@ t.datetime "created_at"
6391t.datetime "updated_at"
6492
6593****/
66- )
67-
68- CREATE TABLE drivers (
69- id SERIAL PRIMARY KEY
70- first_name TEXT NOT NULL ,
71- last_name TEXT NOT NULL ,
72- phone phone_number NOT NULL ,
73- email email NOT NULL
74- )
75-
76- CREATE TABLE routes (
77- id SERIAL PRIMARY KEY
78- )
79-
80- CREATE TABLE payroll (
81- id SERIAL PRIMARY KEY
82- )
83-
84- CREATE TYPE statement_status AS ENUM (' mailed' , ' received' , ' payed' );
94+ );
8595
8696CREATE TABLE IF NOT EXISTS statements (
8797 id SERIAL PRIMARY KEY ,
88-
89- account_id INTEGER REFERENCES accounts(id) NOT NULL ,
90- amount ' money' .
98+ account_id INTEGER REFERENCES accounts(id),
99+ amount NUMERIC (5 ,2 ) NOT NULL DEFAULT 0 , -- WIP money
91100 status statement_status NOT NULL , -- seems redundant
92101 create_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP ,
93102 date_due TIMESTAMPTZ NOT NULL ,
94- amount_due ' money' NOT NULL -- trigger on payment ??
95103
96- )
104+ );
97105
98- CREATE TRIGGER AFTER INSERT ON statements FOR EACH ROW EXECUTE PROCEDURE adjust_balance(account_id, amount)
99-
100- CREATE FUNCTION adjust_balance (accountno integer , amount numeric ) $$
106+ CREATE OR REPLACE FUNCTION adjust_balance ()
107+ RETURNS trigger AS $$
108+ BEGIN
101109 UPDATE accounts
102- SET balance = balance + amount
103- WHERE id = adjust_balance .accountno
104- $$ LANGUAGE SQL;
110+ SET balance = balance + NEW .amount
111+ WHERE id = NEW .account_id
112+ END
113+ $$ LANGUAGE plpgsql;
114+
115+ CREATE TRIGGER foo AFTER INSERT ON statements FOR EACH ROW EXECUTE PROCEDURE adjust_balance()
105116
106117CREATE TABLE IF NOT EXISTS payments (
107- account_id INTEGER REFERENCES accounts(id) NOT NULL ,
108- amount ' money ' NOT NULL ,
109- balance ' money ' NOT NULL ,
118+ id SERIAL PRIMARY KEY ,
119+ account_id INTEGER REFERENCES accounts(id) NOT NULL ,
120+ amount NUMERIC ( 5 , 2 ) NOT NULL DEFAULT 0 , -- WIP money
110121 created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
111122
112123-- t.string "reference"
113124-- t.string "payment_type", default: "bogus", null: false
114125-- t.string "safe_number"
115126-- t.string "payer"
116127-- t.text "params", default: "", null: false
117- )
128+ );
118129CREATE TRIGGER AFTER INSERT ON payment
119130 FOR EACH ROW
120- EXECUTE PROCEDURE adjust_balance(account_id, - amount)
131+ EXECUTE PROCEDURE adjust_balance();
121132
122133CREATE TABLE IF NOT EXISTS allocations (
123- a_amount ' money ' NOT NULL , -- what type of check ????
134+ a_amount NUMERIC ( 5 , 2 ) NOT NULL DEFAULT 0 , -- what type of check ???? WIP money
124135 statement_id INTEGER REFERENCES statements(id) NOT NULL ,
125- payment_id INTEGER REFERENCES payments(id) NOT NULL ,
126- )
136+ payment_id INTEGER REFERENCES payments(id) NOT NULL
137+ );
127138
128139CREATE TYPE job_status AS ENUM (' submitted' , ' dispatched' , ' delivered' ,' invoiced' , ' canceled' );
129140
@@ -139,7 +150,7 @@ CREATE TABLE IF NOT EXISTS jobs (
139150 notes TEXT ,
140151
141152 attn TEXT ,
142- attnPhone phone_number,
153+ -- attnPhone phone_number,
143154 create_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP ,
144155
145156 state job_status NOT NULL DEFAULT ' submitted'
@@ -148,13 +159,13 @@ CREATE TABLE IF NOT EXISTS jobs (
148159 * if we allow no account then capture
149160 * email, phone, cc authorization
150161 */
151- )
162+ );
152163
153164CREATE TABLE IF NOT EXISTS charge_types(
154165 id SERIAL PRIMARY KEY ,
155166 name TEXT ,
156- amount INTEGER
157- )
167+ amount NUMERIC ( 5 , 2 ) NOT NULL DEFAULT 0 -- WIP money
168+ );
158169
159170CREATE TABLE IF NOT EXISTS charges (
160171 id SERIAL PRIMARY KEY ,
@@ -164,17 +175,8 @@ CREATE TABLE IF NOT EXISTS charges (
164175 job_id INTEGER REFERENCES jobs(id) NOT NULL ,
165176 rate SMALLINT NOT NULL DEFAULT 60 CHECK (rate BETWEEN 0 AND 100 ), -- maybe by driver
166177 payroll_id INTEGER REFERENCES payrolls(id),
167- kind INTEGER REFERENCES charge_type(id)
168- )
169-
170- CREATE TABLE IF NOT EXISTS locations (
171- id SERIAL PRIMARY KEY ,
172- name TEXT ,
173- address TEXT NOT NULL ,
174- verified boolean NOT NULL DEFAULT false,
175- latitude float NOT NULL ,
176- longitude float NOT NULL
177- )
178+ kind INTEGER REFERENCES charge_types(id)
179+ );
178180
179181CREATE TABLE IF NOT EXISTS stops (
180182 id SERIAL PRIMARY KEY ,
@@ -185,33 +187,14 @@ CREATE TABLE IF NOT EXISTS stops (
185187 delivery_due TIMESTAMPTZ NOT NULL ,
186188 at TIMESTAMPTZ ,
187189 position integer
188- )
190+ );
189191CREATE TRIGGER
190192 AFTER UPDATE ON stops
191193 FOR EACH ROW
192194 WHEN OLD .at is NULL AND NEW .at IS NOT NULL
193195 EXECUTE PROCEDURE foo
194196
195-
196-
197-
198-
199-
200-
201-
202-
203197/*
204- create_table "invitations", force: :cascade do |t|
205- t.string "email", null: false
206- t.string "key"
207- t.string "status", default: "pending", null: false
208- t.integer "company_id", null: false
209- t.integer "requestor_id", null: false
210- t.datetime "expires", null: false
211- t.datetime "created_at"
212- t.datetime "updated_at"
213- end
214-
215198
216199 create_table "p_stops", force: :cascade do |t|
217200 t.string "name", limit: 100
0 commit comments