Created
February 12, 2016 21:24
-
-
Save jazzsequence/99dbee218c1b9a84df0d to your computer and use it in GitHub Desktop.
Update wp_postmeta and wp_posts database tables with posts from another site
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# What if you are trying to migrate a bunch of posts from one site into | |
# another site that already has content? We'll assume you already have | |
# a database export of the posts and postmeta you want to migrate. You | |
# will want to create a database locally to alter the tables to get them | |
# ready to merge into the production database that already has content. | |
# You will also need to know the highest `meta_id` and `ID` values from | |
# the `wp_postmeta` and `wp_posts` tables, respectively. | |
# First, we need to create new columns in the tables we have locally. To | |
# do this the right way, we are going to figure out what type of data | |
# needs to go into these columns (assuming you didn't already know off the | |
# top of your head. | |
show fields from `wp_postmeta`; | |
# This will show you all the columns in your selected database. In this | |
# case, we'll be working with bigint(20) fields, so we need to create a | |
# new column that accepts the same type of data. | |
ALTER TABLE `wp_postmeta` ADD COLUMN id bigint(20) NOT NULL FIRST; | |
# Now we need to set the starting value to the highest value of `meta_id` | |
# on the destination database +1 and update all the values of our new | |
# column accordingly. | |
# In this case, my last `meta_id` was 103546 so I'm starting with 103547. | |
set @index = 103547; | |
update `wp_postmeta` set id = (@index:=@index+1); | |
# Now we're ready to rename columns. We're going to start by renaming the | |
# `meta_id` column to `meta_id_old` and then we're going to rename `id` | |
# to `meta_id`. | |
ALTER TABLE wp_postmeta CHANGE meta_id meta_id_old bigint(20); | |
ALTER TABLE wp_postmeta CHANGE id meta_id bigint(20); | |
# Now we need to make the new `meta_id` the primary key. | |
alter table wp_postmeta modify meta_id_old bigint, drop primary key, add primary key (meta_id); | |
# At this point you should be good to go, but if you didn't want to keep | |
# the old `meta_id_old` column around, you can drop that. Make a backup | |
# of your `wp_postmeta` table before you do this. | |
alter table wp_postmeta drop meta_id_old; | |
# Now we're going to do the same thing with `wp_posts`. | |
# Show the fields. We will learn that `ID` is also a bigint(20). | |
show fields from wp_posts; | |
# Create a new column. | |
alter table `wp_posts` add column newID bigint(20) not null first; | |
# My highest post ID is 8147, so setting my index at 8148. | |
set @index = 8148; | |
update `wp_posts` set newID = (@index:=@index+1); | |
# Rename tables. | |
ALTER TABLE wp_posts CHANGE ID old_ID bigint(20); | |
ALTER TABLE wp_posts CHANGE newID ID bigint(20); | |
# Drop the primary key off old_ID and set it to ID. | |
alter table wp_posts modify old_ID bigint, drop primary key, add primary key (ID); | |
# Backup your table here and drop the `old_ID` column. | |
alter table wp_posts drop old_ID; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
References:
http://stackoverflow.com/questions/7989342/how-do-i-update-a-table-to-add-a-primary-key-and-update-all-of-the-existing-rows
http://stackoverflow.com/questions/9702766/sql-set-existing-column-as-primary-key-in-mysql
http://www.tech-recipes.com/rx/378/add-a-column-to-an-existing-mysql-table/
http://dba.stackexchange.com/questions/71353/how-to-change-mysql-table-auto-increment-primary-key