Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Jérome Perrin
gitlab-ce
Commits
174bed30
Commit
174bed30
authored
Sep 11, 2017
by
Rémy Coutable
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'docs/document-swapping-tables' into 'master'
Document how to swap database tables. See merge request !14183
parents
b1fc5859
1d4cc240
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
0 deletions
+54
-0
doc/development/README.md
doc/development/README.md
+1
-0
doc/development/swapping_tables.md
doc/development/swapping_tables.md
+53
-0
No files found.
doc/development/README.md
View file @
174bed30
...
...
@@ -60,6 +60,7 @@
-
[
Ordering Table Columns
](
ordering_table_columns.md
)
-
[
Verifying Database Capabilities
](
verifying_database_capabilities.md
)
-
[
Hash Indexes
](
hash_indexes.md
)
-
[
Swapping Tables
](
swapping_tables.md
)
## i18n
...
...
doc/development/swapping_tables.md
0 → 100644
View file @
174bed30
# Swapping Tables
Sometimes you need to replace one table with another. For example, when
migrating data in a very large table it's often better to create a copy of the
table and insert & migrate the data into this new table in the background.
Let's say you want to swap the table "events" with "events_for_migration". In
this case you need to follow 3 steps:
1.
Rename "events" to "events_temporary"
2.
Rename "events_for_migration" to "events"
3.
Rename "events_temporary" to "events_for_migration"
Rails allows you to do this using the
`rename_table`
method:
```
ruby
rename_table
:events
,
:events_temporary
rename_table
:events_for_migration
,
:events
rename_table
:events_temporary
,
:events_for_migration
```
This does not require any downtime as long as the 3
`rename_table`
calls are
executed in the _same_ database transaction. Rails by default uses database
transactions for migrations, but if it doesn't you'll need to start one
manually:
```
ruby
Event
.
transaction
do
rename_table
:events
,
:events_temporary
rename_table
:events_for_migration
,
:events
rename_table
:events_temporary
,
:events_for_migration
end
```
Once swapped you _have to_ reset the primary key of the new table. For
PostgreSQL you can use the
`reset_pk_sequence!`
method like so:
```
ruby
reset_pk_sequence!
(
'events'
)
```
For MySQL however you need to do run the following:
```
ruby
amount
=
Event
.
pluck
(
'COALESCE(MAX(id), 1)'
).
first
execute
"ALTER TABLE events AUTO_INCREMENT =
#{
amount
}
"
```
Failure to reset the primary keys will result in newly created rows starting
with an ID value of 1. Depending on the existing data this can then lead to
duplicate key constraints from popping up, preventing users from creating new
data.
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