BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
ab227473
Commit
ab227473
authored
Jun 22, 2017
by
Yorick Peterse
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'mk-add-datetime-with-timezone-table-definition' into 'master'
Add `datetime_with_timezone` to table definition See merge request !12292
parents
6e66473f
ec7b3a8d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
81 additions
and
14 deletions
+81
-14
active_record_data_types.rb
config/initializers/active_record_data_types.rb
+65
-8
active_record_table_definition.rb
config/initializers/active_record_table_definition.rb
+16
-6
No files found.
config/initializers/active_record_data_types.rb
View file @
ab227473
...
...
@@ -4,21 +4,78 @@
if
Gitlab
::
Database
.
postgresql?
require
'active_record/connection_adapters/postgresql_adapter'
module
ActiveRecord
module
ConnectionAdapters
class
PostgreSQLAdapter
NATIVE_DATABASE_TYPES
.
merge!
(
datetime_with_timezone:
{
name:
'timestamptz'
})
module
ActiveRecord::ConnectionAdapters::PostgreSQL::OID
# Add the class `DateTimeWithTimeZone` so we can map `timestamptz` to it.
class
DateTimeWithTimeZone
<
DateTime
def
type
:datetime_with_timezone
end
end
end
module
RegisterDateTimeWithTimeZone
# Run original `initialize_type_map` and then register `timestamptz` as a
# `DateTimeWithTimeZone`.
#
# Apparently it does not matter that the original `initialize_type_map`
# aliases `timestamptz` to `timestamp`.
#
# When schema dumping, `timestamptz` columns will be output as
# `t.datetime_with_timezone`.
def
initialize_type_map
(
mapping
)
super
mapping
mapping
.
register_type
'timestamptz'
do
|
_
,
_
,
sql_type
|
precision
=
extract_precision
(
sql_type
)
ActiveRecord
::
ConnectionAdapters
::
PostgreSQLAdapter
::
OID
::
DateTimeWithTimeZone
.
new
(
precision:
precision
)
end
end
end
class
ActiveRecord
::
ConnectionAdapters
::
PostgreSQLAdapter
prepend
RegisterDateTimeWithTimeZone
# Add column type `datetime_with_timezone` so we can do this in
# migrations:
#
# add_column(:users, :datetime_with_timezone)
#
NATIVE_DATABASE_TYPES
[
:datetime_with_timezone
]
=
{
name:
'timestamptz'
}
end
elsif
Gitlab
::
Database
.
mysql?
require
'active_record/connection_adapters/mysql2_adapter'
module
ActiveRecord
module
ConnectionAdapters
class
AbstractMysqlAdapter
NATIVE_DATABASE_TYPES
.
merge!
(
datetime_with_timezone:
{
name:
'timestamp'
})
module
RegisterDateTimeWithTimeZone
# Run original `initialize_type_map` and then register `timestamp` as a
# `MysqlDateTimeWithTimeZone`.
#
# When schema dumping, `timestamp` columns will be output as
# `t.datetime_with_timezone`.
def
initialize_type_map
(
mapping
)
super
mapping
mapping
.
register_type
(
%r(timestamp)i
)
do
|
sql_type
|
precision
=
extract_precision
(
sql_type
)
ActiveRecord
::
ConnectionAdapters
::
AbstractMysqlAdapter
::
MysqlDateTimeWithTimeZone
.
new
(
precision:
precision
)
end
end
end
class
ActiveRecord
::
ConnectionAdapters
::
AbstractMysqlAdapter
prepend
RegisterDateTimeWithTimeZone
# Add the class `DateTimeWithTimeZone` so we can map `timestamp` to it.
class
MysqlDateTimeWithTimeZone
<
MysqlDateTime
def
type
:datetime_with_timezone
end
end
# Add column type `datetime_with_timezone` so we can do this in
# migrations:
#
# add_column(:users, :datetime_with_timezone)
#
NATIVE_DATABASE_TYPES
[
:datetime_with_timezone
]
=
{
name:
'timestamp'
}
end
end
config/initializers/active_record_table_definition.rb
View file @
ab227473
...
...
@@ -3,15 +3,15 @@
require
'active_record/connection_adapters/abstract/schema_definitions'
# Appends columns `created_at` and `updated_at` to a table.
#
# It is used in table creation like:
# create_table 'users' do |t|
# t.timestamps_with_timezone
# end
module
ActiveRecord
module
ConnectionAdapters
class
TableDefinition
# Appends columns `created_at` and `updated_at` to a table.
#
# It is used in table creation like:
# create_table 'users' do |t|
# t.timestamps_with_timezone
# end
def
timestamps_with_timezone
(
**
options
)
options
[
:null
]
=
false
if
options
[
:null
].
nil?
...
...
@@ -19,6 +19,16 @@ module ActiveRecord
column
(
column_name
,
:datetime_with_timezone
,
options
)
end
end
# Adds specified column with appropriate timestamp type
#
# It is used in table creation like:
# create_table 'users' do |t|
# t.datetime_with_timezone :did_something_at
# end
def
datetime_with_timezone
(
column_name
,
**
options
)
column
(
column_name
,
:datetime_with_timezone
,
options
)
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment