diff --git a/multifile/main.sql b/multifile/main.sql index d1facf0..4f85e6b 100644 --- a/multifile/main.sql +++ b/multifile/main.sql @@ -19,8 +19,8 @@ \i functions/update_timestamp.sql -- Include core tables (with their constraints, indexes, and policies) +\i tables/addresses.sql \i tables/users.sql -\i tables/orders.sql -- Include other functions (after tables that they reference) \i functions/get_user_count.sql diff --git a/multifile/procedures/set_default_address.sql b/multifile/procedures/set_default_address.sql new file mode 100644 index 0000000..fce9fc8 --- /dev/null +++ b/multifile/procedures/set_default_address.sql @@ -0,0 +1,38 @@ +CREATE OR REPLACE PROCEDURE set_default_address( + p_user_id integer, + p_address_id integer +) +LANGUAGE plpgsql +AS $$ +BEGIN + -- Check if the address exists and belongs to the user + IF NOT EXISTS ( + SELECT 1 FROM addresses + WHERE id = p_address_id AND user_id = p_user_id + ) THEN + RAISE EXCEPTION 'Address % does not exist or does not belong to user %', + p_address_id, p_user_id; + END IF; + + -- Start transaction + BEGIN + -- Set all addresses for this user to non-default + UPDATE addresses + SET is_default = false + WHERE user_id = p_user_id; + + -- Set the specified address as default + UPDATE addresses + SET is_default = true + WHERE id = p_address_id; + + -- Log the change (optional - remove if you don't need logging) + RAISE NOTICE 'Default address set to % for user %', p_address_id, p_user_id; + + EXCEPTION + WHEN OTHERS THEN + -- Rollback will happen automatically + RAISE EXCEPTION 'Failed to set default address: %', SQLERRM; + END; +END; +$$; \ No newline at end of file diff --git a/multifile/tables/addresses.sql b/multifile/tables/addresses.sql new file mode 100644 index 0000000..f9f7884 --- /dev/null +++ b/multifile/tables/addresses.sql @@ -0,0 +1,10 @@ +CREATE TABLE IF NOT EXISTS addresses ( + id integer PRIMARY KEY, + user_id integer NOT NULL REFERENCES users(id), + street_address text NOT NULL, + city text NOT NULL, + state_province text, + postal_code text, + country text NOT NULL DEFAULT 'USA', + is_default boolean DEFAULT false +); \ No newline at end of file diff --git a/multifile/tables/orders.sql b/multifile/tables/orders.sql deleted file mode 100644 index f0a3b0a..0000000 --- a/multifile/tables/orders.sql +++ /dev/null @@ -1,38 +0,0 @@ --- --- Name: orders; Type: TABLE; Schema: -; Owner: - --- - -CREATE TABLE IF NOT EXISTS orders ( - id integer PRIMARY KEY, - user_id integer NOT NULL REFERENCES users(id), - status text DEFAULT 'pending' NOT NULL CHECK (status IN ('pending', 'completed')), - amount numeric(10,2) DEFAULT 0.00 -); - -COMMENT ON TABLE orders IS 'Customer orders'; - -COMMENT ON COLUMN orders.user_id IS 'Reference to user'; - --- --- Name: idx_orders_status; Type: INDEX; Schema: -; Owner: - --- - -CREATE INDEX IF NOT EXISTS idx_orders_status ON orders (status); - --- --- Name: idx_orders_user_id; Type: INDEX; Schema: -; Owner: - --- - -CREATE INDEX IF NOT EXISTS idx_orders_user_id ON orders (user_id); - --- --- Name: orders; Type: RLS; Schema: -; Owner: - --- - -ALTER TABLE orders ENABLE ROW LEVEL SECURITY; - --- --- Name: orders_policy; Type: POLICY; Schema: -; Owner: - --- - -CREATE POLICY orders_policy ON orders TO PUBLIC USING (user_id = 1); \ No newline at end of file diff --git a/multifile/tables/users.sql b/multifile/tables/users.sql index 94aa3a8..eb5945d 100644 --- a/multifile/tables/users.sql +++ b/multifile/tables/users.sql @@ -5,7 +5,8 @@ CREATE TABLE IF NOT EXISTS users ( id integer PRIMARY KEY, email text NOT NULL CHECK (email LIKE '%@%'), - name text NOT NULL + name text NOT NULL, + city text NOT NULL ); COMMENT ON TABLE users IS 'User accounts';