lwb5-in-2025/sql/00-init.sql

52 lines
1.4 KiB
MySQL
Raw Normal View History

2016-01-10 11:01:49 +01:00
-- LegacyWorlds Beta 5
-- PostgreSQL database scripts
--
-- 00-init.sql
--
-- Initialises the various roles and the database itself
--
-- Copyright(C) 2004-2007, DeepClone Development
-- --------------------------------------------------------
-- Connect to the main system database
\c postgres postgres
SET search_path=public;
-- Create the LW users
CREATE ROLE legacyworlds WITH LOGIN ENCRYPTED PASSWORD :'db_user_pass';
CREATE ROLE legacyworlds_admin WITH LOGIN ENCRYPTED PASSWORD :'db_admin_pass' CONNECTION LIMIT 2;
2016-01-10 11:01:49 +01:00
-- Create the database
CREATE DATABASE legacyworlds WITH OWNER=legacyworlds_admin ENCODING='UTF8';
-- Connect to the LW database with the LW admin user
\c legacyworlds legacyworlds_admin
--
-- The following function returns the last inserted identifier for some table.
--
CREATE OR REPLACE FUNCTION last_inserted(tbl NAME) RETURNS BIGINT AS $$
DECLARE
i BIGINT;
BEGIN
SELECT INTO i currval(tbl || '_id_seq');
RETURN i;
EXCEPTION
WHEN undefined_table OR object_not_in_prerequisite_state THEN
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
--
-- UNIX_TIMESPAMP() function for MySQL compatibility
--
CREATE OR REPLACE FUNCTION unix_timestamp(t TIMESTAMP WITH TIME ZONE) RETURNS INT AS $$
SELECT CAST(EXTRACT(EPOCH FROM $1) AS INT);
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION unix_timestamp(t TIMESTAMP WITHOUT TIME ZONE) RETURNS INT AS $$
SELECT CAST(EXTRACT(EPOCH FROM $1) AS INT);
$$ LANGUAGE SQL;