diff --git a/backend/asset_assistant/sql/03_user.sql b/backend/asset_assistant/sql/03_user.sql new file mode 100644 index 0000000..ada096e --- /dev/null +++ b/backend/asset_assistant/sql/03_user.sql @@ -0,0 +1,97 @@ +-- 切换到目标数据库 +\c postgres; + +DO $$ +BEGIN + IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'user') THEN + CREATE TABLE "user" ( -- user是关键字,用双引号包裹 + id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL, + deleted BOOLEAN NOT NULL DEFAULT FALSE, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + CREATE TRIGGER update_user_updated_at + BEFORE UPDATE ON "user" + FOR EACH ROW + EXECUTE FUNCTION update_data_modified_column(); + + RAISE NOTICE 'created user table and trigger'; + ELSE + RAISE NOTICE 'user table already exists'; + END IF; + + IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'user_account') THEN + CREATE TABLE user_account ( + id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL, + user_id UUID NOT NULL, + account VARCHAR NOT NULL, + deleted BOOLEAN NOT NULL DEFAULT FALSE, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + CREATE TRIGGER update_user_account_updated_at + BEFORE UPDATE ON "account" + FOR EACH ROW + EXECUTE FUNCTION update_data_modified_column(); + + RAISE NOTICE 'created user_account table and trigger'; + ELSE + RAISE NOTICE 'user_account table already exists'; + END IF; + + IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'user_password') THEN + CREATE TABLE user_password ( + id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL, + user_id UUID NOT NULL, + password VARCHAR NOT NULL, + deleted BOOLEAN NOT NULL DEFAULT FALSE, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + CREATE TRIGGER update_user_password_updated_at + BEFORE UPDATE ON "password" + FOR EACH ROW + EXECUTE FUNCTION update_data_modified_column(); + + RAISE NOTICE 'created user_password table and trigger'; + ELSE + RAISE NOTICE 'user_password table already exists'; + END IF; +END $$; + +DO $$ +DECLARE + view_exists BOOLEAN; +BEGIN + -- 检查视图是否已存在 + SELECT EXISTS ( + SELECT 1 FROM information_schema.views + WHERE table_name = 'user_info_view' + ) INTO view_exists; + + -- 创建或更新视图 + CREATE OR REPLACE VIEW user_info_view AS + SELECT + u.id AS user_id, + ua.account AS user_account, + up.password AS user_password, + u.deleted AS deleted + FROM + "user" u + JOIN + user_account ua ON u.id = ua.user_id + JOIN + user_password up ON u.id = up.user_id + WHERE + u.deleted = FALSE; + + -- 根据视图是否已存在输出不同提示 + IF view_exists THEN + RAISE NOTICE '视图 user_info_view 已更新'; + ELSE + RAISE NOTICE '视图 user_info_view 已创建'; + END IF; +EXCEPTION + WHEN OTHERS THEN + RAISE NOTICE '处理视图时发生错误: %', SQLERRM; +END $$; diff --git a/backend/asset_assistant/sql/04_country.sql b/backend/asset_assistant/sql/04_country.sql new file mode 100644 index 0000000..5b26630 --- /dev/null +++ b/backend/asset_assistant/sql/04_country.sql @@ -0,0 +1,97 @@ +-- 切换到目标数据库 +\c postgres; + +DO $$ +BEGIN + IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'country') THEN + CREATE TABLE country ( + id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL, + deleted BOOLEAN NOT NULL DEFAULT FALSE, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + CREATE TRIGGER update_country_updated_at + BEFORE UPDATE ON "country" + FOR EACH ROW + EXECUTE FUNCTION update_data_modified_column(); + + RAISE NOTICE 'created country table and trigger'; + ELSE + RAISE NOTICE 'country table already exists'; + END IF; + + IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'country_name') THEN + CREATE TABLE country_ ( + id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL, + country_id UUID NOT NULL, + name VARCHAR NOT NULL, + deleted BOOLEAN NOT NULL DEFAULT FALSE, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + CREATE TRIGGER update_name_updated_at + BEFORE UPDATE ON "name" + FOR EACH ROW + EXECUTE FUNCTION update_data_modified_column(); + + RAISE NOTICE 'created country_name table and trigger'; + ELSE + RAISE NOTICE 'country_name table already exists'; + END IF; + + IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'country_code') THEN + CREATE TABLE country_code ( + id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL, + country_id UUID NOT NULL, + code VARCHAR NOT NULL, + deleted BOOLEAN NOT NULL DEFAULT FALSE, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + CREATE TRIGGER update_code_updated_at + BEFORE UPDATE ON "code" + FOR EACH ROW + EXECUTE FUNCTION update_data_modified_column(); + + RAISE NOTICE 'created country_code table and trigger'; + ELSE + RAISE NOTICE 'country_code table already exists'; + END IF; +END $$; + +DO $$ +DECLARE + view_exists BOOLEAN; +BEGIN + -- 检查视图是否已存在 + SELECT EXISTS ( + SELECT 1 FROM information_schema.views + WHERE table_name = 'country_info_view' + ) INTO view_exists; + + -- 创建或更新视图 + CREATE OR REPLACE VIEW country_info_view AS + SELECT + u.id AS country_id, + n.name AS name, + c.code AS code, + u.deleted AS deleted + FROM + country u + JOIN + country_name n ON u.id = n.country_id + JOIN + country_code c ON u.id = c.country_id + WHERE + u.deleted = FALSE; + + -- 根据视图是否已存在输出不同提示 + IF view_exists THEN + RAISE NOTICE '视图 country_info_view 已更新'; + ELSE + RAISE NOTICE '视图 country_info_view 已创建'; + END IF; +EXCEPTION + WHEN OTHERS THEN + RAISE NOTICE '处理视图时发生错误: %', SQLERRM; +END $$; diff --git a/backend/asset_assistant/sql/05_exchange.sql b/backend/asset_assistant/sql/05_exchange.sql new file mode 100644 index 0000000..b3aae87 --- /dev/null +++ b/backend/asset_assistant/sql/05_exchange.sql @@ -0,0 +1,97 @@ +-- 切换到目标数据库 +\c postgres; + +DO $$ +BEGIN + IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'exchange') THEN + CREATE TABLE exchange ( + id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL, + deleted BOOLEAN NOT NULL DEFAULT FALSE, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + CREATE TRIGGER update_exchange_updated_at + BEFORE UPDATE ON "deleted" + FOR EACH ROW + EXECUTE FUNCTION update_data_modified_column(); + + RAISE NOTICE 'created exchange table and trigger'; + ELSE + RAISE NOTICE 'exchange table already exists'; + END IF; + + IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'exchange_name') THEN + CREATE TABLE exchange_name ( + id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL, + exchange_id UUID NOT NULL, + name VARCHAR NOT NULL, + deleted BOOLEAN NOT NULL DEFAULT FALSE, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + CREATE TRIGGER update_name_updated_at + BEFORE UPDATE ON "name" + FOR EACH ROW + EXECUTE FUNCTION update_data_modified_column(); + + RAISE NOTICE 'created name table and trigger'; + ELSE + RAISE NOTICE 'name table already exists'; + END IF; + + IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'exchange_code') THEN + CREATE TABLE exchange_code ( + id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL, + exchange_id UUID NOT NULL, + code VARCHAR NOT NULL, + deleted BOOLEAN NOT NULL DEFAULT FALSE, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + CREATE TRIGGER update_code_updated_at + BEFORE UPDATE ON "code" + FOR EACH ROW + EXECUTE FUNCTION update_data_modified_column(); + + RAISE NOTICE 'created exchange_code table and trigger'; + ELSE + RAISE NOTICE 'exchange_code table already exists'; + END IF; +END $$; + +DO $$ +DECLARE + view_exists BOOLEAN; +BEGIN + -- 检查视图是否已存在 + SELECT EXISTS ( + SELECT 1 FROM information_schema.views + WHERE table_name = 'exchange_info_view' + ) INTO view_exists; + + -- 创建或更新视图 + CREATE OR REPLACE VIEW exchange_info_view AS + SELECT + u.id AS exchange_id, + n.name AS name, + c.code AS code, + u.deleted AS deleted + FROM + exchange u + JOIN + exchange_name n ON u.id = n.exchange_id + JOIN + exchange_code c ON u.id = c.exchange_id + WHERE + u.deleted = FALSE; + + -- 根据视图是否已存在输出不同提示 + IF view_exists THEN + RAISE NOTICE '视图 exchange_info_view 已更新'; + ELSE + RAISE NOTICE '视图 exchange_info_view 已创建'; + END IF; +EXCEPTION + WHEN OTHERS THEN + RAISE NOTICE '处理视图时发生错误: %', SQLERRM; +END $$; diff --git a/backend/country/sql/02_create_country_table.sql b/backend/country/sql/02_create_country_table.sql index 048e823..a464553 100644 --- a/backend/country/sql/02_create_country_table.sql +++ b/backend/country/sql/02_create_country_table.sql @@ -27,4 +27,79 @@ BEGIN ELSE RAISE NOTICE 'country table already exists'; END IF; -END $$; \ No newline at end of file + + IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'name') THEN + CREATE TABLE name ( + id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL, + country_id UUID NOT NULL, + name VARCHAR NOT NULL, + deleted BOOLEAN NOT NULL DEFAULT FALSE, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + CREATE TRIGGER update_name_updated_at + BEFORE UPDATE ON "name" + FOR EACH ROW + EXECUTE FUNCTION update_name_modified_column(); + + RAISE NOTICE 'Created name table and trigger'; + ELSE + RAISE NOTICE 'name table already exists'; + END IF; + + IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'code') THEN + CREATE TABLE code ( + id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL, + country_id UUID NOT NULL, + code VARCHAR NOT NULL, + deleted BOOLEAN NOT NULL DEFAULT FALSE, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + CREATE TRIGGER update_code_updated_at + BEFORE UPDATE ON "code" + FOR EACH ROW + EXECUTE FUNCTION update_code_modified_column(); + + RAISE NOTICE 'Created code table and trigger'; + ELSE + RAISE NOTICE 'code table already exists'; + END IF; +END $$; + +DO $$ +DECLARE + view_exists BOOLEAN; +BEGIN + -- 检查视图是否已存在 + SELECT EXISTS ( + SELECT 1 FROM information_schema.views + WHERE table_name = 'country_info_view' + ) INTO view_exists; + + -- 创建或更新视图 + CREATE OR REPLACE VIEW country_info_view AS + SELECT + u.id AS country_id, + n.name AS name, + c.code AS code, + u.deleted AS deleted + FROM + "country" u + JOIN + name n ON u.id = n.country_id + JOIN + code c ON u.id = c.country_id + WHERE + u.deleted = FALSE; + + -- 根据视图是否已存在输出不同提示 + IF view_exists THEN + RAISE NOTICE '视图 country_info_view 已更新'; + ELSE + RAISE NOTICE '视图 country_info_view 已创建'; + END IF; +EXCEPTION + WHEN OTHERS THEN + RAISE NOTICE '处理视图时发生错误: %', SQLERRM; +END $$; diff --git a/backend/user/sql/02_create_user_table.sql b/backend/user/sql/02_create_user_table.sql index 0de15d2..f3b0b0e 100644 --- a/backend/user/sql/02_create_user_table.sql +++ b/backend/user/sql/02_create_user_table.sql @@ -23,8 +23,83 @@ BEGIN FOR EACH ROW EXECUTE FUNCTION update_user_modified_column(); - RAISE NOTICE 'Created user table and trigger'; + RAISE NOTICE 'created user table and trigger'; ELSE RAISE NOTICE 'user table already exists'; END IF; -END $$; \ No newline at end of file + + IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'account') THEN + CREATE TABLE account ( + id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL, + user_id UUID NOT NULL, + account VARCHAR NOT NULL, + deleted BOOLEAN NOT NULL DEFAULT FALSE, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + CREATE TRIGGER update_account_updated_at + BEFORE UPDATE ON "account" + FOR EACH ROW + EXECUTE FUNCTION update_account_modified_column(); + + RAISE NOTICE 'created account table and trigger'; + ELSE + RAISE NOTICE 'account table already exists'; + END IF; + + IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'password') THEN + CREATE TABLE password ( + id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL, + user_id UUID NOT NULL, + password VARCHAR NOT NULL, + deleted BOOLEAN NOT NULL DEFAULT FALSE, + created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + CREATE TRIGGER update_password_updated_at + BEFORE UPDATE ON "password" + FOR EACH ROW + EXECUTE FUNCTION update_password_modified_column(); + + RAISE NOTICE 'created password table and trigger'; + ELSE + RAISE NOTICE 'password table already exists'; + END IF; +END $$; + +DO $$ +DECLARE + view_exists BOOLEAN; +BEGIN + -- 检查视图是否已存在 + SELECT EXISTS ( + SELECT 1 FROM information_schema.views + WHERE table_name = 'user_info_view' + ) INTO view_exists; + + -- 创建或更新视图 + CREATE OR REPLACE VIEW user_info_view AS + SELECT + u.id AS user_id, + ua.account AS account, + up.password AS password, + u.deleted AS deleted + FROM + "user" u + JOIN + account ua ON u.id = ua.user_id + JOIN + password up ON u.id = up.user_id + WHERE + u.deleted = FALSE; + + -- 根据视图是否已存在输出不同提示 + IF view_exists THEN + RAISE NOTICE '视图 user_info_view 已更新'; + ELSE + RAISE NOTICE '视图 user_info_view 已创建'; + END IF; +EXCEPTION + WHEN OTHERS THEN + RAISE NOTICE '处理视图时发生错误: %', SQLERRM; +END $$;